Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianArnautu committed Feb 24, 2023
1 parent 4d0c873 commit 07eb5dd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 4 additions & 0 deletions docs/content/rest-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ The path separator is used to access values inside object and array documents. I
- **instrument** - Instrument query evaluation and return a superset of performance metrics in addition to result. See [Performance Metrics](#performance-metrics) for more detail.
- **strict-builtin-errors** - Treat built-in function call errors as fatal and return an error immediately.

#### Request Headers

- **Accept-Encoding: gzip**: Indicates the server should respond with a gzip encoded body

#### Status Codes

- **200** - no error
Expand Down
24 changes: 17 additions & 7 deletions server/handlers/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func (w *compressResponseWriter) Write(b []byte) (int, error) {

func CompressHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
// this handler applies only for data and compile endpoints
shouldApplyCompression := isDataEndpoint(request) || isCompileEndpoint(request)
if !shouldApplyCompression {
// this handler applies only for data and compile endpoints for selected HTTP methods
enabledForEndpoint := isDataEndpoint(request) || isCompileEndpoint(request)
if !enabledForEndpoint {
handler.ServeHTTP(responseWriter, request)
return
}
Expand All @@ -54,10 +54,10 @@ func CompressHandler(handler http.Handler) http.Handler {
gzipWriter.Reset(&b)
defer func() {
gzipWriter.Close()
responseWriter.Header().Set("Content-Length", fmt.Sprint(len(b.Bytes())))
responseWriter.Header().Set(contentLengthHeader, fmt.Sprint(len(b.Bytes())))
_, err := responseWriter.Write(b.Bytes())
if err != nil {
log.Fatalf("Error writing the compressed response: %v", err)
log.Fatalf("Error writing the compressed response: %s\n", err)
}
}()

Expand All @@ -67,11 +67,21 @@ func CompressHandler(handler http.Handler) http.Handler {
}

func isDataEndpoint(req *http.Request) bool {
return strings.HasPrefix(req.URL.Path, "/v1/data") || strings.HasPrefix(req.URL.Path, "/v0/data")
isPostOrGetMethod := isPostMethod(req) || isGetMethod(req)
isV1rV0 := strings.HasPrefix(req.URL.Path, "/v1/data") || strings.HasPrefix(req.URL.Path, "/v0/data")
return isPostOrGetMethod && isV1rV0
}

func isCompileEndpoint(req *http.Request) bool {
return strings.HasPrefix(req.URL.Path, "/v1/compile")
return isPostMethod(req) && strings.HasPrefix(req.URL.Path, "/v1/compile")
}

func isPostMethod(req *http.Request) bool {
return req.Method == "POST"
}

func isGetMethod(req *http.Request) bool {
return req.Method == "GET"
}

var gzipPool = sync.Pool{
Expand Down
8 changes: 4 additions & 4 deletions server/handlers/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
requestBody = "Hello World!\n"
)

func executeRequest(w *httptest.ResponseRecorder, path string, acceptEncoding string) {
func executeRequest(w *httptest.ResponseRecorder, path string, method string, acceptEncoding string) {
CompressHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, err := io.WriteString(w, requestBody)
Expand All @@ -25,7 +25,7 @@ func executeRequest(w *httptest.ResponseRecorder, path string, acceptEncoding st
}
})).ServeHTTP(w, &http.Request{
URL: &url.URL{Path: path},
Method: "GET",
Method: method,
Header: http.Header{
"Accept-Encoding": []string{acceptEncoding},
},
Expand All @@ -36,7 +36,7 @@ func TestCompressHandlerWithGzipOnInScopeEndpoints(t *testing.T) {
endpoints := []string{"/v0/data", "/v1/data", "/v1/compile"}
for _, endpoint := range endpoints {
w := httptest.NewRecorder()
executeRequest(w, endpoint, gzipEncoding)
executeRequest(w, endpoint, "POST", gzipEncoding)
contentEncodingValue := w.Result().Header.Get("Content-Encoding")
if contentEncodingValue != gzipEncoding {
t.Errorf("wrong content encoding, got %q want %q", contentEncodingValue, gzipEncoding)
Expand All @@ -55,7 +55,7 @@ func TestCompressHandlerWithGzipOnInScopeEndpoints(t *testing.T) {

func TestHandlerOnEndpointsWithoutCompression(t *testing.T) {
w := httptest.NewRecorder()
executeRequest(w, "/metrics", gzipEncoding)
executeRequest(w, "/metrics", "GET", gzipEncoding)
contentEncodingValue := w.Result().Header.Get("Content-Encoding")
if contentEncodingValue != "" {
t.Errorf("wrong content encoding, got %q want %q", contentEncodingValue, gzipEncoding)
Expand Down

0 comments on commit 07eb5dd

Please sign in to comment.