Skip to content

Commit

Permalink
Merge branch 'gofiber:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
NorbertHauriel committed Feb 3, 2022
2 parents 122cbfe + 8853190 commit c58a7f6
Show file tree
Hide file tree
Showing 25 changed files with 189 additions and 17 deletions.
1 change: 1 addition & 0 deletions .github/README.md
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions .github/README_ckb.md
Expand Up @@ -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)

## 👍 هاوکار بە

Expand Down
1 change: 1 addition & 0 deletions .github/README_de.md
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions .github/README_es.md
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions .github/README_fa.md
Expand Up @@ -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)

</br>

Expand Down
1 change: 1 addition & 0 deletions .github/README_fr.md
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions .github/README_he.md
Expand Up @@ -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)

</div>

Expand Down
1 change: 1 addition & 0 deletions .github/README_id.md
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions .github/README_it.md
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions .github/README_ja.md
Expand Up @@ -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)

## 👍 貢献する

Expand Down
1 change: 1 addition & 0 deletions .github/README_ko.md
Expand Up @@ -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)

## 👍 기여

Expand Down
1 change: 1 addition & 0 deletions .github/README_nl.md
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions .github/README_pt.md
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions .github/README_ru.md
Expand Up @@ -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)

## 👍 Помощь проекту

Expand Down
1 change: 1 addition & 0 deletions .github/README_sa.md
Expand Up @@ -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)

## 👍 مساهمة

Expand Down
1 change: 1 addition & 0 deletions .github/README_tr.md
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions .github/README_zh-CN.md
Expand Up @@ -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)

## 👍 贡献

Expand Down
1 change: 1 addition & 0 deletions .github/README_zh-TW.md
Expand Up @@ -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)

## 👍 貢獻

Expand Down
8 changes: 6 additions & 2 deletions app.go
Expand Up @@ -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
Expand Down Expand Up @@ -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"`
}
Expand All @@ -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"`
Expand Down
16 changes: 16 additions & 0 deletions app_test.go
Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions ctx.go
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
Expand Down Expand Up @@ -782,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.
Expand Down Expand Up @@ -1128,6 +1137,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()
Expand Down
106 changes: 106 additions & 0 deletions ctx_test.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -2065,6 +2108,69 @@ func Test_Ctx_RenderWithLocalsAndBinding(t *testing.T) {
utils.AssertEqual(t, "<h1>Hello, World!</h1>", 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
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -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
)

0 comments on commit c58a7f6

Please sign in to comment.