Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
865d5767
Commit
865d5767
authored
Feb 01, 2011
by
Jose Luis Vázquez González
Committed by
Russ Cox
Feb 01, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
http: add host patterns
R=bradfitzgo, rsc CC=golang-dev
https://golang.org/cl/4070043
parent
9557103e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
21 deletions
+94
-21
src/pkg/http/serve_test.go
src/pkg/http/serve_test.go
+65
-0
src/pkg/http/server.go
src/pkg/http/server.go
+29
-21
No files found.
src/pkg/http/serve_test.go
View file @
865d5767
...
...
@@ -136,6 +136,71 @@ func TestConsumingBodyOnNextConn(t *testing.T) {
}
}
type
stringHandler
string
func
(
s
stringHandler
)
ServeHTTP
(
w
ResponseWriter
,
r
*
Request
)
{
w
.
SetHeader
(
"Result"
,
string
(
s
))
}
var
handlers
=
[]
struct
{
pattern
string
msg
string
}{
{
"/"
,
"Default"
},
{
"/someDir/"
,
"someDir"
},
{
"someHost.com/someDir/"
,
"someHost.com/someDir"
},
}
var
vtests
=
[]
struct
{
url
string
expected
string
}{
{
"http://localhost/someDir/apage"
,
"someDir"
},
{
"http://localhost/otherDir/apage"
,
"Default"
},
{
"http://someHost.com/someDir/apage"
,
"someHost.com/someDir"
},
{
"http://otherHost.com/someDir/apage"
,
"someDir"
},
{
"http://otherHost.com/aDir/apage"
,
"Default"
},
}
func
TestHostHandlers
(
t
*
testing
.
T
)
{
for
_
,
h
:=
range
handlers
{
Handle
(
h
.
pattern
,
stringHandler
(
h
.
msg
))
}
l
,
err
:=
net
.
Listen
(
"tcp"
,
"127.0.0.1:0"
)
// any port
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
l
.
Close
()
go
Serve
(
l
,
nil
)
conn
,
err
:=
net
.
Dial
(
"tcp"
,
""
,
l
.
Addr
()
.
String
())
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
conn
.
Close
()
cc
:=
NewClientConn
(
conn
,
nil
)
for
_
,
vt
:=
range
vtests
{
var
r
*
Response
var
req
Request
if
req
.
URL
,
err
=
ParseURL
(
vt
.
url
);
err
!=
nil
{
t
.
Errorf
(
"cannot parse url: %v"
,
err
)
continue
}
if
err
:=
cc
.
Write
(
&
req
);
err
!=
nil
{
t
.
Errorf
(
"writing request: %v"
,
err
)
continue
}
r
,
err
:=
cc
.
Read
()
if
err
!=
nil
{
t
.
Errorf
(
"reading response: %v"
,
err
)
continue
}
s
:=
r
.
Header
[
"Result"
]
if
s
!=
vt
.
expected
{
t
.
Errorf
(
"Get(%q) = %q, want %q"
,
vt
.
url
,
s
,
vt
.
expected
)
}
}
}
type
responseWriterMethodCall
struct
{
method
string
headerKey
,
headerValue
string
// if method == "SetHeader"
...
...
src/pkg/http/server.go
View file @
865d5767
...
...
@@ -539,9 +539,8 @@ func RedirectHandler(url string, code int) Handler {
// patterns and calls the handler for the pattern that
// most closely matches the URL.
//
// Patterns named fixed paths, like "/favicon.ico",
// or subtrees, like "/images/" (note the trailing slash).
// Patterns must begin with /.
// Patterns named fixed, rooted paths, like "/favicon.ico",
// or rooted subtrees, like "/images/" (note the trailing slash).
// Longer patterns take precedence over shorter ones, so that
// if there are handlers registered for both "/images/"
// and "/images/thumbnails/", the latter handler will be
...
...
@@ -549,11 +548,11 @@ func RedirectHandler(url string, code int) Handler {
// former will receiver requests for any other paths in the
// "/images/" subtree.
//
//
In the future, the pattern syntax may be relaxed to allow
//
an optional host-name at the beginning of the pattern,
// so that a handler might register for the two patterns
// "/codesearch" and "codesearch.google.com/"
//
without taking over requests for http://www.google.com/
.
//
Patterns may optionally begin with a host name, restricting matches to
//
URLs on that host only. Host-specific patterns take precedence over
//
general patterns,
so that a handler might register for the two patterns
// "/codesearch" and "codesearch.google.com/"
without also taking over
//
requests for "http://www.google.com/"
.
//
// ServeMux also takes care of sanitizing the URL request path,
// redirecting any request containing . or .. elements to an
...
...
@@ -598,21 +597,13 @@ func cleanPath(p string) string {
return
np
}
// ServeHTTP dispatches the request to the handler whose
// pattern most closely matches the request URL.
func
(
mux
*
ServeMux
)
ServeHTTP
(
w
ResponseWriter
,
r
*
Request
)
{
// Clean path to canonical form and redirect.
if
p
:=
cleanPath
(
r
.
URL
.
Path
);
p
!=
r
.
URL
.
Path
{
w
.
SetHeader
(
"Location"
,
p
)
w
.
WriteHeader
(
StatusMovedPermanently
)
return
}
// Most-specific (longest) pattern wins.
// Find a handler on a handler map given a path string
// Most-specific (longest) pattern wins
func
(
mux
*
ServeMux
)
match
(
path
string
)
Handler
{
var
h
Handler
var
n
=
0
for
k
,
v
:=
range
mux
.
m
{
if
!
pathMatch
(
k
,
r
.
URL
.
P
ath
)
{
if
!
pathMatch
(
k
,
p
ath
)
{
continue
}
if
h
==
nil
||
len
(
k
)
>
n
{
...
...
@@ -620,6 +611,23 @@ func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
h
=
v
}
}
return
h
}
// ServeHTTP dispatches the request to the handler whose
// pattern most closely matches the request URL.
func
(
mux
*
ServeMux
)
ServeHTTP
(
w
ResponseWriter
,
r
*
Request
)
{
// Clean path to canonical form and redirect.
if
p
:=
cleanPath
(
r
.
URL
.
Path
);
p
!=
r
.
URL
.
Path
{
w
.
SetHeader
(
"Location"
,
p
)
w
.
WriteHeader
(
StatusMovedPermanently
)
return
}
// Host-specific pattern takes precedence over generic ones
h
:=
mux
.
match
(
r
.
Host
+
r
.
URL
.
Path
)
if
h
==
nil
{
h
=
mux
.
match
(
r
.
URL
.
Path
)
}
if
h
==
nil
{
h
=
NotFoundHandler
()
}
...
...
@@ -628,7 +636,7 @@ func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
// Handle registers the handler for the given pattern.
func
(
mux
*
ServeMux
)
Handle
(
pattern
string
,
handler
Handler
)
{
if
pattern
==
""
||
pattern
[
0
]
!=
'/'
{
if
pattern
==
""
{
panic
(
"http: invalid pattern "
+
pattern
)
}
...
...
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