Skip to content
Open
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
19 changes: 18 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
ObjectAssign,
ObjectDefineProperty,
ObjectKeys,
ObjectPrototypeHasOwnProperty,

Check failure on line 32 in lib/_http_client.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Do not use prototype primordials in this file

Check failure on line 32 in lib/_http_client.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Do not use prototype primordials in this file
ObjectSetPrototypeOf,
ReflectApply,
String,
Expand Down Expand Up @@ -331,12 +332,15 @@
function ClientRequest(input, options, cb) {
OutgoingMessage.call(this);

let pathIsFromURL = false;
if (typeof input === 'string') {
const urlStr = input;
input = urlToHttpOptions(new URL(urlStr));
pathIsFromURL = true;
} else if (isURL(input)) {
// url.URL instance
input = urlToHttpOptions(input);
pathIsFromURL = true;
} else {
cb = options;
options = input;
Expand All @@ -347,6 +351,13 @@
cb = options;
options = input || kEmptyObject;
} else {
const hasPathOverride = pathIsFromURL &&
options != null &&
ObjectPrototypeHasOwnProperty(options, 'path');
if (hasPathOverride) {
pathIsFromURL = false;
}

options = ObjectAssign({ __proto__: null }, input, options);
}

Expand Down Expand Up @@ -466,7 +477,13 @@

this.joinDuplicateHeaders = options.joinDuplicateHeaders;

this[kPath] = options.path || '/';
let path = options.path || '/';
// Strip the leading slash added when the CONNECT target comes from a URL.
if (method === 'CONNECT' && pathIsFromURL && path[0] === '/') {
path = path.slice(1) || '/';
}

this[kPath] = path;
if (cb) {
this.once('response', cb);
}
Expand Down
56 changes: 56 additions & 0 deletions test/parallel/test-http-request-connect-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

{
const server = http.createServer(common.mustNotCall());

server.on('connect', common.mustCall((req, socket) => {
assert.strictEqual(req.url, 'example.com');
socket.end('HTTP/1.1 501 Not Implemented\r\n\r\n');
}));

server.listen(0, common.mustCall(() => {
const port = server.address().port;
const req = http.request(
new URL(`http://localhost:${port}/example.com`),
{ method: 'CONNECT' },
);

req.on('connect', common.mustCall((res, socket) => {
assert.strictEqual(res.statusCode, 501);
socket.destroy();
server.close();
}));

req.end();
}));
}

{
const server = http.createServer(common.mustNotCall());

server.on('connect', common.mustCall((req, socket) => {
assert.strictEqual(req.url, '/example.com');
socket.end('HTTP/1.1 501 Not Implemented\r\n\r\n');
}));

server.listen(0, common.mustCall(() => {
const req = http.request({
host: 'localhost',
port: server.address().port,
method: 'CONNECT',
path: '/example.com',
});

req.on('connect', common.mustCall((res, socket) => {
assert.strictEqual(res.statusCode, 501);
socket.destroy();
server.close();
}));

req.end();
}));
}
Loading