Skip to content

Commit 91b8c8a

Browse files
joeybloggsjoeybloggs
authored andcommitted
update redirect to reset path back to original to ensure logging middleware and alike records to correct original request URL
1 parent 67a1aeb commit 91b8c8a

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

lars.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,15 @@ func (l *LARS) serveHTTP(w http.ResponseWriter, r *http.Request) {
296296
if l.redirectTrailingSlash && len(r.URL.Path) > 1 {
297297

298298
// find again all lowercase
299-
lc := strings.ToLower(r.URL.Path)
299+
orig := r.URL.Path
300+
lc := strings.ToLower(orig)
300301

301302
if lc != r.URL.Path {
302303

303304
if c.handlers, _, _ = root.find(lc, c.params); c.handlers != nil {
304305
r.URL.Path = lc
305-
c.handlers = l.redirect(r.Method)
306+
c.handlers = l.redirect(r.Method, r.URL.String())
307+
r.URL.Path = orig
306308
goto END
307309
}
308310
}
@@ -315,7 +317,8 @@ func (l *LARS) serveHTTP(w http.ResponseWriter, r *http.Request) {
315317

316318
if c.handlers, _, _ = root.find(lc, c.params); c.handlers != nil {
317319
r.URL.Path = lc
318-
c.handlers = l.redirect(r.Method)
320+
c.handlers = l.redirect(r.Method, r.URL.String())
321+
r.URL.Path = orig
319322
goto END
320323
}
321324
}

util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (l *LARS) wrapHandlerWithName(h Handler) (chain HandlerFunc, handlerName st
113113
return
114114
}
115115

116-
func (l *LARS) redirect(method string) (handlers HandlersChain) {
116+
func (l *LARS) redirect(method string, to string) (handlers HandlersChain) {
117117

118118
code := http.StatusMovedPermanently
119119

@@ -123,7 +123,7 @@ func (l *LARS) redirect(method string) (handlers HandlersChain) {
123123

124124
fn := func(c Context) {
125125
inCtx := c.BaseContext()
126-
http.Redirect(inCtx.response, inCtx.request, inCtx.request.URL.String(), code)
126+
http.Redirect(inCtx.response, inCtx.request, to, code)
127127
}
128128

129129
hndlrs := make(HandlersChain, len(l.routeGroup.middleware)+1)

0 commit comments

Comments
 (0)