Refactor redirect code. NFC

This commit is contained in:
Catherine
2025-11-18 22:21:51 +00:00
parent 7773ebd0dc
commit 325c283e05
2 changed files with 18 additions and 16 deletions

View File

@@ -40,10 +40,6 @@ var (
}, []string{"cause"})
)
func makeWebRoot(host string, projectName string) string {
return fmt.Sprintf("%s/%s", strings.ToLower(host), projectName)
}
func reportSiteUpdate(via string, result *UpdateResult) {
siteUpdatesCount.With(prometheus.Labels{"via": via}).Inc()
@@ -63,6 +59,16 @@ func reportSiteUpdate(via string, result *UpdateResult) {
}
}
func makeWebRoot(host string, projectName string) string {
return fmt.Sprintf("%s/%s", strings.ToLower(host), projectName)
}
func writeRedirect(w http.ResponseWriter, code int, path string) {
w.Header().Set("Location", path)
w.WriteHeader(code)
fmt.Fprintf(w, "see %s\n", path)
}
// The `clauspost/compress/zstd` package recommends reusing a decompressor to avoid repeated
// allocations of internal buffers.
var zstdDecoder, _ = zstd.NewReader(nil)
@@ -185,9 +191,7 @@ func getPage(w http.ResponseWriter, r *http.Request) error {
originalURL := (&url.URL{Host: r.Host}).ResolveReference(r.URL)
redirectURL, redirectStatus := ApplyRedirectRules(manifest, originalURL, redirectKind)
if Is3xxHTTPStatus(redirectStatus) {
w.Header().Set("Location", redirectURL.String())
w.WriteHeader(int(redirectStatus))
fmt.Fprintf(w, "see %s\n", redirectURL.String())
writeRedirect(w, redirectStatus, redirectURL.String())
return nil
} else if redirectURL != nil {
entryPath = strings.TrimPrefix(redirectURL.Path, "/")
@@ -232,9 +236,7 @@ func getPage(w http.ResponseWriter, r *http.Request) error {
// redirect from `dir` to `dir/`, otherwise when `dir/index.html` is served,
// links in it will have the wrong base URL
newPath := r.URL.Path + "/"
w.Header().Set("Location", newPath)
w.WriteHeader(http.StatusFound)
fmt.Fprintf(w, "see %s\n", newPath)
writeRedirect(w, http.StatusFound, newPath)
return nil
}
} else if entry.GetType() == Type_Symlink {

View File

@@ -31,7 +31,7 @@ func unparseRule(rule redirects.Rule) string {
return strings.Join(parts, " ")
}
var validRedirectHTTPStatuses []uint = []uint{
var validRedirectHTTPStatuses []int = []int{
http.StatusOK,
http.StatusMovedPermanently,
http.StatusFound,
@@ -45,7 +45,7 @@ var validRedirectHTTPStatuses []uint = []uint{
http.StatusUnavailableForLegalReasons,
}
func Is3xxHTTPStatus(status uint) bool {
func Is3xxHTTPStatus(status int) bool {
return status >= 300 && status <= 399
}
@@ -53,7 +53,7 @@ func validateRedirectRule(rule redirects.Rule) error {
if len(rule.Params) > 0 {
return fmt.Errorf("rules with parameters are not supported")
}
if !slices.Contains(validRedirectHTTPStatuses, uint(rule.Status)) {
if !slices.Contains(validRedirectHTTPStatuses, rule.Status) {
return fmt.Errorf("rule cannot use status %d: must be %v",
rule.Status, validRedirectHTTPStatuses)
}
@@ -74,7 +74,7 @@ func validateRedirectRule(rule redirects.Rule) error {
if err != nil {
return fmt.Errorf("malformed 'to' URL")
}
if !Is3xxHTTPStatus(uint(rule.Status)) {
if !Is3xxHTTPStatus(rule.Status) {
if !strings.HasPrefix(toURL.Path, "/") {
return fmt.Errorf("'to' URL path must start with a / for non-3xx status rules")
}
@@ -140,7 +140,7 @@ const (
func ApplyRedirectRules(
manifest *Manifest, fromURL *url.URL, kind RedirectKind,
) (
toURL *url.URL, status uint,
toURL *url.URL, status int,
) {
fromSegments := pathSegments(fromURL.Path)
next:
@@ -191,7 +191,7 @@ next:
Path: "/" + strings.Join(toSegments, "/"),
RawQuery: fromURL.RawQuery,
}
status = uint(*rule.Status)
status = int(*rule.Status)
break
}
// no redirect found