mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-13 22:51:08 -05:00
Refactored proxy middleware
This commit is contained in:
parent
113b175db7
commit
d8e7adcdb4
2 changed files with 64 additions and 50 deletions
|
@ -2,7 +2,6 @@
|
||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -11,60 +10,75 @@ import (
|
||||||
"github.com/mholt/caddy/middleware"
|
"github.com/mholt/caddy/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Proxy represents a middleware instance that can proxy requests.
|
||||||
|
type Proxy struct {
|
||||||
|
Next middleware.Handler
|
||||||
|
Rules []Rule
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP satisfies the middleware.Handler interface.
|
||||||
|
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
|
|
||||||
|
for _, rule := range p.Rules {
|
||||||
|
if middleware.Path(r.URL.Path).Matches(rule.From) {
|
||||||
|
var base string
|
||||||
|
|
||||||
|
if strings.HasPrefix(rule.To, "http") { // includes https
|
||||||
|
// destination includes a scheme! no need to guess
|
||||||
|
base = rule.To
|
||||||
|
} else {
|
||||||
|
// no scheme specified; assume same as request
|
||||||
|
var scheme string
|
||||||
|
if r.TLS == nil {
|
||||||
|
scheme = "http"
|
||||||
|
} else {
|
||||||
|
scheme = "https"
|
||||||
|
}
|
||||||
|
base = scheme + "://" + rule.To
|
||||||
|
}
|
||||||
|
|
||||||
|
baseUrl, err := url.Parse(base)
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusInternalServerError, err
|
||||||
|
}
|
||||||
|
r.Host = baseUrl.Host
|
||||||
|
|
||||||
|
// TODO: Construct this before; not during every request, if possible
|
||||||
|
proxy := httputil.NewSingleHostReverseProxy(baseUrl)
|
||||||
|
proxy.ServeHTTP(w, r)
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.Next.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
// New creates a new instance of proxy middleware.
|
// New creates a new instance of proxy middleware.
|
||||||
func New(c middleware.Controller) (middleware.Middleware, error) {
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
var rules []proxyRule
|
rules, err := parse(c)
|
||||||
|
if err != nil {
|
||||||
for c.Next() {
|
return nil, err
|
||||||
rule := proxyRule{}
|
|
||||||
|
|
||||||
if !c.Args(&rule.from, &rule.to) {
|
|
||||||
return nil, c.ArgErr()
|
|
||||||
}
|
|
||||||
|
|
||||||
rules = append(rules, rule)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(next middleware.Handler) middleware.Handler {
|
return func(next middleware.Handler) middleware.Handler {
|
||||||
return middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
return Proxy{Next: next, Rules: rules}
|
||||||
|
|
||||||
for _, rule := range rules {
|
|
||||||
if middleware.Path(r.URL.Path).Matches(rule.from) {
|
|
||||||
var base string
|
|
||||||
|
|
||||||
if strings.HasPrefix(rule.to, "http") { // includes https
|
|
||||||
// destination includes a scheme! no need to guess
|
|
||||||
base = rule.to
|
|
||||||
} else {
|
|
||||||
// no scheme specified; assume same as request
|
|
||||||
var scheme string
|
|
||||||
if r.TLS == nil {
|
|
||||||
scheme = "http"
|
|
||||||
} else {
|
|
||||||
scheme = "https"
|
|
||||||
}
|
|
||||||
base = scheme + "://" + rule.to
|
|
||||||
}
|
|
||||||
|
|
||||||
baseUrl, err := url.Parse(base)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
r.Host = baseUrl.Host
|
|
||||||
|
|
||||||
// TODO: Construct this before; not during every request, if possible
|
|
||||||
proxy := httputil.NewSingleHostReverseProxy(baseUrl)
|
|
||||||
proxy.ServeHTTP(w, r)
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return next.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type proxyRule struct {
|
func parse(c middleware.Controller) ([]Rule, error) {
|
||||||
from string
|
var rules []Rule
|
||||||
to string
|
|
||||||
|
for c.Next() {
|
||||||
|
var rule Rule
|
||||||
|
if !c.Args(&rule.From, &rule.To) {
|
||||||
|
return rules, c.ArgErr()
|
||||||
|
}
|
||||||
|
rules = append(rules, rule)
|
||||||
|
}
|
||||||
|
|
||||||
|
return rules, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Rule struct {
|
||||||
|
From, To string
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,7 +108,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
Path: path,
|
Path: path,
|
||||||
Command: cmd,
|
Command: cmd,
|
||||||
Arguments: args,
|
Arguments: args,
|
||||||
Respawn: respawn,
|
Respawn: respawn, // TODO: This isn't used currently
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue