Giter Site home page Giter Site logo

httpxy's Issues

Support HTTP2/3

Describe the feature

Support for HTTP2/3 in order to be a replacement for Vite dev-server and other build tools.

Upstream: http-party/node-http-proxy#1237

Additional information

  • Would you be willing to help implement this feature?

Enable ts strict checks

Initially disabled to make refactor faster. It should be straight forward now if someone wants to pick this.

Relative URL for Location response header causes TypeError (e.g. 201 Created)

Environment

httpxy 0.1.5
node 20 (but doesn't really matter)

Reproduction

not needed, I think

Describe the bug

If the server responds with a relativ URL in the Location header (e.g. in case of 201 Created), it causes an "Uncaught TypeError: Invalid URL" error, since there is no second parameter to the URL constructor in:

const u = new URL(proxyRes.headers.location);

For example:

HTTP/1.1 201 Created
Location: /api/books/1

Additional context

The Location response header is allowed to contain a relativ URL, according to: https://www.rfc-editor.org/rfc/rfc9110#field.location

The URL constructor needs a base argument in that case: https://nodejs.org/api/url.html#new-urlinput-base

Logs

No response

Add port to the `host` breaks some website

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

  • The /api/httpxy is using httpxy(this repo)
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),

  • When using httpxy, the request Host is baidu.com:443

Caused by this:

httpxy/src/_utils.ts

Lines 108 to 110 in 07778fb

hasPort(outgoing.host)
? outgoing.host
: outgoing.host + ":" + outgoing.port;

  • When using http-proxy, the request Host is baidu.com

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

DELETE requests add a "Content-Length: 0" header, causing undici to error "Request body length does not match content-length header"

Environment

  • Operating System: Darwin
  • Node Version: v20.5.1
  • Nuxt Version: 3.7.0
  • CLI Version: 3.7.2
  • Nitro Version: 2.6.2
  • Package Manager: [email protected]
  • Builder: -
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

I confess I haven't made one up-front. I think the explanation below might be enough, but if not I can try to figure out a reproduction.

Describe the bug

When making a DELETE request, the request has a "Content-Length: 0" header added to it, which causes Node's undici to respond with the following error:

TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async sendProxy (file:///myprojectpath/node_modules/h3/dist/index.mjs:1049:20)
    at async Object.handler (file:///myprojectpath/node_modules/h3/dist/index.mjs:1646:19)
    at async Server.toNodeHandle (file:///myprojectpath/node_modules/h3/dist/index.mjs:1857:7) {
  cause: RequestContentLengthMismatchError: Request body length does not match content-length header
      at write (node:internal/deps/undici/undici:10059:41)
      at _resume (node:internal/deps/undici/undici:10037:33)
      at resume (node:internal/deps/undici/undici:9938:7)
      at [dispatch] (node:internal/deps/undici/undici:9286:11)
      at Client.Intercept (node:internal/deps/undici/undici:9017:20)
      at Client.dispatch (node:internal/deps/undici/undici:7772:44)
      at [dispatch] (node:internal/deps/undici/undici:7991:32)
      at Pool.dispatch (node:internal/deps/undici/undici:7772:44)
      at [dispatch] (node:internal/deps/undici/undici:10556:27)
      at Agent.Intercept (node:internal/deps/undici/undici:9017:20) {
    code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
  }

More context has been provided here:
nodejs/undici#2046 (comment)

Here's the same code in this fork:

* Sets `content-length` to '0' if request is of DELETE type.

This is all somewhat beyond me, but I would think it's not the responsibility of the proxy to be adding HTTP headers that weren't provided in the initial request, unless it's intentional behaviour written in middleware or proxy route rules.

I appreciate that Node may end up fixing their underlying behaviour to allow DELETE requests with a Content-Length, but that may not solve the problem for people proxying to other non-JS back-ends.

Similarly, I have read in the linked undici issue that Safari does send Content-Length: 0 with DELETE requests, so it would be useful to know how I can filter those out. I am using extendRouteRules in a Nuxt module to register my proxy rules, and am doing so as a wildcard for a whole path prefix. Is that currently possible?

Additional context

No response

Logs

No response

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>unjs/renovate-config)

Triage from upstream issues and PRs

UNIX socket support

Describe the feature

Hi,

It would be really nice if we could proxy to a UNIX socket :-)

Thanks in advance!

I would happily help implement this feature but can't do it myself right now due to time constraints (I already helped implement a similar feature in listhen)

Additional information

  • Would you be willing to help implement this feature?

Rewrite request and response (MITM proxy)

Describe the feature

It could be a very nice feature to be able to rewrite request and response headers/body.

The goal would be to have a full-featured MITM proxy with programmatic rules that trigger based on patterns
e.g. on hostname my-domain.com, run the provided function

Something like Charles Proxy or Burp Proxy, but with a modern syntax and access to the wide JavaScript dependencies ecosystem

TLS encryption-decryption should be implemented so that we can MITM HTTPS connections.

What do you think?

Additional information

  • Would you be willing to help implement this feature?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): update autofix-ci/action digest to ff86a55

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/autofix.yml
  • actions/checkout v4
  • actions/setup-node v3
  • autofix-ci/action 2891949f3779a1cafafae1523058501de3d4e944
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/setup-node v3
  • codecov/codecov-action v3
npm
package.json
  • @types/node ^20.12.7
  • @vitest/coverage-v8 ^0.34.6
  • changelogen ^0.5.5
  • eslint ^8.57.0
  • eslint-config-unjs ^0.2.1
  • jiti ^1.21.0
  • listhen ^1.7.2
  • ofetch ^1.3.4
  • prettier ^3.2.5
  • typescript ^5.4.5
  • unbuild ^2.0.0
  • vitest ^0.34.6
  • pnpm 8.15.7

  • Check this box to trigger a request for Renovate to run again on this repository

Please remove `followRedirects` from options since it does not support ๐Ÿ™๐Ÿป

Environment

nodejs: v20.12.0

Reproduction

Just look at the code is enough.

Describe the bug

node-http-proxy supports followRedirects, but this rewrite removed this feature.

This should be documented in the readme or with an accurate ProxyServerOptions, instead of just saying "Checkout http-party/node-http-proxy for more options and examples.".

I was misled by it and spent quite a time debugging it until I looked at the source code and saw this commented line ๐Ÿ˜ข .

// const agents = options.followRedirects ? followRedirects : nativeAgents;

Just remove this config from the ProxyServerOptions interface or document this would be less misleading ๐Ÿ™๐Ÿป.

followRedirects?: boolean;

(Still thanks for the rewrite, and I'm willing to contribute โค๏ธ )

Additional context

No response

Logs

No response

WTF? I encountered a problem and asked for a solution. Did you close my issue?

Environment

  • debian
  • nodejs 18

Reproduction

Use the official tutorial

Describe the bug

Describe the change

import { createServer } from "node:http";

import { createProxyServer } from "httpxy";

const proxy = createProxyServer({});

const server = createServer(async (req, res) => {
  try {
    await proxy.web(req, res, {
      target: main.url,
    });
  } catch (error) {
    console.error(error);
    res.statusCode = 500;
    res.end("Proxy error: " + error.toString());
  }
});

server.listen(3000, () => {
  console.log("Proxy is listening on http://localhost:3000");
});

The above code runs with an error
image
image

URLs

No response

Additional information

  • Would you be willing to help?

Additional context

No response

Logs

No response

Target path is not being set correctly when prepending path

Environment

Package version 0.1.2
Node.js 18.16.0

Reproduction

I initially reproduced the error by using nitro and configuring the devProxy like so:

devProxy: {
  "/posts": {
    target: "https://jsonplaceholder.typicode.com/posts",
    changeOrigin: true,
    prependPath: true,
  },
},

It turned out to be a bug with httpxy instead, but I figured the reproduction would be obvious enough using the nitro playground with the above configuration rather than forking the repo.

Describe the bug

Using the provided configuration, I assume the expected behavior would be to proxy a request to the /posts path to https://jsonplaceholder.typicode.com/posts, but it seems to be forwarded to just https://jsonplaceholder.typicode.com instead. I (supposedly) fixed the bug by changing the property used when determining the target path from path to pathname as per the URL class API documentation. I'll submit a PR shortly.

Additional context

Another issue reporting the bug: unjs/nitro#1643

Logs

No response

All unjs official code base documentation is very poor

Describe the change

import { createServer } from "node:http";

import { createProxyServer } from "httpxy";

const proxy = createProxyServer({});

const server = createServer(async (req, res) => {
  try {
    await proxy.web(req, res, {
      target: main.url,
    });
  } catch (error) {
    console.error(error);
    res.statusCode = 500;
    res.end("Proxy error: " + error.toString());
  }
});

server.listen(3000, () => {
  console.log("Proxy is listening on http://localhost:3000");
});

The above code runs with an error
image
image

URLs

No response

Additional information

  • Would you be willing to help?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.