1
0
mirror of https://github.com/labstack/echo.git synced 2025-12-03 22:59:09 +02:00

Proxy: Better errors + remote custom TLS (#1197)

Proxy will be more verbose on errors + possibility to configure custom transport (example: for custom TLS certificates)
This commit is contained in:
Gregor Noczinski
2018-10-13 08:10:19 +02:00
committed by Vishal Rana
parent fcdf096c2c
commit bc37a3a792
6 changed files with 97 additions and 7 deletions

View File

@@ -6,7 +6,6 @@ import (
"math/rand"
"net"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strings"
@@ -38,10 +37,14 @@ type (
// "/users/*/orders/*": "/user/$1/order/$2",
Rewrite map[string]string
// Context key to store selected ProxyTarget into context.
// Context key to store selected ProxyTarget into context.
// Optional. Default value "target".
ContextKey string
// To customize the transport to remote.
// Examples: If custom TLS certificates are required.
Transport http.RoundTripper
rewriteRegex map[*regexp.Regexp]string
}
@@ -85,10 +88,6 @@ var (
}
)
func proxyHTTP(t *ProxyTarget) http.Handler {
return httputil.NewSingleHostReverseProxy(t.URL)
}
func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
in, _, err := c.Response().Hijack()
@@ -250,7 +249,7 @@ func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc {
proxyRaw(tgt, c).ServeHTTP(res, req)
case req.Header.Get(echo.HeaderAccept) == "text/event-stream":
default:
proxyHTTP(tgt).ServeHTTP(res, req)
proxyHTTP(tgt, c, config).ServeHTTP(res, req)
}
return