Example #1
0
/**
 * call-seq:
 *    parser.error? -> true/false
 *
 * Tells you whether the parser is in an error state.
 */
VALUE HttpClientParser_has_error(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_parser, http);

  return httpclient_parser_has_error(http) ? Qtrue : Qfalse;
}
Example #2
0
/**
 * call-seq:
 *    parser.execute(req_hash, data, start) -> Integer
 *
 * Takes a Hash and a String of data, parses the String of data filling in the Hash
 * returning an Integer to indicate how much of the data has been read.  No matter
 * what the return value, you should call HttpClientParser#finished? and HttpClientParser#error?
 * to figure out if it's done parsing or there was an error.
 * 
 * This function now throws an exception when there is a parsing error.  This makes 
 * the logic for working with the parser much easier.  You can still test for an 
 * error, but now you need to wrap the parser with an exception handling block.
 *
 * The third argument allows for parsing a partial request and then continuing
 * the parsing from that position.  It needs all of the original data as well 
 * so you have to append to the data buffer as you read.
 */
VALUE HttpClientParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
{
  httpclient_parser *http = NULL;
  int from = 0;
  char *dptr = NULL;
  long dlen = 0;

  REQUIRE_TYPE(req_hash, T_HASH);
  REQUIRE_TYPE(data, T_STRING);
  REQUIRE_TYPE(start, T_FIXNUM);

  DATA_GET(self, httpclient_parser, http);

  from = FIX2INT(start);
  dptr = RSTRING(data)->ptr;
  dlen = RSTRING(data)->len;

  if(from >= dlen) {
    rb_raise(eHttpClientParserError, "Requested start is after data buffer end.");
  } else {
    http->data = (void *)req_hash;
    httpclient_parser_execute(http, dptr, dlen, from);

    if(httpclient_parser_has_error(http)) {
      rb_raise(eHttpClientParserError, "Invalid HTTP format, parsing fails.");
    } else {
      return INT2FIX(httpclient_parser_nread(http));
    }
  }
}
Example #3
0
char *test_httpclient_parser_basics() 
{
    httpclient_parser p = setup_parser();
    p.data = &p;
    int rc = 0;

    rc = httpclient_parser_finish(&p);
    mu_assert(rc == 0, "Client parser SHOULD be finished if nothing parsed.");

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

    rc = httpclient_parser_is_finished(&p);
    mu_assert(rc == 0, "Should not be finished since never handed anything.");
    return NULL;
}
Example #4
0
char *test_parser_thrashing()
{
    glob_t test_files;
    int i = 0;
    int nparsed = 0;
    int tests_run = 0;
    int execs_run = 0;
    int unfinished = 0;
    int errors = 0;

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

    for(i = 0; i < test_files.gl_pathc; i++) {
        debug("TESTING: %s", test_files.gl_pathv[i]);
        bstring data = load_test_case(test_files.gl_pathv[i]);
        mu_assert(data != NULL, "Failed to load test case.");

        tests_run++;

        httpclient_parser p = setup_parser();
        p.data = &p;

        nparsed = httpclient_parser_execute(&p, bdata(data), blength(data), 0);
        execs_run++;

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

        if(httpclient_parser_has_error(&p)) {
            errors++;
            debug("TEST %s results: delta %d, has_error: %d, is_finished: %d",
                    test_files.gl_pathv[i],
                    nparsed, httpclient_parser_has_error(&p), 
                    httpclient_parser_is_finished(&p));
        } else if(p.chunked) {
            int start = p.body_start;

            do {
                httpclient_parser_init(&p);
                
                nparsed = httpclient_parser_execute(&p, bdata(data), blength(data), start);
                mu_assert(p.body_start > start, "Didn't go past start.");
                mu_assert(p.chunked, "Should still be chunked.");
                start = p.body_start + p.content_len + 1;

                debug("CHUNK: length: %d, done: %d, start: %d",
                        p.content_len, p.chunks_done, start);
            } while(!p.chunks_done && p.content_len > 0 && !httpclient_parser_has_error(&p));

            mu_assert(p.chunks_done, "Should have chunks_done set.");
            mu_assert(p.content_len == 0, "Should have 0 content_len too.");
        }
    }

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

    return NULL;
}