🐛 bug: fix proxy overwrote the wrong scheme (#2004)

* 🐛 bug: fix proxy overwrote the wrong scheme

*  fix: fix io not exist in go1.14
pull/2013/head
Jinquan Wang 2022-08-09 22:14:41 +08:00 committed by GitHub
parent e5eb8d3c98
commit e8a2ba363c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View File

@ -123,11 +123,13 @@ func Do(c *fiber.Ctx, addr string) error {
res := c.Response()
originalURL := utils.CopyString(c.OriginalURL())
defer req.SetRequestURI(originalURL)
req.SetRequestURI(addr)
copiedURL := utils.CopyString(addr)
req.SetRequestURI(copiedURL)
// NOTE: if req.isTLS is true, SetRequestURI keeps the scheme as https.
// issue reference:
// https://github.com/gofiber/fiber/issues/1762
if scheme := getScheme(utils.UnsafeBytes(addr)); len(scheme) > 0 {
if scheme := getScheme(utils.UnsafeBytes(copiedURL)); len(scheme) > 0 {
req.URI().SetSchemeBytes(scheme)
}

View File

@ -4,6 +4,7 @@ import (
"crypto/tls"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
@ -362,3 +363,30 @@ func Test_Proxy_Do_RestoreOriginalURL(t *testing.T) {
utils.AssertEqual(t, nil, err1)
utils.AssertEqual(t, nil, err2)
}
func Test_Proxy_Do_HTTP_Prefix_URL(t *testing.T) {
t.Parallel()
_, addr := createProxyTestServer(func(c *fiber.Ctx) error {
return c.SendString("hello world")
}, t)
app := fiber.New(fiber.Config{DisableStartupMessage: true})
app.Get("/*", func(c *fiber.Ctx) error {
path := c.OriginalURL()
url := strings.TrimPrefix(path, "/")
utils.AssertEqual(t, "http://"+addr, url)
if err := Do(c, url); err != nil {
return err
}
c.Response().Header.Del(fiber.HeaderServer)
return nil
})
resp, err := app.Test(httptest.NewRequest(http.MethodGet, "/http://"+addr, nil))
utils.AssertEqual(t, nil, err)
s, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "hello world", string(s))
}