Eric Wong
2009-Aug-10 00:10 UTC
[Mongrel-development] [PATCH] join repeated headers with a comma
These are joined in in accordance with rfc2616, section 4.2[1]. This is also ticket #50 in Trac: http://mongrel.rubyforge.org/ticket/50 [1] - http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 --- This applies against c365ba16d12a14bdf1cc50a26f67dd3b45f5a4d8 in Evan''s fauna repository, I''m completely certain where I should be applying this but it should be trivial to port this patch to anything else remotely resembling it... ext/http11/http11.c | 9 ++++++++- test/unit/test_http_parser.rb | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ext/http11/http11.c b/ext/http11/http11.c index a228019..a770c60 100644 --- a/ext/http11/http11.c +++ b/ext/http11/http11.c @@ -182,6 +182,7 @@ void http_field(void *data, const char *field, size_t flen, const char *value, s VALUE req = (VALUE)data; VALUE v = Qnil; VALUE f = Qnil; + VALUE e = Qnil; VALIDATE_MAX_LENGTH(flen, FIELD_NAME); VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE); @@ -208,7 +209,13 @@ void http_field(void *data, const char *field, size_t flen, const char *value, s /* fprintf(stderr, "UNKNOWN HEADER <%s>\n", RSTRING_PTR(f)); */ } - rb_hash_aset(req, f, v); + e = rb_hash_aref(req, f); + if (e == Qnil) { + rb_hash_aset(req, f, v); + } else { + rb_str_buf_cat(e, ",", 1); + rb_str_buf_append(e, v); + } } void request_method(void *data, const char *at, size_t length) diff --git a/test/unit/test_http_parser.rb b/test/unit/test_http_parser.rb index c89cd0d..70d9c91 100644 --- a/test/unit/test_http_parser.rb +++ b/test/unit/test_http_parser.rb @@ -173,6 +173,14 @@ class HttpParserTest < Test::Unit::TestCase assert_equal 4,res["zed"].length, "wrong number for zed" assert_equal "11",res["frank"], "wrong number for frank" end - + + def test_combine_repeat_headers + parser = HttpParser.new + http = "GET / HTTP/1.1\r\nA: 1\r\nA: 2\r\n\r\n" + req = {} + assert_nothing_raised { parser.execute(req, http, 0) } + assert_equal ''1,2'', req[''HTTP_A''] + end + end -- Eric Wong
Eric Wong
2009-Aug-10 00:19 UTC
[Mongrel-development] [PATCH] join repeated headers with a comma
> --- a/ext/http11/http11.c > +++ b/ext/http11/http11.c > @@ -182,6 +182,7 @@ void http_field(void *data, const char *field, size_t flen, const char *value, s > VALUE req = (VALUE)data; > VALUE v = Qnil; > VALUE f = Qnil; > + VALUE e = Qnil; > > VALIDATE_MAX_LENGTH(flen, FIELD_NAME); > VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE);On second thought, we''re doing the concatenation _after_ the length validation. I don''t think this affects things much in practice since we already have an overall header size limit and anything capable of running Ruby/Mongrel shouldn''t have to worry given about a few tens of kilobytes...> @@ -208,7 +209,13 @@ void http_field(void *data, const char *field, size_t flen, const char *value, s > /* fprintf(stderr, "UNKNOWN HEADER <%s>\n", RSTRING_PTR(f)); */ > } > > - rb_hash_aset(req, f, v); > + e = rb_hash_aref(req, f); > + if (e == Qnil) { > + rb_hash_aset(req, f, v); > + } else { > + rb_str_buf_cat(e, ",", 1); > + rb_str_buf_append(e, v); > + } > }