Skip to content

Commit b82ed47

Browse files
http: convert content-length to number
1 parent 5d9b63d commit b82ed47

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/_http_outgoing.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {
2525
Array,
2626
ArrayIsArray,
2727
MathFloor,
28+
NumberParseInt,
2829
ObjectDefineProperty,
2930
ObjectHasOwn,
3031
ObjectKeys,
@@ -650,7 +651,7 @@ function matchHeader(self, state, field, value) {
650651
break;
651652
case 'content-length':
652653
state.contLen = true;
653-
self._contentLength = value;
654+
self._contentLength = ~~value;
654655
self._removedContLen = false;
655656
break;
656657
case 'date':

test/parallel/test-http-content-length-mismatch.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,23 @@ function shouldThrowOnFewerBytes() {
7878
shouldThrowOnMoreBytes();
7979
shouldNotThrow();
8080
shouldThrowOnFewerBytes();
81+
82+
83+
{
84+
const server = http.createServer(common.mustCall((req, res) => {
85+
res.strictContentLength = true;
86+
// Pass content-length as string
87+
res.setHeader('content-length', '5');
88+
res.end('12345');
89+
}));
90+
91+
92+
server.listen(0, common.mustCall(() => {
93+
http.get({ port: server.address().port }, (res) => {
94+
res.resume().on('end', () => {
95+
assert.strictEqual(res.statusCode, 200);
96+
server.close();
97+
});
98+
});
99+
}));
100+
}

0 commit comments

Comments
 (0)