更新 proxy/handler.go
This commit is contained in:
@@ -44,13 +44,21 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
path := strings.TrimPrefix(r.URL.Path, "/p/")
|
path := strings.TrimPrefix(r.URL.Path, "/p/")
|
||||||
|
|
||||||
slashIdx := strings.Index(path, "/")
|
slashIdx := strings.Index(path, "/")
|
||||||
|
|
||||||
|
var token, targetPath string
|
||||||
|
|
||||||
if slashIdx == -1 {
|
if slashIdx == -1 {
|
||||||
http.Error(w, "Invalid proxy path", http.StatusBadRequest)
|
token = path
|
||||||
return
|
targetPath = ""
|
||||||
|
} else {
|
||||||
|
token = path[:slashIdx]
|
||||||
|
targetPath = path[slashIdx+1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
token := path[:slashIdx]
|
if token == "" {
|
||||||
targetPath := path[slashIdx+1:]
|
http.Error(w, "Invalid token", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
session := h.sessionManager.Get(token)
|
session := h.sessionManager.Get(token)
|
||||||
if session == nil {
|
if session == nil {
|
||||||
@@ -64,10 +72,14 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
targetURL = targetPath
|
targetURL = targetPath
|
||||||
} else {
|
} else {
|
||||||
baseURL := strings.TrimSuffix(session.TargetURL, "/")
|
baseURL := strings.TrimSuffix(session.TargetURL, "/")
|
||||||
if !strings.HasPrefix(targetPath, "/") {
|
if targetPath == "" {
|
||||||
targetPath = "/" + targetPath
|
targetURL = baseURL
|
||||||
|
} else {
|
||||||
|
if !strings.HasPrefix(targetPath, "/") {
|
||||||
|
targetPath = "/" + targetPath
|
||||||
|
}
|
||||||
|
targetURL = baseURL + targetPath
|
||||||
}
|
}
|
||||||
targetURL = baseURL + targetPath
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.URL.RawQuery != "" {
|
if r.URL.RawQuery != "" {
|
||||||
@@ -150,6 +162,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
h.sendResponse(w, resp, body)
|
h.sendResponse(w, resp, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (h *ProxyHandler) rewriteContent(body []byte, targetURL, contentType, token string) []byte {
|
func (h *ProxyHandler) rewriteContent(body []byte, targetURL, contentType, token string) []byte {
|
||||||
rewriter, err := NewContentRewriter(targetURL, token)
|
rewriter, err := NewContentRewriter(targetURL, token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user