fork of whitequark.org/git-pages with mods for tangled
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactor redirect code. NFC

+18 -16
+12 -10
src/pages.go
··· 40 40 }, []string{"cause"}) 41 41 ) 42 42 43 - func makeWebRoot(host string, projectName string) string { 44 - return fmt.Sprintf("%s/%s", strings.ToLower(host), projectName) 45 - } 46 - 47 43 func reportSiteUpdate(via string, result *UpdateResult) { 48 44 siteUpdatesCount.With(prometheus.Labels{"via": via}).Inc() 49 45 ··· 61 57 case UpdateDeleted: 62 58 siteUpdateOkCount.With(prometheus.Labels{"outcome": "deleted"}).Inc() 63 59 } 60 + } 61 + 62 + func makeWebRoot(host string, projectName string) string { 63 + return fmt.Sprintf("%s/%s", strings.ToLower(host), projectName) 64 + } 65 + 66 + func writeRedirect(w http.ResponseWriter, code int, path string) { 67 + w.Header().Set("Location", path) 68 + w.WriteHeader(code) 69 + fmt.Fprintf(w, "see %s\n", path) 64 70 } 65 71 66 72 // The `clauspost/compress/zstd` package recommends reusing a decompressor to avoid repeated ··· 185 191 originalURL := (&url.URL{Host: r.Host}).ResolveReference(r.URL) 186 192 redirectURL, redirectStatus := ApplyRedirectRules(manifest, originalURL, redirectKind) 187 193 if Is3xxHTTPStatus(redirectStatus) { 188 - w.Header().Set("Location", redirectURL.String()) 189 - w.WriteHeader(int(redirectStatus)) 190 - fmt.Fprintf(w, "see %s\n", redirectURL.String()) 194 + writeRedirect(w, redirectStatus, redirectURL.String()) 191 195 return nil 192 196 } else if redirectURL != nil { 193 197 entryPath = strings.TrimPrefix(redirectURL.Path, "/") ··· 232 236 // redirect from `dir` to `dir/`, otherwise when `dir/index.html` is served, 233 237 // links in it will have the wrong base URL 234 238 newPath := r.URL.Path + "/" 235 - w.Header().Set("Location", newPath) 236 - w.WriteHeader(http.StatusFound) 237 - fmt.Fprintf(w, "see %s\n", newPath) 239 + writeRedirect(w, http.StatusFound, newPath) 238 240 return nil 239 241 } 240 242 } else if entry.GetType() == Type_Symlink {
+6 -6
src/redirects.go
··· 31 31 return strings.Join(parts, " ") 32 32 } 33 33 34 - var validRedirectHTTPStatuses []uint = []uint{ 34 + var validRedirectHTTPStatuses []int = []int{ 35 35 http.StatusOK, 36 36 http.StatusMovedPermanently, 37 37 http.StatusFound, ··· 45 45 http.StatusUnavailableForLegalReasons, 46 46 } 47 47 48 - func Is3xxHTTPStatus(status uint) bool { 48 + func Is3xxHTTPStatus(status int) bool { 49 49 return status >= 300 && status <= 399 50 50 } 51 51 ··· 53 53 if len(rule.Params) > 0 { 54 54 return fmt.Errorf("rules with parameters are not supported") 55 55 } 56 - if !slices.Contains(validRedirectHTTPStatuses, uint(rule.Status)) { 56 + if !slices.Contains(validRedirectHTTPStatuses, rule.Status) { 57 57 return fmt.Errorf("rule cannot use status %d: must be %v", 58 58 rule.Status, validRedirectHTTPStatuses) 59 59 } ··· 74 74 if err != nil { 75 75 return fmt.Errorf("malformed 'to' URL") 76 76 } 77 - if !Is3xxHTTPStatus(uint(rule.Status)) { 77 + if !Is3xxHTTPStatus(rule.Status) { 78 78 if !strings.HasPrefix(toURL.Path, "/") { 79 79 return fmt.Errorf("'to' URL path must start with a / for non-3xx status rules") 80 80 } ··· 140 140 func ApplyRedirectRules( 141 141 manifest *Manifest, fromURL *url.URL, kind RedirectKind, 142 142 ) ( 143 - toURL *url.URL, status uint, 143 + toURL *url.URL, status int, 144 144 ) { 145 145 fromSegments := pathSegments(fromURL.Path) 146 146 next: ··· 191 191 Path: "/" + strings.Join(toSegments, "/"), 192 192 RawQuery: fromURL.RawQuery, 193 193 } 194 - status = uint(*rule.Status) 194 + status = int(*rule.Status) 195 195 break 196 196 } 197 197 // no redirect found