Skip to content

Commit c171294

Browse files
designfrontierevanlucas
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 <[email protected]>
1 parent 2879395 commit c171294

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

doc/api/http.markdown

+11
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

+17-9
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');
@@ -391,7 +390,7 @@ function connectionListener(socket) {
391390
parser = null;
392391

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

@@ -516,14 +515,23 @@ function connectionListener(socket) {
516515
}
517516

518517
if (req.headers.expect !== undefined &&
519-
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1) &&
520-
continueExpression.test(req.headers['expect'])) {
521-
res._expect_continue = true;
522-
if (EventEmitter.listenerCount(self, 'checkContinue') > 0) {
523-
self.emit('checkContinue', req, res);
518+
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1)) {
519+
if (continueExpression.test(req.headers.expect)) {
520+
res._expect_continue = true;
521+
522+
if (self.listenerCount('checkContinue') > 0) {
523+
self.emit('checkContinue', req, res);
524+
} else {
525+
res.writeContinue();
526+
self.emit('request', req, res);
527+
}
524528
} else {
525-
res.writeContinue();
526-
self.emit('request', req, res);
529+
if (self.listenerCount('checkExpectation') > 0) {
530+
self.emit('checkExpectation', req, res);
531+
} else {
532+
res.writeHead(417);
533+
res.end();
534+
}
527535
}
528536
} else {
529537
self.emit('request', req, res);
+55
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)