This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
http_message.c
382 lines (339 loc) · 11 KB
/
http_message.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
#include "http_message.h"
#include <string.h>
#include <stdlib.h>
struct message_header_s {
gchar *name;
gchar *value;
};
typedef struct message_header_s message_header_t;
/* TODO
* - add function to serialize to iovec for speedy writes
* - http://www.jmarshall.com/easy/http/
*/
/*
* normalize http11 message header field names.
*/
static gchar *normalize_header_name(gchar *field) {
int first_letter = 1;
for (gchar *p = field; *p != 0; p++) {
if (!first_letter) {
*p = g_ascii_tolower(*p);
if (*p == '_') {
*p = '-';
first_letter = 1;
} else if (*p == '-') {
first_letter = 1;
}
} else {
*p = g_ascii_toupper(*p);
first_letter = 0;
}
}
return field;
}
static gint header_name_compare(gconstpointer a, gconstpointer b) {
const message_header_t *am = (message_header_t *)a;
const gchar *field = (const gchar *)b;
return g_strcmp0(field, am->name);
}
void http_header_append(GQueue *headers,
GStringChunk *chunk,
const gchar *field, const gchar *value)
{
message_header_t *hdr = g_slice_new(message_header_t);
hdr->name = g_string_chunk_insert(chunk, field);
normalize_header_name(hdr->name);
hdr->value = g_string_chunk_insert(chunk, value);
g_queue_push_tail(headers, hdr);
}
gboolean http_header_remove(GQueue *headers,
const gchar *field)
{
GList *l = g_queue_find_custom(headers, field, header_name_compare);
gboolean found = FALSE;
while (l) {
g_slice_free(message_header_t, l->data);
g_queue_delete_link(headers, l);
found = TRUE;
l = g_queue_find_custom(headers, field, header_name_compare);
}
return found;
}
const gchar *http_header_getstr(GQueue *headers,
const gchar *field)
{
GList *l = g_queue_find_custom(headers, field, header_name_compare);
if (l) {
return ((message_header_t *)l->data)->value;
}
return NULL;
}
unsigned long long http_header_getull(GQueue *headers,
const gchar *field)
{
return strtoull(
http_header_getstr(headers, field), NULL, 0);
}
static void request_method(void *data, const char *at, size_t length) {
http_request_t *msg = (http_request_t *)data;
msg->method = g_string_chunk_insert_len(msg->chunk, at, length);
}
static void request_uri(void *data, const char *at, size_t length) {
http_request_t *msg = (http_request_t *)data;
msg->uri = g_string_chunk_insert_len(msg->chunk, at, length);
}
static void fragment(void *data, const char *at, size_t length) {
http_request_t *msg = (http_request_t *)data;
msg->fragment = g_string_chunk_insert_len(msg->chunk, at, length);
}
static void request_path(void *data, const char *at, size_t length) {
http_request_t *msg = (http_request_t *)data;
msg->path = g_string_chunk_insert_len(msg->chunk, at, length);
}
static void query_string(void *data, const char *at, size_t length) {
http_request_t *msg = (http_request_t *)data;
msg->query_string = g_string_chunk_insert_len(msg->chunk, at, length);
}
static void http_version(void *data, const char *at, size_t length) {
http_request_t *req = (http_request_t *)data;
req->http_version = g_string_chunk_insert_len(req->chunk, at, length);
}
static void header_done(void *data, const char *at, size_t length) {
http_request_t *req = (http_request_t *)data;
/* set body */
/* TODO: not sure this logic is right. length might be wrong */
if (length) {
req->body = at;
req->body_length = length;
}
}
static void http_field(void *data, const char *field,
size_t flen, const char *value, size_t vlen)
{
http_request_t *req = (http_request_t *)data;
/* cast away const then temporarily NULL terminate */
gchar *f = (gchar *)field;
gchar *v = (gchar *)value;
/* save character being replaced by NULL */
gchar svf = f[flen];
gchar vvf = v[vlen];
f[flen] = 0;
v[vlen] = 0;
http_request_header_append(req, field, value);
/* restore saved character */
f[flen] = svf;
v[vlen] = vvf;
}
#if 0
void http_request_init(http_request_t *req) {
req->chunk = g_string_chunk_new(1024 * 4);
req->headers = NULL;
http_request_clear(req);
req->headers = g_queue_new();
}
#endif
http_request_t *http_request_new(void) {
http_request_t *req = g_slice_new0(http_request_t);
req->chunk = g_string_chunk_new(1024 * 4);
req->headers = g_queue_new();
return req;
}
static void free_message_headers(gpointer data, gpointer user_data) {
(void)user_data;
g_slice_free(message_header_t, data);
}
void http_request_free(http_request_t *req) {
g_queue_foreach(req->headers, free_message_headers, NULL);
g_queue_free(req->headers);
g_string_chunk_free(req->chunk);
g_slice_free(http_request_t, req);
}
void http_request_clear(http_request_t *req) {
req->method = NULL;
req->uri = NULL;
req->fragment = NULL;
req->path = NULL;
req->query_string = NULL;
req->http_version = NULL;
req->body = NULL;
req->body_length = 0;
if (req->headers) {
g_queue_foreach(req->headers, free_message_headers, NULL);
g_queue_clear(req->headers);
}
g_string_chunk_clear(req->chunk);
}
void http_request_parser_init(http_request_t *req, http_parser *p) {
#if 0
http_request_init(req);
#endif
p->data = req;
p->http_field = http_field;
p->request_method = request_method;
p->request_uri = request_uri;
p->fragment = fragment;
p->request_path = request_path;
p->query_string = query_string;
p->http_version = http_version;
p->header_done = header_done;
}
void http_request_make(http_request_t *req,
const gchar *method, const gchar *uri)
{
#if 0
http_request_init(req);
#endif
req->method = g_string_chunk_insert(req->chunk, method);
req->uri = g_string_chunk_insert(req->chunk, uri);
req->http_version = g_string_chunk_insert(req->chunk, "HTTP/1.1");
}
static void http_request_write_headers(gpointer data, gpointer user_data) {
FILE *f = (FILE *)user_data;
message_header_t *hdr = (message_header_t *)data;
fprintf(f, "%s: %s\r\n", hdr->name, hdr->value);
}
void http_request_debug_print(http_request_t *req, FILE *f) {
fprintf(f, "method: %s\n", req->method);
fprintf(f, "uri: %s\n", req->uri);
fprintf(f, "fragment: %s\n", req->fragment);
fprintf(f, "path: %s\n", req->path);
fprintf(f, "query: %s\n", req->query_string);
fprintf(f, "http version: %s\n", req->http_version);
fprintf(f, "headers:\n");
g_queue_foreach(req->headers, http_request_write_headers, f);
fprintf(f, "length: %zu\n", req->body_length);
fprintf(f, "body: %s\n", req->body);
}
void http_request_fwrite(http_request_t *req, FILE *f) {
fprintf(f, "%s %s %s\r\n", req->method, req->uri, req->http_version);
g_queue_foreach(req->headers, http_request_write_headers, f);
fprintf(f, "\r\n");
}
static void message_headers_to_data(gpointer data, gpointer user_data) {
GString *s = (GString *)user_data;
message_header_t *hdr = (message_header_t *)data;
g_string_append_printf(s, "%s: %s\r\n", hdr->name, hdr->value);
}
GString *http_request_data(http_request_t *req) {
GString *s = g_string_sized_new(1024 * 4);
g_string_printf(s, "%s %s %s\r\n", req->method,
req->uri, req->http_version);
g_queue_foreach(req->headers, message_headers_to_data, s);
g_string_append_printf(s, "\r\n");
return s;
}
/* http_response_t */
static void http_field_cl(void *data, const char *field,
size_t flen, const char *value, size_t vlen)
{
http_response_t *resp = (http_response_t *)data;
/* cast away const then temporarily NULL terminate */
gchar *f = (gchar *)field;
gchar *v = (gchar *)value;
/* save character being replaced by NULL */
gchar svf = f[flen];
gchar vvf = v[vlen];
f[flen] = 0;
v[vlen] = 0;
/* TODO: need to normalize header. for example, convert to all caps */
http_response_header_append(resp, field, value);
/* restore saved character */
f[flen] = svf;
v[vlen] = vvf;
}
static void reason_phrase_cl(void *data, const char *at, size_t length) {
http_response_t *resp = (http_response_t *)data;
resp->reason = g_string_chunk_insert_len(resp->chunk, at, length);
}
static void status_code_cl(void *data, const char *at, size_t length) {
http_response_t *resp = (http_response_t *)data;
char s = at[length];
((char *)at)[length] = 0;
resp->status_code = strtoul(at, NULL, 0);
((char *)at)[length] = s;
}
static void chunk_size_cl(void *data, const char *at, size_t length) {
http_response_t *resp = (http_response_t *)data;
const gchar *chunk_size = g_string_chunk_insert_len(resp->chunk, at, length);
resp->chunk_size = strtoull(chunk_size, NULL, 16);
}
static void http_version_cl(void *data, const char *at, size_t length) {
http_response_t *resp = (http_response_t *)data;
resp->http_version = g_string_chunk_insert_len(resp->chunk, at, length);
}
static void header_done_cl(void *data, const char *at, size_t length) {
http_response_t *resp = (http_response_t *)data;
/* set body */
/* TODO: the length is not right here */
/* printf("HEADER DONE: %zu [%s]\n", length, at); */
if (at || length) {
resp->body = at;
resp->body_length = length;
}
}
static void last_chunk_cl(void *data, const char *at, size_t length) {
(void)at;
(void)length;
http_response_t *resp = (http_response_t *)data;
resp->last_chunk = TRUE;
}
http_response_t *http_response_new(unsigned long code, const gchar *reason) {
http_response_t *resp = g_slice_new0(http_response_t);
resp->chunk = g_string_chunk_new(1024 * 4);
resp->headers = g_queue_new();
resp->http_version = "HTTP/1.1";
resp->status_code = code;
resp->reason = reason;
return resp;
}
void http_response_parser_init(http_response_t *resp, httpclient_parser *p) {
p->data = resp;
p->http_field = http_field_cl;
p->reason_phrase = reason_phrase_cl;
p->status_code = status_code_cl;
p->chunk_size = chunk_size_cl;
p->http_version = http_version_cl;
p->header_done = header_done_cl;
p->last_chunk = last_chunk_cl;
}
void http_response_set_body(http_response_t *resp, const gchar *body) {
gchar lenstr[32];
resp->body = body;
resp->body_length = strlen(body);
g_snprintf(lenstr, sizeof(lenstr)-1, "%zu", resp->body_length);
/* TODO: remove header first */
http_response_header_append(resp, "Content-Length", lenstr);
}
GString *http_response_data(http_response_t *resp) {
GString *s = g_string_sized_new(1024);
g_string_printf(s, "%s %lu %s\r\n", resp->http_version,
resp->status_code, resp->reason);
/* TODO: maybe add required headers like content-length */
g_queue_foreach(resp->headers, message_headers_to_data, s);
g_string_append_printf(s, "\r\n");
return s;
}
void http_response_clear(http_response_t *resp) {
resp->http_version = NULL;
resp->status_code = 0;
resp->reason = NULL;
resp->body = NULL;
resp->body_length = 0;
resp->chunk_size = 0;
resp->last_chunk = 0;
if (resp->headers) {
g_queue_foreach(resp->headers, free_message_headers, NULL);
g_queue_clear(resp->headers);
}
g_string_chunk_clear(resp->chunk);
}
void http_response_free(http_response_t *resp) {
if (resp->headers) {
g_queue_foreach(resp->headers, free_message_headers, NULL);
g_queue_free(resp->headers);
}
if (resp->chunk) { g_string_chunk_free(resp->chunk); }
resp->headers = 0;
resp->chunk = 0;
g_slice_free(http_response_t, resp);
}