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

Basic proxy feature works

This commit is contained in:
Matthew Holt 2015-02-02 23:41:35 -07:00
parent 5ae1790e52
commit 0e43271cc9

View file

@ -4,7 +4,8 @@ package proxy
import ( import (
"log" "log"
"net/http" "net/http"
"strings" "net/http/httputil"
"net/url"
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
) )
@ -28,16 +29,22 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
for _, rule := range rules { for _, rule := range rules {
if middleware.Path(r.URL.Path).Matches(rule.from) { if middleware.Path(r.URL.Path).Matches(rule.from) {
client := &http.Client{} var scheme string
if r.TLS == nil {
scheme = "http"
} else {
scheme = "https"
}
r.RequestURI = "" baseUrl, err := url.Parse(scheme + "://" + rule.to)
r.URL.Scheme = strings.ToLower(r.URL.Scheme)
resp, err := client.Do(r)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
resp.Write(w) r.Host = baseUrl.Host
// TODO: Construct this before; not during every request, if possible
proxy := httputil.NewSingleHostReverseProxy(baseUrl)
proxy.ServeHTTP(w, r)
} else { } else {
next(w, r) next(w, r)