Fix customerrors query url replacement

This commit is contained in:
Dorian Allen 2025-09-09 15:54:04 +08:00 committed by GitHub
parent e2282b1379
commit ff848c74f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View File

@ -122,11 +122,18 @@ func (c *customErrors) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
} }
var query string var query string
scheme := "http"
if req.TLS != nil {
scheme = "https"
}
orig := &url.URL{Scheme: scheme, Host: req.Host, Path: req.URL.Path, RawPath: req.URL.RawPath, RawQuery: req.URL.RawQuery, Fragment: req.URL.Fragment}
if len(c.backendQuery) > 0 { if len(c.backendQuery) > 0 {
query = "/" + strings.TrimPrefix(c.backendQuery, "/") query = "/" + strings.TrimPrefix(c.backendQuery, "/")
query = strings.ReplaceAll(query, "{status}", strconv.Itoa(code)) query = strings.ReplaceAll(query, "{status}", strconv.Itoa(code))
query = strings.ReplaceAll(query, "{originalStatus}", strconv.Itoa(originalCode)) query = strings.ReplaceAll(query, "{originalStatus}", strconv.Itoa(originalCode))
query = strings.ReplaceAll(query, "{url}", url.QueryEscape(req.URL.String())) query = strings.ReplaceAll(query, "{url}", url.QueryEscape(orig.String()))
} }
pageReq, err := newRequest("http://" + req.Host + query) pageReq, err := newRequest("http://" + req.Host + query)

View File

@ -175,6 +175,10 @@ func TestHandler(t *testing.T) {
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost/test?foo=bar&baz=buz", nil) req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost/test?foo=bar&baz=buz", nil)
// Client like browser and curl will issue a relative HTTP request, which not have a host and scheme in the URL. But the http.NewRequest will set them automatically.
req.URL.Host = ""
req.URL.Scheme = ""
recorder := httptest.NewRecorder() recorder := httptest.NewRecorder()
errorPageHandler.ServeHTTP(recorder, req) errorPageHandler.ServeHTTP(recorder, req)