From 6a4e06181545cffbd2a132dc95ed3ae1ad098360 Mon Sep 17 00:00:00 2001 From: panxiao81 Date: Mon, 21 Sep 2026 05:39:23 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=87=8D=E5=86=99=20Gitea=20=E5=8F=8D?= =?UTF-8?q?=E4=BB=A3=20Host?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/runnerfacade/facade_test.go | 9 ++++++--- internal/runnerfacade/server.go | 8 +++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/internal/runnerfacade/facade_test.go b/internal/runnerfacade/facade_test.go index 87e6d66..6300842 100644 --- a/internal/runnerfacade/facade_test.go +++ b/internal/runnerfacade/facade_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "strings" "testing" "connectrpc.com/connect" @@ -131,9 +132,11 @@ func TestAPIHandlerMatchesOfficialRunnerBasePath(t *testing.T) { } func TestHandlerProxiesRepositoryTrafficToGitea(t *testing.T) { - upstream := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { - if request.URL.Path != "/owner/repo/info/refs" || request.Header.Get("Authorization") != "Basic checkout-token" { - t.Errorf("request path=%q authorization=%q", request.URL.Path, request.Header.Get("Authorization")) + var upstream *httptest.Server + upstream = httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { + upstreamHost := strings.TrimPrefix(upstream.URL, "http://") + if request.URL.Path != "/owner/repo/info/refs" || request.Header.Get("Authorization") != "Basic checkout-token" || request.Host != upstreamHost { + t.Errorf("request path=%q authorization=%q host=%q", request.URL.Path, request.Header.Get("Authorization"), request.Host) response.WriteHeader(http.StatusBadRequest) return } diff --git a/internal/runnerfacade/server.go b/internal/runnerfacade/server.go index 95384c7..ecccca1 100644 --- a/internal/runnerfacade/server.go +++ b/internal/runnerfacade/server.go @@ -39,7 +39,13 @@ func Handler(facade *Facade, upstreamURL string) (http.Handler, error) { path, service := facade.Handler() mux := http.NewServeMux() mux.Handle(APIBasePath+path, http.StripPrefix(APIBasePath, SPIFFEMiddleware(service))) - mux.Handle("/", httputil.NewSingleHostReverseProxy(target)) + proxy := httputil.NewSingleHostReverseProxy(target) + director := proxy.Director + proxy.Director = func(request *http.Request) { + director(request) + request.Host = target.Host + } + mux.Handle("/", proxy) return mux, nil }