From 1b0ee5826ef146a3e2647c40f3bc929d51d1b442 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 4 May 2011 17:04:51 -0700 Subject: http_parser: new add_parse method Combines the following sequence: http_parser.buf << socket.readpartial(0x4000) http_parser.parse Into: http_parser.add_parse(socket.readpartial(0x4000)) It was too damn redundant otherwise... --- ext/unicorn_http/unicorn_http.rl | 22 ++++++++++++++++++++++ lib/unicorn/http_request.rb | 4 +--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ext/unicorn_http/unicorn_http.rl b/ext/unicorn_http/unicorn_http.rl index 741f8ee..1d2705c 100644 --- a/ext/unicorn_http/unicorn_http.rl +++ b/ext/unicorn_http/unicorn_http.rl @@ -710,6 +710,27 @@ static VALUE HttpParser_parse(VALUE self) return Qnil; } +/** + * Document-method: parse + * call-seq: + * parser.add_parse(buffer) => env or nil + * + * adds the contents of +buffer+ to the internal buffer and attempts to + * continue parsing. Returns the +env+ Hash on success or nil if more + * data is needed. + * + * Raises HttpParserError if there are parsing errors. + */ +static VALUE HttpParser_add_parse(VALUE self, VALUE buffer) +{ + struct http_parser *hp = data_get(self); + + Check_Type(buffer, T_STRING); + rb_str_buf_append(hp->buf, buffer); + + return HttpParser_parse(self); +} + /** * Document-method: trailers * call-seq: @@ -908,6 +929,7 @@ void Init_unicorn_http(void) rb_define_method(cHttpParser, "clear", HttpParser_clear, 0); rb_define_method(cHttpParser, "reset", HttpParser_reset, 0); rb_define_method(cHttpParser, "parse", HttpParser_parse, 0); + rb_define_method(cHttpParser, "add_parse", HttpParser_add_parse, 1); rb_define_method(cHttpParser, "headers", HttpParser_headers, 2); rb_define_method(cHttpParser, "trailers", HttpParser_headers, 2); rb_define_method(cHttpParser, "filter_body", HttpParser_filter_body, 2); diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb index e72f571..a0435d6 100644 --- a/lib/unicorn/http_request.rb +++ b/lib/unicorn/http_request.rb @@ -68,9 +68,7 @@ class Unicorn::HttpParser if parse.nil? # Parser is not done, queue up more data to read and continue parsing # an Exception thrown from the parser will throw us out of the loop - begin - buf << socket.kgio_read!(16384) - end while parse.nil? + false until add_parse(socket.kgio_read!(16384)) end e[RACK_INPUT] = 0 == content_length ? NULL_IO : @@input_class.new(socket, self) -- cgit v1.2.3-24-ge0c7