Exemple #1
0
static int
valid_domain(request_rec *r, const char *value)
{
  int  len;
  char *name;
  char *val;
  char *pstat;
  char *p = apr_pstrdup(r->pool, value);
  const char *host = apr_table_get(r->headers_in, HTTP_HOST);

  DBG(r, "start valid_domain() value:[%s]", value);
  DBG(r, "host:[%s]", host);
  if (!host)
    return CHXJ_TRUE;

  name = apr_strtok(p,"=", &pstat);
  name = qs_trim_string(r->pool, name);
  val = apr_strtok(NULL, "=", &pstat);
  val = qs_trim_string(r->pool, val);
  len = strlen(host);
  if (len) {
    if (chxj_strcasenrcmp(r->pool, host, val, strlen(val))) {
      DBG(r, "not match domain. host domain:[%s] vs value:[%s]", host, val);
      return CHXJ_FALSE;
    }
  }
  DBG(r, "end valid_domain() value:[%s]", value);
  return CHXJ_TRUE;
}
Exemple #2
0
static int
valid_path(request_rec *r, const char *value)
{
  char *p = apr_pstrdup(r->pool, value);
  char *uri;
  char *tmp;
  char *name;
  char *val;
  char *pstat;

  DBG(r, "start valid_path() unparsed_uri:[%s] value:[%s]", r->unparsed_uri, value);
  if (chxj_starts_with(r->unparsed_uri, "http://")) {
    uri = strchr(&r->unparsed_uri[sizeof("http://")], '/');
    if (uri != NULL) {
      uri = apr_pstrdup(r->pool, uri);
    }
  }
  else if (chxj_starts_with(r->unparsed_uri, "https://")) {
    uri = strchr(&r->unparsed_uri[sizeof("https://")], '/');
    if (uri != NULL) {
      uri = apr_pstrdup(r->pool, uri);
    }
  }
  else if (chxj_starts_with(r->unparsed_uri, "/")) {
    uri = apr_pstrdup(r->pool, r->unparsed_uri);
  }
  else {
    uri = apr_pstrdup(r->pool, "/");
  }
  
  if ((tmp = strchr(uri, '?'))) {
    *tmp = '\0';
  }
  DBG(r, "uri=[%s]", uri);
  name = apr_strtok(p, "=", &pstat);
  val = apr_strtok(NULL, "=", &pstat);
  name = qs_trim_string(r->pool, name);
  val = qs_trim_string(r->pool, val);
  DBG(r, "name=[%s] val=[%s]", name, val);
  
  DBG(r, "val:[%s] vs uri:[%s]", val, uri);
  if (! chxj_starts_with(uri, val)) {
    DBG(r, "end valid_path() unparsed_uri:[%s] value:[%s] (false)", r->unparsed_uri, value);
    return CHXJ_FALSE;
  }
  DBG(r, "end valid_path() unparsed_uri:[%s] value:[%s] (true)", r->unparsed_uri, value);
  return CHXJ_TRUE;
}
Exemple #3
0
char *
chxj_css_rgb_func_to_value(apr_pool_t *pool, const char *rgb_func_string)
{
  char *s = apr_pstrdup(pool, rgb_func_string);
  if (chxj_starts_with(s, "rgb")) {
    s += 3;
    s = qs_trim_string(pool, s);
    if (*s == '(') s++;
    int len = strlen(s);
    if (s[len - 1] == ')') s[len-1] = 0;
    char *pstat;
    char *red   = qs_trim_string(pool, apr_strtok(s, ",", &pstat));
    char *green = qs_trim_string(pool, apr_strtok(NULL, ",", &pstat));
    char *blue  = qs_trim_string(pool, apr_strtok(NULL, ",", &pstat));
    int red_per_flag   = 0;
    int green_per_flag = 0;
    int blue_per_flag  = 0;
    if (red && (pstat = strchr(red, '%'))) {
      *pstat = 0;
      red_per_flag = 1;
    }
    else {
      red = "0";
    }
    if (green && (pstat = strchr(green, '%'))) {
      *pstat = 0;
      green_per_flag = 1;
    }
    else {
      green = "0";
    }
    if (blue && (pstat = strchr(blue, '%'))) {
      *pstat = 0;
      blue_per_flag = 1;
    }
    else {
      blue = "0";
    }
    double d_red   = (double)chxj_atoi(red);
    double d_green = (double)chxj_atoi(green);
    double d_blue  = (double)chxj_atoi(blue);
    if (red_per_flag) {
      d_red   /= 100.0;
      d_red   *= 255;
    }
    if (green_per_flag) {
      d_green /= 100.0;
      d_green *= 255;
    }
    if (blue_per_flag) {
      d_blue  /= 100.0;
      d_blue  *= 255;
    }
    char *ret = apr_psprintf(pool, "#%02x%02x%02x", (int)d_red, (int)d_green, (int)d_blue);
    return ret;
  }
  else if (s[0] == '#') {
    if (strlen(&s[1]) == 3) {
      char red[2];
      char green[2];
      char blue[2];
      red[0]   = s[1];
      green[0] = s[2];
      blue[0]  = s[3];
      red[1]   = 0;
      green[1] = 0;
      blue[1]  = 0;
      int ired   = chxj_axtoi(red);
      int igreen = chxj_axtoi(green);
      int iblue  = chxj_axtoi(blue);
      return apr_psprintf(pool, "#%02x%02x%02x", ired * 0x10 + ired, igreen * 0x10 + igreen, iblue * 0x10 + iblue);
    }
  }
  else {
    switch(*s) {
    case 'a':
    case 'A':
      if (STRCASEEQ('q','Q', &s[1], "qua")) {
        /* aqua */
        return apr_pstrdup(pool, "#00ffff");
      }
      break;
    case 'b':
    case 'B':
      if (s[1] == 'l' || s[1] == 'L') {
        if (s[2] == 'a' || s[2] == 'A') {
          if (STRCASEEQ('c','C',&s[3], "ck")) {
            /* black */
            return apr_pstrdup(pool, "#000000");
          }
        }
        else if (s[2] == 'u' || s[2] == 'U') {
          if (s[3] == 'e' || s[3] == 'E') {
            /* blue */
            return apr_pstrdup(pool, "#0000ff");
          }
        }
      }
      break;
    case 'f':
    case 'F':
      if (STRCASEEQ('u','U',&s[1], "uchsia")) {
        /* fuchsia */
        return apr_pstrdup(pool, "#ff00ff");
      }
      break;
    case 'g':
    case 'G':
      if (s[1] == 'r'||s[1] == 'R') {
        if (STRCASEEQ('a','A', &s[2], "ay")) {
          /* gray */
          return apr_pstrdup(pool, "#808080");
        }
        else if (STRCASEEQ('e','E',&s[2], "een")) {
          /* green */
          return apr_pstrdup(pool, "#008000");
        }
      }
      break;
    case 'l':
    case 'L':
      if (STRCASEEQ('i','I', &s[1], "ime")) {
        /* lime */
        return apr_pstrdup(pool, "#00ff00");
      }
      break;
    case 'm':
    case 'M':
      if (STRCASEEQ('a','A', &s[1], "aroon")) {
        /* maroon */
        return apr_pstrdup(pool, "#800000");
      }
      break;
    case 'n':
    case 'N':
      if (STRCASEEQ('a','A', &s[1], "avy")) {
        /* navy */
        return apr_pstrdup(pool, "#000080");
      }
      break;
    case 'o':
    case 'O':
      if (STRCASEEQ('l','L', &s[1], "live")) {
        /* olive */
        return apr_pstrdup(pool, "#808000");
      }
      break;
    case 'p':
    case 'P':
      if (STRCASEEQ('u','U', &s[1], "urple")) {
        /* purple */
        return apr_pstrdup(pool, "#800080");
      }
      break;
    case 'r':
    case 'R':
      if (STRCASEEQ('e','E', &s[1], "ed")) {
        /* red */
        return apr_pstrdup(pool, "#ff0000");
      }
      break;
    case 's':
    case 'S':
      if (STRCASEEQ('i','I', &s[1], "ilver")) {
        /* silver */
        return apr_pstrdup(pool, "#c0c0c0");
      }
      break;
    case 't':
    case 'T':
      if (STRCASEEQ('e','E', &s[1], "eal")) {
        /* teal */
        return apr_pstrdup(pool, "#008080");
      }
      break;
    case 'w':
    case 'W':
      if (STRCASEEQ('h','H', &s[1], "hite")) {
        /* white */
        return apr_pstrdup(pool, "#ffffff");
      }
      break;
    case 'y':
    case 'Y':
      if (STRCASEEQ('y','Y', &s[1], "ellow")) {
        /* yellow */
        return apr_pstrdup(pool, "#ffff00");
      }
      break;
    default:
      break;
    }
  }
  return s;
}
Exemple #4
0
static apr_status_t 
s_handle_response(serf_request_t *UNUSED(request), serf_bucket_t *response, void *handler_ctx, apr_pool_t *UNUSED(pool))
{
  const char      *data;
  apr_size_t      len;
  serf_status_line sl;
  apr_status_t     rv;
  handler_ctx_t  *ctx = handler_ctx;

  rv = serf_bucket_response_status(response, &sl);
  if (rv != APR_SUCCESS) {
    if (APR_STATUS_IS_EAGAIN(rv)) {
      return rv;
    }
    ctx->rv = rv;
    apr_atomic_dec32(&ctx->requests_outstanding); 
    return rv;
  }
  ctx->reason = sl.reason;
  ctx->response_code = sl.code;

  while (1) {
    rv = serf_bucket_read(response, 2048, &data, &len);
    if (SERF_BUCKET_READ_ERROR(rv)) {
      ctx->rv = rv;
      apr_atomic_dec32(&ctx->requests_outstanding);
      DBG(ctx->r, "REQ[%X] (ERROR)", TO_ADDR(ctx->r));
      DBG(ctx->r,"REQ[%X] end %s()",TO_ADDR(ctx->r),__func__);
      return rv;
    }
    if (APR_STATUS_IS_EAGAIN(rv)) {
      /* 0 byte return if EAGAIN returned. */
      DBG(ctx->r,"REQ[%X] (EAGAIN) len:[%d]", TO_ADDR(ctx->r), (int)len);
      DBG(ctx->r,"REQ[%X] end %s()",TO_ADDR(ctx->r),__func__);
      return rv;
    }

    if (len > 0) {
      if (! ctx->response) {
        ctx->response = apr_palloc(ctx->pool, len);
        ctx->response[0] = 0;
        ctx->response_len = 0;
      }
      else {
        char *tmp = apr_palloc(ctx->pool, ctx->response_len);
        memcpy(tmp, ctx->response, ctx->response_len);
        ctx->response = apr_palloc(ctx->pool, ctx->response_len + len);
        memcpy(ctx->response, tmp, ctx->response_len);
      }
      memcpy(&ctx->response[ctx->response_len], data, len);
      ctx->response_len += len;
      ctx->response[ctx->response_len] = 0;
    }
    
    if (APR_STATUS_IS_EOF(rv)) {
      serf_bucket_t *hdrs;
      char *tmp_headers = "";
      hdrs = serf_bucket_response_get_headers(response);
      while (1) {
        rv = serf_bucket_read(hdrs, 2048, &data, &len);
        if (SERF_BUCKET_READ_ERROR(rv))
          return rv;
        tmp_headers = apr_pstrcat(ctx->pool, tmp_headers, apr_psprintf(ctx->pool , "%.*s", (unsigned int)len, data), NULL);
        if (APR_STATUS_IS_EOF(rv)) {
          break;
        }
      }
      ctx->headers_out = apr_table_make(ctx->pool, 0);

      char *pstat;
      char *pair = NULL;
      for (;;) {
        pair = apr_strtok(tmp_headers, "\n", &pstat);
        if (!pair) break;
        tmp_headers = NULL;
        char *key;
        char *val;

        char *tpair = apr_pstrdup(ctx->pool, pair);
        key = tpair;
        val = strchr(tpair, ':');
        if (val) {
          *val = 0;
          val++;
          key = qs_trim_string(ctx->pool, key);
          val = qs_trim_string(ctx->pool, val);
          DBG(ctx->r,"REQ[%X] key:[%s], val:[%s]", TO_ADDR(ctx->r),key, val);
          apr_table_add(ctx->headers_out, key, val);
        }
      }
      ctx->rv = APR_SUCCESS;
      apr_atomic_dec32(&ctx->requests_outstanding);
      DBG(ctx->r,"REQ[%X] (NORMAL)", TO_ADDR(ctx->r));
      DBG(ctx->r,"REQ[%X] end %s()",TO_ADDR(ctx->r),__func__);
      return APR_EOF;
    }

    if (APR_STATUS_IS_EAGAIN(rv)) {
      DBG(ctx->r,"REQ[%X] (EAGAIN)", TO_ADDR(ctx->r));
      DBG(ctx->r,"REQ[%X] end %s()",TO_ADDR(ctx->r),__func__);
      return rv;
    }
  }
}
Exemple #5
0
static int
check_valid_cookie_attribute(request_rec *r, const char *value)
{
  char *pstat;
  char *pair;
  char *first_pair;
  char *domain_pair;
  char *path_pair;
  char *expire_pair;
  char *secure_pair;
  char *p;

  DBG(r, "start check_valid_cookie_attribute() value:[%s]", value);

  domain_pair = path_pair = expire_pair = secure_pair = NULL;
  p = apr_pstrdup(r->pool, value);

  /* pass first pair */
  first_pair = apr_strtok(p, ";", &pstat);  

  for (;;) {
    pair = apr_strtok(NULL, ";", &pstat);
    if (! pair) break;
    pair = qs_trim_string(r->pool, pair);
    if (STRNCASEEQ('d','D',"domain", pair, sizeof("domain")-1)) {
      domain_pair = apr_pstrdup(r->pool, pair);
    }
    else if (STRNCASEEQ('p','P',"path", pair, sizeof("path")-1)) {
      path_pair = apr_pstrdup(r->pool, pair);
    }
    else if (STRNCASEEQ('e','E',"expires", pair, sizeof("expires")-1)) {
      expire_pair = apr_pstrdup(r->pool, pair);
    }
    else if (STRNCASEEQ('s','S',"secure", pair, sizeof("secure")-1)) {
      secure_pair = apr_pstrdup(r->pool, pair);
    }
  }

  if (domain_pair) {
    if (!valid_domain(r, domain_pair)) {
      DBG(r, "invalid domain. domain_pair:[%s]", domain_pair);
      return CHXJ_FALSE;
    }
  }
  if (path_pair) {
    if (!valid_path(r, path_pair)) {
      DBG(r, "invalid path. path_pair:[%s]", path_pair);
      return CHXJ_FALSE;
    }
  }
  if (expire_pair) {
    if (!valid_expires(r, expire_pair)) {
      DBG(r, "invalid expire. expire_pair:[%s]", expire_pair);
      return CHXJ_FALSE;
    }
  }
  if (secure_pair) {
    if (!valid_secure(r, secure_pair)) {
      DBG(r, "invalid secure. secure_pair:[%s]", secure_pair);
      return CHXJ_FALSE;
    }
  }
  DBG(r, "end check_valid_cookie_attribute() value:[%s]", value);
  return CHXJ_TRUE;
}