views:

883

answers:

3

I am debugging some code in the Selenium-rc proxy server. It seems the culprit is the HttpURLConnection object, whose interface for getting at the HTTP headers does not cope with duplicate header names, such as:

Set-Cookie: foo=foo; Path=/
Set-Cookie: bar=bar; Path=/

The way of getting at the headers through the HttpURLConnection(using getHeaderField(int n) and getHeaderFieldKey(int n)) seems to be causing my second cookie to be lost. My question is

  1. Is it true that HttpURLConnection itself can't cope with it, and
  2. If so, is there a workaround to it?
A: 

Without actually having tried it (can't remember to have handled that topic myself), there's also getHeaderFields, inherited from UrlConnection. Does this do what you need?

Olaf
No, that methods returns a Map, which definitely won't have duplicate header names.
toby
...but it returns a map with List values, not just single Strings...
Olaf
+1  A: 

My recommended workaround is to not use HttpUtilConnection at all, which is crude and unintuitive, but use commons-httpclient instead.

http://hc.apache.org/httpclient-3.x/

skaffman
A: 

Ok, I found the problem, and the answer to the original question. Basically, the Cookie implementation I used (python's default Cookie Lib) used \r\n to delimit the different Set-Cookie headers(as supposed to \n), this confused HttpUrlConnection and caused it to stop at the first occurence of that delimiter(I am going to guess it stops at the first empty line). So the answer to the first question is: Yes, it can cope with duplicate header names, but is buggy in another way. Currently fixing the python library is a workable workaround, but it's not going to work long term because we don't own that library. I am sure using the httpclient library is a sensible way to go, but I am hoping for a solution that requires less work. So I don't know exactly what to do there yet.

toby
Unless you have references to HttpUtilConnection scattered across your codebase, I honestly think that moving to commons-httpclient will be less work than fixing HttpUtlConnection.
skaffman
Correct me if I'm wrong, but CRLF (\r\n) is the right delimiter for delimiting one HTTP header from the next one according to HTTP RFCs.
Alexander