-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: send Content-Length when possible
This changes the behavior for http to send send a Content-Length header instead of using chunked encoding when we know the size of the body when sending the headers. Fixes: #1044 PR-URL: #1062 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Rod Vagg <[email protected]> Reviewed-By: Brian White <[email protected]>
- Loading branch information
Showing
6 changed files
with
129 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
var http = require('http'); | ||
|
||
var expectedHeadersMultipleWrites = { | ||
'connection': 'close', | ||
'transfer-encoding': 'chunked', | ||
}; | ||
|
||
var expectedHeadersEndWithData = { | ||
'connection': 'close', | ||
'content-length': 'hello world'.length, | ||
}; | ||
|
||
var expectedHeadersEndNoData = { | ||
'connection': 'close', | ||
'content-length': '0', | ||
}; | ||
|
||
var receivedRequests = 0; | ||
var totalRequests = 2; | ||
|
||
var server = http.createServer(function(req, res) { | ||
res.removeHeader('Date'); | ||
|
||
switch (req.url.substr(1)) { | ||
case 'multiple-writes': | ||
assert.deepEqual(req.headers, expectedHeadersMultipleWrites); | ||
res.write('hello'); | ||
res.end('world'); | ||
break; | ||
case 'end-with-data': | ||
assert.deepEqual(req.headers, expectedHeadersEndWithData); | ||
res.end('hello world'); | ||
break; | ||
case 'empty': | ||
assert.deepEqual(req.headers, expectedHeadersEndNoData); | ||
res.end(); | ||
break; | ||
default: | ||
throw new Error('Unreachable'); | ||
break; | ||
} | ||
|
||
receivedRequests++; | ||
if (totalRequests === receivedRequests) server.close(); | ||
}); | ||
|
||
server.listen(common.PORT, function() { | ||
var req; | ||
|
||
req = http.request({ | ||
port: common.PORT, | ||
method: 'POST', | ||
path: '/multiple-writes' | ||
}); | ||
req.removeHeader('Date'); | ||
req.removeHeader('Host'); | ||
req.write('hello '); | ||
req.end('world'); | ||
req.on('response', function(res) { | ||
assert.deepEqual(res.headers, expectedHeadersMultipleWrites); | ||
}); | ||
|
||
req = http.request({ | ||
port: common.PORT, | ||
method: 'POST', | ||
path: '/end-with-data' | ||
}); | ||
req.removeHeader('Date'); | ||
req.removeHeader('Host'); | ||
req.end('hello world'); | ||
req.on('response', function(res) { | ||
assert.deepEqual(res.headers, expectedHeadersEndWithData); | ||
}); | ||
|
||
req = http.request({ | ||
port: common.PORT, | ||
method: 'POST', | ||
path: '/empty' | ||
}); | ||
req.removeHeader('Date'); | ||
req.removeHeader('Host'); | ||
req.end(); | ||
req.on('response', function(res) { | ||
assert.deepEqual(res.headers, expectedHeadersEndNoData); | ||
}); | ||
|
||
}); |
This file contains 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 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