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 {