diff options
author | Eric Wong <normalperson@yhbt.net> | 2010-04-26 19:02:41 -0700 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2010-04-26 19:02:41 -0700 |
commit | 6066e9f608460adf46b215a9b6e5f351a87ba4b0 (patch) | |
tree | 4acf6a3904c51b677a8d0b8ad9f82c2b2e59f97a | |
parent | f071a6f936ba0b22610b1f7ce68e414403f29996 (diff) | |
download | kcar-6066e9f608460adf46b215a9b6e5f351a87ba4b0.tar.gz |
We avoid filter_body overhead for things with known content-length.
-rw-r--r-- | ext/kcar/kcar.rl | 47 |
1 files changed, 17 insertions, 30 deletions
diff --git a/ext/kcar/kcar.rl b/ext/kcar/kcar.rl index 0d22866..8c2b465 100644 --- a/ext/kcar/kcar.rl +++ b/ext/kcar/kcar.rl @@ -560,36 +560,23 @@ static VALUE filter_body(VALUE self, VALUE buf, VALUE data) rb_str_resize(buf, dlen); /* we can never copy more than dlen bytes */ OBJ_TAINT(buf); /* keep weirdo $SAFE users happy */ - if (HP_FL_TEST(hp, CHUNKED)) { - if (!chunked_eof(hp)) { - hp->s.dest_offset = 0; - http_parser_execute(hp, buf, dptr, dlen); - if (hp->cs == http_parser_error) - rb_raise(eParserError, "Invalid HTTP format, parsing fails."); - - assert(hp->s.dest_offset <= hp->offset && - "destination buffer overflow"); - advance_str(data, hp->offset); - rb_str_set_len(buf, hp->s.dest_offset); - - if (RSTRING_LEN(buf) == 0 && chunked_eof(hp)) { - assert(hp->len.chunk == 0 && "chunk at EOF but more to parse"); - } else { - data = Qnil; - } - } - } else { - /* no need to enter the Ragel machine for unchunked transfers */ - assert(hp->len.content >= 0 && "negative Content-Length"); - if (hp->len.content > 0) { - long nr = MIN(dlen, hp->len.content); - - memcpy(RSTRING_PTR(buf), dptr, nr); - hp->len.content -= nr; - if (hp->len.content == 0) - hp->cs = http_parser_first_final; - advance_str(data, nr); - rb_str_set_len(buf, nr); + if (!HP_FL_TEST(hp, CHUNKED)) + rb_raise(rb_eRuntimeError, "filter_body is only for chunked bodies"); + + if (!chunked_eof(hp)) { + hp->s.dest_offset = 0; + http_parser_execute(hp, buf, dptr, dlen); + if (hp->cs == http_parser_error) + rb_raise(eParserError, "Invalid HTTP format, parsing fails."); + + assert(hp->s.dest_offset <= hp->offset && + "destination buffer overflow"); + advance_str(data, hp->offset); + rb_str_set_len(buf, hp->s.dest_offset); + + if (RSTRING_LEN(buf) == 0 && chunked_eof(hp)) { + assert(hp->len.chunk == 0 && "chunk at EOF but more to parse"); + } else { data = Qnil; } } |