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
Łukasz Nowak
caddy
Commits
7949388d
Commit
7949388d
authored
Nov 16, 2015
by
Abiola Ibrahim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Proxy: Allow ignored subpaths.
parent
dd119e04
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
1 deletion
+55
-1
middleware/proxy/proxy.go
middleware/proxy/proxy.go
+3
-1
middleware/proxy/proxy_test.go
middleware/proxy/proxy_test.go
+4
-0
middleware/proxy/upstream.go
middleware/proxy/upstream.go
+18
-0
middleware/proxy/upstream_test.go
middleware/proxy/upstream_test.go
+30
-0
No files found.
middleware/proxy/proxy.go
View file @
7949388d
...
...
@@ -26,6 +26,8 @@ type Upstream interface {
From
()
string
// Selects an upstream host to be routed to.
Select
()
*
UpstreamHost
// Checks if subpath is not an ignored path
IsAllowedPath
(
string
)
bool
}
// UpstreamHostDownFunc can be used to customize how Down behaves.
...
...
@@ -59,7 +61,7 @@ func (uh *UpstreamHost) Down() bool {
func
(
p
Proxy
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
_
,
upstream
:=
range
p
.
Upstreams
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
upstream
.
From
())
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
upstream
.
From
())
&&
upstream
.
IsAllowedPath
(
r
.
URL
.
Path
)
{
var
replacer
middleware
.
Replacer
start
:=
time
.
Now
()
requestHost
:=
r
.
Host
...
...
middleware/proxy/proxy_test.go
View file @
7949388d
...
...
@@ -125,6 +125,10 @@ func (u *fakeUpstream) Select() *UpstreamHost {
}
}
func
(
u
*
fakeUpstream
)
IsAllowedPath
(
requestPath
string
)
bool
{
return
true
}
// recorderHijacker is a ResponseRecorder that can
// be hijacked.
type
recorderHijacker
struct
{
...
...
middleware/proxy/upstream.go
View file @
7949388d
...
...
@@ -5,11 +5,13 @@ import (
"io/ioutil"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"time"
"github.com/mholt/caddy/caddy/parse"
"github.com/mholt/caddy/middleware"
)
var
(
...
...
@@ -29,6 +31,7 @@ type staticUpstream struct {
Interval
time
.
Duration
}
WithoutPathPrefix
string
IgnoredSubPaths
[]
string
}
// NewStaticUpstreams parses the configuration input and sets up
...
...
@@ -165,6 +168,12 @@ func parseBlock(c *parse.Dispenser, u *staticUpstream) error {
return
c
.
ArgErr
()
}
u
.
WithoutPathPrefix
=
c
.
Val
()
case
"except"
:
ignoredPaths
:=
c
.
RemainingArgs
()
if
len
(
ignoredPaths
)
==
0
{
return
c
.
ArgErr
()
}
u
.
IgnoredSubPaths
=
ignoredPaths
default
:
return
c
.
Errf
(
"unknown property '%s'"
,
c
.
Val
())
}
...
...
@@ -223,3 +232,12 @@ func (u *staticUpstream) Select() *UpstreamHost {
}
return
u
.
Policy
.
Select
(
pool
)
}
func
(
u
*
staticUpstream
)
IsAllowedPath
(
requestPath
string
)
bool
{
for
_
,
ignoredSubPath
:=
range
u
.
IgnoredSubPaths
{
if
middleware
.
Path
(
path
.
Clean
(
requestPath
))
.
Matches
(
path
.
Join
(
u
.
From
(),
ignoredSubPath
))
{
return
false
}
}
return
true
}
middleware/proxy/upstream_test.go
View file @
7949388d
...
...
@@ -51,3 +51,33 @@ func TestRegisterPolicy(t *testing.T) {
}
}
func
TestAllowedPaths
(
t
*
testing
.
T
)
{
upstream
:=
&
staticUpstream
{
from
:
"/proxy"
,
IgnoredSubPaths
:
[]
string
{
"/download"
,
"/static"
},
}
tests
:=
[]
struct
{
url
string
expected
bool
}{
{
"/proxy"
,
true
},
{
"/proxy/dl"
,
true
},
{
"/proxy/download"
,
false
},
{
"/proxy/download/static"
,
false
},
{
"/proxy/static"
,
false
},
{
"/proxy/static/download"
,
false
},
{
"/proxy/something/download"
,
true
},
{
"/proxy/something/static"
,
true
},
{
"/proxy//static"
,
false
},
{
"/proxy//static//download"
,
false
},
{
"/proxy//download"
,
false
},
}
for
i
,
test
:=
range
tests
{
isAllowed
:=
upstream
.
IsAllowedPath
(
test
.
url
)
if
test
.
expected
!=
isAllowed
{
t
.
Errorf
(
"Test %d: expected %v found %v"
,
i
+
1
,
test
.
expected
,
isAllowed
)
}
}
}
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