diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2026-09-22 18:57:03 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2026-09-22 19:20:41 -0700 |
| commit | 8e43b5311957568d429463a3f8b21ca2a0268902 (patch) | |
| tree | 0304bef3e07cd153d7da4bcf0b030618bead88b2 | |
| parent | Rely on content sniffing for static templates (diff) | |
| download | roseh.moe-8e43b5311957568d429463a3f8b21ca2a0268902.tar.zst | |
Gzip response from cgit
| -rw-r--r-- | roseh.moe.go | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/roseh.moe.go b/roseh.moe.go index aa2da29..54ce6f4 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -950,6 +950,15 @@ type gzipResponseWriter struct { code int } +func shouldGzip(w http.ResponseWriter) bool { + disposition, _, _ := strings.Cut(w.Header().Get("Content-Disposition"), ";") + length, lengthErr := strconv.Atoi(w.Header().Get("Content-Length")) + return strings.HasPrefix(w.Header().Get("Content-Type"), "text/") && + !(disposition == "attachment" && w.Header().Get("Accept-Ranges") != "" || + w.Header().Get("Content-Encoding") != "" || + lengthErr == nil && length < 512) +} + func (w *gzipResponseWriter) ReadFrom(r io.Reader) (int64, error) { if w.w != nil { return io.Copy(w.w, r) @@ -971,9 +980,7 @@ func (w *gzipResponseWriter) ReadFrom(r io.Reader) (int64, error) { contentType := http.DetectContentType(w.buf) w.Header().Set("Content-Type", contentType) } - if disposition, _, _ := strings.Cut(w.Header().Get("Content-Disposition"), ";"); disposition == "attachment" && len(w.Header().Values("Accept-Ranges")) > 0 || !strings.HasPrefix(w.Header().Get("Content-Type"), "text/") { - w.w = nopCloser{w.ResponseWriter} - } else { + if shouldGzip(w.ResponseWriter) { var err error w.w, err = gzip.NewWriterLevel(w.ResponseWriter, gzip.BestSpeed) if err != nil { @@ -982,6 +989,8 @@ func (w *gzipResponseWriter) ReadFrom(r io.Reader) (int64, error) { w.Header().Set("Content-Encoding", "gzip") w.Header().Del("Content-Length") w.Header().Del("Accept-Ranges") + } else { + w.w = nopCloser{w.ResponseWriter} } if w.code != 0 { w.ResponseWriter.WriteHeader(w.code) @@ -1146,11 +1155,12 @@ func main() { r.SetXForwarded() }, }}) + codeMux := http.NewServeMux() cgitStaticFiles := http.FileServer(http.Dir(*cgit)) - http.Handle("code."+selfURL.Hostname()+"/cgit.css", cgitStaticFiles) - http.Handle("code."+selfURL.Hostname()+"/cgit.js", cgitStaticFiles) - http.Handle("code."+selfURL.Hostname()+"/robots.txt", cgitStaticFiles) - http.HandleFunc("code."+selfURL.Hostname()+"/favicon.ico", favicon) + codeMux.Handle("/cgit.css", cgitStaticFiles) + codeMux.Handle("/cgit.js", cgitStaticFiles) + codeMux.Handle("/robots.txt", cgitStaticFiles) + codeMux.HandleFunc("/favicon.ico", favicon) cgitConfigAbsPath, err := filepath.Abs(*cgitConfig) if err != nil { log.Fatal(err) @@ -1160,7 +1170,8 @@ func main() { Env: []string{"CGIT_CONFIG=" + cgitConfigAbsPath}, } cgitCGI.PathLocationHandler = cgitCGI - http.Handle("code."+selfURL.Hostname()+"/", cgitCGI) + codeMux.Handle("/", cgitCGI) + http.Handle("code."+selfURL.Hostname()+"/", &gzipHandler{codeMux}) port := selfURL.Port() if port == "" { |
