From 5c699400c4d074d3d6afc1b384dee53163bf6cdc Mon Sep 17 00:00:00 2001 From: xEricL <37921711+xEricL@users.noreply.github.com> Date: Sat, 19 Oct 2024 16:29:49 -0400 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=93=9A=20Doc:=20Clarify=20SendFile=20?= =?UTF-8?q?ContentType=20header=20set=20by=20file=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ctx.go | 4 ++-- ctx_interface_gen.go | 4 ++-- docs/api/ctx.md | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ctx.go b/ctx.go index 9e61d0903e..c59773db72 100644 --- a/ctx.go +++ b/ctx.go @@ -1497,8 +1497,8 @@ func (c *DefaultCtx) Send(body []byte) error { } // SendFile transfers the file from the given path. -// The file is not compressed by default, enable this by passing a 'true' argument -// Sets the Content-Type response HTTP header field based on the filenames extension. +// The file is not compressed by default, enable this by passing a 'true' argument. +// Sets the Content-Type response HTTP header field based on the file format. func (c *DefaultCtx) SendFile(file string, config ...SendFile) error { // Save the filename, we will need it in the error message if the file isn't found filename := file diff --git a/ctx_interface_gen.go b/ctx_interface_gen.go index aa317ecb00..68accf565a 100644 --- a/ctx_interface_gen.go +++ b/ctx_interface_gen.go @@ -271,8 +271,8 @@ type Ctx interface { // From this point onward the body argument must not be changed. Send(body []byte) error // SendFile transfers the file from the given path. - // The file is not compressed by default, enable this by passing a 'true' argument - // Sets the Content-Type response HTTP header field based on the filenames extension. + // The file is not compressed by default, enable this by passing a 'true' argument. + // Sets the Content-Type response HTTP header field based on the file format. SendFile(file string, config ...SendFile) error // SendStatus sets the HTTP status code and if the response body is empty, // it sets the correct status message in the body. diff --git a/docs/api/ctx.md b/docs/api/ctx.md index b771d37eab..7104aa6de2 100644 --- a/docs/api/ctx.md +++ b/docs/api/ctx.md @@ -1709,7 +1709,7 @@ app.Get("/", func(c fiber.Ctx) error { ## SendFile -Transfers the file from the given path. Sets the [Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) response HTTP header field based on the **filenames** extension. +Transfers the file from the given path. Sets the [Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) response HTTP header field based on the **file format**. ```go title="Config" title="Config" // SendFile defines configuration options when to transfer file with SendFile. From 9c0b31cf5cdaac2a1ba037d977bd250f64c9e4d0 Mon Sep 17 00:00:00 2001 From: xEricL <37921711+xEricL@users.noreply.github.com> Date: Sat, 19 Oct 2024 16:31:23 -0400 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9A=20Doc:=20Make=20SendFile=20def?= =?UTF-8?q?ault=20value=20formatting=20consistent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ctx.go | 10 +++++----- docs/api/ctx.md | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ctx.go b/ctx.go index c59773db72..1f5abbc9e1 100644 --- a/ctx.go +++ b/ctx.go @@ -83,29 +83,29 @@ type SendFile struct { // You have to set Content-Encoding header to compress the file. // Available compression methods are gzip, br, and zstd. // - // Optional. Default value false + // Optional. Default: false Compress bool `json:"compress"` // When set to true, enables byte range requests. // - // Optional. Default value false + // Optional. Default: false ByteRange bool `json:"byte_range"` // When set to true, enables direct download. // - // Optional. Default: false. + // Optional. Default: false Download bool `json:"download"` // Expiration duration for inactive file handlers. // Use a negative time.Duration to disable it. // - // Optional. Default value 10 * time.Second. + // Optional. Default: 10 * time.Second CacheDuration time.Duration `json:"cache_duration"` // The value for the Cache-Control HTTP-header // that is set on the file response. MaxAge is defined in seconds. // - // Optional. Default value 0. + // Optional. Default: 0 MaxAge int `json:"max_age"` } diff --git a/docs/api/ctx.md b/docs/api/ctx.md index 7104aa6de2..22e54635eb 100644 --- a/docs/api/ctx.md +++ b/docs/api/ctx.md @@ -1725,29 +1725,29 @@ type SendFile struct { // You have to set Content-Encoding header to compress the file. // Available compression methods are gzip, br, and zstd. // - // Optional. Default value false + // Optional. Default: false Compress bool `json:"compress"` // When set to true, enables byte range requests. // - // Optional. Default value false + // Optional. Default: false ByteRange bool `json:"byte_range"` // When set to true, enables direct download. // - // Optional. Default: false. + // Optional. Default: false Download bool `json:"download"` // Expiration duration for inactive file handlers. // Use a negative time.Duration to disable it. // - // Optional. Default value 10 * time.Second. + // Optional. Default: 10 * time.Second CacheDuration time.Duration `json:"cache_duration"` // The value for the Cache-Control HTTP-header // that is set on the file response. MaxAge is defined in seconds. // - // Optional. Default value 0. + // Optional. Default: 0 MaxAge int `json:"max_age"` } ``` From 2d7eb3b32f6271da52f5a4212748b227ea7ef270 Mon Sep 17 00:00:00 2001 From: xEricL <37921711+xEricL@users.noreply.github.com> Date: Sat, 19 Oct 2024 16:32:13 -0400 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=93=9A=20Doc:=20Add=20missing=20`fibe?= =?UTF-8?q?r.`=20in=20SendFile=20usage=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api/ctx.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/api/ctx.md b/docs/api/ctx.md index 22e54635eb..c703c64807 100644 --- a/docs/api/ctx.md +++ b/docs/api/ctx.md @@ -1761,7 +1761,7 @@ app.Get("/not-found", func(c fiber.Ctx) error { return c.SendFile("./public/404.html"); // Disable compression - return c.SendFile("./static/index.html", SendFile{ + return c.SendFile("./static/index.html", fiber.SendFile{ Compress: false, }); }) @@ -1783,7 +1783,7 @@ You can set `CacheDuration` config property to `-1` to disable caching. ```go title="Example" app.Get("/file", func(c fiber.Ctx) error { - return c.SendFile("style.css", SendFile{ + return c.SendFile("style.css", fiber.SendFile{ CacheDuration: -1, }) }) @@ -1797,16 +1797,16 @@ You can use multiple SendFile with different configurations in single route. Fib app.Get("/file", func(c fiber.Ctx) error { switch c.Query("config") { case "filesystem": - return c.SendFile("style.css", SendFile{ + return c.SendFile("style.css", fiber.SendFile{ FS: os.DirFS(".") }) case "filesystem-compress": - return c.SendFile("style.css", SendFile{ + return c.SendFile("style.css", fiber.SendFile{ FS: os.DirFS("."), Compress: true, }) case "compress": - return c.SendFile("style.css", SendFile{ + return c.SendFile("style.css", fiber.SendFile{ Compress: true, }) default: From 6c4c2e0ef6a7b2be04f84e32e100d8efc614be3c Mon Sep 17 00:00:00 2001 From: xEricL <37921711+xEricL@users.noreply.github.com> Date: Sat, 19 Oct 2024 16:34:35 -0400 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=93=9A=20Doc:=20Hyphenate=20'case-sen?= =?UTF-8?q?sitive'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.go | 2 +- app_test.go | 4 ++-- router.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app.go b/app.go index 38a3d17319..e1e607e6fa 100644 --- a/app.go +++ b/app.go @@ -142,7 +142,7 @@ type Config struct { //nolint:govet // Aligning the struct fields is not necessa // Default: false StrictRouting bool `json:"strict_routing"` - // When set to true, enables case sensitive routing. + // When set to true, enables case-sensitive routing. // E.g. "/FoO" and "/foo" are treated as different routes. // By default this is disabled and both "/FoO" and "/foo" will execute the same handler. // diff --git a/app_test.go b/app_test.go index 6b493de1eb..9699c85bce 100644 --- a/app_test.go +++ b/app_test.go @@ -451,9 +451,9 @@ func Test_App_Use_CaseSensitive(t *testing.T) { require.NoError(t, err, "app.Test(req)") require.Equal(t, StatusOK, resp.StatusCode, "Status code") - // check the detected path when the case insensitive recognition is activated + // check the detected path when the case-insensitive recognition is activated app.config.CaseSensitive = false - // check the case sensitive feature + // check the case-sensitive feature resp, err = app.Test(httptest.NewRequest(MethodGet, "/AbC", nil)) require.NoError(t, err, "app.Test(req)") require.Equal(t, StatusOK, resp.StatusCode, "Status code") diff --git a/router.go b/router.go index d2429c194e..27e3bc7e74 100644 --- a/router.go +++ b/router.go @@ -52,7 +52,7 @@ type Route struct { Name string `json:"name"` // Route's name //nolint:revive // Having both a Path (uppercase) and a path (lowercase) is fine Path string `json:"path"` // Original registered route path - Params []string `json:"params"` // Case sensitive param keys + Params []string `json:"params"` // Case-sensitive param keys Handlers []Handler `json:"-"` // Ctx handlers routeParser routeParser // Parameter parser // Data for routing @@ -316,7 +316,7 @@ func (app *App) register(methods []string, pathRaw string, group *Group, handler if pathRaw[0] != '/' { pathRaw = "/" + pathRaw } - // Create a stripped path in-case sensitive / trailing slashes + // Create a stripped path in case-sensitive / trailing slashes pathPretty := pathRaw // Case-sensitive routing, all to lowercase if !app.config.CaseSensitive { From 6df30b5df8a1e0fbcfd51f79b15746d9d001ed12 Mon Sep 17 00:00:00 2001 From: xEricL <37921711+xEricL@users.noreply.github.com> Date: Sat, 19 Oct 2024 17:47:24 -0400 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=93=9A=20Doc:=20Clarify=20`SendFile`?= =?UTF-8?q?=20behavior=20for=20missing/invalid=20file=20extensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ctx.go | 7 ++++--- ctx_interface_gen.go | 7 ++++--- docs/api/ctx.md | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/ctx.go b/ctx.go index 1f5abbc9e1..1bbf2ba130 100644 --- a/ctx.go +++ b/ctx.go @@ -1496,9 +1496,10 @@ func (c *DefaultCtx) Send(body []byte) error { return nil } -// SendFile transfers the file from the given path. -// The file is not compressed by default, enable this by passing a 'true' argument. -// Sets the Content-Type response HTTP header field based on the file format. +// SendFile transfers the file from the specified path. +// By default, the file is not compressed. To enable compression, set SendFile.Compress to true. +// The Content-Type response HTTP header field is set based on the file's extension. +// If the file extension is missing or invalid, the Content-Type is detected from the file's format. func (c *DefaultCtx) SendFile(file string, config ...SendFile) error { // Save the filename, we will need it in the error message if the file isn't found filename := file diff --git a/ctx_interface_gen.go b/ctx_interface_gen.go index 68accf565a..0714b24329 100644 --- a/ctx_interface_gen.go +++ b/ctx_interface_gen.go @@ -270,9 +270,10 @@ type Ctx interface { // Send sets the HTTP response body without copying it. // From this point onward the body argument must not be changed. Send(body []byte) error - // SendFile transfers the file from the given path. - // The file is not compressed by default, enable this by passing a 'true' argument. - // Sets the Content-Type response HTTP header field based on the file format. + // SendFile transfers the file from the specified path. + // By default, the file is not compressed. To enable compression, set SendFile.Compress to true. + // The Content-Type response HTTP header field is set based on the file's extension. + // If the file extension is missing or invalid, the Content-Type is detected from the file's format. SendFile(file string, config ...SendFile) error // SendStatus sets the HTTP status code and if the response body is empty, // it sets the correct status message in the body. diff --git a/docs/api/ctx.md b/docs/api/ctx.md index c703c64807..52d8144187 100644 --- a/docs/api/ctx.md +++ b/docs/api/ctx.md @@ -1709,7 +1709,7 @@ app.Get("/", func(c fiber.Ctx) error { ## SendFile -Transfers the file from the given path. Sets the [Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) response HTTP header field based on the **file format**. +Transfers the file from the given path. Sets the [Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) response HTTP header field based on the **file** extension or format. ```go title="Config" title="Config" // SendFile defines configuration options when to transfer file with SendFile. From caffdb6ea86b20643508cdcf31d8f1c499ac3413 Mon Sep 17 00:00:00 2001 From: xEricL <37921711+xEricL@users.noreply.github.com> Date: Sat, 19 Oct 2024 17:48:31 -0400 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=9A=A8=20Test:=20Validate=20`SendFile?= =?UTF-8?q?`=20Content-Type=20header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/testdata/fs/img/fiberpng | Bin 0 -> 1542 bytes .github/testdata/fs/img/fiberpng.jpeg | Bin 0 -> 1542 bytes .github/testdata/fs/img/fiberpng.notvalidext | Bin 0 -> 1542 bytes ctx_test.go | 43 +++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 .github/testdata/fs/img/fiberpng create mode 100644 .github/testdata/fs/img/fiberpng.jpeg create mode 100644 .github/testdata/fs/img/fiberpng.notvalidext diff --git a/.github/testdata/fs/img/fiberpng b/.github/testdata/fs/img/fiberpng new file mode 100644 index 0000000000000000000000000000000000000000..fa981bcf261035838ef96c52ef4ae9339ca1e788 GIT binary patch literal 1542 zcmb7E`#aMM82`GJW=h3enp<|(j?0?d3#qx}X>DtSA}qHsp`1jAaM(C9BKKV9I*v`! zlv_5}VidXM9%&}z5-J@2fu5(&^S;mLeLv6Vecs#4);8|453)(Sv{H8Pa&<2yML-Oh`C@g@>s zjXR5esQ7LZXQdnlejIO-js*Y_O-s{r*7pSFo5FW5fw0`IQt48`z42FYAIGL;iiD$p z#<2E?+*N+a`p!xaG9kh-6S7+>gU)ta*|i(|B^<*vDgV@{2rOvy2Ks*jn{QfgMhd zUdV&-#afzSwu5wPQP~iS(g(XpO3vqLPaF-2sVjEk5T7gIxjRJK3)*@u*U6*Omrp%HsHi?OQ44Fk@t$x9`ealid73L|jHnlTUkLAT;wz znU)<8C%PQ*gOF6dgl5gns-3HryP99^HNdA!(V~nR%)CFxK0^3?W!MEr25dGLa!~Zp z{VL>WZjXYByjrJsv-_BdgJbebEQL7KT-pb_4`fhCv#*#%3z7AA|8%5f%D&RGZ9zU# z$jhI9CY0o`e@ZUZQ_91%$WM5_6KJSc4b(dU9)0FQz*&~tSJ0qm9b}kygf_P6BbS0? zmVM)64rmJ1PyJAOi_cb6CG~Je~O$Ar7}EcY^uR^NGI@v7~=A4hbQXPYtta%IQc_>B;TXtjUL{RqIzw*-kO!RsE$I z=dCOv9!=)p-yw`=xjot%YEN!oYbzjXgJZJ|RkI)aJgi(0T+^vT6=V5n*5Z^rW$v@X z5@X=&Mt$?Wa%Z2o>DUolkMkFnO;~Pi1*6U~5otYBsWUzpapDrNFSPZLG1uH&T%J}A zyRE=DxE7gz*sF>-C@p+h zaw_T{4jm$f*6UXnRn5mve-WS-Wz=brP{ZZVYpzl)Cci`U;a`{*%-LI|(HMy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