header.c

filter: Statementstmt 89.7% 70 / 78 Branchbran 82.8% 58 / 70 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.4% 128 / 148 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« hlist.c ↑ Summary XSHeaders.xs »
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "glog.h"
6 #include "gmem.h"
7 #include "header.h"
8  
9 #define HEADER_IS_CLASS(h, v) (h->order >= v && h->order < (v+100))
10  
11 #define HEADER_IS_GENERAL(h) HEADER_IS_CLASS(h, HEADER_TYPE_GENERAL)
12 #define HEADER_IS_REQUEST(h) HEADER_IS_CLASS(h, HEADER_TYPE_REQUEST)
13 #define HEADER_IS_RESPONSE(h) HEADER_IS_CLASS(h, HEADER_TYPE_RESPONSE)
14 #define HEADER_IS_ENTITY(h) HEADER_IS_CLASS(h, HEADER_TYPE_ENTITY)
15  
16 /*
17 * List of all standard headers, with the correct ordering for them.
18 */
19 static Header standard_headers[] = {
20 /* general headers */
21 { 100, "Cache-Control" },
22 { 101, "Connection" },
23 { 102, "Date" },
24 { 103, "Pragma" },
25 { 104, "Trailer" },
26 { 105, "Transfer-Encoding" },
27 { 106, "Upgrade" },
28 { 107, "Via" },
29 { 108, "Warning" },
30  
31 /* request headers */
32 { 200, "Accept" },
33 { 201, "Accept-Charset" },
34 { 202, "Accept-Encoding" },
35 { 203, "Accept-Language" },
36 { 204, "Authorization" },
37 { 205, "Expect" },
38 { 206, "From" },
39 { 207, "Host" },
40 { 208, "If-Match" },
41 { 209, "If-Modified-Since" },
42 { 210, "If-None-Match" },
43 { 211, "If-Range" },
44 { 212, "If-Unmodified-Since" },
45 { 213, "Max-Forwards" },
46 { 214, "Proxy-Authorization" },
47 { 215, "Range" },
48 { 216, "Referer" },
49 { 217, "TE" },
50 { 218, "User-Agent" },
51  
52 /* response headers */
53 { 300, "Accept-Ranges" },
54 { 301, "Age" },
55 { 302, "ETag" },
56 { 303, "Location" },
57 { 304, "Proxy-Authenticate" },
58 { 305, "Retry-After" },
59 { 306, "Server" },
60 { 307, "Vary" },
61 { 308, "WWW-Authenticate" },
62  
63 /* entity headers */
64 { 400, "Allow" },
65 { 401, "Content-Encoding" },
66 { 402, "Content-Language" },
67 { 403, "Content-Length" },
68 { 404, "Content-Location" },
69 { 405, "Content-MD5" },
70 { 406, "Content-Range" },
71 { 407, "Content-Type" },
72 { 408, "Expires" },
73 { 409, "Last-Modified" },
74 };
75 static int standard_headers_size = sizeof(standard_headers) / sizeof(standard_headers[0]);
76  
77 static int normalise(char* buf, const char* str);
78  
79  
80 92 Header* header_create(const char* name) {
81 int l;
82 92 Header* h = 0;
83 92 GMEM_NEW(h, Header*, sizeof(Header));
84 92 h->order = HEADER_TYPE_NONE;
85 92 l = strlen(name) + 1;
86 92 GMEM_NEW(h->name, char*, l);
87 92 normalise(h->name, name);
88 GLOG(("=C= Created header [%s] => [%s]", name, h->name));
89 92 return h;
90 }
91  
92 46 Header* header_clone(Header* header) {
93 Header *h;
94 46 ▶ if (header->order != HEADER_TYPE_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (header->order != HEADER_TYPE_NONE) { 11 35 46
95 11 return header;
96 }
97  
98 35 h = header_create(header->name);
99 35 return h;
100 }
101  
102 177 void header_destroy(Header* header) {
103 177 ▶ if (header->order != HEADER_TYPE_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (header->order != HEADER_TYPE_NONE) { 85 92 177
104 85 return;
105 }
106 92 GMEM_DELSTR(header->name, -1);
107 92 GMEM_DEL(header, Header*, sizeof(Header));
108 }
109  
110 #define CONVERT(c) c == '_' ? '-' : isupper(c) ? tolower(c) : c
111  
112 /* TODO: this could probably made faster if we precomputed the CONVERTed */
113 /* values instead of doing it over and over again... */
114 5725 int header_compare(const char* n1, const char* n2) {
115 5725 int p = 0;
116 char c1;
117 char c2;
118 while (1) {
119 9431 ▶ if (n1[p] == '\0' || n2[p] == '\0') {
Branch: 2 branches
BranchTrueFalseTotal
if (n1[p] == '\0' || n2[p] == '\0') { 9146 285 9431
if (n1[p] == '\0' || n2[p] == '\0') { 9139 7 9146
120 break;
121 }
122 9139 ▶ c1 = CONVERT(n1[p]);
Branch: 2 branches
BranchTrueFalseTotal
c1 = CONVERT(n1[p]); 9076 63 9139
c1 = CONVERT(n1[p]); 1817 7259 9076
123 9139 ▶ c2 = CONVERT(n2[p]);
Branch: 2 branches
BranchTrueFalseTotal
c2 = CONVERT(n2[p]); 9139 0 9139
c2 = CONVERT(n2[p]); 6021 3118 9139
124 9139 ▶ if (c1 < c2) {
Branch: 1 branch
BranchTrueFalseTotal
if (c1 < c2) { 2791 6348 9139
125 2791 return -1;
126 }
127 6348 ▶ if (c1 > c2) {
Branch: 1 branch
BranchTrueFalseTotal
if (c1 > c2) { 2642 3706 6348
128 2642 return +1;
129 }
130 3706 ++p;
131 }
132 292 ▶ if (n1[p] == '\0' && n2[p] != '\0') {
Branch: 2 branches
BranchTrueFalseTotal
if (n1[p] == '\0' && n2[p] != '\0') { 285 7 292
if (n1[p] == '\0' && n2[p] != '\0') { 56 229 285
133 56 return -1;
134 }
135 236 ▶ if (n1[p] != '\0' && n2[p] == '\0') {
Branch: 2 branches
BranchTrueFalseTotal
if (n1[p] != '\0' && n2[p] == '\0') { 7 229 236
if (n1[p] != '\0' && n2[p] == '\0') { 7 0 7
136 7 return +1;
137 }
138 229 return 0;
139 }
140  
141 5700 int header_matches_type_or_name(const Header* h, int type, const char* name) {
142 int cmp;
143 5700 ▶ if (type != HEADER_TYPE_NONE && !HEADER_IS_CLASS(h, type)) {
Branch: 3 branches
BranchTrueFalseTotal
if (type != HEADER_TYPE_NONE && !HEADER_IS_CLASS(h, type)) { 0 5700 5700
if (type != HEADER_TYPE_NONE && !HEADER_IS_CLASS(h, type)) { 0 0 0
if (type != HEADER_TYPE_NONE && !HEADER_IS_CLASS(h, type)) { 0 0 0
144 0 return 0;
145 }
146 5700 cmp = header_compare(name, h->name);
147 /* GLOG(("=C= compare [%s] & [%s] => %d", name, h->name, cmp)); */
148 5700 return cmp == 0;
149 }
150  
151 131 Header* header_lookup_standard(int type, const char* name) {
152 int j;
153 5032 ▶ for (j = 0; j < standard_headers_size; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < standard_headers_size; ++j) { 4975 57 5032
154 4975 Header* h = &standard_headers[j];
155 4975 ▶ if (header_matches_type_or_name(h, type, name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (header_matches_type_or_name(h, type, name)) { 74 4901 4975
156 74 return h;
157 }
158 }
159  
160 57 return 0;
161 }
162  
163 0 void header_dump(const Header* h, FILE* fp) {
164 0 fprintf(fp, "[%p", h);
165 0 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 0 0 0
166 0 fprintf(fp, "|%3d|%s", h->order, h->name);
167 }
168 0 fprintf(fp, "]\n");
169 0 fflush(fp);
170 0 }
171  
172 34 int header_is_entity(const Header* h) {
173 34 const char* start = "content-";
174 int j;
175  
176 34 ▶ if (HEADER_IS_ENTITY(h)) {
Branch: 2 branches
BranchTrueFalseTotal
if (HEADER_IS_ENTITY(h)) { 27 7 34
if (HEADER_IS_ENTITY(h)) { 16 11 27
177 GLOG(("=C= header [%s] is entity (QUICK)", h->name));
178 16 return 1;
179 }
180  
181 18 ▶ if (HEADER_IS_GENERAL(h) ||
Branch: 2 branches
BranchTrueFalseTotal
if (HEADER_IS_GENERAL(h) || 18 0 18
if (HEADER_IS_GENERAL(h) || 16 2 18
182 16 ▶ HEADER_IS_REQUEST(h) ||
Branch: 2 branches
BranchTrueFalseTotal
HEADER_IS_REQUEST(h) || 16 0 16
HEADER_IS_REQUEST(h) || 14 2 16
183 14 ▶ HEADER_IS_RESPONSE(h)) {
Branch: 2 branches
BranchTrueFalseTotal
HEADER_IS_RESPONSE(h)) { 14 0 14
HEADER_IS_RESPONSE(h)) { 3 11 14
184 GLOG(("=C= header [%s] is not entity (QUICK)", h->name));
185 7 return 0;
186 }
187  
188 57 ▶ for (j = 0; start[j] != 0; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; start[j] != 0; ++j) { 53 4 57
189 53 ▶ if (h->name[j] == '\0') {
Branch: 1 branch
BranchTrueFalseTotal
if (h->name[j] == '\0') { 2 51 53
190 GLOG(("=C= header [%s] is not entity (EOS)", h->name));
191 2 return 0;
192 }
193 51 ▶ if (tolower(h->name[j]) != start[j]) {
Branch: 1 branch
BranchTrueFalseTotal
if (tolower(h->name[j]) != start[j]) { 5 46 51
194 GLOG(("=C= header [%s] is not entity (DIFF)", h->name));
195 5 return 0;
196 }
197 }
198  
199 GLOG(("=C= header [%s] is entity (CMP)", h->name));
200 4 return 1;
201 }
202  
203 92 static int normalise(char* buf, const char* str) {
204 92 int word = 0;
205 92 int j = 0;
206 474 ▶ for (j = 0; str[j] != '\0'; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; str[j] != '\0'; ++j) { 382 92 474
207 382 ▶ if (isalpha(str[j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (isalpha(str[j])) { 362 20 382
208 362 ▶ if (word) {
Branch: 1 branch
BranchTrueFalseTotal
if (word) { 256 106 362
209 256 buf[j] = tolower(str[j]);
210 } else {
211 106 buf[j] = toupper(str[j]);
212 106 word = 1;
213 }
214 } else {
215 20 ▶ buf[j] = str[j] == '_' ? '-' : str[j];
Branch: 1 branch
BranchTrueFalseTotal
buf[j] = str[j] == '_' ? '-' : str[j]; 15 5 20
216 20 word = 0;
217 }
218 }
219 92 buf[j] = '\0';
220 92 return j;
221 }
« hlist.c ↑ Summary XSHeaders.xs »