-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
http: verify method is a string #10111
Changes from 4 commits
242c5c9
c711ac7
29f78c1
7774c3a
9561854
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,11 @@ function ClientRequest(options, cb) { | |
self.socketPath = options.socketPath; | ||
self.timeout = options.timeout; | ||
|
||
var method = self.method = (options.method || 'GET').toUpperCase(); | ||
let method = options.method; | ||
if (method != null && typeof method !== 'string') { | ||
throw new TypeError('Method must be a string'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the best backwards compatibility (and to emulate the method value selection behavior below the if (options.method && typeof options.method !== 'string') {
throw new TypeError('Method must be a string');
} The reason being that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mscdex that would not fix the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it's a difference between semver-ness. This change is definitely going to be semver-major as-is, but it could be semver-patch with the check I suggested. It's up to you. |
||
method = self.method = (method || 'GET').toUpperCase(); | ||
if (!common._checkIsHttpToken(method)) { | ||
throw new TypeError('Method must be a valid HTTP token'); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const expectedSuccesses = [undefined, null, 'GET', 'post']; | ||
let requestCount = 0; | ||
|
||
const server = http.createServer((req, res) => { | ||
requestCount++; | ||
res.end(); | ||
|
||
if (expectedSuccesses.length === requestCount) { | ||
server.close(); | ||
} | ||
}).listen(0, test); | ||
|
||
function test() { | ||
function fail(input) { | ||
assert.throws(() => { | ||
http.request({ method: input, path: '/' }, common.fail); | ||
}, /^TypeError: Method must be a string$/); | ||
} | ||
|
||
fail(-1); | ||
fail(1); | ||
fail(0); | ||
fail({}); | ||
fail(true); | ||
fail(false); | ||
fail([]); | ||
|
||
function ok(method) { | ||
http.request({ method: method, port: server.address().port }).end(); | ||
} | ||
|
||
expectedSuccesses.forEach((method) => { | ||
ok(method); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you keep
var
. We haven't really made the switch tolet
inlib/
code for performance reasons.