1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-20 03:17:04 +00:00

add internal subdomain redirect handler

TODO more things
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-16 20:16:49 +03:00
parent f8ac760f69
commit a61f743fa8
6 changed files with 142 additions and 49 deletions

View File

@@ -92,16 +92,6 @@ func NewSubdomainRedirectWrapper(rootDomainGetter func() string, from, to string
return sd.Wrapper
}
const sufscheme = "://"
func getFullScheme(r *http.Request) string {
if !r.URL.IsAbs() {
// url scheme is empty.
return netutil.SchemeHTTP + sufscheme
}
return r.URL.Scheme + sufscheme
}
// Wrapper is the function that is being used to wrap the router with a redirect
// service that is able to redirect between (sub)domains as fast as possible.
// Please take a look at the `NewSubdomainRedirectWrapper` function for more.
@@ -142,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, getFullScheme(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, getFullScheme(r)+s.to+root+resturi, http.StatusMovedPermanently)
redirectAbsolute(w, r, context.GetScheme(r)+s.to+root+resturi, http.StatusMovedPermanently)
return
}
@@ -172,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, getFullScheme(r)+s.to+root+resturi, http.StatusMovedPermanently)
redirectAbsolute(w, r, context.GetScheme(r)+s.to+root+resturi, http.StatusMovedPermanently)
return
}
@@ -199,3 +189,30 @@ func redirectAbsolute(w http.ResponseWriter, r *http.Request, url string, code i
fmt.Fprintln(w, body)
}
}
// NewSubdomainPartyRedirectHandler returns a handler which can be registered
// through `UseRouter` or `Use` to redirect from the current request's
// subdomain to the one which the given `to` Party can handle.
func NewSubdomainPartyRedirectHandler(to Party) context.Handler {
return NewSubdomainRedirectHandler(to.GetRelPath())
}
// NewSubdomainRedirectHandler returns a handler which can be registered
// through `UseRouter` or `Use` to redirect from the current request's
// subdomain to the given "toSubdomain".
func NewSubdomainRedirectHandler(toSubdomain string) context.Handler {
toSubdomain, _ = splitSubdomainAndPath(toSubdomain) // let it here so users can just pass the GetRelPath of a Party.
if pathIsWildcard(toSubdomain) {
return nil
}
return func(ctx *context.Context) {
// en-us.test.mydomain.com
host := ctx.Host()
fullSubdomain := ctx.SubdomainFull()
newHost := strings.Replace(host, fullSubdomain, toSubdomain, 1)
resturi := ctx.Request().URL.RequestURI()
urlToRedirect := ctx.Scheme() + newHost + resturi
redirectAbsolute(ctx.ResponseWriter(), ctx.Request(), urlToRedirect, http.StatusMovedPermanently)
}
}

View File

@@ -159,3 +159,40 @@ func TestRouterWrapperOrder(t *testing.T) {
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(expectedOrderStr)
}
}
func TestNewSubdomainPartyRedirectHandler(t *testing.T) {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.WriteString("root index")
})
test := app.Subdomain("test")
test.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
ctx.WriteString("test 404")
})
test.Get("/", func(ctx iris.Context) {
ctx.WriteString("test index")
})
testold := app.Subdomain("testold")
// redirects testold.mydomain.com to test.mydomain.com .
testold.UseRouter(router.NewSubdomainPartyRedirectHandler(test))
testold.Get("/", func(ctx iris.Context) {
ctx.WriteString("test old index (should never be fired)")
})
testoldLeveled := testold.Subdomain("leveled")
testoldLeveled.Get("/", func(ctx iris.Context) {
ctx.WriteString("leveled.testold this can be fired")
})
if redirectHandler := router.NewSubdomainPartyRedirectHandler(app.WildcardSubdomain()); redirectHandler != nil {
t.Fatal("redirect handler should be nil, we cannot redirect to a wildcard")
}
e := httptest.New(t, app)
e.GET("/").WithURL("http://mydomain.com").Expect().Status(iris.StatusOK).Body().Equal("root index")
e.GET("/").WithURL("http://test.mydomain.com").Expect().Status(iris.StatusOK).Body().Equal("test index")
e.GET("/").WithURL("http://testold.mydomain.com").Expect().Status(iris.StatusOK).Body().Equal("test index")
e.GET("/").WithURL("http://testold.mydomain.com/notfound").Expect().Status(iris.StatusNotFound).Body().Equal("test 404")
e.GET("/").WithURL("http://leveled.testold.mydomain.com").Expect().Status(iris.StatusOK).Body().Equal("leveled.testold this can be fired")
}