Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Aug 28, 2024
2 parents 59b290d + 062ac3b commit 0fbb447
Show file tree
Hide file tree
Showing 54 changed files with 1,699 additions and 243 deletions.
6 changes: 3 additions & 3 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Fiber v3 is currently in beta and under active development. While it offers exci

## ⚙️ Installation

Fiber requires **Go version `1.21` or higher** to run. If you need to install or upgrade Go, visit the [official Go download page](https://go.dev/dl/). To start setting up your project. Create a new directory for your project and navigate into it. Then, initialize your project with Go modules by executing the following command in your terminal:
Fiber requires **Go version `1.22` or higher** to run. If you need to install or upgrade Go, visit the [official Go download page](https://go.dev/dl/). To start setting up your project. Create a new directory for your project and navigate into it. Then, initialize your project with Go modules by executing the following command in your terminal:

```bash
go mod init github.com/your/repo
Expand Down Expand Up @@ -124,7 +124,7 @@ We **listen** to our users in [issues](https://github.com/gofiber/fiber/issues),

## ⚠️ Limitations

- Due to Fiber's usage of unsafe, the library may not always be compatible with the latest Go version. Fiber v3 has been tested with Go versions 1.21 and 1.22.
- Due to Fiber's usage of unsafe, the library may not always be compatible with the latest Go version. Fiber v3 has been tested with Go versions 1.22 and 1.23.
- Fiber is not compatible with net/http interfaces. This means you will not be able to use projects like gqlgen, go-swagger, or any others which are part of the net/http ecosystem.

## 👀 Examples
Expand Down Expand Up @@ -615,7 +615,7 @@ List of externally hosted middleware modules and maintained by the [Fiber team](
| :------------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------- |
| [contrib](https://github.com/gofiber/contrib) | Third party middlewares |
| [storage](https://github.com/gofiber/storage) | Premade storage drivers that implement the Storage interface, designed to be used with various Fiber middlewares. |
| [template](https://github.com/gofiber/template) | This package contains 9 template engines that can be used with Fiber `v3` Go version 1.21 or higher is required. |
| [template](https://github.com/gofiber/template) | This package contains 9 template engines that can be used with Fiber `v3` Go version 1.22 or higher is required. |

## 🕶️ Awesome List

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
uses: actions/setup-go@v5
with:
# NOTE: Keep this in sync with the version from go.mod
go-version: "1.21.x"
go-version: "1.22.x"

- name: Run Benchmark
run: set -o pipefail; go test ./... -benchmem -run=^$ -bench . | tee output.txt
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ jobs:
- uses: actions/setup-go@v5
with:
# NOTE: Keep this in sync with the version from go.mod
go-version: "1.21.x"
go-version: "1.22.x"
cache: false

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
# NOTE: Keep this in sync with the version from .golangci.yml
version: v1.59.1
version: v1.60.1
2 changes: 1 addition & 1 deletion .github/workflows/manual-dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Checkout dependabot
run: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
unit:
strategy:
matrix:
go-version: [1.21.x, 1.22.x]
platform: [ubuntu-latest, windows-latest, macos-latest, macos-14]
go-version: [1.22.x, 1.23.x]
platform: [ubuntu-latest, windows-latest, macos-latest, macos-13]
runs-on: ${{ matrix.platform }}
steps:
- name: Fetch Repository
Expand All @@ -31,7 +31,7 @@ jobs:
run: go run gotest.tools/gotestsum@latest -f testname -- ./... -race -count=1 -coverprofile=coverage.txt -covermode=atomic -shuffle=on

- name: Upload coverage reports to Codecov
if: ${{ matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.22.x' }}
if: ${{ matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.23.x' }}
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
Expand Down
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ markdown:
## lint: 🚨 Run lint checks
.PHONY: lint
lint:
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1 run ./...
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.60.1 run ./...

## test: 🚦 Execute all tests
.PHONY: test
Expand All @@ -55,4 +55,13 @@ tidy:
## betteralign: 📐 Optimize alignment of fields in structs
.PHONY: betteralign
betteralign:
go run github.com/dkorunic/betteralign/cmd/betteralign@latest -test_files -generated_files -apply ./...
go run github.com/dkorunic/betteralign/cmd/betteralign@latest -test_files -generated_files -apply ./...

## tidy: ⚡️ Generate msgp
.PHONY: msgp
msgp:
go run github.com/tinylib/msgp@latest -file="middleware/cache/manager.go" -o="middleware/cache/manager_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/session/data.go" -o="middleware/session/data_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/csrf/storage_manager.go" -o="middleware/csrf/storage_manager_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/limiter/manager.go" -o="middleware/limiter/manager_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/idempotency/response.go" -o="middleware/idempotency/response_msgp.go" -tests=true -unexported
4 changes: 4 additions & 0 deletions binder/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func (b *formBinding) Bind(reqCtx *fasthttp.RequestCtx, out any) error {
}
})

if err != nil {
return err
}

return parse(b.Name(), out, data)
}

Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ func setConfigToRequest(req *Request, config ...Config) {
return
}

if cfg.File != nil && len(cfg.File) != 0 {
if len(cfg.File) != 0 {
req.AddFiles(cfg.File...)
return
}
Expand Down
2 changes: 1 addition & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ func Test_Set_Config_To_Request(t *testing.T) {
key := struct{}{}

ctx := context.Background()
ctx = context.WithValue(ctx, key, "v1")
ctx = context.WithValue(ctx, key, "v1") //nolint: staticcheck // not needed for tests

req := AcquireRequest()

Expand Down
6 changes: 3 additions & 3 deletions client/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func Test_Request_Context(t *testing.T) {

require.Nil(t, ctx.Value(key))

ctx = context.WithValue(ctx, key, "string")
ctx = context.WithValue(ctx, key, "string") //nolint: staticcheck // not needed for tests
req.SetContext(ctx)
ctx = req.Context()

Expand Down Expand Up @@ -1603,8 +1603,8 @@ func Benchmark_SetValWithStruct(b *testing.B) {
require.Empty(b, string(p.Peek("TInt")))
require.Empty(b, string(p.Peek("TString")))
require.Empty(b, string(p.Peek("TFloat")))
require.Empty(b, len(p.PeekMulti("TSlice")))
require.Empty(b, len(p.PeekMulti("int_slice")))
require.Empty(b, p.PeekMulti("TSlice"))
require.Empty(b, p.PeekMulti("int_slice"))
})

b.Run("error type should ignore", func(b *testing.B) {
Expand Down
3 changes: 1 addition & 2 deletions client/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"sync"

"github.com/gofiber/utils/v2"
Expand Down Expand Up @@ -68,7 +67,7 @@ func (r *Response) Body() []byte {

// String method returns the body of the server response as String.
func (r *Response) String() string {
return strings.TrimSpace(string(r.Body()))
return utils.Trim(string(r.Body()), ' ')
}

// JSON method will unmarshal body to json.
Expand Down
41 changes: 22 additions & 19 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,7 @@ func (c *DefaultCtx) BaseURL() string {
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting instead.
func (c *DefaultCtx) BodyRaw() []byte {
if c.app.config.Immutable {
return utils.CopyBytes(c.fasthttp.Request.Body())
}
return c.fasthttp.Request.Body()
return c.getBody()
}

func (c *DefaultCtx) tryDecodeBodyInOrder(
Expand Down Expand Up @@ -339,21 +336,19 @@ func (c *DefaultCtx) Body() []byte {
encodingOrder = []string{"", "", ""}
)

// faster than peek
c.Request().Header.VisitAll(func(key, value []byte) {
if c.app.getString(key) == HeaderContentEncoding {
headerEncoding = c.app.getString(value)
}
})
// Get Content-Encoding header
headerEncoding = utils.UnsafeString(c.Request().Header.ContentEncoding())

// If no encoding is provided, return the original body
if len(headerEncoding) == 0 {
return c.getBody()
}

// Split and get the encodings list, in order to attend the
// rule defined at: https://www.rfc-editor.org/rfc/rfc9110#section-8.4-5
encodingOrder = getSplicedStrList(headerEncoding, encodingOrder)
if len(encodingOrder) == 0 {
if c.app.config.Immutable {
return utils.CopyBytes(c.fasthttp.Request.Body())
}
return c.fasthttp.Request.Body()
return c.getBody()
}

var decodesRealized uint8
Expand Down Expand Up @@ -778,7 +773,7 @@ iploop:
i++
}

s := strings.TrimRight(headerValue[i:j], " ")
s := utils.TrimRight(headerValue[i:j], ' ')

if c.app.config.EnableIPValidation {
// Skip validation if IP is clearly not IPv4/IPv6, otherwise validate without allocations
Expand Down Expand Up @@ -828,7 +823,7 @@ func (c *DefaultCtx) extractIPFromHeader(header string) string {
i++
}

s := strings.TrimRight(headerValue[i:j], " ")
s := utils.TrimRight(headerValue[i:j], ' ')

if c.app.config.EnableIPValidation {
if (!v6 && !v4) || (v6 && !utils.IsIPv6(s)) || (v4 && !utils.IsIPv4(s)) {
Expand Down Expand Up @@ -862,7 +857,7 @@ func (c *DefaultCtx) Is(extension string) bool {
}

return strings.HasPrefix(
strings.TrimLeft(utils.UnsafeString(c.fasthttp.Request.Header.ContentType()), " "),
utils.TrimLeft(utils.UnsafeString(c.fasthttp.Request.Header.ContentType()), ' '),
extensionHeader,
)
}
Expand Down Expand Up @@ -939,7 +934,7 @@ func (c *DefaultCtx) Links(link ...string) {
bb.WriteString(`; rel="` + link[i] + `",`)
}
}
c.setCanonical(HeaderLink, strings.TrimRight(c.app.getString(bb.Bytes()), ","))
c.setCanonical(HeaderLink, utils.TrimRight(c.app.getString(bb.Bytes()), ','))
bytebufferpool.Put(bb)
}

Expand Down Expand Up @@ -1810,7 +1805,7 @@ func (c *DefaultCtx) configDependentPaths() {
}
// If StrictRouting is disabled, we strip all trailing slashes
if !c.app.config.StrictRouting && len(c.detectionPathBuffer) > 1 && c.detectionPathBuffer[len(c.detectionPathBuffer)-1] == '/' {
c.detectionPathBuffer = bytes.TrimRight(c.detectionPathBuffer, "/")
c.detectionPathBuffer = utils.TrimRight(c.detectionPathBuffer, '/')
}
c.detectionPath = c.app.getString(c.detectionPathBuffer)

Expand Down Expand Up @@ -1909,6 +1904,14 @@ func (c *DefaultCtx) release() {
}
}

func (c *DefaultCtx) getBody() []byte {
if c.app.config.Immutable {
return utils.CopyBytes(c.fasthttp.Request.Body())
}

return c.fasthttp.Request.Body()
}

// Methods to use with next stack.
func (c *DefaultCtx) getMethodINT() int {
return c.methodINT
Expand Down
60 changes: 57 additions & 3 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,26 @@ func Test_Ctx_Body(t *testing.T) {
require.Equal(t, []byte("john=doe"), c.Body())
}

// go test -run Test_Ctx_BodyRaw
func Test_Ctx_BodyRaw(t *testing.T) {
t.Parallel()
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed

c.Request().SetBodyRaw([]byte("john=doe"))
require.Equal(t, []byte("john=doe"), c.BodyRaw())
}

// go test -run Test_Ctx_BodyRaw_Immutable
func Test_Ctx_BodyRaw_Immutable(t *testing.T) {
t.Parallel()
app := New(Config{Immutable: true})
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed

c.Request().SetBodyRaw([]byte("john=doe"))
require.Equal(t, []byte("john=doe"), c.BodyRaw())
}

// go test -v -run=^$ -bench=Benchmark_Ctx_Body -benchmem -count=4
func Benchmark_Ctx_Body(b *testing.B) {
const input = "john=doe"
Expand All @@ -373,6 +393,40 @@ func Benchmark_Ctx_Body(b *testing.B) {
require.Equal(b, []byte(input), c.Body())
}

// go test -v -run=^$ -bench=Benchmark_Ctx_BodyRaw -benchmem -count=4
func Benchmark_Ctx_BodyRaw(b *testing.B) {
const input = "john=doe"

app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed

c.Request().SetBodyRaw([]byte(input))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = c.BodyRaw()
}

require.Equal(b, []byte(input), c.BodyRaw())
}

// go test -v -run=^$ -bench=Benchmark_Ctx_BodyRaw_Immutable -benchmem -count=4
func Benchmark_Ctx_BodyRaw_Immutable(b *testing.B) {
const input = "john=doe"

app := New(Config{Immutable: true})
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed

c.Request().SetBodyRaw([]byte(input))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = c.BodyRaw()
}

require.Equal(b, []byte(input), c.BodyRaw())
}

// go test -run Test_Ctx_Body_Immutable
func Test_Ctx_Body_Immutable(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -813,7 +867,7 @@ func Test_Ctx_UserContext(t *testing.T) {
t.Parallel()
testKey := struct{}{}
testValue := "Test Value"
ctx := context.WithValue(context.Background(), testKey, testValue)
ctx := context.WithValue(context.Background(), testKey, testValue) //nolint: staticcheck // not needed for tests
require.Equal(t, testValue, ctx.Value(testKey))
})
}
Expand All @@ -826,7 +880,7 @@ func Test_Ctx_SetUserContext(t *testing.T) {

testKey := struct{}{}
testValue := "Test Value"
ctx := context.WithValue(context.Background(), testKey, testValue)
ctx := context.WithValue(context.Background(), testKey, testValue) //nolint: staticcheck // not needed for tests
c.SetUserContext(ctx)
require.Equal(t, testValue, c.UserContext().Value(testKey))
}
Expand All @@ -846,7 +900,7 @@ func Test_Ctx_UserContext_Multiple_Requests(t *testing.T) {
}

input := utils.CopyString(Query(c, "input", "NO_VALUE"))
ctx = context.WithValue(ctx, testKey, fmt.Sprintf("%s_%s", testValue, input))
ctx = context.WithValue(ctx, testKey, fmt.Sprintf("%s_%s", testValue, input)) //nolint: staticcheck // not needed for tests
c.SetUserContext(ctx)

return c.Status(StatusOK).SendString(fmt.Sprintf("resp_%s_returned", input))
Expand Down
2 changes: 1 addition & 1 deletion docs/extra/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ app.Use(func(c fiber.Ctx) error {

## How can i use live reload ?

[Air](https://github.com/cosmtrek/air) is a handy tool that automatically restarts your Go applications whenever the source code changes, making your development process faster and more efficient.
[Air](https://github.com/air-verse/air) is a handy tool that automatically restarts your Go applications whenever the source code changes, making your development process faster and more efficient.

To use Air in a Fiber project, follow these steps:

Expand Down
2 changes: 1 addition & 1 deletion docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ These docs are for **Fiber v3**, which was released on **March XX, 2024**.

### Installation

First of all, [download](https://go.dev/dl/) and install Go. `1.21` or higher is required.
First of all, [download](https://go.dev/dl/) and install Go. `1.22` or higher is required.

Installation is done using the [`go get`](https://pkg.go.dev/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command:

Expand Down
2 changes: 1 addition & 1 deletion docs/whats_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Here's a quick overview of the changes in Fiber `v3`:

## Drop for old Go versions

Fiber `v3` drops support for Go versions below `1.21`. We recommend upgrading to Go `1.21` or higher to use Fiber `v3`.
Fiber `v3` drops support for Go versions below `1.22`. We recommend upgrading to Go `1.22` or higher to use Fiber `v3`.

## 🚀 App

Expand Down
Loading

0 comments on commit 0fbb447

Please sign in to comment.