mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-08-30 03:57:45 +00:00
feat(gitea): support extra outbound HTTP headers via GITEA_EXTRA_HEADERS
Add a GITEA_EXTRA_HEADERS environment variable that accepts a JSON object of header name/value pairs (e.g. Cloudflare Access credentials) and applies them to every outbound request to Gitea, both the raw pkg/gitea.DoJSON/DoBytes path and the SDK-backed pkg/gitea.NewClient path, without overriding Authorization, Content-Type, or Accept. Co-Authored-By: Codet <codet@commitgo.dev> (GPT-5-Codex)
This commit is contained in:
+27
-1
@@ -30,6 +30,32 @@ func sharedTransport() *http.Transport {
|
||||
return sharedTrans
|
||||
}
|
||||
|
||||
// extraHeaderTransport injects flag.ExtraHeaders into every request, without
|
||||
// overriding headers the caller already set (e.g. Authorization, Content-Type,
|
||||
// Accept). It reads flag.ExtraHeaders on each round trip rather than caching
|
||||
// it, so tests can change it between requests.
|
||||
type extraHeaderTransport struct {
|
||||
base http.RoundTripper
|
||||
}
|
||||
|
||||
func (t *extraHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
headers := flag.ExtraHeaders
|
||||
if len(headers) == 0 {
|
||||
return t.base.RoundTrip(req)
|
||||
}
|
||||
cloned := req.Clone(req.Context())
|
||||
for name, values := range headers {
|
||||
if cloned.Header.Get(name) == "" {
|
||||
cloned.Header[name] = values
|
||||
}
|
||||
}
|
||||
return t.base.RoundTrip(cloned)
|
||||
}
|
||||
|
||||
func giteaTransport() http.RoundTripper {
|
||||
return &extraHeaderTransport{base: sharedTransport()}
|
||||
}
|
||||
|
||||
// NewClient returns a cached *gitea.Client keyed by host+token. The SDK's per-client
|
||||
// version cache and the shared transport let us reuse keep-alive connections
|
||||
// and avoid the SDK's /api/v1/version preflight on every tool call.
|
||||
@@ -40,7 +66,7 @@ func NewClient(token string) (*gitea.Client, error) {
|
||||
}
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: sharedTransport(),
|
||||
Transport: giteaTransport(),
|
||||
CheckRedirect: checkRedirect,
|
||||
}
|
||||
opts := []gitea.ClientOption{
|
||||
|
||||
Reference in New Issue
Block a user