diff options
author | Nathan Friend <nathan@gitlab.com> | 2021-04-26 20:56:22 +0300 |
---|---|---|
committer | Nathan Friend <nathan@gitlab.com> | 2021-04-26 20:56:22 +0300 |
commit | 2fe2b82a81e689121a74916aa0f59cc03c7c9f7f (patch) | |
tree | 38cc51e7a2fecc44b979238a504d175b8acce87d /internal/redirects/redirects.go | |
parent | 782e0b46d29d4c7b0b953b51cbaf1bb197124bae (diff) |
Break redirect code into helpers and validations
This commit breaks some of the logic in the redirects package into
separate files. This is in preparation for a change that will add
significant amounts of new logic to these two new files.
Diffstat (limited to 'internal/redirects/redirects.go')
-rw-r--r-- | internal/redirects/redirects.go | 78 |
1 files changed, 0 insertions, 78 deletions
diff --git a/internal/redirects/redirects.go b/internal/redirects/redirects.go index 48dc2c05..8c30f747 100644 --- a/internal/redirects/redirects.go +++ b/internal/redirects/redirects.go @@ -6,7 +6,6 @@ import ( "context" "errors" "fmt" - "net/http" "net/url" "regexp" "strings" @@ -74,83 +73,6 @@ func (r *Redirects) Status() string { return strings.Join(messages, "\n") } -func validateURL(urlText string) error { - url, err := url.Parse(urlText) - if err != nil { - return errFailedToParseURL - } - - // No support for domain-level redirects to outside sites: - // - `https://google.com` - // - `//google.com` - if url.Host != "" || url.Scheme != "" { - return errNoDomainLevelRedirects - } - - // No parent traversing relative URL's with `./` or `../` - // No ambiguous URLs like bare domains `GitLab.com` - if !strings.HasPrefix(url.Path, "/") { - return errNoStartingForwardSlashInURLPath - } - - // No support for splats, https://docs.netlify.com/routing/redirects/redirect-options/#splats - if strings.Contains(url.Path, "*") { - return errNoSplats - } - - // No support for placeholders, https://docs.netlify.com/routing/redirects/redirect-options/#placeholders - if regexpPlaceholder.MatchString(url.Path) { - return errNoPlaceholders - } - - return nil -} - -func validateRule(r netlifyRedirects.Rule) error { - if err := validateURL(r.From); err != nil { - return err - } - - if err := validateURL(r.To); err != nil { - return err - } - - // No support for query parameters, https://docs.netlify.com/routing/redirects/redirect-options/#query-parameters - if r.Params != nil { - return errNoParams - } - - // We strictly validate return status codes - switch r.Status { - case http.StatusMovedPermanently, http.StatusFound: - // noop - default: - return errUnsupportedStatus - } - - // No support for rules that use ! force - if r.Force { - return errNoForce - } - - return nil -} - -func normalizePath(path string) string { - return strings.TrimSuffix(path, "/") + "/" -} - -func (r *Redirects) match(url *url.URL) *netlifyRedirects.Rule { - for _, rule := range r.rules { - // TODO: Likely this should include host comparison once we have domain-level redirects - if normalizePath(rule.From) == normalizePath(url.Path) && validateRule(rule) == nil { - return &rule - } - } - - return nil -} - // Rewrite takes in a URL and uses the parsed Netlify rules to rewrite // the URL to the new location if it matches any rule func (r *Redirects) Rewrite(url *url.URL) (*url.URL, int, error) { |