From 44bd28f567c1a03e3cda7f966f89430201b74571 Mon Sep 17 00:00:00 2001 From: XOF Date: Mon, 15 Dec 2025 05:27:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20proxy/handler.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxy/handler.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/proxy/handler.go b/proxy/handler.go index d185a68..081c6d5 100644 --- a/proxy/handler.go +++ b/proxy/handler.go @@ -44,13 +44,21 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { path := strings.TrimPrefix(r.URL.Path, "/p/") slashIdx := strings.Index(path, "/") + + var token, targetPath string + if slashIdx == -1 { - http.Error(w, "Invalid proxy path", http.StatusBadRequest) - return + token = path + targetPath = "" + } else { + token = path[:slashIdx] + targetPath = path[slashIdx+1:] } - token := path[:slashIdx] - targetPath := path[slashIdx+1:] + if token == "" { + http.Error(w, "Invalid token", http.StatusBadRequest) + return + } session := h.sessionManager.Get(token) if session == nil { @@ -64,10 +72,14 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { targetURL = targetPath } else { baseURL := strings.TrimSuffix(session.TargetURL, "/") - if !strings.HasPrefix(targetPath, "/") { - targetPath = "/" + targetPath + if targetPath == "" { + targetURL = baseURL + } else { + if !strings.HasPrefix(targetPath, "/") { + targetPath = "/" + targetPath + } + targetURL = baseURL + targetPath } - targetURL = baseURL + targetPath } if r.URL.RawQuery != "" { @@ -150,6 +162,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.sendResponse(w, resp, body) } + func (h *ProxyHandler) rewriteContent(body []byte, targetURL, contentType, token string) []byte { rewriter, err := NewContentRewriter(targetURL, token) if err != nil {