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

fix tests and change naming

This commit is contained in:
Viacheslav Biriukov 2015-06-04 13:57:39 +00:00
parent 4790dacbf7
commit 593557659c
4 changed files with 22 additions and 16 deletions

View file

@ -34,15 +34,15 @@ type UpstreamHostDownFunc func(*UpstreamHost) bool
// UpstreamHost represents a single proxy upstream
type UpstreamHost struct {
// The hostname of this upstream host
Name string
ReverseProxy *ReverseProxy
Conns int64
Fails int32
FailTimeout time.Duration
Unhealthy bool
ExtraHeaders http.Header
CheckDown UpstreamHostDownFunc
Without string
Name string
ReverseProxy *ReverseProxy
Conns int64
Fails int32
FailTimeout time.Duration
Unhealthy bool
ExtraHeaders http.Header
CheckDown UpstreamHostDownFunc
WithoutPathPrefix string
}
// Down checks whether the upstream host is down or not.
@ -78,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, host.Without)
proxy = NewSingleHostReverseProxy(baseURL, host.WithoutPathPrefix)
}
} else if proxy == nil {
return http.StatusInternalServerError, err

View file

@ -120,7 +120,7 @@ func (u *fakeUpstream) Select() *UpstreamHost {
uri, _ := url.Parse(u.name)
return &UpstreamHost{
Name: u.name,
ReverseProxy: NewSingleHostReverseProxy(uri),
ReverseProxy: NewSingleHostReverseProxy(uri, ""),
ExtraHeaders: proxyHeaders,
}
}
@ -149,3 +149,9 @@ func (c *fakeConn) SetWriteDeadline(t time.Time) error { return nil }
func (c *fakeConn) Close() error { return nil }
func (c *fakeConn) Read(b []byte) (int, error) { return c.readBuf.Read(b) }
func (c *fakeConn) Write(b []byte) (int, error) { return c.writeBuf.Write(b) }
func newWithoutPathPrefixTestProxy(backendAddr string) *Proxy {
return &Proxy{
Upstreams: []Upstream{&fakeUpstreamWithoutPathPrefix{from: "/api/messages", withoutPathPrefix: "/api"}},
}
}

View file

@ -76,7 +76,7 @@ func NewSingleHostReverseProxy(target *url.URL, without string) *ReverseProxy {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if without != "" {
req.URL.Path = strings.Replace(req.URL.Path, without, "", 1)
req.URL.Path = strings.TrimPrefix(req.URL.Path, without)
}
}
return &ReverseProxy{Director: director}

View file

@ -28,7 +28,7 @@ type staticUpstream struct {
Path string
Interval time.Duration
}
Without string
WithoutPathPrefix string
}
// NewStaticUpstreams parses the configuration input and sets up
@ -108,7 +108,7 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
if !c.NextArg() {
return upstreams, c.ArgErr()
}
upstream.Without = c.Val()
upstream.WithoutPathPrefix = c.Val()
}
}
@ -136,10 +136,10 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
return false
}
}(upstream),
Without: upstream.Without,
WithoutPathPrefix: upstream.WithoutPathPrefix,
}
if baseURL, err := url.Parse(uh.Name); err == nil {
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.Without)
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.WithoutPathPrefix)
} else {
return upstreams, err
}