diff options
author | Kassio Borges <kborges@gitlab.com> | 2023-04-04 12:32:18 +0300 |
---|---|---|
committer | Vladimir Shushlin <vshushlin@gitlab.com> | 2023-04-04 12:32:18 +0300 |
commit | 7946cb0dc92ac6a306b55af1ec3c17dc936d742c (patch) | |
tree | 006e691eb52cb4e33987e11e80026ae17646ebd9 /internal | |
parent | 7817434f90966c6cd5dbee8fd1894c039eddc8af (diff) |
Redirect to unique domain
Diffstat (limited to 'internal')
-rw-r--r-- | internal/serving/lookup_path.go | 1 | ||||
-rw-r--r-- | internal/source/gitlab/api/lookup_path.go | 1 | ||||
-rw-r--r-- | internal/source/gitlab/factory.go | 1 | ||||
-rw-r--r-- | internal/uniqueDomain/middleware.go | 74 |
4 files changed, 77 insertions, 0 deletions
diff --git a/internal/serving/lookup_path.go b/internal/serving/lookup_path.go index 421e2a51..6a283b6e 100644 --- a/internal/serving/lookup_path.go +++ b/internal/serving/lookup_path.go @@ -10,4 +10,5 @@ type LookupPath struct { IsHTTPSOnly bool HasAccessControl bool ProjectID uint64 + UniqueHost string } diff --git a/internal/source/gitlab/api/lookup_path.go b/internal/source/gitlab/api/lookup_path.go index 834767bd..a61d2f7c 100644 --- a/internal/source/gitlab/api/lookup_path.go +++ b/internal/source/gitlab/api/lookup_path.go @@ -7,6 +7,7 @@ type LookupPath struct { HTTPSOnly bool `json:"https_only,omitempty"` Prefix string `json:"prefix,omitempty"` Source Source `json:"source,omitempty"` + UniqueHost string `json:"unique_host,omitempty"` } // Source describes GitLab Page serving variant diff --git a/internal/source/gitlab/factory.go b/internal/source/gitlab/factory.go index 312d09bf..1c5d15f5 100644 --- a/internal/source/gitlab/factory.go +++ b/internal/source/gitlab/factory.go @@ -31,6 +31,7 @@ func fabricateLookupPath(size int, lookup api.LookupPath) *serving.LookupPath { IsHTTPSOnly: lookup.HTTPSOnly, HasAccessControl: lookup.AccessControl, ProjectID: uint64(lookup.ProjectID), + UniqueHost: lookup.UniqueHost, } } diff --git a/internal/uniqueDomain/middleware.go b/internal/uniqueDomain/middleware.go new file mode 100644 index 00000000..b0af761a --- /dev/null +++ b/internal/uniqueDomain/middleware.go @@ -0,0 +1,74 @@ +package uniqueDomain + +import ( + "net" + "net/http" + "strings" + + "gitlab.com/gitlab-org/gitlab-pages/internal/domain" + "gitlab.com/gitlab-org/gitlab-pages/internal/logging" +) + +func NewMiddleware(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + uniqueURL := getUniqueURL(r) + if uniqueURL == "" { + logging. + LogRequest(r). + WithField("uniqueURL", uniqueURL). + Debug("unique domain: doing nothing") + + handler.ServeHTTP(w, r) + return + } + + logging. + LogRequest(r). + WithField("uniqueURL", uniqueURL). + Info("redirecting to unique domain") + + http.Redirect(w, r, uniqueURL, http.StatusPermanentRedirect) + }) +} + +func getUniqueURL(r *http.Request) string { + domain := domain.FromRequest(r) + lookupPath, err := domain.GetLookupPath(r) + if err != nil { + logging. + LogRequest(r). + WithError(err). + Error("uniqueDomain: failed to get lookupPath") + return "" + } + + // No uniqueHost to redirect + if lookupPath.UniqueHost == "" { + return "" + } + + requestHost, port, err := net.SplitHostPort(r.Host) + if err != nil { + requestHost = r.Host + } + + // Already serving the uniqueHost + if lookupPath.UniqueHost == requestHost { + return "" + } + + uniqueURL := *r.URL + if port == "" { + uniqueURL.Host = lookupPath.UniqueHost + } else { + uniqueURL.Host = net.JoinHostPort(lookupPath.UniqueHost, port) + } + + // Ensure to redirect to the same path requested + uniqueURL.Path = strings.TrimPrefix( + r.URL.Path, + strings.TrimSuffix(lookupPath.Prefix, "/"), + ) + + return uniqueURL.String() +} |