这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions docs/graphql/manual/deployment/compression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ HTTP Compression

The Hasura GraphQL Engine supports HTTP compression.
The server looks for the ``Accept-Encoding`` header in request.
If the header contains ``br`` then the server uses `Brotli <https://en.wikipedia.org/wiki/Brotli>`__ compression else if the header contains
``gzip`` then the server uses `Gzip <https://en.wikipedia.org/wiki/Gzip>`__ compression.
If both values are present then the server prefers ``Brotli`` over ``Gzip``.
Also, the server sets the ``Content-Encoding`` response header value to ``br`` for ``Brotli`` compression or ``gzip`` for ``Gzip`` compression.
If the header contains ``gzip`` then the server uses `Gzip <https://en.wikipedia.org/wiki/Gzip>`__ compression.
Also, the server sets the ``Content-Encoding`` response header value to ``gzip``.

**Only responses from "/v1/query" and "/v1/graphql" endpoints are compressed.**
1 change: 0 additions & 1 deletion server/graphql-engine.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ library

-- HTTP compression
, zlib
, brotli

exposed-modules: Hasura.Prelude
, Hasura.Logging
Expand Down
13 changes: 5 additions & 8 deletions server/src-lib/Hasura/Server/Compression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ where

import Hasura.Prelude

import Hasura.Server.Utils (brHeader, gzipHeader)
import Hasura.Server.Utils (gzipHeader)

import qualified Codec.Compression.Brotli as BR
import qualified Codec.Compression.GZip as GZ
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import qualified Network.HTTP.Types.Header as NH

data CompressionType
= CTGZip
| CTBrotli
deriving (Show, Eq)

compressionTypeToTxt :: CompressionType -> T.Text
compressionTypeToTxt CTGZip = "gzip"
compressionTypeToTxt CTBrotli = "brotli"

compressResponse
:: NH.RequestHeaders
Expand All @@ -31,14 +28,14 @@ compressResponse
compressResponse reqHeaders unCompressedResp =
let compressionTypeM = getRequestedCompression reqHeaders
appendCompressionType (res, headerM) = (res, headerM, compressionTypeM)
gzipCompressionParams =
GZ.defaultCompressParams{GZ.compressLevel = GZ.compressionLevel 1}
in appendCompressionType $ case compressionTypeM of
Just CTBrotli -> (BR.compress unCompressedResp, Just brHeader)
Just CTGZip -> (GZ.compress unCompressedResp, Just gzipHeader)
Nothing -> (unCompressedResp, Nothing)
Just CTGZip -> (GZ.compressWith gzipCompressionParams unCompressedResp, Just gzipHeader)
Nothing -> (unCompressedResp, Nothing)

getRequestedCompression :: NH.RequestHeaders -> Maybe CompressionType
getRequestedCompression reqHeaders
| "br" `elem` acceptEncodingVals = Just CTBrotli
| "gzip" `elem` acceptEncodingVals = Just CTGZip
| otherwise = Nothing
where
Expand Down
15 changes: 2 additions & 13 deletions server/tests-py/test_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,16 @@ def test_gzip_compression_v1_query(self, hge_ctx):
resp = self._make_post(hge_ctx, url, q, self.gzip_header)
self._assert_gzip(resp, exp_resp)

def test_brotli_compression_graphql(self, hge_ctx):
url, q, exp_resp = self._get_config(self.dir() + '/graphql_query.yaml')
resp = self._make_post(hge_ctx, url, q, self.brotli_header)
self._assert_brotli(resp, exp_resp)

def test_brotli_compression_v1_query(self, hge_ctx):
url, q, exp_resp = self._get_config(self.dir() + '/v1_query.yaml')
resp = self._make_post(hge_ctx, url, q, self.brotli_header)
self._assert_brotli(resp, exp_resp)


# If gzip and brotli encoding are requested the server prefers brotli
def test_gzip_brotli_graphql_query(self, hge_ctx):
url, q, exp_resp = self._get_config(self.dir() + '/graphql_query.yaml')
resp = self._make_post(hge_ctx, url, q, self.gzip_brotli_header)
self._assert_brotli(resp, exp_resp)
self._assert_gzip(resp, exp_resp)

def test_gzip_brotli_v1_query(self, hge_ctx):
url, q, exp_resp = self._get_config(self.dir() + '/v1_query.yaml')
resp = self._make_post(hge_ctx, url, q, self.gzip_brotli_header)
self._assert_brotli(resp, exp_resp)
self._assert_gzip(resp, exp_resp)

@classmethod
def dir(cls):
Expand Down