Skip to content

Commit d755432

Browse files
designfrontierjasnell
authored andcommitted
http: improves expect header handling
Now returns a 417 error status or allows for an event listener on the `checkExpectation` event. Before we were ignoring requests that had misspelled `100-continue` values for expect headers. This is a quick port of the work done here: nodejs/node-v0.x-archive#7132 by alFReD-NSH with surrounding discussion here: nodejs/node-v0.x-archive#4651 Also updates all the instances of the deprecated EventEmitter.listenerCount to the current self.listenerCount. Most of these were in the new code ported over but there was another legacy instance. Refs: #2403 PR-URL: #4501 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 4383acd commit d755432

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

doc/api/http.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ The request implements the [Writable Stream][] interface. This is an
192192
Emitted when the request has been aborted by the client. This event is only
193193
emitted on the first call to `abort()`.
194194

195+
### Event: 'checkExpectation'
196+
197+
`function (request, response) { }`
198+
199+
Emitted each time a request with an http Expect header is received, where the
200+
value is not 100-continue. If this event isn't listened for, the server will
201+
automatically respond with a 417 Expectation Failed as appropriate.
202+
203+
Note that when this event is emitted and handled, the `request` event will
204+
not be emitted.
205+
195206
### Event: 'connect'
196207

197208
`function (response, socket, head) { }`

lib/_http_server.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const util = require('util');
44
const net = require('net');
5-
const EventEmitter = require('events');
65
const HTTPParser = process.binding('http_parser').HTTPParser;
76
const assert = require('assert').ok;
87
const common = require('_http_common');
@@ -392,7 +391,7 @@ function connectionListener(socket) {
392391
parser = null;
393392

394393
var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
395-
if (EventEmitter.listenerCount(self, eventName) > 0) {
394+
if (self.listenerCount(eventName) > 0) {
396395
debug('SERVER have listener for %s', eventName);
397396
var bodyHead = d.slice(bytesParsed, d.length);
398397

@@ -517,14 +516,23 @@ function connectionListener(socket) {
517516
}
518517

519518
if (req.headers.expect !== undefined &&
520-
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1) &&
521-
continueExpression.test(req.headers['expect'])) {
522-
res._expect_continue = true;
523-
if (EventEmitter.listenerCount(self, 'checkContinue') > 0) {
524-
self.emit('checkContinue', req, res);
519+
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1)) {
520+
if (continueExpression.test(req.headers.expect)) {
521+
res._expect_continue = true;
522+
523+
if (self.listenerCount('checkContinue') > 0) {
524+
self.emit('checkContinue', req, res);
525+
} else {
526+
res.writeContinue();
527+
self.emit('request', req, res);
528+
}
525529
} else {
526-
res.writeContinue();
527-
self.emit('request', req, res);
530+
if (self.listenerCount('checkExpectation') > 0) {
531+
self.emit('checkExpectation', req, res);
532+
} else {
533+
res.writeHead(417);
534+
res.end();
535+
}
528536
}
529537
} else {
530538
self.emit('request', req, res);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Spec documentation http://httpwg.github.io/specs/rfc7231.html#header.expect
2+
'use strict';
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
7+
const tests = [417, 417];
8+
9+
let testsComplete = 0;
10+
let testIdx = 0;
11+
12+
const s = http.createServer(function(req, res) {
13+
throw new Error('this should never be executed');
14+
});
15+
16+
s.listen(common.PORT, nextTest);
17+
18+
function nextTest() {
19+
const options = {
20+
port: common.PORT,
21+
headers: { 'Expect': 'meoww' }
22+
};
23+
24+
if (testIdx === tests.length) {
25+
return s.close();
26+
}
27+
28+
const test = tests[testIdx];
29+
30+
if (testIdx > 0) {
31+
s.on('checkExpectation', common.mustCall((req, res) => {
32+
res.statusCode = 417;
33+
res.end();
34+
}));
35+
}
36+
37+
http.get(options, function(response) {
38+
console.log('client: expected status: ' + test);
39+
console.log('client: statusCode: ' + response.statusCode);
40+
assert.equal(response.statusCode, test);
41+
assert.equal(response.statusMessage, 'Expectation Failed');
42+
43+
response.on('end', function() {
44+
testsComplete++;
45+
testIdx++;
46+
nextTest();
47+
});
48+
response.resume();
49+
});
50+
}
51+
52+
53+
process.on('exit', function() {
54+
assert.equal(2, testsComplete);
55+
});

0 commit comments

Comments
 (0)