Skip to content

fix(forward): handle absolute URLs in HTTP-proxy mode - #127

Merged
ranxianglei merged 2 commits into
masterfrom
2026-08-13_fix-forward-absolute-url
Aug 12, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-08-13_fix-forward-absolute-url

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Problem

When a client is configured to use bili as an HTTP forward proxy (e.g. ZCode with proxy=http://127.0.0.1:8787) and sends requests to a custom localhost upstream (e.g. http://127.0.0.1:18081/v1/messages, http://localhost:8199/v1), bili mangles the URL:

forward POST → https://api.anthropic.comhttp://127.0.0.1:18081/v1/messages
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
→ ECONNRESET (garbage hostname)

(72 occurrences in the user's bili.log.)

Root cause: server.ts forward assumed req.url is always a path and did opts.upstream + req.url. But in HTTP-proxy mode the request line carries the full absolute URL, so opts.upstream + fullUrl produces garbage.

Fix (src/server.ts, 3 edits)

Detect absolute URLs (/^https?:\/\//i) and use them as-is instead of prepending the default upstream:

  1. upstreamOrigin (line ~417): route ? route.upstream : isAbsolute ? new URL(url).origin : opts.upstream — so session/upstream tracking sees the real host.
  2. rewritten URL (line ~1062): route ? route.rewrittenUrl : isAbsolute ? reqUrl : opts.upstream + reqUrl — don't prepend for absolute URLs.
  3. Host header (line ~1114): new URL(upstreamUrl).host — derive from the final URL (correct for all modes).

Unaffected paths

  • /bili/ prefix mode → route handles it (route.rewrittenUrl), unchanged.
  • MITM transparent-proxy mode (path-only req.url, e.g. /v1/messages) → not absolute → opts.upstream + path, unchanged.

Test

tests/forward-absolute-url.test.ts: spins up a stub upstream + bili (with a non-matching default upstream), sends a proxy-mode request with an absolute URL via Node http.request({path: 'http://127.0.0.1:<port>/v1/chat/completions'}). Asserts the stub receives /v1/chat/completions (not mangled) with the correct Host header.

  • typecheck: clean
  • full suite: 324/324 (was 323, +1)
  • build: OK

Verification

The user's bili.log showed the exact mangled URLs (https://api.anthropic.comhttp://127.0.0.1:18081/...) → this PR makes those resolve to the real localhost upstream.

Sisyphus and others added 2 commits August 12, 2026 21:42
HTTP proxy-mode requests (client sends full absolute URL in the request
line, e.g. POST http://127.0.0.1:8199/v1/messages) were mangled: with no
configured route, the forward path did opts.upstream + req.url, which for
an absolute req.url produced garbage like
"https://api.anthropic.comhttp://127.0.0.1:8199/v1/messages" -> ECONNRESET.

Now: when req.url is an absolute http(s) URL and there is no matching route,
derive upstreamOrigin from the URL's own origin, forward to the URL as-is,
and set Host to the URL's host. /bili/ prefix routing and MITM path-mode
(/v1/messages) are unchanged.

Regression test tests/forward-absolute-url.test.ts: sends a proxy-mode
absolute-URL request and asserts the upstream receives /v1/chat/completions
with the real Host (not the default upstream).
@ranxianglei
ranxianglei merged commit ae9e410 into master Aug 12, 2026
3 checks passed
This was referenced Aug 12, 2026
@ranxianglei ranxianglei mentioned this pull request Oct 7, 2026
@ranxianglei
ranxianglei deleted the 2026-08-13_fix-forward-absolute-url branch October 15, 2026 05:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant