-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
http: append Cookie header values with semicolon #11259
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
Merged
mscdex
merged 2 commits into
nodejs:master
from
mscdex:http-use-semicolon-for-cookie-header
Mar 9, 2017
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev
Previous commit
http: concatenate outgoing Cookie headers
This commit enables automatic concatenation of multiple Cookie header values with a semicolon, except when 2D header arrays are used. Fixes: #11256 PR-URL: #11259 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
- Loading branch information
commit d3480776c70bb1622afee5a2a5f39abf9e42a507
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,84 +1,113 @@ | ||
| 'use strict'; | ||
| require('../common'); | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const http = require('http'); | ||
| const url = require('url'); | ||
|
|
||
| let responses_sent = 0; | ||
| let responses_recvd = 0; | ||
| let body0 = ''; | ||
| let body1 = ''; | ||
| const expectedRequests = ['/hello', '/there', '/world']; | ||
|
|
||
| const server = http.Server(function(req, res) { | ||
| if (responses_sent === 0) { | ||
| assert.strictEqual('GET', req.method); | ||
| assert.strictEqual('/hello', url.parse(req.url).pathname); | ||
| const server = http.Server(common.mustCall(function(req, res) { | ||
| assert.strictEqual(expectedRequests.shift(), req.url); | ||
|
|
||
| console.dir(req.headers); | ||
| assert.strictEqual(true, 'accept' in req.headers); | ||
| assert.strictEqual('*/*', req.headers['accept']); | ||
|
|
||
| assert.strictEqual(true, 'foo' in req.headers); | ||
| assert.strictEqual('bar', req.headers['foo']); | ||
| switch (req.url) { | ||
| case '/hello': | ||
| assert.strictEqual(req.method, 'GET'); | ||
| assert.strictEqual(req.headers['accept'], '*/*'); | ||
| assert.strictEqual(req.headers['foo'], 'bar'); | ||
| assert.strictEqual(req.headers.cookie, 'foo=bar; bar=baz; baz=quux'); | ||
| break; | ||
| case '/there': | ||
| assert.strictEqual(req.method, 'PUT'); | ||
| assert.strictEqual(req.headers.cookie, 'node=awesome; ta=da'); | ||
| break; | ||
| case '/world': | ||
| assert.strictEqual(req.method, 'POST'); | ||
| assert.deepStrictEqual(req.headers.cookie, 'abc=123; def=456; ghi=789'); | ||
| break; | ||
| default: | ||
| assert(false, `Unexpected request for ${req.url}`); | ||
| } | ||
|
|
||
| if (responses_sent === 1) { | ||
| assert.strictEqual('POST', req.method); | ||
| assert.strictEqual('/world', url.parse(req.url).pathname); | ||
| if (expectedRequests.length === 0) | ||
| this.close(); | ||
| } | ||
|
|
||
| req.on('end', function() { | ||
| res.writeHead(200, {'Content-Type': 'text/plain'}); | ||
| res.write('The path was ' + url.parse(req.url).pathname); | ||
| res.end(); | ||
| responses_sent += 1; | ||
| }); | ||
| req.resume(); | ||
|
|
||
| //assert.strictEqual('127.0.0.1', res.connection.remoteAddress); | ||
| }); | ||
| }, 3)); | ||
| server.listen(0); | ||
|
|
||
| server.on('listening', function() { | ||
| const agent = new http.Agent({ port: this.address().port, maxSockets: 1 }); | ||
| http.get({ | ||
| const req = http.get({ | ||
| port: this.address().port, | ||
| path: '/hello', | ||
| headers: {'Accept': '*/*', 'Foo': 'bar'}, | ||
| headers: { | ||
| Accept: '*/*', | ||
| Foo: 'bar', | ||
| Cookie: [ 'foo=bar', 'bar=baz', 'baz=quux' ] | ||
| }, | ||
| agent: agent | ||
| }, function(res) { | ||
| assert.strictEqual(200, res.statusCode); | ||
| responses_recvd += 1; | ||
| }, common.mustCall(function(res) { | ||
| const cookieHeaders = req._header.match(/^Cookie: .+$/img); | ||
| assert.deepStrictEqual(cookieHeaders, | ||
| ['Cookie: foo=bar; bar=baz; baz=quux']); | ||
| assert.strictEqual(res.statusCode, 200); | ||
| let body = ''; | ||
| res.setEncoding('utf8'); | ||
| res.on('data', function(chunk) { body0 += chunk; }); | ||
| console.error('Got /hello response'); | ||
| }); | ||
| res.on('data', function(chunk) { body += chunk; }); | ||
| res.on('end', common.mustCall(function() { | ||
| assert.strictEqual(body, 'The path was /hello'); | ||
| })); | ||
| })); | ||
|
|
||
| setTimeout(function() { | ||
| setTimeout(common.mustCall(function() { | ||
| const req = http.request({ | ||
| port: server.address().port, | ||
| method: 'PUT', | ||
| path: '/there', | ||
| agent: agent | ||
| }, common.mustCall(function(res) { | ||
| const cookieHeaders = req._header.match(/^Cookie: .+$/img); | ||
| assert.deepStrictEqual(cookieHeaders, ['Cookie: node=awesome; ta=da']); | ||
| assert.strictEqual(res.statusCode, 200); | ||
| let body = ''; | ||
| res.setEncoding('utf8'); | ||
| res.on('data', function(chunk) { body += chunk; }); | ||
| res.on('end', common.mustCall(function() { | ||
| assert.strictEqual(body, 'The path was /there'); | ||
| })); | ||
| })); | ||
| req.setHeader('Cookie', ['node=awesome', 'ta=da']); | ||
| req.end(); | ||
| }), 1); | ||
|
|
||
| setTimeout(common.mustCall(function() { | ||
| const req = http.request({ | ||
| port: server.address().port, | ||
| method: 'POST', | ||
| path: '/world', | ||
| headers: [ ['Cookie', 'abc=123'], | ||
| ['Cookie', 'def=456'], | ||
| ['Cookie', 'ghi=789'] ], | ||
| agent: agent | ||
| }, function(res) { | ||
| assert.strictEqual(200, res.statusCode); | ||
| responses_recvd += 1; | ||
| }, common.mustCall(function(res) { | ||
| const cookieHeaders = req._header.match(/^Cookie: .+$/img); | ||
| assert.deepStrictEqual(cookieHeaders, | ||
| ['Cookie: abc=123', | ||
| 'Cookie: def=456', | ||
| 'Cookie: ghi=789']); | ||
| assert.strictEqual(res.statusCode, 200); | ||
| let body = ''; | ||
| res.setEncoding('utf8'); | ||
| res.on('data', function(chunk) { body1 += chunk; }); | ||
| console.error('Got /world response'); | ||
| }); | ||
| res.on('data', function(chunk) { body += chunk; }); | ||
| res.on('end', common.mustCall(function() { | ||
| assert.strictEqual(body, 'The path was /world'); | ||
| })); | ||
| })); | ||
| req.end(); | ||
| }, 1); | ||
| }); | ||
|
|
||
| process.on('exit', function() { | ||
| console.error('responses_recvd: ' + responses_recvd); | ||
| assert.strictEqual(2, responses_recvd); | ||
|
|
||
| console.error('responses_sent: ' + responses_sent); | ||
| assert.strictEqual(2, responses_sent); | ||
|
|
||
| assert.strictEqual('The path was /hello', body0); | ||
| assert.strictEqual('The path was /world', body1); | ||
| }), 2); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about lowercasing the string and comparing?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toLowerCase()+ compare is very slow (slower than a case-insensitive regexp IIRC).