Skip to content

Commit 55cb3b6

Browse files
vishrclaude
authored andcommitted
Optimize realm quoting to happen once during middleware creation
Move strconv.Quote(config.Realm) from per-request execution to middleware initialization for better performance. - Pre-compute quoted realm at middleware creation time - Avoids repeated string operations on every auth failure - Maintains same behavior with better efficiency Performance improvement suggested during code review. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent dbd583f commit 55cb3b6

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

middleware/basic_auth.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
6666
config.Realm = defaultRealm
6767
}
6868

69+
// Pre-compute the quoted realm for WWW-Authenticate header (RFC 7617)
70+
quotedRealm := strconv.Quote(config.Realm)
71+
6972
return func(next echo.HandlerFunc) echo.HandlerFunc {
7073
return func(c echo.Context) error {
7174
if config.Skipper(c) {
@@ -98,7 +101,7 @@ func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
98101

99102
// Need to return `401` for browsers to pop-up login box.
100103
// Realm is case-insensitive, so we can use "basic" directly. See RFC 7617.
101-
c.Response().Header().Set(echo.HeaderWWWAuthenticate, basic+" realm="+strconv.Quote(config.Realm))
104+
c.Response().Header().Set(echo.HeaderWWWAuthenticate, basic+" realm="+quotedRealm)
102105
return echo.ErrUnauthorized
103106
}
104107
}

0 commit comments

Comments
 (0)