mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-17 07:08:01 +08:00
- Introduce `sdk/httptransport` package with `Decorator`, `WithDecorator`, `WithoutDecorator`, `InheritDecorator`, and `Decorate` functions. - Propagate transport decorators from incoming HTTP request contexts in `BaseAPIHandler.GetContextWithCancel`. - Apply transport decoration after client transport selection across proxy-aware, Devin, uTLS, and Antigravity HTTP clients. Closes: #5616
76 lines
2.5 KiB
Go
76 lines
2.5 KiB
Go
// Package httptransport exposes an additive, request-scoped observation seam.
|
|
// A decorator wraps the transport already selected by the executor: it must not
|
|
// replace provider fingerprints, proxy routing, pools, or HTTP protocol policy.
|
|
package httptransport
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
)
|
|
|
|
type decoratorKey struct{}
|
|
|
|
// Decorator may wrap next to observe final HTTP requests and response bodies.
|
|
// It is called during client construction; implementations must be concurrency
|
|
// safe, must delegate to next, and must not retain secrets or consume bodies.
|
|
// A decorator does not observe WebSocket messages after an upgrade.
|
|
type Decorator func(next http.RoundTripper) http.RoundTripper
|
|
|
|
// WithDecorator attaches a decorator to one execution context. It does not
|
|
// enable request logging or modify executor credential/selection policy.
|
|
func WithDecorator(ctx context.Context, decorator Decorator) context.Context {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
return context.WithValue(ctx, decoratorKey{}, decorator)
|
|
}
|
|
|
|
// WithoutDecorator suppresses intermediate helper decoration. A specialized
|
|
// executor that further configures its helper's transport must apply Decorate
|
|
// once, after its own protocol/pool policy is complete.
|
|
func WithoutDecorator(ctx context.Context) context.Context {
|
|
return WithDecorator(ctx, nil)
|
|
}
|
|
|
|
// InheritDecorator copies a decorator from fallback into ctx when ctx does not
|
|
// already specify a decorator or explicit suppression via WithoutDecorator.
|
|
func InheritDecorator(ctx, fallback context.Context) context.Context {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
if fallback == nil {
|
|
return ctx
|
|
}
|
|
if _, ok := ctx.Value(decoratorKey{}).(Decorator); ok {
|
|
return ctx
|
|
}
|
|
if decorator, ok := fallback.Value(decoratorKey{}).(Decorator); ok && decorator != nil {
|
|
return WithDecorator(ctx, decorator)
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
// Decorate applies a context decorator AFTER transport selection. With no
|
|
// decorator, the exact original client is returned. Only the client value is
|
|
// copied; the original selected transport/pool and all client policy survive.
|
|
func Decorate(ctx context.Context, client *http.Client) *http.Client {
|
|
if ctx == nil || client == nil {
|
|
return client
|
|
}
|
|
decorator, _ := ctx.Value(decoratorKey{}).(Decorator)
|
|
if decorator == nil {
|
|
return client
|
|
}
|
|
next := client.Transport
|
|
if next == nil {
|
|
next = http.DefaultTransport
|
|
}
|
|
wrapped := decorator(next)
|
|
if wrapped == nil {
|
|
return client
|
|
}
|
|
clone := *client
|
|
clone.Transport = wrapped
|
|
return &clone
|
|
}
|