diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-04-02 01:12:12 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-04-02 01:12:12 +0900 |
| commit | 03e718023603cc06b75a08faa1b5988d50551caa (patch) | |
| tree | a4c8c2ac92041125ab256af2fe73fe999395eacb | |
| parent | 638b9f28a59613a128d01ff56ad92258ef3c6d03 (diff) | |
| download | mioproxy-03e718023603cc06b75a08faa1b5988d50551caa.tar.gz mioproxy-03e718023603cc06b75a08faa1b5988d50551caa.tar.zst mioproxy-03e718023603cc06b75a08faa1b5988d50551caa.zip | |
modify location header the origin server respondsv0.3.0
| -rw-r--r-- | server.go | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -64,6 +64,33 @@ func newMultipleReverseProxyServer(ps []ProxyConfig) (*multipleReverseProxyServe r.SetURL(targetUrl) r.SetXForwarded() }, + ModifyResponse: func(r *http.Response) error { + if r.StatusCode < 300 || 400 <= r.StatusCode { + return nil + } + + // If the response is redirect and has location header, rewrite it. + location := r.Header.Get("Location") + if location == "" { + return nil + } + locationUrl, err := url.Parse(location) + if err != nil { + return nil + } + if !locationUrl.IsAbs() { + return nil + } + if locationUrl.Hostname() != targetUrl.Hostname() { + return nil + } + locationUrl.Host = p.From.Host + if locationUrl.Scheme == "http" { + locationUrl.Scheme = "https" + } + r.Header.Set("Location", locationUrl.String()) + return nil + }, } if p.BasicAuth != nil { credentialFileContent, err := os.ReadFile(p.BasicAuth.CredentialFile) |
