1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-06 03:27:27 +00:00

add accesslog+proxy example

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-19 17:47:44 +03:00
parent fc2aada3c7
commit a04a6b5011
9 changed files with 199 additions and 28 deletions

View File

@@ -5,24 +5,12 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strings"
"path"
"time"
"github.com/kataras/iris/v12/core/netutil"
)
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
// ProxyHandler returns a new ReverseProxy that rewrites
// 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",
@@ -35,7 +23,9 @@ func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.Host = target.Host
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
req.URL.Path = path.Join(target.Path, req.URL.Path)
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
@@ -110,7 +100,7 @@ func RedirectHandler(target *url.URL, redirectStatus int) http.Handler {
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
redirectTo := singleJoiningSlash(targetURI, r.URL.Path)
redirectTo := path.Join(targetURI, r.URL.Path)
if len(r.URL.RawQuery) > 0 {
redirectTo += "?" + r.URL.RawQuery
}

View File

@@ -397,8 +397,12 @@ func StripPrefix(prefix string, h context.Handler) context.Handler {
canonicalPrefix = toWebPath(canonicalPrefix)
return func(ctx *context.Context) {
if p := strings.TrimPrefix(ctx.Request().URL.Path, canonicalPrefix); len(p) < len(ctx.Request().URL.Path) {
ctx.Request().URL.Path = p
u := ctx.Request().URL
if p := strings.TrimPrefix(u.Path, canonicalPrefix); len(p) < len(u.Path) {
if p == "" {
p = "/"
}
u.Path = p
h(ctx)
} else {
ctx.NotFound()