-
Notifications
You must be signed in to change notification settings - Fork 6
/
headers.c
446 lines (405 loc) · 11.6 KB
/
headers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
#include "headers.h"
#include "fiftyone.h"
/* HTTP header prefix used when processing collections of parameters. */
#define HTTP_PREFIX_UPPER "HTTP_"
MAP_TYPE(HeaderID)
/**
* Counts the number of segments in a header name.
*/
static int countHeaderSegments(const char* name) {
int count = 0;
char* current = (char*)name;
char* last = current;
// Loop checking each character ensuring at least some non separator
// characters are present before counting a segment.
while (*current != '\0') {
if (*current == PSEUDO_HEADER_SEP &&
*last != PSEUDO_HEADER_SEP) {
count++;
}
last = current;
current++;
}
// If the last character was not a separator then the null terminator at
// the of the string indicates that there is a segment of valid characters
// so increase the count.
if (*last != PSEUDO_HEADER_SEP) {
count++;
}
return count;
}
/**
* Count the number of segments for all the headers.
*/
static int countAllSegments(void* state, HeadersGetMethod get) {
uint32_t count = 0, index = 0, segments;
Item name;
DataReset(&name.data);
while (get(state, index, &name) >= 0) {
// Count the number of segments.
segments = countHeaderSegments(STRING(name.data.ptr));
count += segments;
// If there are more than one segment then this is a pseudo header and
// the count should also include the full header.
if (segments > 1) {
count++;
}
COLLECTION_RELEASE(name.collection, &name);
index++;
}
return count;
}
/**
* Counts the number of headers that have more than 1 segment indicating
* they are pseudo headers.
*/
static int countPseudoHeaders(Headers* headers) {
Header* header;
int pseudoCount = 0;
for (uint32_t i = 0; i < headers->count; i++) {
header = &headers->items[i];
if (header->segments->count > 1) {
pseudoCount++;
}
}
return pseudoCount;
}
/**
* Adds the segment to the array of segments returning the character position
* for the next segment.
* If there was not enough space in the array, the exception is set.
* @param segments pre allocated array to populate
* @param start pointer to the first character of the segment string
* @param end pointer to the last character of the segment string
* @param exception set if there was not enough space in the array
* @return char* pointer to the first character of the next segment
*/
static char* addSegment(
HeaderSegmentArray* segments,
char* start,
char* end,
Exception *exception) {
if (segments->count >= segments->capacity) {
EXCEPTION_SET(POINTER_OUT_OF_BOUNDS);
return end;
}
HeaderSegment* segment = &segments->items[segments->count++];
segment->segment = start;
segment->length = end - start;
return end + 1;
}
/**
* Create the array of segments from the name string, or NULL if there are no
* segments or the memory can't be allocated.
* All headers should have at least one segment, so a result of NULL indicates
* something is wrong.
*/
static HeaderSegmentArray* createSegmentArray(const char* name) {
HeaderSegmentArray* segments;
EXCEPTION_CREATE;
int count = countHeaderSegments(name);
char* current, *last;
FIFTYONE_DEGREES_ARRAY_CREATE(
HeaderSegment,
segments,
count);
if (segments != NULL) {
current = (char*)name;
last = current;
while (*current != '\0' && EXCEPTION_OKAY) {
if (*current == PSEUDO_HEADER_SEP) {
if (current != last) {
last = addSegment(segments, last, current, exception);
}
else {
last++;
}
}
current++;
}
if (current != last && EXCEPTION_OKAY) {
last = addSegment(segments, last, current, exception);
}
if (EXCEPTION_FAILED) {
Free(segments);
return NULL;
}
}
return segments;
}
/**
* Copies the length of the source string characters to a new string array
* associated with the header provided.
*/
static bool copyHeaderName(Header* header, const char* source, size_t length) {
size_t size = length + 1;
char* name = (char*)Malloc(sizeof(char) * size);
if (name == NULL) {
return false;
}
if (memset(name, '\0', size) == NULL) {
Free(name);
return false;
}
header->name = memcpy(name, source, length);
if (header->name == NULL) {
Free(name);
return false;
}
header->nameLength = length;
return true;
}
/**
* Sets the header from the data set including segments.
*/
static bool setHeaderFromDataSet(
Header* header,
const char* name,
size_t nameLength,
HeaderID uniqueId) {
if (copyHeaderName(header, name, nameLength) == false) {
return false;
}
header->isDataSet = true;
header->uniqueId = uniqueId;
header->segments = createSegmentArray(header->name);
return header->segments != NULL;
}
/**
* Sets the header from the source header and source segment.
*/
static bool setHeaderFromSegment(Header* header, HeaderSegment* segment) {
if (copyHeaderName(header, segment->segment, segment->length) == false) {
return false;
}
header->segments = createSegmentArray(header->name);
if (header->segments == NULL) {
Free((void*)header->name);
return false;
}
header->isDataSet = false;
header->uniqueId = 0;
return true;
}
/**
* Returns the index to the header if it exits, or -1 if it doesn't.
*/
static int getHeaderIndex(Headers *headers, const char *name, size_t length) {
Header item;
uint32_t i;
if (name == NULL) {
return false;
}
for (i = 0; i < headers->count; i++) {
item = headers->items[i];
if (item.nameLength == length &&
StringCompareLength(name, item.name, length) == 0) {
return (int)i;
}
}
return -1;
}
/**
* Returns a pointer to the header if it exits, or NULL if it doesn't.
*/
static Header* getHeader(Headers* headers, const char* name, size_t length) {
int i = getHeaderIndex(headers, name, length);
if (i >= 0) {
return &headers->items[i];
}
return NULL;
}
/**
* Adds headers returned from the state and get method. The capacity of the
* headers must be sufficient for the data set headers that will be returned.
*/
static bool addHeadersFromDataSet(
void* state,
HeadersGetMethod get,
HeaderArray* headers) {
Item item;
long uniqueId;
uint32_t index = 0;
const char* name;
size_t nameLength;
DataReset(&item.data);
// Get the first name item from the data set.
while ((uniqueId = get(state, index, &item)) >= 0) {
// Only include the header if it is not zero length, has at least one
// segment, and does not already exist in the collection.
name = STRING(item.data.ptr);
nameLength = strlen(name);
if (nameLength > 0 &&
countHeaderSegments(name) > 0 &&
getHeaderIndex(headers, name, nameLength) < 0) {
// Set the next header from the data set name item.
if (setHeaderFromDataSet(
&headers->items[headers->count],
name,
nameLength,
(HeaderID)uniqueId) == false) {
return false;
}
// Release the item and update the counters.
headers->count++;
}
// Release the item from the caller.
COLLECTION_RELEASE(item.collection, &item);
// Get the next name item from the data set.
index++;
}
return true;
}
/**
* Loops through the existing headers checking the segments to ensure they are
* also included in the array of headers.
*/
static bool addHeadersFromSegments(HeaderArray* headers) {
Header *header, *existing;
HeaderSegment* segment;
uint32_t i, s;
uint32_t max = headers->count;
for (i = 0; i < max; i++) {
header = &headers->items[i];
for (s = 0; s < header->segments->count; s++) {
segment = &header->segments->items[s];
existing = getHeader(headers, segment->segment, segment->length);
if (existing == NULL) {
if (setHeaderFromSegment(
&headers->items[headers->count],
segment) == false) {
return false;
}
headers->count++;
}
}
}
return true;
}
/**
* Check if a header is a pseudo header.
*/
bool fiftyoneDegreesHeadersIsPseudo(const char *headerName) {
return strchr(headerName, PSEUDO_HEADER_SEP) != NULL;
}
fiftyoneDegreesHeaders* fiftyoneDegreesHeadersCreate(
bool expectUpperPrefixedHeaders,
void *state,
fiftyoneDegreesHeadersGetMethod get) {
Headers *headers;
// Count the number of headers and create an array with sufficient capacity
// to store all of them.
int32_t count = countAllSegments(state, get);
FIFTYONE_DEGREES_ARRAY_CREATE(
fiftyoneDegreesHeader,
headers,
count);
if (headers != NULL) {
// Set the prefixed headers flag.
headers->expectUpperPrefixedHeaders = expectUpperPrefixedHeaders;
// Add the headers from the data set.
if (addHeadersFromDataSet(state, get, headers) == false) {
HeadersFree(headers);
return NULL;
}
// Add the headers from the segments that were found from the data set.
if (addHeadersFromSegments(headers) == false) {
HeadersFree(headers);
return NULL;
}
// Count the number of pseudo headers for the purposes of handling
// evidence.
headers->pseudoHeadersCount = countPseudoHeaders(headers);
}
return headers;
}
int fiftyoneDegreesHeaderGetIndex(
fiftyoneDegreesHeaders *headers,
const char* httpHeaderName,
size_t length) {
uint32_t i;
Header* header;
// Check if header is from a Perl or PHP wrapper in the form of HTTP_*
// and if present skip these characters.
if (headers->expectUpperPrefixedHeaders == true &&
length > sizeof(HTTP_PREFIX_UPPER) &&
StringCompareLength(
httpHeaderName,
HTTP_PREFIX_UPPER,
sizeof(HTTP_PREFIX_UPPER) - 1) == 0) {
length -= sizeof(HTTP_PREFIX_UPPER) - 1;
httpHeaderName += sizeof(HTTP_PREFIX_UPPER) - 1;
}
// Perform a case insensitive compare of the remaining characters.
for (i = 0; i < headers->count; i++) {
header = &headers->items[i];
if (header->nameLength == length &&
StringCompareLength(
httpHeaderName,
header->name,
length) == 0) {
return i;
}
}
return -1;
}
fiftyoneDegreesHeader* fiftyoneDegreesHeadersGetHeaderFromUniqueId(
fiftyoneDegreesHeaders *headers,
HeaderID uniqueId) {
uint32_t i;
for (i = 0; i < headers->count; i++) {
if (headers->items[i].uniqueId == uniqueId) {
return &headers->items[i];
}
}
return (Header*)NULL;
}
void fiftyoneDegreesHeadersFree(fiftyoneDegreesHeaders *headers) {
Header* header;
uint32_t i;
if (headers != NULL) {
for (i = 0; i < headers->count; i++) {
header = &headers->items[i];
Free((void*)header->name);
Free((void*)header->segments);
}
Free((void*)headers);
headers = NULL;
}
}
bool fiftyoneDegreesHeadersIsHttp(
void *state,
fiftyoneDegreesEvidenceKeyValuePair *pair) {
return HeaderGetIndex(
(Headers*)state,
pair->field,
strlen(pair->field)) >= 0;
}
/**
* SIZE CALCULATION METHODS
*/
size_t fiftyoneDegreesHeadersSize(int count) {
return sizeof(Headers) + // Headers structure
(count * sizeof(Header)); // Header names
}