1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 13:05:56 +00:00

implement a rewrite middleware

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-19 22:40:17 +03:00
parent dddfeb9ca9
commit 12737c5b7f
9 changed files with 410 additions and 8 deletions

View File

@@ -145,7 +145,7 @@ func (su *Supervisor) newListener() (net.Listener, error) {
// restarts we may want for the server.
//
// User still be able to call .Serve instead.
// l, err := netutil.TCPKeepAlive(su.Server.Addr, su.SocketReuse)
// l, err := netutil.TCPKeepAlive(su.Server.Addr, su.SocketSharding)
l, err := netutil.TCP(su.Server.Addr, su.SocketSharding)
if err != nil {
return nil, err

View File

@@ -132,11 +132,11 @@ func (s *subdomainRedirectWrapper) Wrapper(w http.ResponseWriter, r *http.Reques
resturi := r.URL.RequestURI()
if s.isToRoot {
// from a specific subdomain or any subdomain to the root domain.
redirectAbsolute(w, r, context.GetScheme(r)+root+resturi, http.StatusMovedPermanently)
RedirectAbsolute(w, r, context.GetScheme(r)+root+resturi, http.StatusMovedPermanently)
return
}
// from a specific subdomain or any subdomain to a specific subdomain.
redirectAbsolute(w, r, context.GetScheme(r)+s.to+root+resturi, http.StatusMovedPermanently)
RedirectAbsolute(w, r, context.GetScheme(r)+s.to+root+resturi, http.StatusMovedPermanently)
return
}
@@ -162,7 +162,7 @@ func (s *subdomainRedirectWrapper) Wrapper(w http.ResponseWriter, r *http.Reques
resturi := r.URL.RequestURI()
// we are not inside a subdomain, so we are in the root domain
// and the redirect is configured to be used from root domain to a subdomain.
redirectAbsolute(w, r, context.GetScheme(r)+s.to+root+resturi, http.StatusMovedPermanently)
RedirectAbsolute(w, r, context.GetScheme(r)+s.to+root+resturi, http.StatusMovedPermanently)
return
}
@@ -196,11 +196,20 @@ func NewSubdomainRedirectHandler(toSubdomain string) context.Handler {
r.Host = targetHost
r.URL.Host = targetHost
urlToRedirect := r.URL.String()
redirectAbsolute(ctx.ResponseWriter(), r, urlToRedirect, http.StatusMovedPermanently)
RedirectAbsolute(ctx.ResponseWriter(), r, urlToRedirect, http.StatusMovedPermanently)
}
}
func redirectAbsolute(w http.ResponseWriter, r *http.Request, url string, code int) {
// RedirectAbsolute replies to the request with a redirect to an absolute URL.
//
// The provided code should be in the 3xx range and is usually
// StatusMovedPermanently, StatusFound or StatusSeeOther.
//
// If the Content-Type header has not been set, Redirect sets it
// to "text/html; charset=utf-8" and writes a small HTML body.
// Setting the Content-Type header to any value, including nil,
// disables that behavior.
func RedirectAbsolute(w http.ResponseWriter, r *http.Request, url string, code int) {
h := w.Header()
// RFC 7231 notes that a short HTML body is usually included in