From a7e972d8deaaf1d77b5b7a2fa6d19661afe1ecc7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 13 May 2026 19:10:52 +0000 Subject: [PATCH] fix: respect proxy env vars in runner client (#962) Fixes #957. ## Why The runner builds a custom `http.Transport` for its RPC client. From first principles, once we stop using the default transport we also stop inheriting its default proxy resolution behavior, so `HTTP_PROXY`/`HTTPS_PROXY` are ignored unless we wire that behavior back explicitly. ## What - set `Proxy: http.ProxyFromEnvironment` on the custom transport - add a regression test that verifies `getHTTPClient` honors proxy environment variables Reviewed-on: https://gitea.com/gitea/runner/pulls/962 Reviewed-by: ChristopherHX <38043+christopherhx@noreply.gitea.com> --- internal/pkg/client/http.go | 1 + internal/pkg/client/http_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 internal/pkg/client/http_test.go diff --git a/internal/pkg/client/http.go b/internal/pkg/client/http.go index 444a9a47..d976d12c 100644 --- a/internal/pkg/client/http.go +++ b/internal/pkg/client/http.go @@ -17,6 +17,7 @@ import ( func getHTTPClient(endpoint string, insecure bool) *http.Client { transport := &http.Transport{ + Proxy: http.ProxyFromEnvironment, MaxIdleConns: 10, MaxIdleConnsPerHost: 10, // All requests go to one host; default is 2 which causes frequent reconnects. IdleConnTimeout: 90 * time.Second, diff --git a/internal/pkg/client/http_test.go b/internal/pkg/client/http_test.go new file mode 100644 index 00000000..9859d1d8 --- /dev/null +++ b/internal/pkg/client/http_test.go @@ -0,0 +1,27 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package client + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestGetHTTPClientUsesProxyFromEnvironment(t *testing.T) { + t.Setenv("HTTP_PROXY", "http://proxy.example.com:8080") + + client := getHTTPClient("http://gitea.example.com", false) + transport, ok := client.Transport.(*http.Transport) + require.True(t, ok) + + req, err := http.NewRequest(http.MethodGet, "http://gitea.example.com/api/actions/ping", nil) + require.NoError(t, err) + + proxyURL, err := transport.Proxy(req) + require.NoError(t, err) + require.NotNil(t, proxyURL) + require.Equal(t, "http://proxy.example.com:8080", proxyURL.String()) +}