diff --git a/ctx.go b/ctx.go index 4ac587f89d..4a3f088944 100644 --- a/ctx.go +++ b/ctx.go @@ -1074,6 +1074,9 @@ func (c *Ctx) SendFile(file string, compress ...bool) error { file += "/" } } + // Restore the original requested URL + originalURL := c.OriginalURL() + defer c.fasthttp.Request.SetRequestURI(originalURL) // Set new URI for fileHandler c.fasthttp.Request.SetRequestURI(file) // Save status code diff --git a/ctx_test.go b/ctx_test.go index 902f808fb5..91ab336623 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -1656,6 +1656,21 @@ func Test_Ctx_SendFile_Immutable(t *testing.T) { utils.AssertEqual(t, StatusOK, resp.StatusCode) } +// go test -race -run Test_Ctx_SendFile_RestoreOriginalURL +func Test_Ctx_SendFile_RestoreOriginalURL(t *testing.T) { + t.Parallel() + app := New() + app.Get("/", func(c *Ctx) error { + originalURL := c.OriginalURL() + err := c.SendFile("ctx.go") + utils.AssertEqual(t, originalURL, c.OriginalURL()) + return err + }) + + _, err := app.Test(httptest.NewRequest("GET", "/?test=true", nil)) + utils.AssertEqual(t, nil, err) +} + // go test -run Test_Ctx_JSON func Test_Ctx_JSON(t *testing.T) { t.Parallel()