include/hyperman/hm_compress.h

filter: Statementstmt 94.1% 64 / 68 Branchbran 80.8% 94 / 120 (9 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.6% 158 / 188 (9 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_future.h ↑ Summary include/hyperman/hm_core.h »
1 #ifndef HM_COMPRESS_H
2 #define HM_COMPRESS_H
3  
4 /* hm_compress.h - gzip on the way out.
5 *
6 * Why this layer and not the framework: compression is a property of the
7 * write path, and Hyperman serves apps that are not Punk. It is also the
8 * only layer that could ever compress a STREAMED body, since a framework
9 * hands psgi.streaming responses off as coderefs and never sees the bytes
10 * (streaming compression is not in this release; this layer leaves the door
11 * open).
12 *
13 * THE ANTI-COLLISION RULE, the whole contract with anything above: never
14 * touch a response that already carries a Content-Encoding. A framework
15 * serving precompressed files sets `gzip`; a route that opts out sets
16 * `identity`, which we strip before writing. That channel is a plain
17 * response header, so any PSGI framework can use it.
18 *
19 * Off by default - a server that starts compressing on upgrade is a
20 * surprise. `compress => 1` on run() turns it on.
21 *
22 * Everything is prefixed hz_ / HZ_. Included from hm_core.h after hm_win.h.
23 */
24  
25 #include "hm_parse.h" /* hm_strncasecmp: header names are case-blind */
26  
27 #ifdef HM_HAVE_ZLIB
28 #include <zlib.h>
29 #endif
30  
31 /* One MTU. Below this a gzip member's own header and trailer are a
32 * meaningful fraction of the payload and the packet count does not change,
33 * so there is nothing to win. */
34 #define HZ_MIN_LENGTH 1400
35  
36 /* Level 1, not the customary 6, and the number is measured rather than
37 * assumed. A 37KB JSON document, 2 workers / 5s / 64 conns on loopback
38 * (Punk bench/apps/bare-json40k.psgi, 2026-08-17):
39 *
40 * off 130,000 req/s 37.3 KB/req
41 * level 1 66,900 req/s 3.19 KB/req 11.7x smaller, 1.9x slower
42 * level 6 26,300 req/s 2.96 KB/req 12.6x smaller, 4.9x slower
43 *
44 * Level 6 buys 7% more compression for 2.5x the CPU. On text that repeats -
45 * which is what an API payload is - almost all of the win is in the first
46 * level, and a worker that is also serving the next request should not pay
47 * for the rest of it. `compress_level` on run() overrides for anyone whose
48 * bandwidth costs more than their CPU. */
49 #define HZ_LEVEL 1
50  
51 /* ---- Accept-Encoding ------------------------------------------------------
52 * A left-to-right walk of the header: no split, no hash, no SV. `q=0` is an
53 * explicit refusal and loses to nothing; `*` matches anything not separately
54 * refused. Runs once per request, on the request path, so it stays a scan. */
55 47 static int hz_accepts_gzip(const char *ae, size_t len) {
56 47 size_t i = 0;
57 47 int star = 0;
58 47 ▶ if (!ae || !len) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!ae || !len) return 0; 46 1 47
if (!ae || !len) return 0; 1 45 46
59 61 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 51 10 61
60 size_t start, end, j;
61 51 int refused = 0, is_star, is_gzip;
62 67 ▶ while (i < len && (ae[i] == ' ' || ae[i] == '\t' || ae[i] == ',')) i++;
Branch: 4 branches
BranchTrueFalseTotal
while (i < len && (ae[i] == ' ' || ae[i] == '\t' || ae[i] == ',')) i++; 66 1 67
while (i < len && (ae[i] == ' ' || ae[i] == '\t' || ae[i] == ',')) i++; 6 60 66
while (i < len && (ae[i] == ' ' || ae[i] == '\t' || ae[i] == ',')) i++; 1 59 60
while (i < len && (ae[i] == ' ' || ae[i] == '\t' || ae[i] == ',')) i++; 9 50 59
63 51 start = i;
64 259 ▶ while (i < len && ae[i] != ',' && ae[i] != ';') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && ae[i] != ',' && ae[i] != ';') i++; 229 30 259
while (i < len && ae[i] != ',' && ae[i] != ';') i++; 222 7 229
while (i < len && ae[i] != ',' && ae[i] != ';') i++; 208 14 222
65 51 end = i;
66 52 ▶ while (end > start && (ae[end - 1] == ' ' || ae[end - 1] == '\t')) end--; /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
while (end > start && (ae[end - 1] == ' ' || ae[end - 1] == '\t')) end--; /* # uncoverable condition left count:3 class:ignore_covered_err */ 51 1 52
while (end > start && (ae[end - 1] == ' ' || ae[end - 1] == '\t')) end--; /* # uncoverable condition left count:3 class:ignore_covered_err */ 1 50 51
while (end > start && (ae[end - 1] == ' ' || ae[end - 1] == '\t')) end--; /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 50 50
67 51 ▶ if (i < len && ae[i] == ';') { /* parameters; only q matters */
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && ae[i] == ';') { /* parameters; only q matters */ 21 30 51
if (i < len && ae[i] == ';') { /* parameters; only q matters */ 14 7 21
68 14 size_t ps = i;
69 87 ▶ while (i < len && ae[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && ae[i] != ',') i++; 76 11 87
while (i < len && ae[i] != ',') i++; 73 3 76
70 /*
71 # uncoverable branch false class:ignore_covered_err
72 # uncoverable condition right count:1 class:ignore_covered_err */
73 29 ▶ for (j = ps; j + 1 < i; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = ps; j + 1 < i; j++) { 29 0 29
74 29 ▶ if ((ae[j] | 32) != 'q') continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((ae[j] | 32) != 'q') continue; 15 14 29
75 /*
76 # uncoverable condition right count:1 class:ignore_covered_err
77 # uncoverable condition left count:2 class:ignore_covered_err */
78 28 ▶ while (j + 1 < i && (ae[j + 1] == ' ' || ae[j + 1] == '=')) j++;
Branch: 3 branches
BranchTrueFalseTotal
while (j + 1 < i && (ae[j + 1] == ' ' || ae[j + 1] == '=')) j++; 28 0 28
while (j + 1 < i && (ae[j + 1] == ' ' || ae[j + 1] == '=')) j++; 0 28 28
while (j + 1 < i && (ae[j + 1] == ' ' || ae[j + 1] == '=')) j++; 14 14 28
79 14 ▶ if (j + 1 < i && ae[j + 1] == '0') { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (j + 1 < i && ae[j + 1] == '0') { /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 0 14
if (j + 1 < i && ae[j + 1] == '0') { /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 2 14
80 12 size_t k = j + 2;
81 12 int nonzero = 0;
82 12 ▶ if (k < i && ae[k] == '.') /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (k < i && ae[k] == '.') /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 7 12
if (k < i && ae[k] == '.') /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
83 /*
84 # uncoverable condition right count:2 class:ignore_covered_err
85 # uncoverable condition right count:3 class:ignore_covered_err */
86 14 ▶ for (k++; k < i && ae[k] >= '0' && ae[k] <= '9'; k++)
Branch: 3 branches
BranchTrueFalseTotal
for (k++; k < i && ae[k] >= '0' && ae[k] <= '9'; k++) 9 5 14
for (k++; k < i && ae[k] >= '0' && ae[k] <= '9'; k++) 9 0 9
for (k++; k < i && ae[k] >= '0' && ae[k] <= '9'; k++) 9 0 9
87 9 ▶ if (ae[k] != '0') nonzero = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ae[k] != '0') nonzero = 1; 3 6 9
88 12 ▶ if (!nonzero) refused = 1; /* q=0, q=0.0, q=0.000 */
Branch: 1 branch
BranchTrueFalseTotal
if (!nonzero) refused = 1; /* q=0, q=0.0, q=0.000 */ 9 3 12
89 }
90 14 break;
91 }
92 }
93 51 ▶ is_star = (end - start == 1 && ae[start] == '*'); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
is_star = (end - start == 1 && ae[start] == '*'); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 46 51
is_star = (end - start == 1 && ae[start] == '*'); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
94 102 is_gzip = (end - start == 4
95 51 ▶ && hm_strncasecmp(ae + start, "gzip", 4) == 0); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
&& hm_strncasecmp(ae + start, "gzip", 4) == 0); /* # uncoverable condition right count:2 class:ignore_covered_err */ 35 16 51
&& hm_strncasecmp(ae + start, "gzip", 4) == 0); /* # uncoverable condition right count:2 class:ignore_covered_err */ 35 0 35
96 51 ▶ if (is_gzip) return !refused; /* named: it decides */
Branch: 1 branch
BranchTrueFalseTotal
if (is_gzip) return !refused; /* named: it decides */ 35 16 51
97 16 ▶ if (is_star && !refused) star = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (is_star && !refused) star = 1; 5 11 16
if (is_star && !refused) star = 1; 3 2 5
98 }
99 10 return star;
100 }
101  
102 /* ---- the compressible-type allowlist --------------------------------------
103 * An allowlist, not a denylist: a new binary media type must not become
104 * compressible by default just because nobody thought to exclude it. The
105 * match is on the type up to any ';charset=...' parameter. */
106 8 static int hz_type_ok(const char *ct, size_t len) {
107 static const char *const HZ_TYPES[] = {
108 "text/", /* prefix match: html, css, plain, ... */
109 "application/json",
110 "application/javascript",
111 "application/xml",
112 "application/x-ndjson",
113 "application/manifest+json",
114 "image/svg+xml",
115 NULL
116 };
117 8 size_t i, n = len;
118 int k;
119 /*
120 # uncoverable branch true class:ignore_covered_err
121 # uncoverable condition left count:1 class:ignore_covered_err
122 # uncoverable condition left count:2 class:ignore_covered_err */
123 8 ▶ if (!ct || !len) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!ct || !len) return 0; 8 0 8
if (!ct || !len) return 0; 0 8 8
124 93 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 85 8 93
125 /*
126 # uncoverable branch true class:ignore_covered_err
127 # uncoverable condition left count:1 class:ignore_covered_err */
128 85 ▶ if (ct[i] == ';') { n = i; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (ct[i] == ';') { n = i; break; } 0 85 85
129 /*
130 # uncoverable branch true
131 # uncoverable condition left count:1 class:ignore_covered_err
132 # uncoverable condition right count:1 class:ignore_covered_err
133 # uncoverable condition left count:2 class:ignore_covered_err
134 # uncoverable condition left count:3 class:ignore_covered_err */
135 8 ▶ while (n && (ct[n - 1] == ' ' || ct[n - 1] == '\t')) n--;
Branch: 3 branches
BranchTrueFalseTotal
while (n && (ct[n - 1] == ' ' || ct[n - 1] == '\t')) n--; 8 0 8
while (n && (ct[n - 1] == ' ' || ct[n - 1] == '\t')) n--; 0 8 8
while (n && (ct[n - 1] == ' ' || ct[n - 1] == '\t')) n--; 0 8 8
136  
137 16 ▶ for (k = 0; HZ_TYPES[k]; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; HZ_TYPES[k]; k++) { 15 1 16
138 15 size_t tl = strlen(HZ_TYPES[k]);
139 15 ▶ if (HZ_TYPES[k][tl - 1] == '/') { /* prefix form */
Branch: 1 branch
BranchTrueFalseTotal
if (HZ_TYPES[k][tl - 1] == '/') { /* prefix form */ 8 7 15
140 8 ▶ if (n >= tl && hm_strncasecmp(ct, HZ_TYPES[k], tl) == 0) return 1; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (n >= tl && hm_strncasecmp(ct, HZ_TYPES[k], tl) == 0) return 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ 8 0 8
if (n >= tl && hm_strncasecmp(ct, HZ_TYPES[k], tl) == 0) return 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 2 8
141 7 ▶ } else if (n == tl && hm_strncasecmp(ct, HZ_TYPES[k], tl) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
} else if (n == tl && hm_strncasecmp(ct, HZ_TYPES[k], tl) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 6 7
} else if (n == tl && hm_strncasecmp(ct, HZ_TYPES[k], tl) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
142 1 return 1;
143 }
144 }
145 /* the structured-suffix families: anything +json or +xml */
146 /*
147 # uncoverable branch true class:ignore_covered_err
148 # uncoverable condition left count:1 class:ignore_covered_err
149 # uncoverable condition right count:1 class:ignore_covered_err
150 # uncoverable condition left count:2 class:ignore_covered_err */
151 1 ▶ if (n > 5 && hm_strncasecmp(ct + n - 5, "+json", 5) == 0) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (n > 5 && hm_strncasecmp(ct + n - 5, "+json", 5) == 0) return 1; 1 0 1
if (n > 5 && hm_strncasecmp(ct + n - 5, "+json", 5) == 0) return 1; 0 1 1
152 /*
153 # uncoverable branch true class:ignore_covered_err
154 # uncoverable condition left count:1 class:ignore_covered_err
155 # uncoverable condition right count:1 class:ignore_covered_err
156 # uncoverable condition left count:2 class:ignore_covered_err */
157 1 ▶ if (n > 4 && hm_strncasecmp(ct + n - 4, "+xml", 4) == 0) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (n > 4 && hm_strncasecmp(ct + n - 4, "+xml", 4) == 0) return 1; 1 0 1
if (n > 4 && hm_strncasecmp(ct + n - 4, "+xml", 4) == 0) return 1; 0 1 1
158 1 return 0;
159 }
160  
161 /* ---- the compressor -------------------------------------------------------
162 * Deflate src into a freshly allocated buffer with a gzip wrapper
163 * (windowBits 15 or 16). Returns 1 with the out and olen params owned by
164 * the caller
165 * (Safefree), or 0 - without zlib, on any error, or when the result is not
166 * SMALLER than the input. An incompressible body must go out identity, not
167 * larger: a client that asked for gzip and received more bytes than the
168 * plain form would have cost is strictly worse off. */
169 9 static int hz_gzip(const char *src, size_t len, int level,
170 char **out, size_t *olen) {
171 #ifdef HM_HAVE_ZLIB
172 z_stream s;
173 uLong bound;
174 char *buf;
175  
176 /*
177 # uncoverable branch true class:ignore_covered_err
178 # uncoverable condition left count:1 class:ignore_covered_err
179 # uncoverable condition left count:2 class:ignore_covered_err */
180 9 ▶ if (!src || !len) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!src || !len) return 0; 9 0 9
if (!src || !len) return 0; 0 9 9
181 9 memset(&s, 0, sizeof s);
182 /*
183 # uncoverable branch true
184 # uncoverable condition left count:1 class:ignore_covered_err */
185 9 ▶ if (deflateInit2(&s, level, Z_DEFLATED, 15 | 16, 8,
Branch: 1 branch
BranchTrueFalseTotal
if (deflateInit2(&s, level, Z_DEFLATED, 15 | 16, 8, 0 9 9
186 Z_DEFAULT_STRATEGY) != Z_OK)
187 0 return 0; /* # uncoverable statement */
188 9 bound = deflateBound(&s, (uLong)len) + 18; /* + gzip header/trailer */
189 9 Newx(buf, (size_t)bound, char);
190 9 s.next_in = (Bytef *)(void *)src;
191 9 s.avail_in = (uInt)len;
192 9 s.next_out = (Bytef *)buf;
193 9 s.avail_out = (uInt)bound;
194 /*
195 # uncoverable branch true
196 # uncoverable condition left count:1 class:ignore_covered_err */
197 9 ▶ if (deflate(&s, Z_FINISH) != Z_STREAM_END) {
Branch: 1 branch
BranchTrueFalseTotal
if (deflate(&s, Z_FINISH) != Z_STREAM_END) { 0 9 9
198 0 deflateEnd(&s); /* # uncoverable statement */
199 0 Safefree(buf); /* # uncoverable statement */
200 0 return 0; /* # uncoverable statement */
201 }
202 9 *olen = (size_t)s.total_out;
203 9 deflateEnd(&s);
204 9 ▶ if (*olen >= len) { Safefree(buf); return 0; } /* not worth it */
Branch: 1 branch
BranchTrueFalseTotal
if (*olen >= len) { Safefree(buf); return 0; } /* not worth it */ 1 8 9
205 8 *out = buf;
206 8 return 1;
207 #else
208 (void)src; (void)len; (void)level; (void)out; (void)olen;
209 return 0;
210 #endif
211 }
212  
213 3 static int hz_available(void) {
214 #ifdef HM_HAVE_ZLIB
215 3 return 1;
216 #else
217 return 0;
218 #endif
219 }
220  
221 #endif /* HM_COMPRESS_H */
« include/hyperman/hm_future.h ↑ Summary include/hyperman/hm_core.h »