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

Bump github.com/valyala/fasthttp from 1.35.0 to 1.37.0 #1882

Prev Previous commit
Next Next commit
Bump github.com/valyala/fasthttp from 1.35.0 to 1.36.0 #1882
ReneWerner87 committed May 6, 2022
commit 928c961f4996e1ee1c6002f4e250cbd77c839568
14 changes: 11 additions & 3 deletions ctx.go
Original file line number Diff line number Diff line change
@@ -1350,10 +1350,14 @@ func (c *Ctx) SendFile(file string, compress ...bool) error {
// Save the filename, we will need it in the error message if the file isn't found
filename := file

// https://github.com/valyala/fasthttp/blob/master/fs.go#L81
// https://github.com/valyala/fasthttp/blob/c7576cc10cabfc9c993317a2d3f8355497bea156/fs.go#L129-L134
sendFileOnce.Do(func() {
// change it to absolute
path, _ := filepath.Abs("/")
sendFileFS = &fasthttp.FS{
Root: "/",
// fasthttp will change it to absolute path
// https://github.com/valyala/fasthttp/blob/c7576cc10cabfc9c993317a2d3f8355497bea156/fs.go#L390-L395
Root: path,
GenerateIndexPages: false,
AcceptByteRange: true,
Compress: true,
@@ -1374,7 +1378,7 @@ func (c *Ctx) SendFile(file string, compress ...bool) error {
// https://github.com/valyala/fasthttp/blob/7cc6f4c513f9e0d3686142e0a1a5aa2f76b3194a/fs.go#L55
c.fasthttp.Request.Header.Del(HeaderAcceptEncoding)
}
// https://github.com/valyala/fasthttp/blob/7cc6f4c513f9e0d3686142e0a1a5aa2f76b3194a/fs.go#L103-L121
// copy of https://github.com/valyala/fasthttp/blob/7cc6f4c513f9e0d3686142e0a1a5aa2f76b3194a/fs.go#L103-L121 with small adjustments
if len(file) == 0 || !filepath.IsAbs(file) {
// extend relative path to absolute path
hasTrailingSlash := len(file) > 0 && (file[len(file)-1] == '/' || file[len(file)-1] == '\\')
@@ -1387,6 +1391,10 @@ func (c *Ctx) SendFile(file string, compress ...bool) error {
if hasTrailingSlash {
file += "/"
}
// remove the relative data with dot as they are not necessary
if len(file) > 2 && strings.HasPrefix(file, "."+string(filepath.Separator)) {
file = file[2:]
}
}
// convert the path to forward slashes regardless the OS in order to set the URI properly
// the handler will convert back to OS path separator before opening the file
52 changes: 37 additions & 15 deletions ctx_test.go
Original file line number Diff line number Diff line change
@@ -1923,22 +1923,44 @@ func Test_Ctx_SendFile_404(t *testing.T) {
func Test_Ctx_SendFile_Immutable(t *testing.T) {
t.Parallel()
app := New()
app.Get("/:file", func(c *Ctx) error {
file := c.Params("file")
if err := c.SendFile(filepath.FromSlash(".github/" + file + ".html")); err != nil {
var endpointsForTest []string
addEndpoint := func(file, endpoint string) {
endpointsForTest = append(endpointsForTest, endpoint)
app.Get(endpoint, func(c *Ctx) error {
if err := c.SendFile(file); err != nil {
utils.AssertEqual(t, nil, err)
return err
}
return c.SendStatus(200)
})
}

// relative paths
addEndpoint("./.github/index.html", "/relativeWithDot")
addEndpoint(filepath.FromSlash("./.github/index.html"), "/relativeOSWithDot")
addEndpoint(".github/index.html", "/relative")
addEndpoint(filepath.FromSlash(".github/index.html"), "/relativeOS")

// absolute paths
if path, err := filepath.Abs(".github/index.html"); err != nil {
utils.AssertEqual(t, nil, err)
} else {
addEndpoint(path, "/absolute")
addEndpoint(filepath.FromSlash(path), "/absoluteOS") // os related
}

for _, endpoint := range endpointsForTest {
t.Run(endpoint, func(t *testing.T) {
// 1st try
resp, err := app.Test(httptest.NewRequest("GET", endpoint, nil))
utils.AssertEqual(t, nil, err)
}
utils.AssertEqual(t, "index", file)
return c.SendString(file)
})
// 1st try
resp, err := app.Test(httptest.NewRequest("GET", "/index", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, StatusOK, resp.StatusCode)
// 2nd try
resp, err = app.Test(httptest.NewRequest("GET", "/index", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, StatusOK, resp.StatusCode)
utils.AssertEqual(t, StatusOK, resp.StatusCode)
// 2nd try
resp, err = app.Test(httptest.NewRequest("GET", endpoint, nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, StatusOK, resp.StatusCode)
})
}
}

// go test -race -run Test_Ctx_SendFile_RestoreOriginalURL