diff options
Diffstat (limited to 'nonsense-time.go')
| -rw-r--r-- | nonsense-time.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/nonsense-time.go b/nonsense-time.go index e57e048..1d4e218 100644 --- a/nonsense-time.go +++ b/nonsense-time.go @@ -1,6 +1,7 @@ package main import ( + "compress/gzip" "context" "flag" "fmt" @@ -23,6 +24,36 @@ func timeoutMiddleware(next http.Handler, duration time.Duration) http.Handler { }) } +type gzipResponseWriter struct { + gzw gzip.Writer + hrw http.ResponseWriter +} + +func (gzrw gzipResponseWriter) Header() http.Header { + headers := gzrw.hrw.Header() + // TODO: add gzip header + return headers +} + +func (gzrw gzipResponseWriter) Write(p []byte) (int, error) { + // TODO: write data using gzrw.gzw.Write + return gzrw.hrw.Write(p) +} + +func (gzrw gzipResponseWriter) WriteHeader(statuscode int) { + gzrw.hrw.WriteHeader(statuscode) +} + +// Middleware to conditionally gzip a response +func gzipMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // TODO: check for support gzip header + gzrw := gzipResponseWriter{hrw: w} + + next.ServeHTTP(gzrw, r) + }) +} + func main() { port := flag.Int("p", 8080, "the port to listen on") |
