mirror of mongrel-development@rubyforge.org (inactive)
 help / color / mirror / Atom feed
* [PATCH] join repeated headers with a comma
@ 2009-08-10  0:10 Eric Wong
       [not found] ` <20090810001022.GA17572-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2009-08-10  0:10 UTC (permalink / raw)
  To: mongrel-development-GrnCvJ7WPxnNLxjTenLetw

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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] join repeated headers with a comma
       [not found] ` <20090810001022.GA17572-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
@ 2009-08-10  0:19   ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2009-08-10  0:19 UTC (permalink / raw)
  To: mongrel-development-GrnCvJ7WPxnNLxjTenLetw

> --- 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);
> +  }
>  }

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-08-10  0:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-10  0:10 [PATCH] join repeated headers with a comma Eric Wong
     [not found] ` <20090810001022.GA17572-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
2009-08-10  0:19   ` Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).