]> Cypherpunks repositories - gostls13.git/commitdiff
http: add StripPrefix handler wrapper
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 27 Jun 2011 18:03:43 +0000 (11:03 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 27 Jun 2011 18:03:43 +0000 (11:03 -0700)
R=rsc
CC=golang-dev
https://golang.org/cl/4626067

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

index 56512980c8d114bec861a42654d39dec357d428b..866abe6a4bdb50393b0e4d78f45670a8553f9984 100644 (file)
@@ -192,23 +192,19 @@ func ServeFile(w ResponseWriter, r *Request, name string) {
 }
 
 type fileHandler struct {
-       root   string
-       prefix string
+       root string
 }
 
 // FileServer returns a handler that serves HTTP requests
 // with the contents of the file system rooted at root.
 // It strips prefix from the incoming requests before
 // looking up the file name in the file system.
-func FileServer(root, prefix string) Handler { return &fileHandler{root, prefix} }
+func FileServer(root, prefix string) Handler {
+       return StripPrefix(prefix, &fileHandler{root})
+}
 
 func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
        path := r.URL.Path
-       if !strings.HasPrefix(path, f.prefix) {
-               NotFound(w, r)
-               return
-       }
-       path = path[len(f.prefix):]
        serveFile(w, r, filepath.Join(f.root, filepath.FromSlash(path)), true)
 }
 
index 207646f9a00d5368a5d5a310178247e4bfbd6b1e..a6a566a9c3e78448db83d8153dce653a7318163a 100644 (file)
@@ -798,6 +798,30 @@ func TestNoDate(t *testing.T) {
        }
 }
 
+func TestStripPrefix(t *testing.T) {
+       h := HandlerFunc(func(w ResponseWriter, r *Request) {
+               w.Header().Set("X-Path", r.URL.Path)
+       })
+       ts := httptest.NewServer(StripPrefix("/foo", h))
+       defer ts.Close()
+
+       res, err := Get(ts.URL + "/foo/bar")
+       if err != nil {
+               t.Fatal(err)
+       }
+       if g, e := res.Header.Get("X-Path"), "/bar"; g != e {
+               t.Errorf("test 1: got %s, want %s", g, e)
+       }
+
+       res, err = Get(ts.URL + "/bar")
+       if err != nil {
+               t.Fatal(err)
+       }
+       if g, e := res.StatusCode, 404; g != e {
+               t.Errorf("test 2: got status %v, want %v", g, e)
+       }
+}
+
 type errorListener struct {
        errs []os.Error
 }
index 03b9cd86f6b04782764964b91e7fe1d819c700e9..1e06c24af356ed3e6369d78f2ca625db3fde508a 100644 (file)
@@ -592,6 +592,22 @@ func NotFound(w ResponseWriter, r *Request) { Error(w, "404 page not found", Sta
 // that replies to each request with a ``404 page not found'' reply.
 func NotFoundHandler() Handler { return HandlerFunc(NotFound) }
 
+// StripPrefix returns a handler that serves HTTP requests
+// by removing the given prefix from the request URL's Path
+// and invoking the handler h. StripPrefix handles a
+// request for a path that doesn't begin with prefix by
+// replying with an HTTP 404 not found error.
+func StripPrefix(prefix string, h Handler) Handler {
+       return HandlerFunc(func(w ResponseWriter, r *Request) {
+               if !strings.HasPrefix(r.URL.Path, prefix) {
+                       NotFound(w, r)
+                       return
+               }
+               r.URL.Path = r.URL.Path[len(prefix):]
+               h.ServeHTTP(w, r)
+       })
+}
+
 // Redirect replies to the request with a redirect to url,
 // which may be a path relative to the request path.
 func Redirect(w ResponseWriter, r *Request, url string, code int) {