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

Response Header.get() implementaion is wrong #911

Closed
giy-debug opened this issue Aug 8, 2020 · 5 comments
Closed

Response Header.get() implementaion is wrong #911

giy-debug opened this issue Aug 8, 2020 · 5 comments
Labels

Comments

@giy-debug
Copy link

The Response Headers implementation is wrong. The Headers.get function converts arrays to strings. This neither conforms to Map nor the URLSearchParams class definitions. This is a problem when you try to parse the set-cookie headers

/**
  * Return combined header value given name
  *
  * @param   String  name  Header name
  * @return  Mixed
  */
	get(name) {
		name = `${name}`;
		validateName(name);
		const key = find(this[MAP], name);
		if (key === undefined) {
			return null;
		}

		return this[MAP][key].join(', '); //this line is wrong
	}

Expected behavior, if applicable

Map implementation

const map = new Map();
map.set('set-cookie', ['cookie1','cookie2'])
map.get('set-cookie'); 
// ['cookie1','cookie2']

The Headers class looks like a re-implementaion of URLSearchParams class, which is supported natively. You could save yourself a lot of code, and the implementation could match the standard.

const searchParams = new URLSearchParams();
searchParams.append('set-cookie', 'cookie1');
searchParams.append('set-cookie', 'cookie2');
searchParams.get('set-cookie');      //"cookie1"
searchParams.getAll('set-cookie');  //["cookie1", "cookie2"]

As you can see searchParams.get('set-cookie') doesn't join the values but Headers.getAll doesn't exist in your implementation.

Your Environment

software version
node-fetch 2.6.0
node 12.18.2
npm 6.14.5
Operating System Win10

From MDN

https://developer.mozilla.org/de/docs/Web/API/Headers

Obsolete methods

Headers.getAll()
Used to return an array of all the values of a header within a Headers object with a given name; this method has now been deleted from the spec, and Headers.get() now returns all values of a given name instead of just the first one

@jimmywarting
Copy link
Collaborator

Did you forgot to read the part of where it says: "Obsolete"?

Used to return an array of all the values of a header within a Headers object with a given name; this method has now been deleted from the spec, and Headers.get() now returns all values of a given name instead of just the first one.

@jimmywarting
Copy link
Collaborator

jimmywarting commented Aug 8, 2020

I agree it's somewhat problematic. but we try to follow the spec
workaround: #251 (comment)

@giy-debug
Copy link
Author

giy-debug commented Aug 8, 2020

But the join is still wrong. And it also says Headers.get() now returns all values of a given name

@jimmywarting
Copy link
Collaborator

jimmywarting commented Aug 8, 2020

But the join is still wrong. And it also says Headers.get() now returns all values of a given name

Did MDN clearly say how to return it? no, there is no mentioning of array or string in that sentence.
But one thing is for certain: They also mention elsewhere that it's returning a string...

Headers.get()
Returns a ByteString sequence of all the values of a header within a Headers object with a given name.

The official spec mention it also:

To get a name name from a header list list, run these steps:

  1. If list does not contain name, then return null.
  2. Return the values of all headers in list whose name is a byte-case-insensitive match for name, separated from each other by 0x2C 0x20, in order.

https://fetch.spec.whatwg.org/#terminology-headers

0x2C === comma
0x20 === space

You can test out the browser behavior in this fiddle: https://jsfiddle.net/zL04o17w/ (same thing)

@jimmywarting
Copy link
Collaborator

This might be relevant for ya: whatwg/fetch#973

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants