-
Notifications
You must be signed in to change notification settings - Fork 887
rpc: add gzip response compression #3595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
bd84804
74db3f9
7b6f87b
84d4c4f
9306c22
007a8ff
af77abc
7148f8c
a9086de
8c8e7ac
fb7ce82
86f6977
2c2b124
52b09e8
4889d8a
840656a
c2f301f
affed4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "compress/gzip" | ||
| "io" | ||
| "net/http" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| ) | ||
|
|
||
| var gzPool = sync.Pool{ | ||
| New: func() any { | ||
| w := gzip.NewWriter(io.Discard) | ||
| return w | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| }, | ||
| } | ||
|
|
||
| type gzipResponseWriter struct { | ||
| resp http.ResponseWriter | ||
|
|
||
| gz *gzip.Writer | ||
| contentLength uint64 | ||
| written uint64 | ||
| hasLength bool | ||
| inited bool | ||
| } | ||
|
|
||
| func (w *gzipResponseWriter) init() { | ||
| if w.inited { | ||
| return | ||
| } | ||
| w.inited = true | ||
|
|
||
| hdr := w.resp.Header() | ||
| length := hdr.Get("content-length") | ||
| if len(length) > 0 { | ||
| if n, err := strconv.ParseUint(length, 10, 64); err == nil { | ||
| w.hasLength = true | ||
| w.contentLength = n | ||
| } | ||
| } | ||
|
|
||
| // Skip compression if the inner handler already encoded the response or | ||
| // explicitly opted out via Transfer-Encoding: identity. | ||
| if hdr.Get("content-encoding") != "" || hdr.Get("transfer-encoding") == "identity" { | ||
| return | ||
| } | ||
|
|
||
| w.gz = gzPool.Get().(*gzip.Writer) | ||
| w.gz.Reset(w.resp) | ||
| hdr.Del("content-length") | ||
| hdr.Set("content-encoding", "gzip") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] Setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] Content-Type is not preserved before compression. Once |
||
| hdr.Add("vary", "Accept-Encoding") | ||
|
amir-deris marked this conversation as resolved.
Outdated
|
||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| func (w *gzipResponseWriter) Header() http.Header { | ||
| return w.resp.Header() | ||
| } | ||
|
|
||
| func (w *gzipResponseWriter) WriteHeader(status int) { | ||
| // Bodyless responses must not be compressed — gzip would write a stream | ||
| // terminator into what must be an empty body (RFC 7230 §3.3). | ||
| if status == http.StatusNoContent || status == http.StatusNotModified || | ||
|
amir-deris marked this conversation as resolved.
|
||
| (status >= 100 && status <= 199) { | ||
| w.inited = true // skip gzip setup | ||
| w.resp.WriteHeader(status) | ||
| return | ||
| } | ||
| w.init() | ||
| w.resp.WriteHeader(status) | ||
| } | ||
|
|
||
| func (w *gzipResponseWriter) Write(b []byte) (int, error) { | ||
| w.init() | ||
|
|
||
| if w.gz == nil { | ||
| return w.resp.Write(b) | ||
| } | ||
|
|
||
| n, err := w.gz.Write(b) | ||
| w.written += uint64(n) //nolint:gosec | ||
| if w.hasLength && w.written >= w.contentLength { | ||
|
amir-deris marked this conversation as resolved.
Outdated
|
||
| if cerr := w.gz.Close(); cerr != nil && err == nil { | ||
|
amir-deris marked this conversation as resolved.
Outdated
|
||
| err = cerr | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| gzPool.Put(w.gz) | ||
|
amir-deris marked this conversation as resolved.
Outdated
|
||
| w.gz = nil | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| return n, err | ||
| } | ||
|
|
||
| func (w *gzipResponseWriter) Flush() { | ||
|
amir-deris marked this conversation as resolved.
|
||
| if w.gz != nil { | ||
| _ = w.gz.Flush() | ||
| } | ||
| if f, ok := w.resp.(http.Flusher); ok { | ||
| f.Flush() | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flush blocked by statusWriter wrapperLow Severity
Reviewed by Cursor Bugbot for commit affed4d. Configure here. |
||
|
|
||
| func (w *gzipResponseWriter) close() { | ||
| if w.gz == nil { | ||
| return | ||
| } | ||
| _ = w.gz.Close() | ||
| gzPool.Put(w.gz) | ||
| w.gz = nil | ||
| } | ||
|
|
||
| // acceptsGzip reports whether the request advertises gzip encoding support, | ||
| // respecting q-values and the "*" wildcard per RFC 7231 §5.3.4. | ||
| func acceptsGzip(r *http.Request) bool { | ||
| gzipQ := -1.0 | ||
| starQ := -1.0 | ||
| for _, field := range r.Header["Accept-Encoding"] { | ||
| for part := range strings.SplitSeq(field, ",") { | ||
| part = strings.TrimSpace(part) | ||
| coding, params, _ := strings.Cut(part, ";") | ||
| coding = strings.ToLower(strings.TrimSpace(coding)) | ||
| q := 1.0 | ||
| for p := range strings.SplitSeq(params, ";") { | ||
| p = strings.TrimSpace(p) | ||
| if k, v, ok := strings.Cut(p, "="); ok && strings.ToLower(strings.TrimSpace(k)) == "q" { | ||
| if f, err := strconv.ParseFloat(strings.TrimSpace(v), 64); err == nil { | ||
| q = f | ||
| } | ||
| } | ||
| } | ||
| switch coding { | ||
| case "gzip": | ||
| gzipQ = q | ||
| case "*": | ||
| starQ = q | ||
| } | ||
| } | ||
| } | ||
| if gzipQ >= 0 { | ||
| return gzipQ > 0 | ||
| } | ||
| if starQ >= 0 { | ||
| return starQ > 0 | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // NewGzipHandler wraps next with gzip response compression. Compression is | ||
| // skipped for clients that do not advertise gzip support via Accept-Encoding | ||
| // and for WebSocket upgrade requests, preserving the http.Hijacker interface. | ||
| func NewGzipHandler(next http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if !acceptsGzip(r) || strings.EqualFold(r.Header.Get("Upgrade"), "websocket") { | ||
|
amir-deris marked this conversation as resolved.
Outdated
|
||
| next.ServeHTTP(w, r) | ||
| return | ||
| } | ||
|
|
||
| wrapper := &gzipResponseWriter{resp: w} | ||
| defer wrapper.close() | ||
|
amir-deris marked this conversation as resolved.
Outdated
amir-deris marked this conversation as resolved.
Outdated
|
||
|
|
||
| next.ServeHTTP(wrapper, r) | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| }) | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.