Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Headers object iterators for multiple values of the Set-Cookie header #230

Closed
jl3053 opened this issue Dec 19, 2022 · 1 comment
Closed

Comments

@jl3053
Copy link

jl3053 commented Dec 19, 2022

Currently, if you attempt to iterate over a Headers object in a worker with multiple Set-Cookie values, the iterator will only yield a single, comma-delimited value (which breaks the Set-Cookie header) instead of multiple, independent values.

For example:

const headers = new Headers();
headers.append("Set-Cookie", "A=1");
headers.append("Set-Cookie", "B=2");

console.log("Headers from Headers.entries:")
for (const [k, v] of headers.entries()) {
    console.log(`${k}: ${v}`);
}

console.log("\nHeaders from Headers.forEach:")
headers.forEach((v, k) => {
    console.log(`${k}: ${v}`);
});

Expected outcome:

Headers from Headers.entries:
set-cookie: A=1
set-cookie: B=2

Headers from Headers.forEach:
set-cookie: A=1
set-cookie: B=2

Actual outcome:

Headers from Headers.entries:
set-cookie: A=1, B=2

Headers from Headers.forEach:
set-cookie: A=1, B=2

While this could be worked around using Headers.getAll for the Set-Cookie header as documented here, supporting iterators for Set-Cookie would make it much easier to do things like merge multiple Headers objects and read all headers at once without breaking anything.

@kentonv
Copy link
Member

kentonv commented Dec 19, 2022

Hi @jtk3053,

I agree that would be a better API. However, unfortunately, this is governed by the spec and not something we can change.

The handling of Set-Cookie in the Headers API is a contentious subject, given that this header is the only known header that breaks HTTP's comma-concatenation rule, and this header is also never visible directly from browser-side JS, therefore the browser designers prefer to ignore it. See e.g.: whatwg/fetch#973

We support getAll() as a work-around, since it doesn't create any incompatibility with the spec (in fact, it was once part of the spec, though removed).

In contrast, changing the way iterators work such that the same key could be returned multiple times would be a pretty clear spec violation, conceivably leading to bugs in applications that aren't expecting this. If we really wanted to provide a way to iterate in this way, we'd probably have to give it a different name, like entriesWithMultipleSetCookie() or something. I don't really think this would be worth it, I think apps will just have to special-case Set-Cookie and use getAll() to handle it.

@kentonv kentonv closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants