include/hyperman/hm_trailers.h

filter: Statementstmt 90.7% 59 / 65 Branchbran 75.0% 63 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.8% 122 / 149 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/bus.xs ↑ Summary include/hyperman/hm_sa_bus_perl.h »
1 #ifndef HM_TRAILERS_H
2 #define HM_TRAILERS_H
3  
4 /* Trailer fields: header-shaped fields that follow a body.
5 *
6 * The response array stays a triple. A fourth element would not survive the
7 * middleware that rebuilds [status, headers, body], so trailers travel only
8 * through the two calls that end a body explicitly: Hyperman::Writer->close
9 * and the ABI's stream_close_trailers. Both hand their list to
10 * hm_trailers_build below, which is the one place the rules live.
11 *
12 * gRPC is the consumer. Its call status is a trailer and nothing else, since
13 * the HTTP status of a gRPC reply is 200 whatever happened to the call.
14 *
15 * A list is judged whole, before a byte is sent:
16 *
17 * refused, with a message and the body left open: not an array reference,
18 * an odd length, an empty or undefined name, a name with a character
19 * outside the RFC 9110 token set (which excludes the ':' of an h2
20 * pseudo-header), an undefined value, a control character other than tab
21 * in a value. On HTTP/1.1 a trailer is written raw after the last chunk,
22 * so CR or LF in one would split the response.
23 *
24 * dropped quietly, as the same names already are from a header list:
25 * connection-level fields, which HTTP/2 forbids outright, and the framing
26 * and routing fields a receiver must never learn from a trailer. A proxy
27 * that merged a trailing content-length into the header block would frame
28 * the next message by it.
29 *
30 * rewritten: names lowercased for HTTP/2, values trimmed of surrounding
31 * blanks since every parser discards them anyway. */
32  
33 /* RFC 9110 tchar: ALPHA, DIGIT and the fifteen punctuation marks. */
34 64 static int hm_tchar_ok(unsigned char ch) {
35 64 ▶ if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
Branch: 4 branches
BranchTrueFalseTotal
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') 50 14 64
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') 0 50 50
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') 4 10 14
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') 0 4 4
36 10 ▶ || (ch >= '0' && ch <= '9'))
Branch: 2 branches
BranchTrueFalseTotal
|| (ch >= '0' && ch <= '9')) 1 9 10
|| (ch >= '0' && ch <= '9')) 0 1 1
37 54 return 1;
38 10 ▶ switch (ch) {
Branch: 1 branch
BranchTrueFalseTotal
switch (ch) { 6 4 10
39 6 case '!': case '#': case '$': case '%': case '&': case '\'':
40 case '*': case '+': case '-': case '.': case '^': case '_':
41 case '`': case '|': case '~':
42 6 return 1;
43 4 default:
44 4 return 0;
45 }
46 }
47  
48 /* The names a trailer section never carries, matched in any case. */
49 9 static int hm_trailer_banned(const char *n, STRLEN l) {
50 9 switch (l) {
51 2 case 2: return hm_strncasecmp(n, "te", 2) == 0;
52 0 case 4: return hm_strncasecmp(n, "host", 4) == 0;
53 0 case 7: return hm_strncasecmp(n, "upgrade", 7) == 0
54 0 ▶ || hm_strncasecmp(n, "trailer", 7) == 0;
Branch: 2 branches
BranchTrueFalseTotal
|| hm_strncasecmp(n, "trailer", 7) == 0; 0 0 0
|| hm_strncasecmp(n, "trailer", 7) == 0; 0 0 0
55 2 case 10: return hm_strncasecmp(n, "connection", 10) == 0
56 2 ▶ || hm_strncasecmp(n, "keep-alive", 10) == 0;
Branch: 2 branches
BranchTrueFalseTotal
|| hm_strncasecmp(n, "keep-alive", 10) == 0; 0 2 2
|| hm_strncasecmp(n, "keep-alive", 10) == 0; 0 0 0
57 0 case 14: return hm_strncasecmp(n, "content-length", 14) == 0;
58 0 case 16: return hm_strncasecmp(n, "proxy-connection", 16) == 0;
59 0 case 17: return hm_strncasecmp(n, "transfer-encoding", 17) == 0;
60 5 default: return 0;
61 }
62 }
63  
64 /* A refusal: the partial list goes, the message becomes the caller's
65 * mortal, and NULL is the result. */
66 13 static AV *hm_trailers_refuse(pTHX_ AV *partial, SV **err, SV *msg) {
67 13 ▶ if (partial) SvREFCNT_dec((SV *)partial);
Branch: 1 branch
BranchTrueFalseTotal
if (partial) SvREFCNT_dec((SV *)partial); 10 3 13
68 13 *err = sv_2mortal(msg);
69 13 return NULL;
70 }
71  
72 /* Check a trailer list and build the one that goes to the wire.
73 *
74 * `given` is whatever the application passed. The result is a new AV the
75 * caller owns, holding lowercased names and trimmed values in order, or
76 * NULL. NULL with *err clear means there is nothing to send: no list, an
77 * empty one, or one made only of banned names, and the body then ends as a
78 * plain close ends it. NULL with *err set is a refusal, *err being a mortal
79 * SV that says what was wrong. */
80 47 static AV *hm_trailers_build(pTHX_ SV *given, SV **err) {
81 AV *in, *out;
82 SSize_t i, n;
83 47 *err = NULL;
84 47 ▶ if (!given || !SvOK(given)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!given || !SvOK(given)) return NULL; 23 24 47
if (!given || !SvOK(given)) return NULL; 2 21 23
85 21 ▶ if (!SvROK(given) || SvTYPE(SvRV(given)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(given) || SvTYPE(SvRV(given)) != SVt_PVAV) 20 1 21
if (!SvROK(given) || SvTYPE(SvRV(given)) != SVt_PVAV) 1 19 20
86 2 return hm_trailers_refuse(aTHX_ NULL, err, newSVpvs(
87 "trailers must be an array reference of name => value pairs"));
88 19 in = (AV *)SvRV(given);
89 19 n = av_len(in) + 1;
90 19 ▶ if (n & 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n & 1) 1 18 19
91 1 return hm_trailers_refuse(aTHX_ NULL, err, newSVpvs(
92 "trailers must be name => value pairs (an odd number of "
93 "elements was given)"));
94 18 out = newAV();
95 27 ▶ for (i = 0; i < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i += 2) { 19 8 27
96 19 SV **k = av_fetch(in, i, 0);
97 19 SV **v = av_fetch(in, i + 1, 0);
98 STRLEN kl, vl, j, from, to;
99 const char *ks, *vs;
100 SV *name;
101 char *lc;
102 37 ▶ if (!(k && *k && SvOK(*k)) || !(ks = SvPV(*k, kl), kl))
Branch: 4 branches
BranchTrueFalseTotal
if (!(k && *k && SvOK(*k)) || !(ks = SvPV(*k, kl), kl)) 19 0 19
if (!(k && *k && SvOK(*k)) || !(ks = SvPV(*k, kl), kl)) 19 0 19
if (!(k && *k && SvOK(*k)) || !(ks = SvPV(*k, kl), kl)) 18 1 19
if (!(k && *k && SvOK(*k)) || !(ks = SvPV(*k, kl), kl)) 1 17 18
103 10 return hm_trailers_refuse(aTHX_ out, err,
104 newSVpvs("a trailer name is empty"));
105 77 ▶ for (j = 0; j < kl; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < kl; j++) 64 13 77
106 64 ▶ if (!hm_tchar_ok((unsigned char)ks[j]))
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_tchar_ok((unsigned char)ks[j])) 4 60 64
107 4 return hm_trailers_refuse(aTHX_ out, err, newSVpvf(
108 "trailer name '%" SVf "' is not a valid field name",
109 SVfARG(*k)));
110 13 ▶ if (!(v && *v && SvOK(*v)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(v && *v && SvOK(*v))) 13 0 13
if (!(v && *v && SvOK(*v))) 13 0 13
if (!(v && *v && SvOK(*v))) 1 12 13
111 1 return hm_trailers_refuse(aTHX_ out, err, newSVpvf(
112 "trailer '%" SVf "' has an undefined value", SVfARG(*k)));
113 12 vs = SvPV(*v, vl);
114 12 from = 0;
115 12 to = vl;
116 12 ▶ while (from < to && (vs[from] == ' ' || vs[from] == '\t')) from++;
Branch: 3 branches
BranchTrueFalseTotal
while (from < to && (vs[from] == ' ' || vs[from] == '\t')) from++; 12 0 12
while (from < to && (vs[from] == ' ' || vs[from] == '\t')) from++; 0 12 12
while (from < to && (vs[from] == ' ' || vs[from] == '\t')) from++; 0 12 12
117 12 ▶ while (to > from && (vs[to - 1] == ' ' || vs[to - 1] == '\t')) to--;
Branch: 3 branches
BranchTrueFalseTotal
while (to > from && (vs[to - 1] == ' ' || vs[to - 1] == '\t')) to--; 12 0 12
while (to > from && (vs[to - 1] == ' ' || vs[to - 1] == '\t')) to--; 0 12 12
while (to > from && (vs[to - 1] == ' ' || vs[to - 1] == '\t')) to--; 0 12 12
118 37 ▶ for (j = from; j < to; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = from; j < to; j++) { 28 9 37
119 28 unsigned char ch = (unsigned char)vs[j];
120 28 ▶ if ((ch < 0x20 && ch != '\t') || ch == 0x7f)
Branch: 3 branches
BranchTrueFalseTotal
if ((ch < 0x20 && ch != '\t') || ch == 0x7f) 3 25 28
if ((ch < 0x20 && ch != '\t') || ch == 0x7f) 1 2 3
if ((ch < 0x20 && ch != '\t') || ch == 0x7f) 1 25 26
121 3 return hm_trailers_refuse(aTHX_ out, err, newSVpvf(
122 "trailer '%" SVf "' has a control character in its value "
123 "(CR, LF and NUL would split the response)",
124 SVfARG(*k)));
125 }
126 9 ▶ if (hm_trailer_banned(ks, kl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_trailer_banned(ks, kl)) continue; 3 6 9
127 6 name = newSVpvn(ks, kl);
128 6 lc = SvPVX(name);
129 27 ▶ for (j = 0; j < kl; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < kl; j++) 21 6 27
130 21 ▶ if (lc[j] >= 'A' && lc[j] <= 'Z') lc[j] = (char)(lc[j] + 32);
Branch: 2 branches
BranchTrueFalseTotal
if (lc[j] >= 'A' && lc[j] <= 'Z') lc[j] = (char)(lc[j] + 32); 19 2 21
if (lc[j] >= 'A' && lc[j] <= 'Z') lc[j] = (char)(lc[j] + 32); 0 19 19
131 6 av_push(out, name);
132 6 av_push(out, newSVpvn(vs + from, to - from));
133 }
134 8 ▶ if (av_len(out) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(out) < 0) { 3 5 8
135 3 SvREFCNT_dec((SV *)out);
136 3 return NULL;
137 }
138 5 return out;
139 }
140  
141 #endif /* HM_TRAILERS_H */
« xs/bus.xs ↑ Summary include/hyperman/hm_sa_bus_perl.h »