http: improves expect header handling · nodejs/node@d755432 · GitHub
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

3 files changed

Lines changed: 83 additions & 9 deletions

File tree

doc/api/http.markdown

Lines changed: 11 additions & 0 deletions

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

0 commit comments

Comments
 (0)