forked from nicolasff/webdis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.c
318 lines (243 loc) · 7.23 KB
/
http.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
#include "http.h"
#include "server.h"
#include "worker.h"
#include "client.h"
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
/* HTTP Response */
struct http_response *
http_response_init(struct worker *w, int code, const char *msg) {
/* create object */
struct http_response *r = calloc(1, sizeof(struct http_response));
r->code = code;
r->msg = msg;
r->w = w;
r->keep_alive = 0; /* default */
http_response_set_header(r, "Server", "Webdis");
/* Cross-Origin Resource Sharing, CORS. */
http_response_set_header(r, "Allow", "GET,POST,PUT,OPTIONS");
http_response_set_header(r, "Access-Control-Allow-Origin", "*");
return r;
}
void
http_response_set_header(struct http_response *r, const char *k, const char *v) {
int i, pos = r->header_count;
size_t key_sz = strlen(k);
size_t val_sz = strlen(v);
for(i = 0; i < r->header_count; ++i) {
if(strncmp(r->headers[i].key, k, key_sz) == 0) {
pos = i;
/* free old value before replacing it. */
free(r->headers[i].key);
free(r->headers[i].val);
break;
}
}
/* extend array */
if(pos == r->header_count) {
r->headers = realloc(r->headers,
sizeof(struct http_header)*(r->header_count + 1));
r->header_count++;
}
/* copy key */
r->headers[pos].key = calloc(key_sz + 1, 1);
memcpy(r->headers[pos].key, k, key_sz);
r->headers[pos].key_sz = key_sz;
/* copy val */
r->headers[pos].val = calloc(val_sz + 1, 1);
memcpy(r->headers[pos].val, v, val_sz);
r->headers[pos].val_sz = val_sz;
if(!r->chunked && !strcmp(k, "Transfer-Encoding") && !strcmp(v, "chunked")) {
r->chunked = 1;
}
}
void
http_response_set_body(struct http_response *r, const char *body, size_t body_len) {
r->body = body;
r->body_len = body_len;
}
static void
http_response_cleanup(struct http_response *r, int fd, int success) {
int i;
/* cleanup buffer */
free(r->out);
if(!r->keep_alive || !success) {
/* Close fd is client doesn't support Keep-Alive. */
close(fd);
}
/* cleanup response object */
for(i = 0; i < r->header_count; ++i) {
free(r->headers[i].key);
free(r->headers[i].val);
}
free(r->headers);
free(r);
}
static void
http_can_write(int fd, short event, void *p) {
int ret;
struct http_response *r = p;
(void)event;
ret = write(fd, r->out + r->sent, r->out_sz - r->sent);
if(ret > 0)
r->sent += ret;
if(ret <= 0 || r->out_sz - r->sent == 0) { /* error or done */
http_response_cleanup(r, fd, (int)r->out_sz == r->sent ? 1 : 0);
} else { /* reschedule write */
http_schedule_write(fd, r);
}
}
void
http_schedule_write(int fd, struct http_response *r) {
if(r->w) { /* async */
event_set(&r->ev, fd, EV_WRITE, http_can_write, r);
event_base_set(r->w->base, &r->ev);
event_add(&r->ev, NULL);
} else { /* blocking */
http_can_write(fd, 0, r);
}
}
static char *
format_chunk(const char *p, size_t sz, size_t *out_sz) {
char *out, tmp[64];
int chunk_size;
/* calculate format size */
chunk_size = sprintf(tmp, "%x\r\n", (int)sz);
*out_sz = chunk_size + sz + 2;
out = malloc(*out_sz);
memcpy(out, tmp, chunk_size);
memcpy(out + chunk_size, p, sz);
memcpy(out + chunk_size + sz, "\r\n", 2);
return out;
}
void
http_response_write(struct http_response *r, int fd) {
char *p;
int i, ret;
/*r->keep_alive = 0;*/
r->out_sz = sizeof("HTTP/1.x xxx ")-1 + strlen(r->msg) + 2;
r->out = calloc(r->out_sz + 1, 1);
ret = sprintf(r->out, "HTTP/1.%d %d %s\r\n", (r->http_version?1:0), r->code, r->msg);
(void)ret;
p = r->out;
if(!r->chunked) {
if(r->code == 200 && r->body) {
char content_length[10];
sprintf(content_length, "%zd", r->body_len);
http_response_set_header(r, "Content-Length", content_length);
} else {
http_response_set_header(r, "Content-Length", "0");
}
}
for(i = 0; i < r->header_count; ++i) {
/* "Key: Value\r\n" */
size_t header_sz = r->headers[i].key_sz + 2 + r->headers[i].val_sz + 2;
r->out = realloc(r->out, r->out_sz + header_sz);
p = r->out + r->out_sz;
/* add key */
memcpy(p, r->headers[i].key, r->headers[i].key_sz);
p += r->headers[i].key_sz;
/* add ": " */
*(p++) = ':';
*(p++) = ' ';
/* add value */
memcpy(p, r->headers[i].val, r->headers[i].val_sz);
p += r->headers[i].val_sz;
/* add "\r\n" */
*(p++) = '\r';
*(p++) = '\n';
r->out_sz += header_sz;
if(strncasecmp("Connection", r->headers[i].key, r->headers[i].key_sz) == 0 &&
strncasecmp("Keep-Alive", r->headers[i].val, r->headers[i].val_sz) == 0) {
r->keep_alive = 1;
}
}
/* end of headers */
r->out = realloc(r->out, r->out_sz + 2);
memcpy(r->out + r->out_sz, "\r\n", 2);
r->out_sz += 2;
/* append body if there is one. */
if(r->body && r->body_len) {
char *tmp = (char*)r->body;
size_t tmp_len = r->body_len;
if(r->chunked) { /* replace body with formatted chunk */
tmp = format_chunk(r->body, r->body_len, &tmp_len);
}
r->out = realloc(r->out, r->out_sz + tmp_len);
memcpy(r->out + r->out_sz, tmp, tmp_len);
r->out_sz += tmp_len;
if(r->chunked) { /* need to free the chunk */
free(tmp);
}
}
/* send buffer to client */
r->sent = 0;
http_schedule_write(fd, r);
}
static void
http_response_set_connection_header(struct http_client *c, struct http_response *r) {
http_response_set_keep_alive(r, c->keep_alive);
}
/* Adobe flash cross-domain request */
void
http_crossdomain(struct http_client *c) {
struct http_response *resp = http_response_init(NULL, 200, "OK");
char out[] = "<?xml version=\"1.0\"?>\n"
"<!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">\n"
"<cross-domain-policy>\n"
"<allow-access-from domain=\"*\" />\n"
"</cross-domain-policy>\n";
resp->http_version = c->http_version;
http_response_set_connection_header(c, resp);
http_response_set_header(resp, "Content-Type", "application/xml");
http_response_set_body(resp, out, sizeof(out)-1);
http_response_write(resp, c->fd);
http_client_reset(c);
}
/* Simple error response */
void
http_send_error(struct http_client *c, short code, const char *msg) {
struct http_response *resp = http_response_init(NULL, code, msg);
resp->http_version = c->http_version;
http_response_set_connection_header(c, resp);
http_response_set_body(resp, NULL, 0);
http_response_write(resp, c->fd);
http_client_reset(c);
}
/**
* Set Connection field, either Keep-Alive or Close.
*/
void
http_response_set_keep_alive(struct http_response *r, int enabled) {
r->keep_alive = enabled;
if(enabled) {
http_response_set_header(r, "Connection", "Keep-Alive");
} else {
http_response_set_header(r, "Connection", "Close");
}
}
/* Response to HTTP OPTIONS */
void
http_send_options(struct http_client *c) {
struct http_response *resp = http_response_init(NULL, 200, "OK");
resp->http_version = c->http_version;
http_response_set_connection_header(c, resp);
http_response_set_header(resp, "Content-Type", "text/html");
http_response_set_header(resp, "Content-Length", "0");
http_response_write(resp, c->fd);
http_client_reset(c);
}
/**
* Write HTTP chunk.
*/
void
http_response_write_chunk(int fd, struct worker *w, const char *p, size_t sz) {
struct http_response *r = http_response_init(w, 0, NULL);
r->keep_alive = 1; /* chunks are always keep-alive */
/* format packet */
r->out = format_chunk(p, sz, &r->out_sz);
/* send async write */
http_schedule_write(fd, r);
}