libhttppp ..
Loading...
Searching...
No Matches
httpcompression.h
1/*******************************************************************************
2Copyright (c) 2026, Jan Koester jan.koester@gmx.net
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the <organization> nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*******************************************************************************/
27
28#pragma once
29
30#include <string>
31#include <vector>
32
33namespace libhttppp {
34namespace compression {
35
36// None: no encoding (or "identity") -- send as-is.
37// Gzip/Brotli: an encoding this module can itself produce and consume.
38// Other: some encoding present that isn't gzip or brotli (e.g. "deflate", "zstd", or a
39// comma-joined list) -- opaque to this module, callers must leave the body untouched.
40enum class Encoding { None, Gzip, Brotli, Other };
41
42// Maps a single Content-Encoding token (case-insensitive) to Encoding.
43Encoding fromToken(const std::string &contentEncoding);
44
45// Content-Encoding token this module would send for enc ("gzip"/"br"). Only valid for
46// Gzip/Brotli.
47const char *toToken(Encoding enc);
48
49// Picks the best encoding this module can produce for a client's Accept-Encoding header,
50// honoring q-values (q=0 excludes) and "*" per RFC 7231 ยง5.3.4. Prefers Brotli over gzip
51// when both are acceptable with equal preference (better ratio at comparable CPU cost).
52// Returns Encoding::None if the header is empty/absent or nothing acceptable remains.
53Encoding negotiate(const std::string &acceptEncoding);
54
55// True if the client's Accept-Encoding header declares enc acceptable at all (q>0, via an
56// explicit token or "*"), independent of whether it's negotiate()'s top pick. Only meaningful
57// for Encoding::Gzip/Brotli -- always false otherwise. Use this before forwarding a response
58// in an encoding other than what negotiate() picked (e.g. a decode-and-transcode fallback):
59// negotiate() returning a different encoding only means enc wasn't the *preferred* one, not
60// that the client can't decode it at all.
61bool accepts(const std::string &acceptEncoding, Encoding enc);
62
63// True if contentType (as returned by HttpResponse::getContentType() -- already lowercased,
64// parameters stripped) is worth spending CPU to compress. Binary/already-compressed formats
65// (images, video, archives) are excluded.
66bool isCompressible(const std::string &contentType);
67
68bool gzipCompress(const std::vector<char> &in, std::vector<char> &out, int level = 6);
69bool gzipDecompress(const std::vector<char> &in, std::vector<char> &out);
70
71bool brotliCompress(const std::vector<char> &in, std::vector<char> &out, int quality = 5);
72bool brotliDecompress(const std::vector<char> &in, std::vector<char> &out);
73
74} // namespace compression
75} // namespace libhttppp