Patches: Browse | Submit New | Admin

[#24085] HttpClient: Support multiple occurences of the same header

Date:
2009-02-23 23:47
Priority:
3
Submitted By:
Andreas Karlsson (jeltz)
Assigned To:
Nobody (None)
Category:
None
State:
Open
Summary:
HttpClient: Support multiple occurences of the same header

Detailed description
Quite often webserver give you multiple cookies as separate lines in the HTTP headers. My solution to this is to put
them in array if you get the same type of header more than once. This solution is not ideal but it is better than not
being able to see the headers at all.

To test my patch use the following code in irb.

Without my patch:

require 'rubygems'
=> true
irb(main):002:0> require 'rev'
=> true
irb(main):003:0>  h = {}; Rev::HttpClientParser.new.execute(h, "HTTP/1.0 200 OK\r\nFoo: bar\r\nFoo: baz\r\nBar:
foo\r\nFoo: foo2\r\n\r\n", 0); h
=> {"FOO"=>"foo2", "BAR"=>"foo"}

With my patch:

irb(main):001:0> require 'rev'
=> true
irb(main):002:0>  h = {}; Rev::HttpClientParser.new.execute(h, "HTTP/1.0 200 OK\r\nFoo: bar\r\nFoo: baz\r\nBar:
foo\r\nFoo: foo2\r\n\r\n", 0); h
=> {"FOO"=>["bar", "baz", "foo2"], "BAR"=>"foo"}

diff --git a/ext/http11_client/http11_client.c b/ext/http11_client/http11_client.c
index 4cd7140..9b0903c 100644
--- a/ext/http11_client/http11_client.c
+++ b/ext/http11_client/http11_client.c
@@ -27,6 +27,7 @@ void client_http_field(void *data, const char *field, size_t flen, const char *v
   VALUE req = (VALUE)data;
   VALUE v = Qnil;
   VALUE f = Qnil;
+  VALUE currv;
 
   v = rb_str_new(value, vlen);
   f = rb_str_new(field, flen);
@@ -41,7 +42,14 @@ void client_http_field(void *data, const char *field, size_t flen, const char *v
     }
   }
 
-  rb_hash_aset(req, f, v);
+  currv = rb_hash_aref(req, f);
+  if(currv == Qnil) {
+    rb_hash_aset(req, f, v);
+  } else if(TYPE(currv) == T_ARRAY) {
+    rb_ary_push(currv, v);
+  } else {
+    rb_hash_aset(req, f, rb_ary_new3(2, currv, v));
+  }
 }
 
 void client_reason_phrase(void *data, const char *at, size_t length)

Add A Comment: Notepad

Please login


Followup

No Followups Have Been Posted

Attached Files:

Name Description Download
No Files Currently Attached

Changes:

No Changes Have Been Made to This Item