更新 proxy/handler.go

This commit is contained in:
XOF
2025-12-15 05:27:48 +08:00
parent 3b2a02cfed
commit 44bd28f567

View File

@@ -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 {