Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
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
nexedi
caddy
Commits
5989eb06
Commit
5989eb06
authored
Mar 28, 2016
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #699 from eiszfuchs/socket-url
proxy: fix req.URL.Path for unix sockets
parents
32dbbfd6
cbd9b814
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
132 additions
and
3 deletions
+132
-3
middleware/proxy/proxy_test.go
middleware/proxy/proxy_test.go
+120
-3
middleware/proxy/reverseproxy.go
middleware/proxy/reverseproxy.go
+12
-0
No files found.
middleware/proxy/proxy_test.go
View file @
5989eb06
...
...
@@ -238,6 +238,116 @@ func TestUnixSocketProxy(t *testing.T) {
}
}
func
GetHTTPProxy
(
messageFormat
string
,
prefix
string
)
(
*
Proxy
,
*
httptest
.
Server
)
{
ts
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Fprintf
(
w
,
messageFormat
,
r
.
URL
.
String
())
}))
return
newPrefixedWebSocketTestProxy
(
ts
.
URL
,
prefix
),
ts
}
func
GetSocketProxy
(
messageFormat
string
,
prefix
string
)
(
*
Proxy
,
*
httptest
.
Server
,
error
)
{
ts
:=
httptest
.
NewUnstartedServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Fprintf
(
w
,
messageFormat
,
r
.
URL
.
String
())
}))
socketPath
,
err
:=
filepath
.
Abs
(
"./test_socket"
)
if
err
!=
nil
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Unable to get absolute path: %v"
,
err
)
}
ln
,
err
:=
net
.
Listen
(
"unix"
,
socketPath
)
if
err
!=
nil
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Unable to listen: %v"
,
err
)
}
ts
.
Listener
=
ln
ts
.
Start
()
tsURL
:=
strings
.
Replace
(
ts
.
URL
,
"http://"
,
"unix:"
,
1
)
return
newPrefixedWebSocketTestProxy
(
tsURL
,
prefix
),
ts
,
nil
}
func
GetTestServerMessage
(
p
*
Proxy
,
ts
*
httptest
.
Server
,
path
string
)
(
string
,
error
)
{
echoProxy
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
p
.
ServeHTTP
(
w
,
r
)
}))
// *httptest.Server is passed so it can be `defer`red properly
defer
ts
.
Close
()
defer
echoProxy
.
Close
()
res
,
err
:=
http
.
Get
(
echoProxy
.
URL
+
path
)
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"Unable to GET: %v"
,
err
)
}
greeting
,
err
:=
ioutil
.
ReadAll
(
res
.
Body
)
res
.
Body
.
Close
()
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"Unable to read body: %v"
,
err
)
}
return
fmt
.
Sprintf
(
"%s"
,
greeting
),
nil
}
func
TestUnixSocketProxyPaths
(
t
*
testing
.
T
)
{
greeting
:=
"Hello route %s"
tests
:=
[]
struct
{
url
string
prefix
string
expected
string
}{
{
""
,
""
,
fmt
.
Sprintf
(
greeting
,
"/"
)},
{
"/hello"
,
""
,
fmt
.
Sprintf
(
greeting
,
"/hello"
)},
{
"/foo/bar"
,
""
,
fmt
.
Sprintf
(
greeting
,
"/foo/bar"
)},
{
"/foo?bar"
,
""
,
fmt
.
Sprintf
(
greeting
,
"/foo?bar"
)},
{
"/greet?name=john"
,
""
,
fmt
.
Sprintf
(
greeting
,
"/greet?name=john"
)},
{
"/world?wonderful&colorful"
,
""
,
fmt
.
Sprintf
(
greeting
,
"/world?wonderful&colorful"
)},
{
"/proxy/hello"
,
"/proxy"
,
fmt
.
Sprintf
(
greeting
,
"/hello"
)},
{
"/proxy/foo/bar"
,
"/proxy"
,
fmt
.
Sprintf
(
greeting
,
"/foo/bar"
)},
{
"/proxy/?foo=bar"
,
"/proxy"
,
fmt
.
Sprintf
(
greeting
,
"/?foo=bar"
)},
}
for
_
,
test
:=
range
tests
{
p
,
ts
:=
GetHTTPProxy
(
greeting
,
test
.
prefix
)
actualMsg
,
err
:=
GetTestServerMessage
(
p
,
ts
,
test
.
url
)
if
err
!=
nil
{
t
.
Fatalf
(
"Getting server message failed - %v"
,
err
)
}
if
actualMsg
!=
test
.
expected
{
t
.
Errorf
(
"Expected '%s' but got '%s' instead"
,
test
.
expected
,
actualMsg
)
}
}
if
runtime
.
GOOS
==
"windows"
{
return
}
for
_
,
test
:=
range
tests
{
p
,
ts
,
err
:=
GetSocketProxy
(
greeting
,
test
.
prefix
)
if
err
!=
nil
{
t
.
Fatalf
(
"Getting socket proxy failed - %v"
,
err
)
}
actualMsg
,
err
:=
GetTestServerMessage
(
p
,
ts
,
test
.
url
)
if
err
!=
nil
{
t
.
Fatalf
(
"Getting server message failed - %v"
,
err
)
}
if
actualMsg
!=
test
.
expected
{
t
.
Errorf
(
"Expected '%s' but got '%s' instead"
,
test
.
expected
,
actualMsg
)
}
}
}
func
newFakeUpstream
(
name
string
,
insecure
bool
)
*
fakeUpstream
{
uri
,
_
:=
url
.
Parse
(
name
)
u
:=
&
fakeUpstream
{
...
...
@@ -276,12 +386,19 @@ func (u *fakeUpstream) AllowedPath(requestPath string) bool {
// proxy.
func
newWebSocketTestProxy
(
backendAddr
string
)
*
Proxy
{
return
&
Proxy
{
Upstreams
:
[]
Upstream
{
&
fakeWsUpstream
{
name
:
backendAddr
}},
Upstreams
:
[]
Upstream
{
&
fakeWsUpstream
{
name
:
backendAddr
,
without
:
""
}},
}
}
func
newPrefixedWebSocketTestProxy
(
backendAddr
string
,
prefix
string
)
*
Proxy
{
return
&
Proxy
{
Upstreams
:
[]
Upstream
{
&
fakeWsUpstream
{
name
:
backendAddr
,
without
:
prefix
}},
}
}
type
fakeWsUpstream
struct
{
name
string
name
string
without
string
}
func
(
u
*
fakeWsUpstream
)
From
()
string
{
...
...
@@ -292,7 +409,7 @@ func (u *fakeWsUpstream) Select() *UpstreamHost {
uri
,
_
:=
url
.
Parse
(
u
.
name
)
return
&
UpstreamHost
{
Name
:
u
.
name
,
ReverseProxy
:
NewSingleHostReverseProxy
(
uri
,
""
),
ReverseProxy
:
NewSingleHostReverseProxy
(
uri
,
u
.
without
),
ExtraHeaders
:
http
.
Header
{
"Connection"
:
{
"{>Connection}"
},
"Upgrade"
:
{
"{>Upgrade}"
}},
...
...
middleware/proxy/reverseproxy.go
View file @
5989eb06
...
...
@@ -95,6 +95,18 @@ func NewSingleHostReverseProxy(target *url.URL, without string) *ReverseProxy {
}
else
{
req
.
URL
.
RawQuery
=
targetQuery
+
"&"
+
req
.
URL
.
RawQuery
}
// Trims the path of the socket from the URL path.
// This is done because req.URL passed to your proxied service
// will have the full path of the socket file prefixed to it.
// Calling /test on a server that proxies requests to
// unix:/var/run/www.socket will thus set the requested path
// to /var/run/www.socket/test, rendering paths useless.
if
target
.
Scheme
==
"unix"
{
// See comment on socketDial for the trim
socketPrefix
:=
target
.
String
()[
len
(
"unix://"
)
:
]
req
.
URL
.
Path
=
strings
.
TrimPrefix
(
req
.
URL
.
Path
,
socketPrefix
)
}
// We are then safe to remove the `without` prefix.
if
without
!=
""
{
req
.
URL
.
Path
=
strings
.
TrimPrefix
(
req
.
URL
.
Path
,
without
)
}
...
...
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