mirror of
https://github.com/kataras/iris.git
synced 2025-12-25 22:07:03 +00:00
Happy weekend! Due the latest news we have a single change for your own safety. iris.AutoTLS users should pass all the necessary information now, these are the recommended by letsencrypt.
Iris devs should declare all the information now, there is no option to "leave something out" anymore, it's for your own good. Version is not changed yet, giving you time to see that changelog and do the necessary changes to your codebase. Happy weekend! Former-commit-id: 490ce14a1022a2b81d347d7f59c2bb5412cfcdf2
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/core/netutil"
|
||||
)
|
||||
@@ -59,13 +60,13 @@ func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
|
||||
}
|
||||
|
||||
// NewProxy returns a new host (server supervisor) which
|
||||
// redirects all requests to the target.
|
||||
// proxies all requests to the target.
|
||||
// It uses the httputil.NewSingleHostReverseProxy.
|
||||
//
|
||||
// Usage:
|
||||
// target, _ := url.Parse("https://mydomain.com")
|
||||
// proxy := NewProxy("mydomain.com:80", target)
|
||||
// proxy.ListenAndServe() // use of proxy.Shutdown to close the proxy server.
|
||||
// proxy.ListenAndServe() // use of `proxy.Shutdown` to close the proxy server.
|
||||
func NewProxy(hostAddr string, target *url.URL) *Supervisor {
|
||||
proxyHandler := ProxyHandler(target)
|
||||
proxy := New(&http.Server{
|
||||
@@ -75,3 +76,38 @@ func NewProxy(hostAddr string, target *url.URL) *Supervisor {
|
||||
|
||||
return proxy
|
||||
}
|
||||
|
||||
// NewRedirection returns a new host (server supervisor) which
|
||||
// redirects all requests to the target.
|
||||
// Usage:
|
||||
// target, _ := url.Parse("https://mydomain.com")
|
||||
// r := NewRedirection(":80", target, 307)
|
||||
// r.ListenAndServe() // use of `r.Shutdown` to close this server.
|
||||
func NewRedirection(hostAddr string, target *url.URL, redirectStatus int) *Supervisor {
|
||||
targetURI := target.String()
|
||||
if redirectStatus <= 300 {
|
||||
// here we should use StatusPermanentRedirect but
|
||||
// that may result on unexpected behavior
|
||||
// for end-developers who might change their minds
|
||||
// after a while, so keep status temporary.
|
||||
// Note thatwe could also use StatusFound
|
||||
// as we do on the `Context#Redirect`.
|
||||
// It will also help us to prevent any post data issues.
|
||||
redirectStatus = http.StatusTemporaryRedirect
|
||||
}
|
||||
|
||||
redirectSrv := &http.Server{
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 60 * time.Second,
|
||||
Addr: hostAddr,
|
||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
redirectTo := singleJoiningSlash(targetURI, r.URL.Path)
|
||||
if len(r.URL.RawQuery) > 0 {
|
||||
redirectTo += "?" + r.URL.RawQuery
|
||||
}
|
||||
http.Redirect(w, r, redirectTo, redirectStatus)
|
||||
}),
|
||||
}
|
||||
|
||||
return New(redirectSrv)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user