]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: fix inserting of implicit redirects in serve mux
authorChristian Himpel <chressie@googlemail.com>
Fri, 31 Aug 2012 16:00:01 +0000 (12:00 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 31 Aug 2012 16:00:01 +0000 (12:00 -0400)
In serve mux, if pattern contains a host name, pass only the path to
the redirect handler.

Add tests for serve mux redirections.

R=rsc
CC=bradfitz, gobot, golang-dev
https://golang.org/cl/6329045

src/pkg/net/http/serve_test.go
src/pkg/net/http/server.go

index 17b7566b381dc3ec733d12f42d50155392f05701..c5cf6ae7115c3f52150435bc07b4d15df70011c8 100644 (file)
@@ -173,6 +173,9 @@ var vtests = []struct {
        {"http://someHost.com/someDir/apage", "someHost.com/someDir"},
        {"http://otherHost.com/someDir/apage", "someDir"},
        {"http://otherHost.com/aDir/apage", "Default"},
+       // redirections for trees
+       {"http://localhost/someDir", "/someDir/"},
+       {"http://someHost.com/someDir", "/someDir/"},
 }
 
 func TestHostHandlers(t *testing.T) {
@@ -204,9 +207,19 @@ func TestHostHandlers(t *testing.T) {
                        t.Errorf("reading response: %v", err)
                        continue
                }
-               s := r.Header.Get("Result")
-               if s != vt.expected {
-                       t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected)
+               switch r.StatusCode {
+               case StatusOK:
+                       s := r.Header.Get("Result")
+                       if s != vt.expected {
+                               t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected)
+                       }
+               case StatusMovedPermanently:
+                       s := r.Header.Get("Location")
+                       if s != vt.expected {
+                               t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected)
+                       }
+               default:
+                       t.Errorf("Get(%q) unhandled status code %d", vt.url, r.StatusCode)
                }
        }
 }
index 272c8408c78a7a731946652b858fc648458680b5..bac5faed1bedc728460a87c884c6a3efe48118fd 100644 (file)
@@ -997,7 +997,15 @@ func (mux *ServeMux) Handle(pattern string, handler Handler) {
        // It can be overridden by an explicit registration.
        n := len(pattern)
        if n > 0 && pattern[n-1] == '/' && !mux.m[pattern[0:n-1]].explicit {
-               mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(pattern, StatusMovedPermanently)}
+               // If pattern contains a host name, strip it and use remaining
+               // path for redirect.
+               path := pattern
+               if pattern[0] != '/' {
+                       // In pattern, at least the last character is a '/', so
+                       // strings.Index can't be -1.
+                       path = pattern[strings.Index(pattern, "/"):]
+               }
+               mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(path, StatusMovedPermanently)}
        }
 }