-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: add gzip to http responses for query/batch-query #232
Changes from 1 commit
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,37 @@ | ||
package api | ||
|
||
import ( | ||
"compress/gzip" | ||
"io" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type gzipResponseWriter struct { | ||
io.Writer | ||
http.ResponseWriter | ||
} | ||
|
||
func (w gzipResponseWriter) Write(b []byte) (int, error) { | ||
return w.Writer.Write(b) | ||
} | ||
|
||
func gzipWrapper(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Check that path ends in /query or /batch-query | ||
if !strings.HasSuffix(r.URL.Path, "/query") && !strings.HasSuffix(r.URL.Path, "/batch-query") { | ||
handler.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { | ||
handler.ServeHTTP(w, r) | ||
return | ||
} | ||
w.Header().Set("Content-Encoding", "gzip") | ||
gz := gzip.NewWriter(w) | ||
defer gz.Close() | ||
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w} | ||
handler.ServeHTTP(gzw, r) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,7 +118,7 @@ func Test_QueryNoTopics(t *testing.T) { | |
require.Equal(t, codes.InvalidArgument, grpcErr.Code()) | ||
require.EqualError(t, err, `rpc error: code = InvalidArgument desc = content topics required`) | ||
} else { | ||
require.EqualError(t, err, "400 Bad Request: {\"code\":3, \"message\":\"content topics required\", \"details\":[]}") | ||
require.Regexp(t, `400 Bad Request: {"code\":3,\s?"message":"content topics required",\s?"details":\[\]}`, err.Error()) | ||
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. somehow the whitespaces get stripped now? I noticed the test above already accounts for this via regexp so did the same here 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. Oh right I think this is what's happening golang/protobuf#1121 |
||
} | ||
require.Nil(t, queryRes) | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Want to just call this file
gzip.go
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call, will make that change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up renaming to gzip_handler.go, I balked at
gzip.go
since it seemed like a name that could be used a lot elsewhere.