Skip to content

Conversation

charlie0129
Copy link

@charlie0129 charlie0129 commented Jul 21, 2025

Add an option TrailingSlashInsensitivity in engine to allow trailing slash insensitive matching, e.g., if a handler for /path is defined, /path and /path/ will go the the same handler without redirection responses.

See the tests for details.

TrailingSlashInsensitivity is disabled by default so existing behavior should not be affected.

fixes #915

go test -benchmem -run="^$" -bench "^BenchmarkRouteTrailingSlashInsensitivity$" github.com/gin-gonic/gin
goos: linux
goarch: amd64
pkg: github.com/gin-gonic/gin
cpu: Intel(R) Xeon(R) Platinum 8352V CPU @ 2.10GHz
BenchmarkRouteTrailingSlashInsensitivity/Insensitive-144         	  116824	     10373 ns/op	    6238 B/op	      18 allocs/op
BenchmarkRouteTrailingSlashInsensitivity/Exact-144               	  116791	     10574 ns/op	    6237 B/op	      18 allocs/op
BenchmarkRouteTrailingSlashInsensitivity/Redirect-144            	   83282	     13627 ns/op	    6613 B/op	      27 allocs/op
PASS
ok  	github.com/gin-gonic/gin	3.987s
goos: darwin
goarch: arm64
pkg: github.com/gin-gonic/gin
cpu: Apple M2
BenchmarkRouteTrailingSlashInsensitivity/Insensitive-8         	  845089	      1404 ns/op	    6150 B/op	      18 allocs/op
BenchmarkRouteTrailingSlashInsensitivity/Exact-8               	  857839	      1461 ns/op	    6150 B/op	      18 allocs/op
BenchmarkRouteTrailingSlashInsensitivity/Redirect-8            	  674316	      1739 ns/op	    6474 B/op	      27 allocs/op
PASS
ok  	github.com/gin-gonic/gin	4.532

Copy link

codecov bot commented Jul 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.92%. Comparing base (3dc1cd6) to head (c3b1ea6).
Report is 148 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4306      +/-   ##
==========================================
- Coverage   99.21%   98.92%   -0.29%     
==========================================
  Files          42       44       +2     
  Lines        3182     3452     +270     
==========================================
+ Hits         3157     3415     +258     
- Misses         17       26       +9     
- Partials        8       11       +3     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.86% <100.00%> (?)
-tags go_json 98.86% <100.00%> (?)
-tags nomsgpack 98.91% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.23 98.92% <100.00%> (?)
go-1.24 98.92% <100.00%> (?)
macos-latest 98.92% <100.00%> (-0.29%) ⬇️
ubuntu-latest 98.92% <100.00%> (-0.29%) ⬇️

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

@ddevsr ddevsr left a comment

Choose a reason for hiding this comment

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

	if length := len(p); length > 1 && p[length-1] == '/' {
		return p[:length-1];
	}
$ go test -run=^$ -bench='TrimWith' -benchmem
goos: windows
goarch: amd64
pkg: github.com/gin-gonic/gin
cpu: Intel(R) Core(TM) i5-3570 CPU @ 3.40GHz
BenchmarkTrimWithHasSuffix-4            100000000               11.13 ns/op            0 B/op          0 allocs/op
BenchmarkTrimWithManualCheck-4          1000000000               0.2761 ns/op          0 B/op          0 allocs/op
PASS
ok      github.com/PHPDevsr/gin-forked  1.586s

Keep this logic, faster than using strings.HasSuffix().

@charlie0129
Copy link
Author

@ddevsr Hi, I kept the original logic

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.

Support for optional trailing slash
2 participants