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
e50de809
Commit
e50de809
authored
Feb 16, 2017
by
Matt Holt
Committed by
GitHub
Feb 16, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1378 from tw4452852/1362
proxy: handle encoded path in URL
parents
91ff7343
c37481cc
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
11 deletions
+62
-11
caddyhttp/proxy/proxy.go
caddyhttp/proxy/proxy.go
+0
-5
caddyhttp/proxy/proxy_test.go
caddyhttp/proxy/proxy_test.go
+20
-0
caddyhttp/proxy/reverseproxy.go
caddyhttp/proxy/reverseproxy.go
+42
-6
No files found.
caddyhttp/proxy/proxy.go
View file @
e50de809
...
@@ -273,11 +273,6 @@ func createUpstreamRequest(r *http.Request) *http.Request {
...
@@ -273,11 +273,6 @@ func createUpstreamRequest(r *http.Request) *http.Request {
outreq
.
Body
=
nil
outreq
.
Body
=
nil
}
}
// Restore URL Path if it has been modified
if
outreq
.
URL
.
RawPath
!=
""
{
outreq
.
URL
.
Opaque
=
outreq
.
URL
.
RawPath
}
// We are modifying the same underlying map from req (shallow
// We are modifying the same underlying map from req (shallow
// copied above) so we only copy it if necessary.
// copied above) so we only copy it if necessary.
copiedHeaders
:=
false
copiedHeaders
:=
false
...
...
caddyhttp/proxy/proxy_test.go
View file @
e50de809
...
@@ -949,6 +949,26 @@ func TestProxyDirectorURL(t *testing.T) {
...
@@ -949,6 +949,26 @@ func TestProxyDirectorURL(t *testing.T) {
expectURL
:
`https://localhost:2021/t/mypath`
,
expectURL
:
`https://localhost:2021/t/mypath`
,
without
:
"/test"
,
without
:
"/test"
,
},
},
{
requestURL
:
`http://localhost:2020/%2C`
,
targetURL
:
`https://localhost:2021/t/`
,
expectURL
:
`https://localhost:2021/t/%2C`
,
},
{
requestURL
:
`http://localhost:2020/%2C/`
,
targetURL
:
`https://localhost:2021/t/`
,
expectURL
:
`https://localhost:2021/t/%2C/`
,
},
{
requestURL
:
`http://localhost:2020/test`
,
targetURL
:
`https://localhost:2021/%2C`
,
expectURL
:
`https://localhost:2021/%2C/test`
,
},
{
requestURL
:
`http://localhost:2020/%2C`
,
targetURL
:
`https://localhost:2021/%2C`
,
expectURL
:
`https://localhost:2021/%2C/%2C`
,
},
}
{
}
{
targetURL
,
err
:=
url
.
Parse
(
c
.
targetURL
)
targetURL
,
err
:=
url
.
Parse
(
c
.
targetURL
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
caddyhttp/proxy/reverseproxy.go
View file @
e50de809
...
@@ -18,7 +18,6 @@ import (
...
@@ -18,7 +18,6 @@ import (
"net"
"net"
"net/http"
"net/http"
"net/url"
"net/url"
"path"
"strings"
"strings"
"sync"
"sync"
"time"
"time"
...
@@ -89,6 +88,18 @@ func socketDial(hostName string) func(network, addr string) (conn net.Conn, err
...
@@ -89,6 +88,18 @@ func socketDial(hostName string) func(network, addr string) (conn net.Conn, err
}
}
}
}
func
singleJoiningSlash
(
a
,
b
string
)
string
{
aslash
:=
strings
.
HasSuffix
(
a
,
"/"
)
bslash
:=
strings
.
HasPrefix
(
b
,
"/"
)
switch
{
case
aslash
&&
bslash
:
return
a
+
b
[
1
:
]
case
!
aslash
&&
!
bslash
&&
b
!=
""
:
return
a
+
"/"
+
b
}
return
a
+
b
}
// NewSingleHostReverseProxy returns a new ReverseProxy that rewrites
// NewSingleHostReverseProxy returns a new ReverseProxy that rewrites
// URLs to the scheme, host, and base path provided in target. If the
// URLs to the scheme, host, and base path provided in target. If the
// target's path is "/base" and the incoming request was for "/dir",
// target's path is "/base" and the incoming request was for "/dir",
...
@@ -119,12 +130,31 @@ func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int) *
...
@@ -119,12 +130,31 @@ func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int) *
}
}
}
}
hadTrailingSlash
:=
strings
.
HasSuffix
(
req
.
URL
.
Path
,
"/"
)
// prefer returns val if it isn't empty, otherwise def
req
.
URL
.
Path
=
path
.
Join
(
target
.
Path
,
req
.
URL
.
Path
)
prefer
:=
func
(
val
,
def
string
)
string
{
// path.Join will strip off the last /, so put it back if it was there.
if
val
!=
""
{
if
hadTrailingSlash
&&
!
strings
.
HasSuffix
(
req
.
URL
.
Path
,
"/"
)
{
return
val
req
.
URL
.
Path
=
req
.
URL
.
Path
+
"/"
}
}
return
def
}
// Make up the final URL by concatenating the request and target URL.
//
// If there is encoded part in request or target URL,
// the final URL should also be in encoded format.
// Here, we concatenate their encoded parts which are stored
// in URL.Opaque and URL.RawPath, if it is empty use
// URL.Path instead.
if
req
.
URL
.
Opaque
!=
""
||
target
.
Opaque
!=
""
{
req
.
URL
.
Opaque
=
singleJoiningSlash
(
prefer
(
target
.
Opaque
,
target
.
Path
),
prefer
(
req
.
URL
.
Opaque
,
req
.
URL
.
Path
))
}
if
req
.
URL
.
RawPath
!=
""
||
target
.
RawPath
!=
""
{
req
.
URL
.
RawPath
=
singleJoiningSlash
(
prefer
(
target
.
RawPath
,
target
.
Path
),
prefer
(
req
.
URL
.
RawPath
,
req
.
URL
.
Path
))
}
req
.
URL
.
Path
=
singleJoiningSlash
(
target
.
Path
,
req
.
URL
.
Path
)
// Trims the path of the socket from the URL path.
// Trims the path of the socket from the URL path.
// This is done because req.URL passed to your proxied service
// This is done because req.URL passed to your proxied service
...
@@ -136,6 +166,12 @@ func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int) *
...
@@ -136,6 +166,12 @@ func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int) *
// See comment on socketDial for the trim
// See comment on socketDial for the trim
socketPrefix
:=
target
.
String
()[
len
(
"unix://"
)
:
]
socketPrefix
:=
target
.
String
()[
len
(
"unix://"
)
:
]
req
.
URL
.
Path
=
strings
.
TrimPrefix
(
req
.
URL
.
Path
,
socketPrefix
)
req
.
URL
.
Path
=
strings
.
TrimPrefix
(
req
.
URL
.
Path
,
socketPrefix
)
if
req
.
URL
.
Opaque
!=
""
{
req
.
URL
.
Opaque
=
strings
.
TrimPrefix
(
req
.
URL
.
Opaque
,
socketPrefix
)
}
if
req
.
URL
.
RawPath
!=
""
{
req
.
URL
.
RawPath
=
strings
.
TrimPrefix
(
req
.
URL
.
RawPath
,
socketPrefix
)
}
}
}
if
targetQuery
==
""
||
req
.
URL
.
RawQuery
==
""
{
if
targetQuery
==
""
||
req
.
URL
.
RawQuery
==
""
{
...
...
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