forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: add headersTimeout timer and response logic
Fixes: nodejs#33440
- Loading branch information
1 parent
f7be6ab
commit ab8677f
Showing
18 changed files
with
327 additions
and
287 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
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
62 changes: 62 additions & 0 deletions
62
test/parallel/test-http-server-headers-timeout-delayed-headers.js
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,62 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { createServer } = require('http'); | ||
const { connect } = require('net'); | ||
|
||
// This test validates that the server returns 408 | ||
// after server.headersTimeout if the client | ||
// pauses before start sending the request. | ||
|
||
let sendDelayedRequestHeaders; | ||
const server = createServer(common.mustNotCall()); | ||
server.on('connection', common.mustCall(() => { | ||
assert.strictEqual(typeof sendDelayedRequestHeaders, 'function'); | ||
sendDelayedRequestHeaders(); | ||
})); | ||
|
||
// 60 seconds is the default | ||
assert.strictEqual(server.headersTimeout, 60000); | ||
const headersTimeout = common.platformTimeout(1000); | ||
server.headersTimeout = headersTimeout; | ||
assert.strictEqual(server.headersTimeout, headersTimeout); | ||
|
||
// Make sure requestTimeout is big enough for the headersTimeout. | ||
server.requestTimeout = 0; | ||
|
||
// Check that timeout event is not triggered | ||
server.once('timeout', common.mustNotCall((socket) => { | ||
socket.destroy(); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const client = connect(server.address().port); | ||
let response = ''; | ||
|
||
client.on('data', common.mustCall((chunk) => { | ||
response += chunk.toString('utf-8'); | ||
})); | ||
|
||
const errOrEnd = common.mustSucceed(function(err) { | ||
assert.strictEqual( | ||
response, | ||
'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n' | ||
); | ||
server.close(); | ||
}); | ||
|
||
client.on('end', errOrEnd); | ||
client.on('error', errOrEnd); | ||
|
||
client.resume(); | ||
|
||
sendDelayedRequestHeaders = common.mustCall(() => { | ||
setTimeout(() => { | ||
client.write('POST / HTTP/1.1\r\n'); | ||
client.write('Content-Length: 20\r\n'); | ||
client.write('Connection: close\r\n\r\n'); | ||
client.write('12345678901234567890\r\n\r\n'); | ||
}, common.platformTimeout(headersTimeout * 2)).unref(); | ||
}); | ||
})); |
62 changes: 62 additions & 0 deletions
62
test/parallel/test-http-server-headers-timeout-interrupted-headers.js
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,62 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { createServer } = require('http'); | ||
const { connect } = require('net'); | ||
|
||
// This test validates that the server returns 408 | ||
// after server.headersTimeout if the client | ||
// pauses sending in the middle of a header. | ||
|
||
let sendDelayedRequestHeaders; | ||
const server = createServer(common.mustNotCall()); | ||
server.on('connection', common.mustCall(() => { | ||
assert.strictEqual(typeof sendDelayedRequestHeaders, 'function'); | ||
sendDelayedRequestHeaders(); | ||
})); | ||
|
||
// 60 seconds is the default | ||
assert.strictEqual(server.headersTimeout, 60000); | ||
const headersTimeout = common.platformTimeout(1000); | ||
server.headersTimeout = headersTimeout; | ||
assert.strictEqual(server.headersTimeout, headersTimeout); | ||
|
||
// Make sure requestTimeout is big enough for the headersTimeout. | ||
server.requestTimeout = 0; | ||
|
||
// Check that timeout event is not triggered | ||
server.once('timeout', common.mustNotCall((socket) => { | ||
socket.destroy(); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const client = connect(server.address().port); | ||
let response = ''; | ||
|
||
client.on('data', common.mustCall((chunk) => { | ||
response += chunk.toString('utf-8'); | ||
})); | ||
|
||
const errOrEnd = common.mustSucceed(function(err) { | ||
assert.strictEqual( | ||
response, | ||
'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n' | ||
); | ||
server.close(); | ||
}); | ||
|
||
client.on('end', errOrEnd); | ||
client.on('error', errOrEnd); | ||
|
||
client.resume(); | ||
client.write('GET / HTTP/1.1\r\n'); | ||
client.write('Connection: close\r\n'); | ||
client.write('X-CRASH: '); | ||
|
||
sendDelayedRequestHeaders = common.mustCall(() => { | ||
setTimeout(() => { | ||
client.write('1234567890\r\n\r\n'); | ||
}, common.platformTimeout(headersTimeout * 2)).unref(); | ||
}); | ||
})); |
Oops, something went wrong.