question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't add multiple headers with the same name (cookies)

See original GitHub issue

The code:

val (request, response, result) = reqUrl.httpGet()
                        .header(Pair("Cookie", "val1=x"),
                                Pair("Cookie","val2=y"),
                                Pair("Cookie","val3=z"))
                        .responseString()

Expected functionality:

All cookies are included in request

Actual functionality:

Only the last Pair with val3 is included in the request

This happens because, in Request.kt line 28, the headers variable is a MutableMap, instead of perhaps an ArrayList of Pairs or a similar datatype.

Multiple headers with the same name is allowed in the HTTP spec, section 4.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
markGilchristcommented, Jul 26, 2018

@SleeplessByte do you have any thoughts on the comment above? I think producing

Cookie: val1=x
Cookie: val2=y
Cookie: val3=z

is almost impossible, however

cookie: a=b; c=d; e=f

is what my current pr hope to achieve.

@Fibonacci- your feedback would be warmly welcome!

0reactions
SleeplessBytecommented, Jul 25, 2018

AAs per the spec, you should add the space: i.e. this would be Cookie: val1=x; val2=y; val3=z. Note that I used a semi-colon, which is the correct delimiter. Here is the RFC spec:

https://tools.ietf.org/html/rfc7540#section-8.1.2.5

8.1.2.5.  Compressing the Cookie Header Field

   The Cookie header field [COOKIE] uses a semi-colon (";") to delimit
   cookie-pairs (or "crumbs").  This header field doesn't follow the
   list construction rules in HTTP (see [RFC7230], Section 3.2.2), which
   prevents cookie-pairs from being separated into different name-value
   pairs.  This can significantly reduce compression efficiency as
   individual cookie-pairs are updated.

   To allow for better compression efficiency, the Cookie header field
   MAY be split into separate header fields, each with one or more
   cookie-pairs.  If there are multiple Cookie header fields after
   decompression, these MUST be concatenated into a single octet string
   using the two-octet delimiter of 0x3B, 0x20 (the ASCII string "; ")
   before being passed into a non-HTTP/2 context, such as an HTTP/1.1
   connection, or a generic HTTP server application.

   Therefore, the following two lists of Cookie header fields are
   semantically equivalent.

     cookie: a=b; c=d; e=f

     cookie: a=b
     cookie: c=d
     cookie: e=f

However from experience I know that this format:

Cookie: val1=x
Cookie: val2=y
Cookie: val3=z

is not accepted by ALL nodes out there (especially older software/nodes will simply drop all but the first or drop all but the last).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle multiple cookies with the same name?
If I access /example on the server both paths are valid, so I have two cookies named "a"! Since the browser doesn't send...
Read more >
multiple headers of the "same name" allowed in http #3591
It seems like the solution here is to either allow multiple "same name" headers, or, fail loudly when the user tries to send...
Read more >
Can't add more than one cookie with the same name
Issue. We have two cookies with the same name, but different paths. One of the headers is being ignored.
Read more >
URL Fetch cannot get multiple cookie headers [35887589]
Yes, it's a bug. The response contains multiple headers, so the library should return those headers as they are in the response and...
Read more >
setcookie - Manual - PHP
Note that the $_COOKIE variable not will hold multiple cookies with the same name. It is legitimate to set two cookies with the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found