Ejemplo n.º 1
0
static struct rest_json_payload *
rest_get_json_upload(mtev_http_rest_closure_t *restc,
                    int *mask, int *complete) {
  struct rest_json_payload *rxc;
  mtev_http_request *req = mtev_http_session_request(restc->http_ctx);
  httptrap_closure_t *ccl = NULL;
  int content_length;
  char buffer[32768];

  content_length = mtev_http_request_content_length(req);
  rxc = restc->call_closure;
  rxc->check = noit_poller_lookup(rxc->check_id);
  if (!rxc->check) {
    *complete = 1;
    return NULL;
  }

  if(!strcmp(rxc->check->module, "httptrap")) ccl = rxc->check->closure;
  rxc->immediate = noit_httptrap_check_asynch(ccl ? ccl->self : NULL, rxc->check);
  while(!rxc->complete) {
    int len;
    len = mtev_http_session_req_consume(
            restc->http_ctx, buffer,
            MIN(content_length - rxc->len, sizeof(buffer)),
            sizeof(buffer),
            mask);
    if(len > 0) {
      yajl_status status;
      _YD("inbound payload chunk (%d bytes) continuing YAJL parse\n", len);
      status = yajl_parse(rxc->parser, (unsigned char *)buffer, len);
      if(status != yajl_status_ok) {
        unsigned char *err;
        *complete = 1;
        err = yajl_get_error(rxc->parser, 0, (unsigned char *)buffer, len);
        rxc->error = strdup((char *)err);
        yajl_free_error(rxc->parser, err);
        return rxc;
      }
      rxc->len += len;
    }
    if(len < 0 && errno == EAGAIN) return NULL;
    else if(len < 0) {
      *complete = 1;
      return NULL;
    }
    content_length = mtev_http_request_content_length(req);
    if((mtev_http_request_payload_chunked(req) && len == 0) ||
       (rxc->len == content_length)) {
      rxc->complete = 1;
      _YD("no more data, finishing YAJL parse\n");
      yajl_complete_parse(rxc->parser);
    }
  }

  *complete = 1;
  return rxc;
}
Ejemplo n.º 2
0
static struct rest_json_payload *
rest_get_json_upload(mtev_http_rest_closure_t *restc,
                    int *mask, int *complete) {
  struct rest_json_payload *rxc;
  mtev_http_request *req = mtev_http_session_request(restc->http_ctx);
  httptrap_closure_t *ccl;
  int content_length;
  char buffer[32768];

  content_length = mtev_http_request_content_length(req);
  rxc = restc->call_closure;
  ccl = rxc->check->closure;
  rxc->immediate = mtev_httptrap_check_aynsch(ccl->self, rxc->check);
  while(!rxc->complete) {
    int len;
    len = mtev_http_session_req_consume(
            restc->http_ctx, buffer,
            MIN(content_length - rxc->len, sizeof(buffer)),
            sizeof(buffer),
            mask);
    if(len > 0) {
      yajl_status status;
      status = yajl_parse(rxc->parser, (unsigned char *)buffer, len);
      if(status != yajl_status_ok) {
        unsigned char *err;
        *complete = 1;
        err = yajl_get_error(rxc->parser, 0, (unsigned char *)buffer, len);
        rxc->error = strdup((char *)err);
        yajl_free_error(rxc->parser, err);
        return rxc;
      }
      rxc->len += len;
    }
    if(len < 0 && errno == EAGAIN) return NULL;
    else if(len < 0) {
      *complete = 1;
      return NULL;
    }
    content_length = mtev_http_request_content_length(req);
    if((mtev_http_request_payload_chunked(req) && len == 0) ||
       (rxc->len == content_length)) {
      rxc->complete = 1;
      yajl_complete_parse(rxc->parser);
    }
  }

  *complete = 1;
  return rxc;
}