mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 14:26:58 +00:00
add gzip support
This commit is contained in:
parent
16a8d5d575
commit
fac052f052
3 changed files with 43 additions and 11 deletions
37
server/gzip/gzip.go
Normal file
37
server/gzip/gzip.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package gzip
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type gzipResponseWriter struct {
|
||||
Writer *gzip.Writer
|
||||
ResponseWriter http.ResponseWriter
|
||||
}
|
||||
|
||||
func (gz gzipResponseWriter) Header() http.Header {
|
||||
return gz.ResponseWriter.Header()
|
||||
}
|
||||
|
||||
func (gz gzipResponseWriter) Write(b []byte) (int, error) {
|
||||
return gz.Writer.Write(b)
|
||||
}
|
||||
|
||||
func (gz gzipResponseWriter) WriteHeader(statusCode int) {
|
||||
gz.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func SetupCompression(handler http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||
handler(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
gz := gzip.NewWriter(w)
|
||||
defer gz.Close()
|
||||
handler(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
|
||||
}
|
||||
}
|
|
@ -9,11 +9,6 @@ import (
|
|||
"codeberg.org/codeberg/pages/server/utils"
|
||||
)
|
||||
|
||||
func SetupServer(handler http.HandlerFunc) http.HandlerFunc {
|
||||
// TODO: enagle gzip compression
|
||||
return handler
|
||||
}
|
||||
|
||||
func SetupHTTPACMEChallengeServer(challengeCache cache.SetGetKey) http.HandlerFunc {
|
||||
challengePath := "/.well-known/acme-challenge/"
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue