-
Notifications
You must be signed in to change notification settings - Fork 134
/
http-request.c
218 lines (199 loc) · 6.99 KB
/
http-request.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
#define _GNU_SOURCE
#include "http-request.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdatomic.h>
// we declare because we need them... implementation comes later.
static struct HttpRequest* request_new(void);
static void request_clear(struct HttpRequest* self);
static void request_destroy(struct HttpRequest* self);
static void request_first(struct HttpRequest* self);
static int request_next(struct HttpRequest* self);
static int request_find(struct HttpRequest* self, char* const name);
static char* request_name(struct HttpRequest* self);
static char* request_value(struct HttpRequest* self);
static int request_is_request(struct HttpRequest* self);
static ssize_t decode_url(char* dest, const char* url_data, size_t length);
const struct HttpRequestClass HttpRequest = {
// retures an new heap allocated request object
.create = request_new,
// releases the resources used by a request object and keeps it's memory.
.clear = request_clear,
// releases the resources used by a request object and frees it's memory.
.destroy = request_destroy,
// validated that this is a request object
.is_request = request_is_request,
// URL decoding
.decode_url = decode_url,
// Header handling
/// restarts the header itteration
.first = request_first,
/// moves to the next header. returns 0 if the end of the list was
/// reached.
.next = request_next,
/// finds a specific header matching the requested string.
/// all headers are lower-case, so the string should be lower case.
/// returns 0 if the header couldn't be found.
.find = request_find,
/// returns the name of the current header in the itteration cycle.
.name = request_name,
/// returns the value of the current header in the itteration cycle.
.value = request_value,
};
////////////////
// The Request object pool implementation
#ifndef REQUEST_POOL_SIZE
#define REQUEST_POOL_SIZE 32
#endif
struct HttpRequestPool {
struct HttpRequest request;
struct HttpRequestPool* next;
};
// The global packet container pool
static struct {
struct HttpRequestPool* _Atomic pool;
struct HttpRequestPool* initialized;
} ContainerPool = {NULL};
static atomic_flag pool_initialized;
static void create_request_pool(void) {
while (atomic_flag_test_and_set(&pool_initialized)) {
}
if (ContainerPool.initialized == NULL) {
ContainerPool.initialized =
calloc(REQUEST_POOL_SIZE, sizeof(struct HttpRequestPool));
for (size_t i = 0; i < REQUEST_POOL_SIZE - 1; i++) {
ContainerPool.initialized[i].next = &ContainerPool.initialized[i + 1];
}
ContainerPool.pool = ContainerPool.initialized;
}
atomic_flag_clear(&pool_initialized);
}
////////////////
// The Request object implementation
// The constructor
static struct HttpRequest* request_new(void) {
if (ContainerPool.initialized == NULL)
create_request_pool();
struct HttpRequestPool* req = atomic_load(&ContainerPool.pool);
while (req) {
if (atomic_compare_exchange_weak(&ContainerPool.pool, &req, req->next))
break;
}
if (!req) {
req = calloc(sizeof(struct HttpRequest), 1);
} else {
memset(req, 0, sizeof(struct HttpRequest) - HTTP_HEAD_MAX_SIZE);
}
req->request.internal.is_request = request_is_request;
return (struct HttpRequest*)req;
}
// the destructor
static void request_destroy(struct HttpRequest* self) {
if (!self || !self->server)
return;
if (self->body_file)
fclose(self->body_file);
self->server = 0;
if (ContainerPool.initialized == NULL ||
((struct HttpRequestPool*)self) < ContainerPool.initialized ||
((struct HttpRequestPool*)self) >
(ContainerPool.initialized +
(sizeof(struct HttpRequestPool) * REQUEST_POOL_SIZE))) {
free(self);
} else {
((struct HttpRequestPool*)self)->next = atomic_load(&ContainerPool.pool);
for (;;) {
if (atomic_compare_exchange_weak(&ContainerPool.pool,
&((struct HttpRequestPool*)self)->next,
((struct HttpRequestPool*)self)))
break;
}
}
}
// resetting the request
static void request_clear(struct HttpRequest* self) {
if (!self || !self->server)
return;
if (self->body_file)
fclose(self->body_file);
*self = (struct HttpRequest){.internal.is_request = request_is_request};
}
// validating a request object
static int request_is_request(struct HttpRequest* self) {
return (self && (self->internal.is_request == request_is_request));
}
// implement the following request handlers:
static void request_first(struct HttpRequest* self) {
self->internal.pos = 0;
};
static int request_next(struct HttpRequest* self) {
// repeat the following 2 times, as it's a name + value pair
for (int i = 0; i < 2; i++) {
// move over characters
while (self->internal.pos < self->internal.max &&
self->internal.header_hash[self->internal.pos])
self->internal.pos++;
// move over NULL
while (self->internal.pos < self->internal.max &&
!self->internal.header_hash[self->internal.pos])
self->internal.pos++;
}
if (self->internal.pos == self->internal.max)
return 0;
return 1;
}
static int request_find(struct HttpRequest* self, char* const name) {
self->internal.pos = 0;
do {
if (!strcasecmp(self->internal.header_hash + self->internal.pos, name))
return 1;
} while (request_next(self));
return 0;
}
static char* request_name(struct HttpRequest* self) {
if (!self->internal.header_hash[self->internal.pos])
return NULL;
return self->internal.header_hash + self->internal.pos;
};
static char* request_value(struct HttpRequest* self) {
if (!self->internal.header_hash[self->internal.pos])
return NULL;
int pos = self->internal.pos;
// move over characters
while (pos < self->internal.max && self->internal.header_hash[pos])
pos++;
// move over NULL
while (pos < self->internal.max && !self->internal.header_hash[pos])
pos++;
if (self->internal.pos == self->internal.max)
return 0;
return self->internal.header_hash + pos;
};
/* *****************************************************************************
*/
#define is_hex(c) \
(((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || \
((c) >= 'A' && c <= 'F'))
#define hex_val(c) (((c) >= '0' && (c) <= '9') ? ((c)-48) : (((c) | 32) - 87))
static ssize_t decode_url(char* dest, const char* url_data, size_t length) {
char* pos = dest;
for (size_t i = 0; i < length; i++) {
if (url_data[i] == '+') // decode space
*(pos++) = ' ';
else if (url_data[i] == '%') {
// decode hex value
if (is_hex(url_data[i + 1]) && is_hex(url_data[i + 2])) {
// this is a percent encoded value.
*(pos++) = (hex_val(url_data[i + 1]) << 4) | hex_val(url_data[i + 2]);
i += 2;
} else {
// there was an error in the URL encoding... what to do? ignore?
return -1;
}
} else
*(pos++) = url_data[i];
}
*pos = 0;
return pos - dest;
}