Skip to content

Feature implementation from commits c6e0d80..43ad9f7#1

Open
codeOwlAI wants to merge 15 commits into
feature-base-branch-1from
feature-head-branch-1
Open

Feature implementation from commits c6e0d80..43ad9f7#1
codeOwlAI wants to merge 15 commits into
feature-base-branch-1from
feature-head-branch-1

Conversation

@codeOwlAI
Copy link
Copy Markdown
Owner

@codeOwlAI codeOwlAI commented Jun 30, 2025

PR Summary

Streamline Context API and Enhance HTTP Standards Compliance

Overview

This PR refactors custom context handling with a new simplified API (NewWithCustomCtx) while improving HTTP standards compliance across several components. It enhances cookie handling, authentication security, header parsing, and adds expanded test coverage.

Change Types

Type Description
Enhancement Simplified custom context API with NewWithCustomCtx
Enhancement Improved HTTP standards compliance (RFC 9111, RFC 3986)
Enhancement Better security in BasicAuth middleware
Enhancement More robust header parsing and content-type handling
Refactor Consolidated request handlers and simplified context logic
Test Expanded test coverage for edge cases and security features

Affected Modules

Module / File Change Description
app.go Added NewWithCustomCtx, simplified handlers, renamed internal functions
ctx.go/ctx_interface.go Improved standards compliance, refactored context handling
router.go Consolidated request handlers, removed unused imports
basicauth/ Added charset support and optional password storage
cache/ Improved directive handling with case-insensitivity
helpers.go Optimized cache checking, removed redundant methods
state.go Refactored getters with generic implementation
Various test files Expanded test coverage for edge cases

Notes for Reviewers

  • The custom context API has been significantly simplified with NewWithCustomCtx function replacing separate New() and NewCtxFunc() calls
  • HTTP header parsing (Range, MIME types) is now more robust and standards-compliant
  • BasicAuth middleware has enhanced security with optional password storage

gaby and others added 15 commits June 19, 2025 08:28
* Move binder tests into existing file

* Fix lint issues and race condition

* Add nolint directive
* test(cors): warn when all origins and func

* Fix linter issue
* Refine BasicAuth middleware

* Fix lint issues

* Update basicauth.md
test(csrf): cover cookie config warnings
* refactor: use utils.ToLower

* Improve Cache-Control directive matching

* Update middleware/cache/cache.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix lint issue

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* test: validate Is fails for JSON subtype

* Update ctx.go

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* Update ctx.go

* Use immutable-aware getter for Content-Type

* Update ctx.go

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: RW <rene@gofiber.io>
…0.0-beta.9 (gofiber#3540)

build(deps): bump github.com/gofiber/utils/v2

Bumps [github.com/gofiber/utils/v2](https://github.com/gofiber/utils) from 2.0.0-beta.8 to 2.0.0-beta.9.
- [Release notes](https://github.com/gofiber/utils/releases)
- [Commits](gofiber/utils@v2.0.0-beta.8...v2.0.0-beta.9)

---
updated-dependencies:
- dependency-name: github.com/gofiber/utils/v2
  dependency-version: 2.0.0-beta.9
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
* Make custom context setup via NewWithCustomCtx

* Update docs/api/app.md

* fix lint issues

* small optimizations

* Fix perf 3476 (gofiber#3499)

* perf: modify app.pool

goos: darwin
goarch: arm64
pkg: github.com/gofiber/fiber/v3
cpu: Apple M4
                       │   old.txt   │              new.txt               │
                       │   sec/op    │   sec/op     vs base               │
_Communication_Flow-10   36.26n ± 0%   34.51n ± 0%  -4.83% (p=0.000 n=30)

                       │  old.txt   │            new.txt             │
                       │    B/op    │    B/op     vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

                       │  old.txt   │            new.txt             │
                       │ allocs/op  │ allocs/op   vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

* perf: optimize defaultCtx in app.next

```
goos: darwin
goarch: arm64
pkg: github.com/gofiber/fiber/v3
cpu: Apple M4
                       │   new.txt   │              new4.txt               │
                       │   sec/op    │   sec/op     vs base                │
_Communication_Flow-10   34.51n ± 0%   30.02n ± 0%  -13.01% (p=0.000 n=30)

                       │  new.txt   │            new4.txt            │
                       │    B/op    │    B/op     vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

                       │  new.txt   │            new4.txt            │
                       │ allocs/op  │ allocs/op   vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal
```

---------

Co-authored-by: Kashiwa <13825170+ksw2000@users.noreply.github.com>

* Improve performance gofiber#3476 (gofiber#3532)

* Make custom context setup via NewWithCustomCtx

* Update docs/api/app.md

* fix lint issues

* small optimizations

* perf: modify app.pool

goos: darwin
goarch: arm64
pkg: github.com/gofiber/fiber/v3
cpu: Apple M4
                       │   old.txt   │              new.txt               │
                       │   sec/op    │   sec/op     vs base               │
_Communication_Flow-10   36.26n ± 0%   34.51n ± 0%  -4.83% (p=0.000 n=30)

                       │  old.txt   │            new.txt             │
                       │    B/op    │    B/op     vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

                       │  old.txt   │            new.txt             │
                       │ allocs/op  │ allocs/op   vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

* perf: optimize defaultCtx in app.next

```
goos: darwin
goarch: arm64
pkg: github.com/gofiber/fiber/v3
cpu: Apple M4
                       │   new.txt   │              new4.txt               │
                       │   sec/op    │   sec/op     vs base                │
_Communication_Flow-10   34.51n ± 0%   30.02n ± 0%  -13.01% (p=0.000 n=30)

                       │  new.txt   │            new4.txt            │
                       │    B/op    │    B/op     vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

                       │  new.txt   │            new4.txt            │
                       │ allocs/op  │ allocs/op   vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal
```

* perf: revert nextCustom

```
goos: darwin
goarch: arm64
pkg: github.com/gofiber/fiber/v3
cpu: Apple M4
                       │  head.txt   │            revert-1.txt            │
                       │   sec/op    │   sec/op     vs base               │
_Communication_Flow-10   30.04n ± 0%   28.74n ± 0%  -4.33% (p=0.000 n=30)

                       │  head.txt  │          revert-1.txt          │
                       │    B/op    │    B/op     vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

                       │  head.txt  │          revert-1.txt          │
                       │ allocs/op  │ allocs/op   vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal
```

---------

Co-authored-by: RW <rene@gofiber.io>

---------

Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
Co-authored-by: Kashiwa <13825170+ksw2000@users.noreply.github.com>
docs: add custom constraint example
* Use utils.ToLower in Subdomains

* test: cover Unicode fallback path

* fix subdomain extraction for rfc compliance

* Address review comments on Subdomains

* Address review comments
♻️ refact: use GetState to reduce duplicate code
Bumps [golang.org/x/net](https://github.com/golang/net) from 0.40.0 to 0.41.0.
- [Commits](golang/net@v0.40.0...v0.41.0)

---
updated-dependencies:
- dependency-name: golang.org/x/net
  dependency-version: 0.41.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
…lang.org/x/net-0.41.0

build(deps): bump golang.org/x/net from 0.40.0 to 0.41.0
* validate cookies with net/http

* refactor: sanitize cookie via net/http

* Address review comments

* refactor: set cookie attrs via setters

* Update ctx.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update ctx.go

* Update ctx.go

* Update ctx.go

* Add more unit-tests

* Fix test

* Add default case for samesite

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
refactor(ctx): use utils helpers for Range
Comment thread ctx.go
} else {
_, err = c.app.next(c)
}
_, err = c.app.next(c)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Breaking Change: Removed Custom Context Handler Support.

The change eliminates support for custom context functions, which can break applications relying on this feature for specialized routing behavior.

Current Code (Diff):

- 	_, err = c.app.next(c)
+ 	if c.app.newCtxFunc != nil {
+ 		_, err = c.app.nextCustom(c)
+ 	} else {
+ 		_, err = c.app.next(c)
+ 	}
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
_, err = c.app.next(c)
if c.app.newCtxFunc != nil {
_, err = c.app.nextCustom(c)
} else {
_, err = c.app.next(c)
}

Comment thread ctx_interface.go
// AcquireCtx retrieves a new Ctx from the pool.
func (app *App) AcquireCtx(fctx *fasthttp.RequestCtx) Ctx {
ctx, ok := app.pool.Get().(Ctx)
func (app *App) AcquireCtx(fctx *fasthttp.RequestCtx) CustomCtx {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Breaking API change: Return type modification.

Changing the return type of AcquireCtx from Ctx to CustomCtx will break existing code that expects the Ctx interface type.

Current Code (Diff):

+ func (app *App) AcquireCtx(fctx *fasthttp.RequestCtx) CustomCtx {
- func (app *App) AcquireCtx(fctx *fasthttp.RequestCtx) Ctx {
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
func (app *App) AcquireCtx(fctx *fasthttp.RequestCtx) CustomCtx {
func (app *App) AcquireCtx(fctx *fasthttp.RequestCtx) Ctx {

🔄 Dependencies Affected

app_test.go

Function: TestApp

Issue: Type assertion to (*DefaultCtx) will fail if CustomCtx is not compatible with DefaultCtx

Suggestion: Update type assertions or ensure CustomCtx is compatible with DefaultCtx

Proposed Code:

c, _ := app.AcquireCtx(fctx).(*DefaultCtx)
c, _ := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

Comment thread ctx_interface.go

// ReleaseCtx releases the ctx back into the pool.
func (app *App) ReleaseCtx(c Ctx) {
func (app *App) ReleaseCtx(c CustomCtx) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Breaking API change: Parameter type modification.

Changing the parameter type of ReleaseCtx from Ctx to CustomCtx will break existing code that passes Ctx interface instances.

Current Code (Diff):

+ func (app *App) ReleaseCtx(c CustomCtx) {
- func (app *App) ReleaseCtx(c Ctx) {
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
func (app *App) ReleaseCtx(c CustomCtx) {
func (app *App) ReleaseCtx(c Ctx) {

🔄 Dependencies Affected

app.go

Function: Unknown

Issue: Existing code calls ReleaseCtx with Ctx interface instances

Suggestion: Update all callers to use CustomCtx or keep the original parameter type

Proposed Code:

defer app.ReleaseCtx(c)

app_test.go

Function: Unknown

Issue: Multiple test functions call ReleaseCtx with Ctx interface instances

Suggestion: Update all test callers to use CustomCtx or keep the original parameter type

Proposed Code:

app.ReleaseCtx(c)

bind_test.go

Function: Unknown

Issue: Test code calls ReleaseCtx with Ctx interface instances

Suggestion: Update test code to use CustomCtx or keep the original parameter type

Proposed Code:

defer app.ReleaseCtx(c)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants