unjs / httpxy

🔀 A Full-Featured HTTP and WebSocket Proxy for Node.js
Other
194 stars 12 forks source link

Add port to the `host` breaks some website #58

Open kricsleo opened 3 months ago

kricsleo commented 3 months ago

Environment

nodejs: v20.12.0 httpxy: v0.1.5(latest)

Reproduction

stackblitz - reporduciton

You have to download this project to run locally (Some TLS error prevents it from running in Stackblitz).

Describe the bug

import { createProxyServer } from 'httpxy';

const proxy = createProxyServer({
  target: 'https://baidu.com',
  changeOrigin: true,
});

export default defineEventHandler(async (event) => {
  await proxy.web(event.node.req, event.node.res);
});
import HttpProxy from 'http-proxy'

const httpProxy  = new HttpProxy({
  target: 'https://baidu.com',
  changeOrigin: true,
})

export default defineEventHandler(async event => {
  await new Promise<void>((resolve, reject) => {
    httpProxy.web(event.node.req, event.node.res, undefined, err => {
      err ? reject(err) : resolve()
    })
  })
})
  1. Open the network panel, click httpxy and http-proxy buttons to both fetch https://baidu.com (the Chinese largest search engine, I just use it as an example).
  2. httpxy response with a 405(❌), and the http-proxy response with a 302(✅).

I have verified it's the Host that caused the wrong response(maybe some check by baidu.com),

Caused by this:

https://github.com/unjs/httpxy/blob/07778fb8984c7f63d5cd00e38b787e7da27209fc/src/_utils.ts#L108-L110

I'm not saying Baidu's strategy in the response, I just wonder why we add the port to the Host, it might break some other sites as well.

Sorry to disturb if anything I misunderstood ❤️.

Additional context

No response

Logs

No response