Skip to content

Commit

Permalink
fix(logger-plugin): handle undefined protocol and hostname (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai authored Sep 3, 2024
1 parent 45364e4 commit 36cd3a9
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/plugins/default/logger-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,30 @@ export const loggerPlugin: Plugin = (proxyServer, options) => {
const originalUrl = req.originalUrl ?? `${req.baseUrl || ''}${req.url}`;

// construct targetUrl
const port = getPort(proxyRes.req?.agent?.sockets);
let target: URL;

const obj = {
protocol: proxyRes.req.protocol,
host: proxyRes.req.host,
pathname: proxyRes.req.path,
} as URL;
try {
const port = getPort(proxyRes.req?.agent?.sockets);

const target = new URL(`${obj.protocol}//${obj.host}${obj.pathname}`);
const obj = {
protocol: proxyRes.req.protocol,
host: proxyRes.req.host,
pathname: proxyRes.req.path,
} as URL;

if (port) {
target.port = port;
target = new URL(`${obj.protocol}//${obj.host}${obj.pathname}`);

if (port) {
target.port = port;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
// nock issue (https://github.com/chimurai/http-proxy-middleware/issues/1035)
// fallback to old implementation (less correct - without port)
target = new URL(options.target as URL);
target.pathname = proxyRes.req.path;
}

const targetUrl = target.toString();

const exchange = `[HPM] ${req.method} ${originalUrl} -> ${targetUrl} [${proxyRes.statusCode}]`;
Expand Down

0 comments on commit 36cd3a9

Please sign in to comment.