Skip to content

Lift URL and header handling into tw-upstream - #135

Merged
fylorn merged 1 commit into
mainfrom
refactor/shared-transport
Sep 23, 2026
Merged

fylorn merged 1 commit into
mainfrom
refactor/shared-transport

Conversation

@fylorn

@fylorn fylorn commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Next step toward retiring tw-provider. Before the enterprise gateway can send a request that tw-dialect already converted, there has to be somewhere to send it from.

What moves

Ten items out of tw-gateway/src/forward.rs into a new tw-upstream crate — URL assembly, which client headers travel onward, which upstream headers come back. One dependency: http.

None of it knows it is this gateway. upstream_url(base_url, path, query) is already the exact shape tw_dialect::convert::Prepared hands over.

It also carries the comment worth keeping visible:

出站直通:请求体一个字节都不改 […] cc-switch 有一次为了兼容性把 role=system 消息提到顶层,结果上游的前缀缓存命中率从 99% 掉到 20%,而且是静默的,只有账单会涨。

What stays behind

apply_set and check_body_size need the rule engine; map_reqwest_error needs the secret layer. Those are this gateway's, not shared.

What is deliberately not in here: the HTTP client

Both gateways build one, and the policies are opposite — and both are right for where they run:

desktop enterprise
overall timeout none — "一个跑了六分钟的 Opus 任务不该被中间层掐断" 300s
redirects unset refused — multi-tenant, base URLs typed in by an admin
HTTP/2 keep-alive on — "挂 Clash / Surge 的桌面用户几乎必踩" off

Merging them would impose one deployment's assumptions on the other. Each keeps its own.

(Two gaps that comparison exposed, noted for separate work: desktop does not refuse redirects though its base URLs are equally user-supplied, and the enterprise client has no HTTP/2 keep-alive though it can sit behind NAT too.)

Verification

Nine tests move with the code. cargo fmt --check, clippy --workspace --all-targets -- -D warnings, 87 test binaries green.

🤖 Generated with Claude Code

`forward` is where this gateway talks to an upstream, and most of it
does not know it is this gateway: assembling a URL from base + path +
query, deciding which client headers travel onward, deciding which
upstream headers come back. Ten items, no dependency beyond `http`.

That is exactly the shape the enterprise gateway needs. It reaches its
upstreams through `tw-provider`, which is built around a DTO and cannot
carry a request that was already converted — and converting is the whole
point of the dialect layer. What replaces it is this: a prepared body,
a path, a query, and headers.

Four things stay behind because they are not general: `apply_set` and
`check_body_size` need the rule engine, `map_reqwest_error` needs the
secret layer, and `should_strip`'s sibling logic is tied to neither so
it moves.

Deliberately absent: the HTTP client. The two gateways have opposite
policies and both are right. Desktop sets no overall timeout because a
six-minute task should not be cut off by something in the middle, and
keeps HTTP/2 pings alive because a desktop behind Clash or Surge loses
idle connections silently. The enterprise side sets 300 seconds and
refuses redirects, because it is multi-tenant and its base URLs are
typed in by an admin. Merging those would impose one deployment's
assumptions on the other.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 4a58310 into main Sep 23, 2026
2 checks passed
@fylorn
fylorn deleted the refactor/shared-transport branch September 23, 2026 10:40
This was referenced Sep 23, 2026
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