From 42dcc259e7b85ca52b9425dbbb488f4633837547 Mon Sep 17 00:00:00 2001 From: minhvh93 Date: Mon, 2 Dec 2019 20:35:15 +0700 Subject: [PATCH 1/3] #1399 [BUG]Can't set file server in subdomain with request path is "/" Former-commit-id: 2914cafeab26ae8a716138bec95ade6953ddd04b --- core/router/path.go | 5 +++++ core/router/path_test.go | 2 ++ 2 files changed, 7 insertions(+) diff --git a/core/router/path.go b/core/router/path.go index 6c32c863..6d9b687a 100644 --- a/core/router/path.go +++ b/core/router/path.go @@ -231,6 +231,11 @@ func splitSubdomainAndPath(fullUnparsedPath string) (subdomain string, path stri return "", "/" } + splitPath := strings.Split(s, ".") + if len(splitPath) == 2 && splitPath[1] == "" { + return splitPath[0] + ".", "/" + } + slashIdx := strings.IndexByte(s, '/') if slashIdx > 0 { // has subdomain diff --git a/core/router/path_test.go b/core/router/path_test.go index 2b2c7538..bdf2d4ad 100644 --- a/core/router/path_test.go +++ b/core/router/path_test.go @@ -116,6 +116,8 @@ func TestSplitSubdomainAndPath(t *testing.T) { path string }{ {"admin./users/42", "admin.", "/users/42"}, + {"admin.", "admin.", "/"}, + {"admin./" + WildcardFileParam(), "admin.", "/" + WildcardFileParam()}, {"//api/users\\42", "", "/api/users/42"}, {"admin./users//42", "admin.", "/users/42"}, {"*./users/42/", "*.", "/users/42"}, From 6dcbbc546436095918dac48182ef348c37d65268 Mon Sep 17 00:00:00 2001 From: minhvh Date: Tue, 3 Dec 2019 10:51:00 +0700 Subject: [PATCH 2/3] #1399[FIX] file server in subdomain with request path "/" Former-commit-id: 30a86cba134b87db803aa47a277795717c694c3b --- core/router/path.go | 2 +- core/router/path_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/router/path.go b/core/router/path.go index 6d9b687a..61d7e065 100644 --- a/core/router/path.go +++ b/core/router/path.go @@ -233,7 +233,7 @@ func splitSubdomainAndPath(fullUnparsedPath string) (subdomain string, path stri splitPath := strings.Split(s, ".") if len(splitPath) == 2 && splitPath[1] == "" { - return splitPath[0] + ".", "/" + return splitPath[0], "/" } slashIdx := strings.IndexByte(s, '/') diff --git a/core/router/path_test.go b/core/router/path_test.go index bdf2d4ad..d9b36ff6 100644 --- a/core/router/path_test.go +++ b/core/router/path_test.go @@ -116,7 +116,7 @@ func TestSplitSubdomainAndPath(t *testing.T) { path string }{ {"admin./users/42", "admin.", "/users/42"}, - {"admin.", "admin.", "/"}, + {"admin.", "admin", "/"}, {"admin./" + WildcardFileParam(), "admin.", "/" + WildcardFileParam()}, {"//api/users\\42", "", "/api/users/42"}, {"admin./users//42", "admin.", "/users/42"}, From 137fa92da52bed7f02425031f73d1a36a63cc0d3 Mon Sep 17 00:00:00 2001 From: minhvh Date: Tue, 3 Dec 2019 16:12:15 +0700 Subject: [PATCH 3/3] #1399[FIX] file server in subdomain with request path "/" Former-commit-id: de4326943640706a9a2f1418327c4a69bc5f85d9 --- core/router/api_builder.go | 6 +++++- core/router/path.go | 2 +- core/router/path_test.go | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/core/router/api_builder.go b/core/router/api_builder.go index b789bb3b..d9c244e2 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -488,7 +488,11 @@ func (api *APIBuilder) HandleDir(requestPath, directory string, opts ...DirOptio continue } - requestPath := s.RequestPath[strings.Index(s.RequestPath, api.relativePath)+len(api.relativePath):] + slashIdx := strings.IndexByte(s.RequestPath, '/') + if slashIdx == -1 { + slashIdx = 0 + } + requestPath = s.RequestPath[slashIdx:] routes = append(routes, api.createRoutes([]string{http.MethodGet}, requestPath, h)...) getRoute.StaticSites = append(getRoute.StaticSites, s) } diff --git a/core/router/path.go b/core/router/path.go index 61d7e065..6d9b687a 100644 --- a/core/router/path.go +++ b/core/router/path.go @@ -233,7 +233,7 @@ func splitSubdomainAndPath(fullUnparsedPath string) (subdomain string, path stri splitPath := strings.Split(s, ".") if len(splitPath) == 2 && splitPath[1] == "" { - return splitPath[0], "/" + return splitPath[0] + ".", "/" } slashIdx := strings.IndexByte(s, '/') diff --git a/core/router/path_test.go b/core/router/path_test.go index d9b36ff6..5e5c6abc 100644 --- a/core/router/path_test.go +++ b/core/router/path_test.go @@ -116,8 +116,8 @@ func TestSplitSubdomainAndPath(t *testing.T) { path string }{ {"admin./users/42", "admin.", "/users/42"}, - {"admin.", "admin", "/"}, - {"admin./" + WildcardFileParam(), "admin.", "/" + WildcardFileParam()}, + {"static.", "static.", "/"}, + {"static./" + WildcardFileParam(), "static.", "/" + WildcardFileParam()}, {"//api/users\\42", "", "/api/users/42"}, {"admin./users//42", "admin.", "/users/42"}, {"*./users/42/", "*.", "/users/42"},