From 719992cb754dcdc955899a018d46e7fc354563c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20Can=20=C3=96ZER?= Date: Wed, 2 Feb 2022 19:04:24 +0200 Subject: [PATCH 1/7] Add 3rd party middleware eozer/fiber_ldapauth (#1743) * Add 3rd party middleware eozer/fiber_ldapauth * Add 3rd party middleware eozer/fiber_ldapauth to other READMEs as well --- .github/README.md | 1 + .github/README_ckb.md | 1 + .github/README_de.md | 1 + .github/README_es.md | 1 + .github/README_fa.md | 1 + .github/README_fr.md | 1 + .github/README_he.md | 1 + .github/README_id.md | 1 + .github/README_it.md | 1 + .github/README_ja.md | 1 + .github/README_ko.md | 1 + .github/README_nl.md | 1 + .github/README_pt.md | 1 + .github/README_ru.md | 1 + .github/README_sa.md | 1 + .github/README_tr.md | 1 + .github/README_zh-CN.md | 1 + .github/README_zh-TW.md | 1 + 18 files changed, 18 insertions(+) diff --git a/.github/README.md b/.github/README.md index 3ffe71d55be..64bc6e37f12 100644 --- a/.github/README.md +++ b/.github/README.md @@ -604,6 +604,7 @@ This is a list of middlewares that are created by the Fiber community, please cr - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 Contribute diff --git a/.github/README_ckb.md b/.github/README_ckb.md index 0cceafa7d41..4b12e914e4a 100644 --- a/.github/README_ckb.md +++ b/.github/README_ckb.md @@ -604,6 +604,7 @@ func main() { - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 هاوکار بە diff --git a/.github/README_de.md b/.github/README_de.md index 517349462ad..9f3dd5587ec 100644 --- a/.github/README_de.md +++ b/.github/README_de.md @@ -574,6 +574,7 @@ This is a list of middlewares that are created by the Fiber community, please cr - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 Mitwirken diff --git a/.github/README_es.md b/.github/README_es.md index e753e8bca94..c50e524b0c8 100644 --- a/.github/README_es.md +++ b/.github/README_es.md @@ -574,6 +574,7 @@ La siguiente es una lista de los proyectos de middleware creados por la comunida - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 Contribuir diff --git a/.github/README_fa.md b/.github/README_fa.md index 81129bd3bd9..ad2ddbbadde 100644 --- a/.github/README_fa.md +++ b/.github/README_fa.md @@ -705,6 +705,7 @@ Middleware های لیست زیر توسط انجمن Fiber ایجاد است, - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth)
diff --git a/.github/README_fr.md b/.github/README_fr.md index 7e47f6b8dd6..0dcc691089e 100644 --- a/.github/README_fr.md +++ b/.github/README_fr.md @@ -576,6 +576,7 @@ This is a list of middlewares that are created by the Fiber community, please cr - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 Contribuer diff --git a/.github/README_he.md b/.github/README_he.md index f65aac435e4..6708d6fcaec 100644 --- a/.github/README_he.md +++ b/.github/README_he.md @@ -710,6 +710,7 @@ This is a list of middlewares that are created by the Fiber community, please cr - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) diff --git a/.github/README_id.md b/.github/README_id.md index 82648f9ca75..e83ceee61b8 100644 --- a/.github/README_id.md +++ b/.github/README_id.md @@ -577,6 +577,7 @@ Berikut adalah kumpulan _middlewares_ yang dibuat oleh komunitas Fiber, silahkan - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 Berkontribusi diff --git a/.github/README_it.md b/.github/README_it.md index ca3c3996b62..a752de5455a 100644 --- a/.github/README_it.md +++ b/.github/README_it.md @@ -600,6 +600,7 @@ Questa è una lista dei middleware che sono stati creati dalla comunità di Fibe - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 Contribuire diff --git a/.github/README_ja.md b/.github/README_ja.md index 6c9277dfa29..68d4bcd4bcb 100644 --- a/.github/README_ja.md +++ b/.github/README_ja.md @@ -580,6 +580,7 @@ This is a list of middlewares that are created by the Fiber community, please cr - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 貢献する diff --git a/.github/README_ko.md b/.github/README_ko.md index addae78ac9c..ddc6c07a8c7 100644 --- a/.github/README_ko.md +++ b/.github/README_ko.md @@ -580,6 +580,7 @@ Fiber 프레임워크에 포함되는 미들웨어 목록입니다. - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 기여 diff --git a/.github/README_nl.md b/.github/README_nl.md index 2a7b21ed71d..a52f32d916f 100644 --- a/.github/README_nl.md +++ b/.github/README_nl.md @@ -580,6 +580,7 @@ This is a list of middlewares that are created by the Fiber community, please cr - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 Bijdragen diff --git a/.github/README_pt.md b/.github/README_pt.md index 60143741c5b..3e818406a22 100644 --- a/.github/README_pt.md +++ b/.github/README_pt.md @@ -574,6 +574,7 @@ Esta é uma lista de middlewares criados pela comunidade do Fiber, se quiser ter - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 Contribuindo diff --git a/.github/README_ru.md b/.github/README_ru.md index a17b5e39f45..eee845895c1 100644 --- a/.github/README_ru.md +++ b/.github/README_ru.md @@ -581,6 +581,7 @@ func main() { - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 Помощь проекту diff --git a/.github/README_sa.md b/.github/README_sa.md index 39729ee4d17..4518eea3829 100644 --- a/.github/README_sa.md +++ b/.github/README_sa.md @@ -641,6 +641,7 @@ List of externally hosted middleware modules and maintained by the [Fiber team]( - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 مساهمة diff --git a/.github/README_tr.md b/.github/README_tr.md index 2d6dd228152..ba10a4cc9ca 100644 --- a/.github/README_tr.md +++ b/.github/README_tr.md @@ -574,6 +574,7 @@ Bu, Fiber topluluğu tarafından oluşturulan middleware'lerin bir listesidir, s - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 Destek diff --git a/.github/README_zh-CN.md b/.github/README_zh-CN.md index 18db31a757a..d54c56149ca 100644 --- a/.github/README_zh-CN.md +++ b/.github/README_zh-CN.md @@ -576,6 +576,7 @@ func main() { - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 贡献 diff --git a/.github/README_zh-TW.md b/.github/README_zh-TW.md index 720d9d4906b..5d8c8ba93c3 100644 --- a/.github/README_zh-TW.md +++ b/.github/README_zh-TW.md @@ -574,6 +574,7 @@ func main() { - [theArtechnology/fiber-inertia](https://github.com/theArtechnology/fiber-inertia) - [aschenmaker/fiber-health-check](https://github.com/aschenmaker/fiber-health-check) - [elastic/apmfiber](https://github.com/elastic/apm-agent-go/tree/master/module/apmfiber) +- [eozer/fiber_ldapauth](https://github.com/eozer/fiber_ldapauth) ## 👍 貢獻 From d85ae2bce3cfeb8f4d3216330a46b5f5abf66987 Mon Sep 17 00:00:00 2001 From: Norbert Hauriel Date: Thu, 3 Feb 2022 09:26:25 +0100 Subject: [PATCH 2/7] Doc(limiter): clarify variable name 'expire' (#1742) This clarifies the intent of variable 'expire' by renaming to the more understandable 'resetInSec'. All mentions are renamed as well. --- middleware/limiter/limited_fixed.go | 6 +++--- middleware/limiter/limited_sliding.go | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/middleware/limiter/limited_fixed.go b/middleware/limiter/limited_fixed.go index 7936ca7a31e..3e41a2c9cd3 100644 --- a/middleware/limiter/limited_fixed.go +++ b/middleware/limiter/limited_fixed.go @@ -64,7 +64,7 @@ func (FixedWindow) New(cfg Config) fiber.Handler { e.currHits++ // Calculate when it resets in seconds - expire := e.exp - ts + resetInSec := e.exp - ts // Set how many hits we have left remaining := cfg.Max - e.currHits @@ -79,7 +79,7 @@ func (FixedWindow) New(cfg Config) fiber.Handler { if remaining < 0 { // Return response with Retry-After header // https://tools.ietf.org/html/rfc6584 - c.Set(fiber.HeaderRetryAfter, strconv.FormatUint(expire, 10)) + c.Set(fiber.HeaderRetryAfter, strconv.FormatUint(resetInSec, 10)) // Call LimitReached handler return cfg.LimitReached(c) @@ -101,7 +101,7 @@ func (FixedWindow) New(cfg Config) fiber.Handler { // We can continue, update RateLimit headers c.Set(xRateLimitLimit, max) c.Set(xRateLimitRemaining, strconv.Itoa(remaining)) - c.Set(xRateLimitReset, strconv.FormatUint(expire, 10)) + c.Set(xRateLimitReset, strconv.FormatUint(resetInSec, 10)) return err } diff --git a/middleware/limiter/limited_sliding.go b/middleware/limiter/limited_sliding.go index 5f6ed434d66..9369998d29e 100644 --- a/middleware/limiter/limited_sliding.go +++ b/middleware/limiter/limited_sliding.go @@ -77,10 +77,10 @@ func (SlidingWindow) New(cfg Config) fiber.Handler { e.currHits++ // Calculate when it resets in seconds - expire := e.exp - ts + resetInSec := e.exp - ts // weight = time until current window reset / total window length - weight := float64(expire) / float64(expiration) + weight := float64(resetInSec) / float64(expiration) // rate = request count in previous window - weight + request count in current window rate := int(float64(e.prevHits)*weight) + e.currHits @@ -89,18 +89,18 @@ func (SlidingWindow) New(cfg Config) fiber.Handler { remaining := cfg.Max - rate // Update storage. Garbage collect when the next window ends. - // |-------------------------|-------------------------| - // ^ ^ ^ ^ - // ts e.exp End sample window End next window - // <-----------> - // expire - // expire = e.exp - ts - time until end of current window. + // |--------------------------|--------------------------| + // ^ ^ ^ ^ + // ts e.exp End sample window End next window + // <------------> + // resetInSec + // resetInSec = e.exp - ts - time until end of current window. // duration + expiration = end of next window. // Because we don't want to garbage collect in the middle of a window // we add the expiration to the duration. // Otherwise after the end of "sample window", attackers could launch // a new request with the full window length. - manager.set(key, e, time.Duration(expire+expiration)*time.Second) + manager.set(key, e, time.Duration(resetInSec+expiration)*time.Second) // Unlock entry mux.Unlock() @@ -109,7 +109,7 @@ func (SlidingWindow) New(cfg Config) fiber.Handler { if remaining < 0 { // Return response with Retry-After header // https://tools.ietf.org/html/rfc6584 - c.Set(fiber.HeaderRetryAfter, strconv.FormatUint(expire, 10)) + c.Set(fiber.HeaderRetryAfter, strconv.FormatUint(resetInSec, 10)) // Call LimitReached handler return cfg.LimitReached(c) @@ -131,7 +131,7 @@ func (SlidingWindow) New(cfg Config) fiber.Handler { // We can continue, update RateLimit headers c.Set(xRateLimitLimit, max) c.Set(xRateLimitRemaining, strconv.Itoa(remaining)) - c.Set(xRateLimitReset, strconv.FormatUint(expire, 10)) + c.Set(xRateLimitReset, strconv.FormatUint(resetInSec, 10)) return err } From 6b29698d91d833d5ec694363dbb809bc88618d4b Mon Sep 17 00:00:00 2001 From: RW Date: Thu, 3 Feb 2022 09:43:41 +0100 Subject: [PATCH 3/7] prepare release 2.26.0 --- app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.go b/app.go index fb2d2587ca0..49d190fae0e 100644 --- a/app.go +++ b/app.go @@ -36,7 +36,7 @@ import ( ) // Version of current fiber package -const Version = "2.25.0" +const Version = "2.26.0" // Handler defines a function to serve HTTP requests. type Handler = func(*Ctx) error From 39a503cc1d2d08f55954cd1963a3276924bf2838 Mon Sep 17 00:00:00 2001 From: Aliqyan Tapia Date: Thu, 3 Feb 2022 08:09:11 -0500 Subject: [PATCH 4/7] =?UTF-8?q?=F0=9F=9A=80=20SaveFile=20to=20default=20Ex?= =?UTF-8?q?ternal=20Storage=20(#1557)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fiber change * reference correct storage in SaveFile * seperate logic to new function * Change storage defination style, fix tests on go1.14 * Add unit test. Co-authored-by: Alex Bakker Co-authored-by: M. Efe Çetin --- app.go | 2 +- ctx.go | 16 ++++++++++++++++ ctx_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index 49d190fae0e..493621fdd12 100644 --- a/app.go +++ b/app.go @@ -359,7 +359,7 @@ type Config struct { trustedProxiesMap map[string]struct{} trustedProxyRanges []*net.IPNet - //If set to true, will print all routes with their method, path and handler. + // If set to true, will print all routes with their method, path and handler. // Default: false EnablePrintRoutes bool `json:"enable_print_routes"` } diff --git a/ctx.go b/ctx.go index af2032ac080..c14b3a2773a 100644 --- a/ctx.go +++ b/ctx.go @@ -11,6 +11,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "mime/multipart" "net" "net/http" @@ -1128,6 +1129,21 @@ func (c *Ctx) SaveFile(fileheader *multipart.FileHeader, path string) error { return fasthttp.SaveMultipartFile(fileheader, path) } +// SaveFileToStorage saves any multipart file to an external storage system. +func (c *Ctx) SaveFileToStorage(fileheader *multipart.FileHeader, path string, storage Storage) error { + file, err := fileheader.Open() + if err != nil { + return err + } + + content, err := ioutil.ReadAll(file) + if err != nil { + return err + } + + return storage.Set(path, content, 0) +} + // Secure returns a boolean property, that is true, if a TLS connection is established. func (c *Ctx) Secure() bool { return c.fasthttp.IsTLS() diff --git a/ctx_test.go b/ctx_test.go index f5149dbb02d..3a790bf315a 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -27,6 +27,7 @@ import ( "time" "github.com/gofiber/fiber/v2/internal/bytebufferpool" + "github.com/gofiber/fiber/v2/internal/storage/memory" "github.com/gofiber/fiber/v2/utils" "github.com/valyala/fasthttp" ) @@ -1614,6 +1615,48 @@ func Test_Ctx_SaveFile(t *testing.T) { utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") } +// go test -run Test_Ctx_SaveFileToStorage +func Test_Ctx_SaveFileToStorage(t *testing.T) { + t.Parallel() + app := New() + storage := memory.New() + + app.Post("/test", func(c *Ctx) error { + fh, err := c.FormFile("file") + utils.AssertEqual(t, nil, err) + + err = c.SaveFileToStorage(fh, "test", storage) + utils.AssertEqual(t, nil, err) + + file, err := storage.Get("test") + utils.AssertEqual(t, []byte("hello world"), file) + utils.AssertEqual(t, nil, err) + + err = storage.Delete("test") + utils.AssertEqual(t, nil, err) + + return nil + }) + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + + ioWriter, err := writer.CreateFormFile("file", "test") + utils.AssertEqual(t, nil, err) + + _, err = ioWriter.Write([]byte("hello world")) + utils.AssertEqual(t, nil, err) + writer.Close() + + req := httptest.NewRequest(MethodPost, "/test", body) + req.Header.Set("Content-Type", writer.FormDataContentType()) + req.Header.Set("Content-Length", strconv.Itoa(len(body.Bytes()))) + + resp, err := app.Test(req) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") +} + // go test -run Test_Ctx_Secure func Test_Ctx_Secure(t *testing.T) { t.Parallel() From 4d3cc6cd89f95a4b143d9a6d18874b906c8ed416 Mon Sep 17 00:00:00 2001 From: RW Date: Thu, 3 Feb 2022 14:14:32 +0100 Subject: [PATCH 5/7] Bump github.com/valyala/fasthttp from 1.32.0 to 1.33.0 (#1744) --- go.mod | 4 ++-- go.sum | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index a676731cd09..4e2f97572f3 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/gofiber/fiber/v2 go 1.16 require ( - github.com/valyala/fasthttp v1.32.0 - golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 + github.com/valyala/fasthttp v1.33.0 + golang.org/x/sys v0.0.0-20220111092808-5a964db01320 ) diff --git a/go.sum b/go.sum index e77d494f835..67e5a03ee74 100644 --- a/go.sum +++ b/go.sum @@ -1,22 +1,22 @@ -github.com/andybalholm/brotli v1.0.2 h1:JKnhI/XQ75uFBTiuzXpzFrUriDPiZjlOSzh6wXogP0E= -github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= -github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= -github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= +github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/klauspost/compress v1.14.1 h1:hLQYb23E8/fO+1u53d02A97a8UnsddcvYzq4ERRU4ds= +github.com/klauspost/compress v1.14.1/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.32.0 h1:keswgWzyKyNIIjz2a7JmCYHOOIkRp6HMx9oTV6QrZWY= -github.com/valyala/fasthttp v1.32.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus= +github.com/valyala/fasthttp v1.33.0 h1:mHBKd98J5NcXuBddgjvim1i3kWzlng1SzLhrnBOU9g8= +github.com/valyala/fasthttp v1.33.0/go.mod h1:KJRK/MXx0J+yd0c5hlR+s1tIHD72sniU8ZJjl97LIw4= github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= -golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220111093109-d55c255bac03/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E= -golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY= +golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= From af10fab05b14bf7fa1d31c9ca37c6a50dd510abc Mon Sep 17 00:00:00 2001 From: Lian Date: Thu, 3 Feb 2022 08:41:45 -0500 Subject: [PATCH 6/7] Add direct download option when serving static files (#1729) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * enable download for app.Static wording * Add unit test. Co-authored-by: Muhammed Efe Çetin --- app.go | 4 ++++ app_test.go | 16 ++++++++++++++++ router.go | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/app.go b/app.go index 493621fdd12..17ca68d1066 100644 --- a/app.go +++ b/app.go @@ -379,6 +379,10 @@ type Static struct { // Optional. Default value false. Browse bool `json:"browse"` + // When set to true, enables direct download. + // Optional. Default value false. + Download bool `json:"download"` + // The name of the index file for serving a directory. // Optional. Default value "index.html". Index string `json:"index"` diff --git a/app_test.go b/app_test.go index 5c17866e5db..a9ec4d01da4 100644 --- a/app_test.go +++ b/app_test.go @@ -678,6 +678,22 @@ func Test_App_Static_MaxAge(t *testing.T) { utils.AssertEqual(t, "public, max-age=100", resp.Header.Get(HeaderCacheControl), "CacheControl Control") } +// go test -run Test_App_Static_Download +func Test_App_Static_Download(t *testing.T) { + app := New() + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + defer app.ReleaseCtx(c) + + app.Static("/fiber.png", "./.github/testdata/fs/img/fiber.png", Static{Download: true}) + + resp, err := app.Test(httptest.NewRequest("GET", "/fiber.png", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, 200, resp.StatusCode, "Status code") + utils.AssertEqual(t, false, resp.Header.Get(HeaderContentLength) == "") + utils.AssertEqual(t, "image/png", resp.Header.Get(HeaderContentType)) + utils.AssertEqual(t, `attachment`, resp.Header.Get(HeaderContentDisposition)) +} + // go test -run Test_App_Static_Group func Test_App_Static_Group(t *testing.T) { app := New() diff --git a/router.go b/router.go index cc88299ea16..4190771bcf7 100644 --- a/router.go +++ b/router.go @@ -377,6 +377,10 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router { } // Serve file fileHandler(c.fasthttp) + // Sets the response Content-Disposition header to attachment if the Download option is true + if len(config) > 0 && config[0].Download { + c.Attachment() + } // Return request if found and not forbidden status := c.fasthttp.Response.StatusCode() if status != StatusNotFound && status != StatusForbidden { From 8853190031b86bb7ea7148954eaf55818d3bb4d6 Mon Sep 17 00:00:00 2001 From: Matthias Neugebauer Date: Thu, 3 Feb 2022 15:09:46 +0100 Subject: [PATCH 7/7] =?UTF-8?q?=F0=9F=94=A5=20Feature:=20Add=20ability=20t?= =?UTF-8?q?o=20restart=20route=20handling=20(#1739)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🔥 Feature: Add ability to restart route handling * Change test names Co-authored-by: RW --- ctx.go | 8 +++++++ ctx_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/ctx.go b/ctx.go index c14b3a2773a..ab1a9eaa333 100644 --- a/ctx.go +++ b/ctx.go @@ -783,6 +783,14 @@ func (c *Ctx) Next() (err error) { return err } +// RestartRouting instead of going to the next handler. This may be usefull after +// changing the request path. Note that handlers might be executed again. +func (c *Ctx) RestartRouting() error { + c.indexRoute = -1 + _, err := c.app.next(c) + return err +} + // OriginalURL contains the original request URL. // Returned value is only valid within the handler. Do not store any references. // Make copies or use the Immutable setting to use the value outside the Handler. diff --git a/ctx_test.go b/ctx_test.go index 3a790bf315a..1e857143b8e 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -2108,6 +2108,69 @@ func Test_Ctx_RenderWithLocalsAndBinding(t *testing.T) { utils.AssertEqual(t, "

Hello, World!

", string(c.Response().Body())) } +// go test -run Test_Ctx_RestartRouting +func Test_Ctx_RestartRouting(t *testing.T) { + app := New() + calls := 0 + app.Get("/", func(c *Ctx) error { + calls++ + if calls < 3 { + return c.RestartRouting() + } + return nil + }) + resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") + utils.AssertEqual(t, 3, calls, "Number of calls") +} + +// go test -run Test_Ctx_RestartRoutingWithChangedPath +func Test_Ctx_RestartRoutingWithChangedPath(t *testing.T) { + app := New() + executedOldHandler := false + executedNewHandler := false + + app.Get("/old", func(c *Ctx) error { + c.Path("/new") + return c.RestartRouting() + }) + app.Get("/old", func(c *Ctx) error { + executedOldHandler = true + return nil + }) + app.Get("/new", func(c *Ctx) error { + executedNewHandler = true + return nil + }) + + resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/old", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") + utils.AssertEqual(t, false, executedOldHandler, "Executed old handler") + utils.AssertEqual(t, true, executedNewHandler, "Executed new handler") +} + +// go test -run Test_Ctx_RestartRoutingWithChangedPathAnd404 +func Test_Ctx_RestartRoutingWithChangedPathAndCatchAll(t *testing.T) { + app := New() + app.Get("/new", func(c *Ctx) error { + return nil + }) + app.Use(func(c *Ctx) error { + c.Path("/new") + // c.Next() would fail this test as a 404 is returned from the next handler + return c.RestartRouting() + }) + app.Use(func(c *Ctx) error { + return ErrNotFound + }) + + resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/old", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") +} + type testTemplateEngine struct { templates *template.Template }