Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix c.OriginalURL() changed after SendFile #1553

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions ctx.go
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions ctx_test.go
Expand Up @@ -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()
Expand Down