|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2024 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package connection |
| 22 | + |
| 23 | +import ( |
| 24 | + "compress/gzip" |
| 25 | + "compress/zlib" |
| 26 | + "io" |
| 27 | + |
| 28 | + "github.com/pkg/errors" |
| 29 | + |
| 30 | + "github.com/arangodb/go-driver/v2/log" |
| 31 | +) |
| 32 | + |
| 33 | +type Compression interface { |
| 34 | + ApplyRequestHeaders(r Request) |
| 35 | + ApplyRequestCompression(r *httpRequest, rootWriter io.Writer) (io.WriteCloser, error) |
| 36 | +} |
| 37 | + |
| 38 | +type compression struct { |
| 39 | + config *CompressionConfig |
| 40 | +} |
| 41 | + |
| 42 | +func newCompression(config *CompressionConfig) Compression { |
| 43 | + return &compression{ |
| 44 | + config: config, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func (g compression) ApplyRequestHeaders(r Request) { |
| 49 | + if g.config != nil && g.config.ResponseCompressionEnabled { |
| 50 | + if g.config.CompressionType == "gzip" { |
| 51 | + r.AddHeader("Accept-Encoding", "gzip") |
| 52 | + } else if g.config.CompressionType == "deflate" { |
| 53 | + r.AddHeader("Accept-Encoding", "deflate") |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (g compression) ApplyRequestCompression(r *httpRequest, rootWriter io.Writer) (io.WriteCloser, error) { |
| 59 | + config := g.config |
| 60 | + |
| 61 | + if config != nil && config.RequestCompressionEnabled { |
| 62 | + if config.CompressionType == "gzip" { |
| 63 | + r.headers["Content-Encoding"] = "gzip" |
| 64 | + |
| 65 | + gzipWriter, err := gzip.NewWriterLevel(rootWriter, config.RequestCompressionLevel) |
| 66 | + if err != nil { |
| 67 | + log.Errorf(err, "error creating gzip writer") |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + return gzipWriter, nil |
| 71 | + } else if config.CompressionType == "deflate" { |
| 72 | + r.headers["Content-Encoding"] = "deflate" |
| 73 | + |
| 74 | + zlibWriter, err := zlib.NewWriterLevel(rootWriter, config.RequestCompressionLevel) |
| 75 | + if err != nil { |
| 76 | + log.Errorf(err, "error creating zlib writer") |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + return zlibWriter, nil |
| 81 | + } else { |
| 82 | + return nil, errors.Errorf("unsupported compression type: %s", config.CompressionType) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return nil, nil |
| 87 | +} |
0 commit comments