0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

add without to proxy middleware

This commit is contained in:
Viacheslav Biriukov 2015-06-03 18:06:24 +00:00
parent 4852f0580b
commit 4790dacbf7
3 changed files with 16 additions and 4 deletions

View file

@ -42,6 +42,7 @@ type UpstreamHost struct {
Unhealthy bool
ExtraHeaders http.Header
CheckDown UpstreamHostDownFunc
Without string
}
// Down checks whether the upstream host is down or not.
@ -77,7 +78,7 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if baseURL, err := url.Parse(host.Name); err == nil {
r.Host = baseURL.Host
if proxy == nil {
proxy = NewSingleHostReverseProxy(baseURL)
proxy = NewSingleHostReverseProxy(baseURL, host.Without)
}
} else if proxy == nil {
return http.StatusInternalServerError, err

View file

@ -62,7 +62,9 @@ func singleJoiningSlash(a, b string) string {
// 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",
// the target request will be for /base/dir.
func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
// Without logic: target's path is "/", incoming is "/api/messages",
// without is "/api", then the target request will be for /messages.
func NewSingleHostReverseProxy(target *url.URL, without string) *ReverseProxy {
targetQuery := target.RawQuery
director := func(req *http.Request) {
req.URL.Scheme = target.Scheme
@ -73,6 +75,9 @@ func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if without != "" {
req.URL.Path = strings.Replace(req.URL.Path, without, "", 1)
}
}
return &ReverseProxy{Director: director}
}

View file

@ -28,13 +28,13 @@ type staticUpstream struct {
Path string
Interval time.Duration
}
Without string
}
// NewStaticUpstreams parses the configuration input and sets up
// static upstreams for the proxy middleware.
func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
var upstreams []Upstream
for c.Next() {
upstream := &staticUpstream{
from: "",
@ -104,6 +104,11 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
case "websocket":
proxyHeaders.Add("Connection", "{>Connection}")
proxyHeaders.Add("Upgrade", "{>Upgrade}")
case "without":
if !c.NextArg() {
return upstreams, c.ArgErr()
}
upstream.Without = c.Val()
}
}
@ -131,9 +136,10 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
return false
}
}(upstream),
Without: upstream.Without,
}
if baseURL, err := url.Parse(uh.Name); err == nil {
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL)
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.Without)
} else {
return upstreams, err
}