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: refactor to make servername option normalization testable #38733

Merged
merged 2 commits into from
Oct 6, 2023
Merged
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
11 changes: 7 additions & 4 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
if (options.socketPath)
options.path = options.socketPath;

if (!options.servername && options.servername !== '')
options.servername = calculateServerName(options, req);
normalizeServerName(options, req);

const name = this.getName(options);
if (!this.sockets[name]) {
Expand Down Expand Up @@ -313,8 +312,7 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
if (options.socketPath)
options.path = options.socketPath;

if (!options.servername && options.servername !== '')
options.servername = calculateServerName(options, req);
normalizeServerName(options, req);

const name = this.getName(options);
options._agentKey = name;
Expand Down Expand Up @@ -344,6 +342,11 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
oncreate(null, newSocket);
};

function normalizeServerName(options, req) {
if (!options.servername && options.servername !== '')
options.servername = calculateServerName(options, req);
}

Comment on lines +345 to +349
Copy link
Member

Choose a reason for hiding this comment

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

Should we avoid mutating an argument in a function?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree it would be good to refactor that away. But can happen in a follow up PR.

function calculateServerName(options, req) {
let servername = options.host;
const hostHeader = req.getHeader('host');
Expand Down