Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
6c6c2320
Commit
6c6c2320
authored
Jun 27, 2011
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
http: add StripPrefix handler wrapper
R=rsc CC=golang-dev
https://golang.org/cl/4626067
parent
f79bcb4b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
7 deletions
+43
-7
fs.go
src/pkg/http/fs.go
+3
-7
serve_test.go
src/pkg/http/serve_test.go
+24
-0
server.go
src/pkg/http/server.go
+16
-0
No files found.
src/pkg/http/fs.go
View file @
6c6c2320
...
...
@@ -193,22 +193,18 @@ func ServeFile(w ResponseWriter, r *Request, name string) {
type
fileHandler
struct
{
root
string
prefix
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
)
}
...
...
src/pkg/http/serve_test.go
View file @
6c6c2320
...
...
@@ -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
}
...
...
src/pkg/http/server.go
View file @
6c6c2320
...
...
@@ -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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment