-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add HTTP proxy strategy for caching absolute-form proxy requests #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,3 +44,5 @@ gomod { | |
| } | ||
|
|
||
| hermit { } | ||
|
|
||
| proxy { } | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package strategy | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "github.com/block/cachew/internal/cache" | ||
| "github.com/block/cachew/internal/logging" | ||
| "github.com/block/cachew/internal/strategy/handler" | ||
| ) | ||
|
|
||
| // RegisterHTTPProxy registers a caching HTTP proxy strategy. It intercepts | ||
| // absolute-form proxy requests (e.g. from sdkmanager with --proxy_host / | ||
| // --proxy_port) where the client sends: | ||
| // | ||
| // GET http://dl.google.com/some/path HTTP/1.1 | ||
| // | ||
| // The request URI is upgraded to HTTPS and the response is fetched and cached. | ||
| // Only GET requests are intercepted; other methods are passed through. | ||
| func RegisterHTTPProxy(r *Registry) { | ||
| Register(r, "proxy", "Caching HTTP proxy for absolute-form proxy requests.", func(ctx context.Context, _ ProxyConfig, c cache.Cache, mux Mux) (*HTTPProxy, error) { | ||
| return NewHTTPProxy(ctx, c, mux) | ||
| }) | ||
| } | ||
|
|
||
| // ProxyConfig holds configuration for the HTTP proxy strategy. | ||
| // Currently no options are required. | ||
| type ProxyConfig struct{} | ||
|
|
||
| // HTTPProxy is a caching HTTP proxy strategy that handles standard HTTP proxy | ||
| // requests in absolute form (GET http://host/path HTTP/1.1). | ||
| // | ||
| // It implements the Interceptor interface rather than registering on the mux | ||
| // directly, so that absolute-form request detection happens before ServeMux | ||
| // route matching. This prevents overlap with more-specific routes such as | ||
| // /api/v1/ or /admin/ when the proxied upstream path happens to match them. | ||
| type HTTPProxy struct { | ||
| logger *slog.Logger | ||
| handler http.Handler | ||
| } | ||
|
|
||
| var ( | ||
| _ Strategy = (*HTTPProxy)(nil) | ||
| _ Interceptor = (*HTTPProxy)(nil) | ||
| ) | ||
|
|
||
| func NewHTTPProxy(ctx context.Context, c cache.Cache, _ Mux) (*HTTPProxy, error) { | ||
| logger := logging.FromContext(ctx) | ||
| client := &http.Client{} | ||
| p := &HTTPProxy{logger: logger} | ||
|
|
||
| p.handler = handler.New(client, c). | ||
| CacheKey(func(r *http.Request) string { | ||
| target := p.parseProxyURI(r) | ||
| if target == nil { | ||
| return "" | ||
| } | ||
| return target.String() | ||
| }). | ||
| Transform(func(r *http.Request) (*http.Request, error) { | ||
| target := p.parseProxyURI(r) | ||
| if target == nil { | ||
| return r, nil | ||
| } | ||
| return http.NewRequestWithContext(r.Context(), http.MethodGet, target.String(), nil) | ||
| }). | ||
| OnError(func(err error, w http.ResponseWriter, r *http.Request) { | ||
| target := p.parseProxyURI(r) | ||
| if target == nil { | ||
| http.NotFound(w, r) | ||
| return | ||
| } | ||
| p.logger.ErrorContext(r.Context(), "Proxy request failed", | ||
| slog.String("url", target.String()), | ||
| slog.String("error", err.Error())) | ||
| http.Error(w, "proxy error: "+err.Error(), http.StatusBadGateway) | ||
| }) | ||
|
|
||
| logger.InfoContext(ctx, "HTTP proxy strategy initialized") | ||
| return p, nil | ||
| } | ||
|
|
||
| func (p *HTTPProxy) String() string { return "proxy" } | ||
|
|
||
| // Intercept returns an http.Handler that intercepts absolute-form GET proxy | ||
| // requests before they reach the ServeMux, delegating all other requests to | ||
| // next. This ensures that a proxied path like /api/v1/... is not accidentally | ||
| // routed to cachew's own API handler. | ||
| func (p *HTTPProxy) Intercept(next http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| // Only intercept absolute-form GET requests. Non-GET requests | ||
| // (HEAD, POST, …) are not cached and are passed through. | ||
| if r.Method == http.MethodGet && strings.HasPrefix(r.RequestURI, "http://") { | ||
| p.handler.ServeHTTP(w, r) | ||
| return | ||
| } | ||
| next.ServeHTTP(w, r) | ||
| }) | ||
| } | ||
|
|
||
| // parseProxyURI returns the HTTPS upstream URL for an absolute-form proxy | ||
| // request, or nil if the request is not a proxy request. | ||
| func (p *HTTPProxy) parseProxyURI(r *http.Request) *url.URL { | ||
| if !strings.HasPrefix(r.RequestURI, "http://") { | ||
| return nil | ||
| } | ||
| target, err := url.Parse(r.RequestURI) | ||
| if err != nil || target.Host == "" { | ||
| return nil | ||
| } | ||
| // Upgrade to HTTPS for the upstream fetch. | ||
| target.Scheme = "https" | ||
| return target | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The transform unconditionally rewrites upstream calls to
GET, but this handler is mounted on"/"for all methods. Any absolute-formHEAD,POST,PUT, etc. request is therefore silently converted into a GET, which returns incorrect semantics and can cause clients to misbehave (for example, HEAD probes downloading content or POST never reaching upstream). Either constrain this strategy to GET/HEAD at routing time or forward method/body/headers as-is.Useful? React with 👍 / 👎.