Skip to content

🔥 feat: Add request inspection helpers#3727

Merged
ReneWerner87 merged 8 commits intomainfrom
add-isnotfound-and-ismiddleware-functions
Sep 3, 2025
Merged

🔥 feat: Add request inspection helpers#3727
ReneWerner87 merged 8 commits intomainfrom
add-isnotfound-and-ismiddleware-functions

Conversation

@gaby
Copy link
Member

@gaby gaby commented Sep 2, 2025

Summary

  • add HasBody, IsWebSocket, IsPreflight, and Matched helpers on Ctx
  • document new helpers in the Ctx API guide

Fixes #3721

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 2, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Adds five boolean query methods to the Ctx API (Matched, IsMiddleware, HasBody, IsWebSocket, IsPreflight), updates the Ctx interface and DefaultCtx implementation, adds unit tests for those methods, and documents them in API and "what's new" docs.

Changes

Cohort / File(s) Summary
Core context implementation
ctx.go
Adds Matched(), IsMiddleware(), HasBody(), IsWebSocket(), IsPreflight() on DefaultCtx; imports strings for header parsing.
Ctx interface
ctx_interface_gen.go
Extends Ctx interface with Matched(), IsMiddleware(), HasBody(), IsWebSocket(), IsPreflight().
Tests
ctx_test.go
Adds tests: Test_Ctx_Matched_AfterNext, Test_Ctx_Matched_RouteError, Test_Ctx_IsMiddleware, Test_Ctx_HasBody, Test_Ctx_IsWebSocket, Test_Ctx_IsPreflight.
API docs
docs/api/ctx.md
Documents the five new Ctx methods (duplicate insertion present).
Release notes
docs/whats_new.md
Notes the five new context query methods under "Context > New Methods".

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant App
  participant Middleware as Middleware Handler
  participant Router
  participant Ctx

  Client->>App: HTTP request
  App->>Middleware: invoke middleware(c)
  Middleware->>Ctx: call c.Matched()/c.IsMiddleware()/c.HasBody()/c.IsWebSocket()/c.IsPreflight()
  Middleware->>Router: c.Next()
  Router-->>Middleware: route handler (found) / no handler
  Middleware->>App: return (may use inspected flags)
  App-->>Client: response
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Assessment against linked issues

Objective Addressed Explanation
Provide a way to check if a handler/route was found within middleware (#3721)

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
Add HasBody() (ctx.go) New predicate not required by #3721; unrelated to detecting matched handler.
Add IsWebSocket() (ctx.go) WebSocket upgrade detection is unrelated to the middleware matched-handler question.
Add IsPreflight() (ctx.go) CORS preflight detection is unrelated to the middleware matched-handler question.

Possibly related PRs

Suggested labels

📒 Documentation

Suggested reviewers

  • sixcolors
  • efectn
  • ReneWerner87

Poem

A hop and a check along the route I tread,
Matched or middleware — I sniff and spread.
WebSocket waves and preflight sigh,
Body found? I give a twitch and cry.
Rabbit-approved, the context is read. 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 8f9dbda and dd1f5aa.

📒 Files selected for processing (1)
  • ctx.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • ctx.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: unit (1.25.x, macos-13)
  • GitHub Check: unit (1.25.x, windows-latest)
  • GitHub Check: Compare
  • GitHub Check: repeated
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch add-isnotfound-and-ismiddleware-functions

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@gaby gaby added v3 and removed 📝 Proposal labels Sep 2, 2025
@gaby gaby added this to v3 Sep 2, 2025
@gaby gaby added this to the v3 milestone Sep 2, 2025
@gaby gaby moved this to In Progress in v3 Sep 2, 2025
@codecov
Copy link

codecov bot commented Sep 2, 2025

Codecov Report

❌ Patch coverage is 91.66667% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.02%. Comparing base (e878cd8) to head (dd1f5aa).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
ctx.go 91.66% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3727      +/-   ##
==========================================
- Coverage   92.08%   92.02%   -0.07%     
==========================================
  Files         115      115              
  Lines       11535    11585      +50     
==========================================
+ Hits        10622    10661      +39     
- Misses        662      670       +8     
- Partials      251      254       +3     
Flag Coverage Δ
unittests 92.02% <91.66%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: dd6a8f7 Previous: e83a762 Ratio
Benchmark_Compress_Levels_Parallel/Brotli_LevelBestCompression - B/op 1 B/op 0 B/op +∞

This comment was automatically generated by workflow using github-action-benchmark.

@rugleb
Copy link

rugleb commented Sep 2, 2025

Nice PR for me.

Suggestions from me:

  • Rename IsFound -> Matched, because "found" means also "resource" (by id) not found.
  • Drop IsNotFound, because don't really need.

@gaby
Copy link
Member Author

gaby commented Sep 2, 2025

Nice PR for me.

Suggestions from me:

  • Rename IsFound -> Matched, because "found" means also "resource" (by id) not found.
  • Drop IsNotFound, because don't really need.

Makes sense, will update!

@gaby gaby marked this pull request as ready for review September 2, 2025 22:30
Copilot AI review requested due to automatic review settings September 2, 2025 22:30
@gaby gaby requested a review from a team as a code owner September 2, 2025 22:30
@gaby gaby changed the title feat(ctx): add request inspection helpers 🔥 feat: Add request inspection helpers Sep 2, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds five new inspection helper methods to the Fiber Ctx interface to simplify common request analysis patterns. The helpers detect route matching status, middleware execution, request body presence, WebSocket upgrades, and CORS preflight requests.

  • Adds Matched(), IsMiddleware(), HasBody(), IsWebSocket(), and IsPreflight() methods to the Ctx interface
  • Provides comprehensive test coverage for all new helper methods
  • Documents the new methods in the API guide with usage examples

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
ctx.go Implements the five new helper methods with proper HTTP header inspection logic
ctx_interface_gen.go Adds method signatures to the Ctx interface
ctx_test.go Comprehensive test suite covering all scenarios for the new helpers
docs/api/ctx.md Documentation with signatures and usage examples
docs/whats_new.md Brief changelog entries for the new features

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (14)
docs/whats_new.md (1)

491-496: Polish phrasing of new Ctx helpers (and align with code semantics).

Minor grammar/style tweaks and clarify IsMiddleware intent. Also addresses the linter hint.

- - **Matched**: Detects when the current request path matched a registered route.
- - **IsMiddleware**: Indicates if the current handler was registered as middleware.
- - **HasBody**: Quickly checks whether the request includes a body.
- - **IsWebSocket**: Reports if the request attempts a WebSocket upgrade.
- - **IsPreflight**: Identifies CORS preflight requests before handlers run.
+ - **Matched**: Returns true when the current request path was matched by the router.
+ - **IsMiddleware**: Returns true if the current handler comes from a middleware route (registered via `app.Use`).
+ - **HasBody**: Returns true if the request includes a body.
+ - **IsWebSocket**: Returns true if the request attempts a WebSocket upgrade.
+ - **IsPreflight**: Returns true for CORS preflight requests.
ctx.go (4)

309-313: IsMiddleware(): clarify semantics vs route-level middleware.

This reports true only for routes registered via App.Use (c.route.use). Route-level middleware attached to a non-use route will return false, which is fine if that’s the intended contract. Consider reflecting that nuance in the function comment (and docs) to avoid confusion.

Apply the same docstring refinement suggested in ctx_interface_gen.go.


314-321: HasBody(): acceptable heuristic.

Content-Length > 0 OR non-empty body covers common cases. If you want to avoid touching the body buffer when CL is unknown (chunked), you could first check Transfer-Encoding, but this is optional.


322-330: IsWebSocket(): solid header checks; minor micro-optimization optional.

Logic matches the typical handshake. Optional: avoid an allocation from ToLowerBytes by scanning for 'u'/'U' and comparing case-insensitively, but readability likely wins here.


331-335: IsPreflight(): consider requiring Origin header to avoid false positives.

Per CORS, preflights are OPTIONS requests with both Origin and Access-Control-Request-Method. Tightening the check reduces accidental matches from generic OPTIONS requests.

-func (c *DefaultCtx) IsPreflight() bool {
-	return c.Method() == MethodOptions && len(c.fasthttp.Request.Header.Peek(HeaderAccessControlRequestMethod)) > 0
-}
+func (c *DefaultCtx) IsPreflight() bool {
+	if c.Method() != MethodOptions {
+		return false
+	}
+	hdr := &c.fasthttp.Request.Header
+	if len(hdr.Peek(HeaderAccessControlRequestMethod)) == 0 {
+		return false
+	}
+	// Guard on Origin to align with CORS preflight semantics
+	return len(hdr.Peek(HeaderOrigin)) > 0
+}
ctx_test.go (5)

4931-4955: Matched(): add an after-Next assertion to exercise both matched and 404 paths

Exercise Matched() from middleware after c.Next() to confirm true on matched routes and false on 404s.

   app.Get("/two", func(c Ctx) error {
     return c.SendStatus(StatusOK)
   })

+  // Assert Matched() after the downstream chain has executed
+  app.Use(func(c Ctx) error {
+    err := c.Next()
+    if c.Response().StatusCode() == StatusNotFound {
+      require.False(t, c.Matched(), "Matched() should be false on 404")
+    } else {
+      require.True(t, c.Matched(), "Matched() should be true when a route matched")
+    }
+    return err
+  })
+
   app.Use(func(c Ctx) error {
     require.False(t, c.Matched())
     return c.Status(StatusNotFound).SendString("not found")
   })

4957-4973: Nit: assert error handler body

Also assert the body content to fully specify behavior.

   resp, err := app.Test(httptest.NewRequest(MethodGet, "/", nil))
   require.NoError(t, err)
   require.Equal(t, StatusNotFound, resp.StatusCode)
+  b, _ := io.ReadAll(resp.Body)
+  require.Equal(t, "Not Found", string(b))

5017-5037: HasBody: add Content-Length: 0 case

Guard against regressions where zero length is treated as present.

   require.True(t, ctxWithHeader.HasBody())

+  ctxZero := app.AcquireCtx(&fasthttp.RequestCtx{})
+  require.NotNil(t, ctxZero)
+  t.Cleanup(func() { app.ReleaseCtx(ctxZero) })
+  ctxZero.Request().Header.SetContentLength(0)
+  require.False(t, ctxZero.HasBody())
+
   ctxWithoutBody := app.AcquireCtx(&fasthttp.RequestCtx{})

5039-5054: IsWebSocket: add case-insensitivity and multi-token Connection

Increase robustness of the check.

   require.True(t, ws.IsWebSocket())

+  ws2 := app.AcquireCtx(&fasthttp.RequestCtx{})
+  require.NotNil(t, ws2)
+  t.Cleanup(func() { app.ReleaseCtx(ws2) })
+  ws2.Request().Header.Set(HeaderConnection, "keep-alive, Upgrade")
+  ws2.Request().Header.Set(HeaderUpgrade, "WebSocket")
+  require.True(t, ws2.IsWebSocket())
+
   non := app.AcquireCtx(&fasthttp.RequestCtx{})

5056-5074: IsPreflight: add non-OPTIONS guard

Preflight must be OPTIONS even if ACRM is present.

   require.True(t, pre.IsPreflight())

+  notOpt := &fasthttp.RequestCtx{}
+  notOpt.Request.Header.SetMethod(MethodGet)
+  notOpt.Request.Header.Set(HeaderAccessControlRequestMethod, MethodGet)
+  pre2 := app.AcquireCtx(notOpt)
+  require.NotNil(t, pre2)
+  t.Cleanup(func() { app.ReleaseCtx(pre2) })
+  require.False(t, pre2.IsPreflight())
+
   optCtx := &fasthttp.RequestCtx{}
docs/api/ctx.md (4)

387-403: Clarify when to use Matched() vs Route()

Explicitly call out safe usage timing relative to Next().

 ### Matched

-Returns `true` if the current request path was matched by the router.
+Returns `true` if the current request path was matched by the router.
+Note: Unlike c.Route(), Matched() can be called in middleware before c.Next().

419-435: HasBody: document zero-length behavior

Tiny clarification improves predictability.

 ### HasBody

-Returns `true` if the incoming request contains a body or a `Content-Length` header greater than zero.
+Returns `true` if the incoming request contains a body or a `Content-Length` header greater than zero. Returns `false` when `Content-Length` is `0`.

444-451: IsWebSocket example: avoid calling Next() from a terminal handler

Use a simple OK to keep the example minimal and accurate.

 app.Get("/", func(c fiber.Ctx) error {
   if c.IsWebSocket() {
     // handle websocket
   }
-  return c.Next()
+  return c.Status(fiber.StatusOK).SendString("OK")
 })

453-469: IsPreflight: emphasize both conditions

Minor wording tweak for clarity.

-Returns `true` if the request is a CORS preflight (`OPTIONS` + `Access-Control-Request-Method`).
+Returns `true` only if the request is a CORS preflight (`OPTIONS` method and an `Access-Control-Request-Method` header).
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between e83a762 and d04c271.

📒 Files selected for processing (5)
  • ctx.go (2 hunks)
  • ctx_interface_gen.go (1 hunks)
  • ctx_test.go (1 hunks)
  • docs/api/ctx.md (1 hunks)
  • docs/whats_new.md (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*.go: Format Go code using gofumpt (enforced via make format)
Ensure code passes golangci-lint checks (enforced via make lint)
Optimize struct field alignment using betteralign (enforced via make betteralign)
Modernize Go code using gopls modernize (enforced via make modernize)

Files:

  • ctx_interface_gen.go
  • ctx.go
  • ctx_test.go
docs/**

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Review and update the contents of the docs folder if necessary when modifying code

Files:

  • docs/whats_new.md
  • docs/api/ctx.md
🧠 Learnings (2)
📚 Learning: 2024-11-10T23:44:13.704Z
Learnt from: gaby
PR: gofiber/fiber#3193
File: middleware/adaptor/adaptor.go:111-111
Timestamp: 2024-11-10T23:44:13.704Z
Learning: In the `middleware/adaptor/adaptor.go` file of the Fiber framework, when updating context handling, replacing `c.Context()` with `c.RequestCtx()` is appropriate to access the `fasthttp.RequestCtx`.

Applied to files:

  • ctx_interface_gen.go
  • ctx.go
  • docs/whats_new.md
  • docs/api/ctx.md
📚 Learning: 2024-11-08T04:10:42.990Z
Learnt from: gaby
PR: gofiber/fiber#3193
File: middleware/cache/cache_test.go:897-897
Timestamp: 2024-11-08T04:10:42.990Z
Learning: In the Fiber framework, `Context()` is being renamed to `RequestCtx()`, and `UserContext()` to `Context()` to improve clarity and align with Go's context conventions.

Applied to files:

  • ctx.go
🧬 Code graph analysis (2)
ctx.go (1)
constants.go (4)
  • HeaderConnection (188-188)
  • HeaderUpgrade (278-278)
  • MethodOptions (12-12)
  • HeaderAccessControlRequestMethod (205-205)
ctx_test.go (3)
app.go (3)
  • New (515-628)
  • Config (113-418)
  • Error (62-65)
ctx_interface_gen.go (1)
  • Ctx (17-424)
constants.go (8)
  • StatusOK (52-52)
  • StatusNotFound (77-77)
  • MethodGet (5-5)
  • ErrNotFound (122-122)
  • HeaderConnection (188-188)
  • HeaderUpgrade (278-278)
  • MethodOptions (12-12)
  • HeaderAccessControlRequestMethod (205-205)
🪛 LanguageTool
docs/whats_new.md

[grammar] ~491-~491: There might be a mistake here.
Context: ...request path matched a registered route. - IsMiddleware: Indicates if the current...

(QB_NEW_EN)

🔇 Additional comments (5)
ctx_interface_gen.go (1)

111-120: Public Ctx surface LGTM; tighten docstrings for clarity.

Tighten the docs as follows:

-	// IsMiddleware returns true if the current request handler was registered as middleware.
+	// IsMiddleware returns true if the current handler comes from a middleware route (registered via App.Use).
-	// IsWebSocket returns true if the request includes a WebSocket upgrade handshake.
+	// IsWebSocket returns true if Connection contains "upgrade" and Upgrade equals "websocket" (case-insensitive).

The IsPreflight doc is already sufficiently clear. No stale IsFound/IsNotFound references remain.

ctx.go (2)

8-8: bytes import – OK.

Required for EqualFold/Contains usage in IsWebSocket.


304-307: Matched(): simple and correct.

ctx_test.go (2)

4975-4996: LGTM: baseline coverage for Matched()

Covers positive path and fallback middleware path.


4998-5015: LGTM: IsMiddleware semantics validated

Asserts true in Use and false in route handler.

@gaby
Copy link
Member Author

gaby commented Sep 2, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces several useful request inspection helpers to the Ctx object, namely HasBody, IsWebSocket, IsPreflight, and Matched, complete with documentation and tests. The implementation is solid, but I've identified a couple of areas for improvement. The HasBody function can be made more concise. More importantly, the IsWebSocket function has a potential correctness issue in how it parses the Connection header, which could result in false positives. I've provided suggestions to address these points.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
ctx.go (1)

321-327: Simplify HasBody() to a single return.

Minor readability win; same behavior.

Apply this diff:

 func (c *DefaultCtx) HasBody() bool {
-	if c.fasthttp.Request.Header.ContentLength() > 0 {
-		return true
-	}
-	return len(c.fasthttp.Request.Body()) > 0
+	return c.fasthttp.Request.Header.ContentLength() > 0 ||
+		len(c.fasthttp.Request.Body()) > 0
 }
🧹 Nitpick comments (1)
ctx.go (1)

345-355: IsPreflight(): logic is correct; optional micro-tweak.

Consider hoisting the header pointer here as well (matches IsWebSocket style) for tiny clarity/consistency.

Apply this diff:

 func (c *DefaultCtx) IsPreflight() bool {
 	if c.Method() != MethodOptions {
 		return false
 	}
-	hdr := &c.fasthttp.Request.Header
+	hdr := &c.fasthttp.Request.Header
 	if len(hdr.Peek(HeaderAccessControlRequestMethod)) == 0 {
 		return false
 	}
 	return len(hdr.Peek(HeaderOrigin)) > 0
 }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between d04c271 and d1c6112.

📒 Files selected for processing (3)
  • ctx.go (2 hunks)
  • ctx_test.go (1 hunks)
  • docs/api/ctx.md (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • ctx_test.go
  • docs/api/ctx.md
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*.go: Format Go code using gofumpt (enforced via make format)
Ensure code passes golangci-lint checks (enforced via make lint)
Optimize struct field alignment using betteralign (enforced via make betteralign)
Modernize Go code using gopls modernize (enforced via make modernize)

Files:

  • ctx.go
🧠 Learnings (1)
📚 Learning: 2024-11-10T23:44:13.704Z
Learnt from: gaby
PR: gofiber/fiber#3193
File: middleware/adaptor/adaptor.go:111-111
Timestamp: 2024-11-10T23:44:13.704Z
Learning: In the `middleware/adaptor/adaptor.go` file of the Fiber framework, when updating context handling, replacing `c.Context()` with `c.RequestCtx()` is appropriate to access the `fasthttp.RequestCtx`.

Applied to files:

  • ctx.go
🧬 Code graph analysis (1)
ctx.go (1)
constants.go (5)
  • HeaderConnection (188-188)
  • HeaderUpgrade (278-278)
  • MethodOptions (12-12)
  • HeaderAccessControlRequestMethod (205-205)
  • HeaderOrigin (206-206)
🪛 GitHub Check: modernize
ctx.go

[failure] 352-352:
Ranging over SplitSeq is more efficient

🪛 GitHub Actions: Modernize Lint
ctx.go

[error] 352-352: Command failed: go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test=false ./...: /home/runner/work/fiber/fiber/ctx.go:352:20: Ranging over SplitSeq is more efficient

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: unit (1.25.x, macos-13)
  • GitHub Check: unit (1.25.x, ubuntu-latest)
  • GitHub Check: unit (1.25.x, windows-latest)
  • GitHub Check: repeated
  • GitHub Check: unit (1.25.x, macos-latest)
  • GitHub Check: Compare
  • GitHub Check: lint
  • GitHub Check: govulncheck-check
🔇 Additional comments (4)
ctx.go (4)

8-8: Import addition is appropriate.

bytes is required for the tokenized header parsing below.


304-307: Matched(): simple, correct exposure of router outcome.

Returns the internal flag; reset path and router set/update already manage it. LGTM.


309-319: IsMiddleware() semantics—double‑check edge cases.

Logic looks sane: true for app/group .Use(...) routes, and for early handlers in a multi-handler chain. Please verify it returns false for the last handler in a multi-handler route and true for earlier ones (two test cases).


329-343: Use bytes.SplitSeq and hoist Header lookup for efficiency

Replace the allocation-heavy split and repeated header lookups:

 func (c *DefaultCtx) IsWebSocket() bool {
-   conn := c.fasthttp.Request.Header.Peek(HeaderConnection)
+   hdr := &c.fasthttp.Request.Header
+   conn := hdr.Peek(HeaderConnection)
    var isUpgrade bool
-   for _, v := range bytes.Split(conn, []byte{','}) {
+   for v := range bytes.SplitSeq(conn, []byte{','}) {
        if bytes.EqualFold(bytes.TrimSpace(v), []byte("upgrade")) {
            isUpgrade = true
            break
        }
    }
    if !isUpgrade {
        return false
    }
-   return bytes.EqualFold(c.fasthttp.Request.Header.Peek(HeaderUpgrade), []byte("websocket"))
+   return bytes.EqualFold(hdr.Peek(HeaderUpgrade), []byte("websocket"))
 }

Ensure the project’s minimum Go version is 1.24 or newer before applying.

@gaby
Copy link
Member Author

gaby commented Sep 3, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces several helpful request inspection methods to the Ctx object: Matched, IsMiddleware, HasBody, IsWebSocket, and IsPreflight. The changes are well-implemented, include thorough tests, and are properly documented. My feedback is minor and focuses on a small code simplification for better readability and maintainability.

@gaby gaby removed the 📝 Proposal label Sep 3, 2025
@ReneWerner87 ReneWerner87 merged commit f1b7f6d into main Sep 3, 2025
18 of 20 checks passed
@ReneWerner87 ReneWerner87 deleted the add-isnotfound-and-ismiddleware-functions branch September 3, 2025 10:52
@github-project-automation github-project-automation bot moved this from In Progress to Done in v3 Sep 3, 2025
Abhirup-99 pushed a commit to Abhirup-99/fiber that referenced this pull request Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

🤗 [Question]: How to check if a handler has been found inside middleware or not

4 participants