Skip to content

Commit

Permalink
Optimize header toLowerCasing
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Mar 22, 2024
1 parent 1bffa31 commit c226d9a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
33 changes: 12 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,34 @@ internals.Client = class {
uri.method = method.toUpperCase();
uri.headers = Object.assign({}, options.headers);

const hostHeader = internals.findHeader('host', uri.headers);
const usedHeaders = new Set();
for (const [key, value] of Object.entries(uri.headers)) {
if (value !== undefined) {
usedHeaders.add(key.toLowerCase());
}
}

if (!hostHeader) {
if (!usedHeaders.has('host')) {
uri.headers.host = uri.host;
}

const hasContentLength = internals.findHeader('content-length', uri.headers) !== undefined;

if (options.payload && typeof options.payload === 'object' && !(options.payload instanceof Stream) && !Buffer.isBuffer(options.payload)) {
options.payload = JSON.stringify(options.payload);
if (!internals.findHeader('content-type', uri.headers)) {
if (!usedHeaders.has('content-type')) {
uri.headers['content-type'] = 'application/json';
}
}

if (options.gunzip &&
internals.findHeader('accept-encoding', uri.headers) === undefined) {
!usedHeaders.has('accept-encoding')) {

uri.headers['accept-encoding'] = 'gzip';
}

const payloadSupported = uri.method !== 'GET' && uri.method !== 'HEAD' && !internals.isNullOrUndefined(options.payload);
if (payloadSupported &&
(typeof options.payload === 'string' || Buffer.isBuffer(options.payload)) &&
!hasContentLength) {
!usedHeaders.has('content-length')) {

uri.headers['content-length'] = Buffer.isBuffer(options.payload) ? options.payload.length : Buffer.byteLength(options.payload);
}
Expand Down Expand Up @@ -393,7 +396,7 @@ internals.Client = class {

// 'strict' or true

const contentType = res.headers && internals.findHeader('content-type', res.headers) || '';
const contentType = res.headers?.['content-type'] ?? '';
const mime = contentType.split(';')[0].trim().toLowerCase();

if (!internals.jsonRegex.test(mime)) {
Expand Down Expand Up @@ -459,7 +462,7 @@ internals.Client = class {
if (options.gunzip) {
const contentEncoding = options.gunzip === 'force' ?
'gzip' :
res.headers && internals.findHeader('content-encoding', res.headers) || '';
res.headers?.['content-encoding'] ?? '';

if (/^(x-)?gzip(\s*,\s*identity)?$/.test(contentEncoding)) {
const gunzip = Zlib.createGunzip();
Expand Down Expand Up @@ -653,18 +656,6 @@ internals.tryParseBuffer = function (buffer, next) {
};


internals.findHeader = function (headerName, headers) {

const normalizedName = headerName.toLowerCase();

for (const key of Object.keys(headers)) {
if (key.toLowerCase() === normalizedName) {
return headers[key];
}
}
};


internals.applyUrlToOptions = (options, url) => {

options.host = url.host;
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,15 @@ describe('options.baseUrl', () => {
flags.onCleanup = () => server.close();
});

it('ignores host header when it is undefined', async (flags) => {

const server = await internals.server('ok');
flags.onCleanup = () => server.close();
const promise = Wreck.request('get', '/foo', { baseUrl: `http://localhost:${server.address().port}/`, headers: { host: undefined } });
await expect(promise).to.not.reject();
expect(promise.req.getHeader('host')).to.equal(`localhost:${server.address().port}`);
});

it('uses baseUrl option with trailing slash and uri is prefixed with a slash', async (flags) => {

const server = await internals.server('ok');
Expand Down

0 comments on commit c226d9a

Please sign in to comment.