Repository navigation
fix(forward): handle absolute URLs in HTTP-proxy mode - #127
Merged
Merged
Conversation
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).
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:(72 occurrences in the user's
bili.log.)Root cause:
server.tsforward assumedreq.urlis always a path and didopts.upstream + req.url. But in HTTP-proxy mode the request line carries the full absolute URL, soopts.upstream + fullUrlproduces garbage.Fix (
src/server.ts, 3 edits)Detect absolute URLs (
/^https?:\/\//i) and use them as-is instead of prepending the default upstream:route ? route.upstream : isAbsolute ? new URL(url).origin : opts.upstream— so session/upstream tracking sees the real host.route ? route.rewrittenUrl : isAbsolute ? reqUrl : opts.upstream + reqUrl— don't prepend for absolute URLs.new URL(upstreamUrl).host— derive from the final URL (correct for all modes).Unaffected paths
/bili/prefix mode →routehandles it (route.rewrittenUrl), unchanged.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 Nodehttp.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.Verification
The user's
bili.logshowed the exact mangled URLs (https://api.anthropic.comhttp://127.0.0.1:18081/...) → this PR makes those resolve to the real localhost upstream.