Exemple #1
0
char *test_parser_thrashing()
{
    glob_t test_files;
    unsigned int i = 0;
    int nparsed = 0;
    int delta = 0;
    int tests_run = 0;
    int execs_run = 0;
    int unfinished = 0;
    int errors = 0;

    int rc = glob("tests/and_suite/*", 0, NULL, &test_files);
    mu_assert(rc == 0, "Failed to glob file sin tests/and_suite/*");

    for(i = 0; i < test_files.gl_pathc; i++) {
        FILE *infile = fopen(test_files.gl_pathv[i], "r");
        mu_assert(infile != NULL, "Failed to open test file.");

        bstring data = bread((bNread)fread, infile);
        fclose(infile);
        mu_assert(data != NULL, "Failed to read test file.");

        tests_run++;

        http_parser p = setup_parser();

        nparsed = 0;
        delta = 0;

        while(nparsed < blength(data)) {
            debug("json PARSING: %d of %d at %s", nparsed, blength(data), bdataofs(data, nparsed));

            delta = http_parser_execute(&p, bdata(data), blength(data), nparsed);
            execs_run++;

            if(delta == 0) { break; }

            if(!http_parser_finish(&p)) {
                unfinished++;
            }

            nparsed += delta;

            if(http_parser_has_error(&p)) {
                errors++;
            }

            debug("TEST %s results: delta %d, has_error: %d, is_finished: %d",
                    test_files.gl_pathv[i],
                    nparsed, http_parser_has_error(&p), http_parser_is_finished(&p));

            http_parser_init(&p);  // reset for the next try
        }
    }

    debug("HTTP PARSING: tests_run: %d, execs_run: %d, unfinished: %d, errors: %d",
            tests_run, execs_run, unfinished, errors);

    return NULL;
}
Exemple #2
0
/**
 * call-seq:
 *    parser.finished? -> true/false
 *
 * Tells you whether the parser is finished or not and in a good state.
 */
VALUE HttpParser_is_finished(VALUE self)
{
  http_parser *http = NULL;
  DATA_GET(self, http_parser, http);

  return http_parser_is_finished(http) ? Qtrue : Qfalse;
}
Exemple #3
0
static PyObject *
HttpParserObject_is_finished(HttpParserObject* self)
{    
    if (http_parser_is_finished(self->http)) {
        Py_RETURN_TRUE;
    } else {
        Py_RETURN_FALSE;
    }
}
Exemple #4
0
char *test_http11_parser_basics() 
{
    http_parser p = setup_parser();
    int rc = 0;

    rc = http_parser_finish(&p);
    mu_assert(rc == 0, "Should NOT be finished if nothing parsed.");

    rc = http_parser_has_error(&p);
    mu_assert(rc == 0, "Should not have an error at the beginning.");

    rc = http_parser_is_finished(&p);
    mu_assert(rc == 0, "Should not be finished since never handed anything.");
    return NULL;
}
Exemple #5
0
static void
on_read_cb(uv_stream_t *stream, ssize_t nread, uv_buf_t buf)
{
  Connection *conn = (Connection*) stream;

  assert(conn->read_clear == false);

  if (nread < 0) {
    //
    // XXX: Do we have to call uv_read_stop(), or is it implicit?
    //
    // on_read_cb will never be called again for this connection, so we don't
    // have to reset anything.
    //
    uv_close((uv_handle_t*) conn, on_close_cb);
  }
  else if (nread > 0) {
    conn->read_buffer.increase_size(nread);
    conn->read_clear = true;

    if (conn->in_body) {
      // We are reading in the body. No need to call the parser!
      handle_http_body(conn);
    }
    else {
      conn->parse_offset = 
        http_parser_run(&conn->parser, &conn->req, conn->read_buffer.data(), conn->read_buffer.size(), conn->parse_offset);

      if (http_parser_is_finished(&conn->parser)) {
        handle_http_header_parsed(conn);
      }
      else if (http_parser_has_error(&conn->parser)) {
        handle_http_error(conn);
      }
    }
  }
  else {
    //
    // This happens when libuv calls alloc_read_buffer, but does not need the buffer.
    // We don't have to free the buffer, as it is part of the connection.
    //
    assert(nread == 0);
    conn->read_clear = true;
  }
}
Exemple #6
0
int http_parser_finish(http_parser *parser)
{
    int cs = parser->cs;
    
    
#line 1202 "http11_parser.c"
#line 134 "http11_parser.rl"
    
    parser->cs = cs;
    
    if (http_parser_has_error(parser) ) {
        return -1;
    } else if (http_parser_is_finished(parser) ) {
        return 1;
    } else {
        return 0;
    }
}
Exemple #7
0
static void parse_header(int fd, struct http_data *data)
{
    struct http_callbacks callbacks = {
        .data = data,

        .request_method = http_request_method,
        .request_uri    = http_request_uri,
        .http_version   = http_request_version,

        .http_field     = http_field,
        .header_done    = http_request_done
    };

    struct sock sock = { .fd = fd };
    http_parser_init(&parser, &sock);

    do {
        refill_buffer(&sock);

        /* TODO: parser and sock object should be rolled together */
        ssize_t nbytes_p = http_parser_execute(&parser, &sock, &callbacks);
        sock.pos += nbytes_p;
    } while (!http_parser_is_finished(&sock));
}
Exemple #8
0
int HTTPParser_isFinished(HTTPParser *self)
{
	return http_parser_is_finished(self->parser);
}