Skip to content
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: be more specific when reporting errors #14759

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ function storeHeader(self, state, key, value, validate) {
throw new Error('Header "%s" value must not be undefined', key);
} else if (checkInvalidHeaderChar(value)) {
debug('Header "%s" contains invalid characters', key);
throw new TypeError('The header content contains invalid characters');
throw new TypeError(
'The header content contains invalid characters ["' + key + '"]');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the error messages are being changed, it would be nice to migrate the files to use internal/errors at the same time.

}
}
state.header += key + ': ' + escapeHeaderValue(value) + CRLF;
Expand Down Expand Up @@ -498,7 +499,8 @@ function validateHeader(msg, name, value) {
throw new Error('Can\'t set headers after they are sent.');
if (checkInvalidHeaderChar(value)) {
debug('Header "%s" contains invalid characters', name);
throw new TypeError('The header content contains invalid characters');
throw new TypeError(
'The header content contains invalid characters ["' + name + '"]');
}
}
OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-outgoing-proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ assert.throws(() => {
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader('200', 'あ');
}, /^TypeError: The header content contains invalid characters$/);
}, /^TypeError: The header content contains invalid characters \["200"\]$/);

// write
assert.throws(() => {
Expand Down
13 changes: 8 additions & 5 deletions test/parallel/test-http-response-splitting.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@ const y = 'foo⠊Set-Cookie: foo=bar';

let count = 0;

function test(res, code, header) {
function test(res, code, header, failing) {
const expected =
'^TypeError: The header content contains invalid characters \\["' +
failing + '"\\]$';
assert.throws(() => {
res.writeHead(code, header);
}, /^TypeError: The header content contains invalid characters$/);
}, new RegExp(expected));
}

const server = http.createServer((req, res) => {
switch (count++) {
case 0:
const loc = url.parse(req.url, true).query.lang;
test(res, 302, { Location: `/foo?lang=${loc}` });
test(res, 302, { Location: `/foo?lang=${loc}` }, 'Location');
break;
case 1:
test(res, 200, { 'foo': x });
test(res, 200, { 'foo': x }, 'foo');
break;
case 2:
test(res, 200, { 'foo': y });
test(res, 200, { 'foo': y }, 'foo');
break;
default:
assert.fail('should not get to here.');
Expand Down