-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathhttp_server.c
356 lines (318 loc) · 12.2 KB
/
http_server.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
/*
* Copyright (C) 2004-2021 Christos Tsantilas
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*/
#include "common.h"
#include "c-icap.h"
#include "debug.h"
#include "http_server.h"
#include "request.h"
#include "request_util.h"
#include "access.h"
#include "array.h"
#include "acl.h"
#include "util.h"
#include "cfg_param.h"
#include "stats.h"
#include "body.h"
extern int TIMEOUT;
static ci_thread_mutex_t STAT_MTX;
static ci_stat_memblock_t *STATS = NULL;
static int STAT_WS_REQUESTS = -1;
static int STAT_WS_FAILED_REQUESTS = -1;
static int STAT_WS_BYTES_IN = -1;
static int STAT_WS_BYTES_OUT = -1;
static int STAT_WS_BODY_BYTES_IN = -1;
static int STAT_WS_BODY_BYTES_OUT = -1;
struct http_service_handler {
int (*handler)(ci_request_t *req);
char descr[256];
/*Statistics info and other members:*/
};
ci_array_t *ServiceHandlers = NULL;
static int main_web_service(ci_request_t *req);
static int request_stat_entry_register(const char *name, int type, const char *gname)
{
int stat = ci_stat_entry_register(name, type, gname);
assert(stat >= 0);
return stat;
}
void http_server_init()
{
STAT_WS_REQUESTS = request_stat_entry_register("REQUESTS", CI_STAT_INT64_T, "WEB SERVER");
STAT_WS_FAILED_REQUESTS = request_stat_entry_register("FAILED REQUESTS", CI_STAT_INT64_T, "WEB SERVER");
STAT_WS_BYTES_IN = request_stat_entry_register("BYTES IN", CI_STAT_KBS_T, "WEB SERVER");
STAT_WS_BYTES_OUT = request_stat_entry_register("BYTES OUT", CI_STAT_KBS_T, "WEB SERVER");
STAT_WS_BODY_BYTES_IN = request_stat_entry_register("BODY BYTES IN", CI_STAT_KBS_T, "WEB SERVER");
STAT_WS_BODY_BYTES_OUT = request_stat_entry_register("BODY BYTES OUT", CI_STAT_KBS_T, "WEB SERVER");
ci_thread_mutex_init(&STAT_MTX);
ci_http_server_register_service("/", "The c-icap main web page", main_web_service, 0);
}
void http_server_close()
{
if (ServiceHandlers) {
ci_array_destroy(ServiceHandlers);
ServiceHandlers = NULL;
}
}
void ci_http_server_register_service(const char *path, const char *descr, int (*handler)(ci_request_t *req), unsigned flags)
{
/*
if (KIDS_STARTED || GO_MULTITHREAD) {
ci_debug_printf(1, "ERROR: registering '%s' HTTP service. Web services can only be registered during c-icap initialization procedure\n");
return;
}
*/
struct http_service_handler service_handler;
service_handler.handler = handler;
snprintf(service_handler.descr, sizeof(service_handler.descr), "%s", descr);
if (!ServiceHandlers)
ServiceHandlers = ci_array_new2(1024, sizeof(struct http_service_handler));
ci_array_add(ServiceHandlers, path, &service_handler, sizeof(struct http_service_handler));
}
void ci_http_server_response_set_status(ci_request_t *req, int status)
{
_CI_ASSERT(req);
req->return_code = status;
}
static int http_server_response_send(ci_request_t *req)
{
char buf[256];
int len, ok;
if (req->return_code < 0)
req->return_code = EC_200;
ci_headers_reset(req->response_header);
/*ICAP status codes matches HTTP codes:*/
snprintf(buf, sizeof(buf), "HTTP/1.1 %d %s",
ci_status_code(req->return_code), ci_status_code_string(req->return_code));
ci_headers_add(req->response_header, buf);
ci_headers_add(req->response_header, "Server: C-ICAP/" VERSION);
strncpy(buf, "Date: ", sizeof(buf));
buf[sizeof(buf) - 1] = '\0';
const size_t DATE_PREFIX_LEN = strlen(buf);
ci_strntime_rfc822(buf + DATE_PREFIX_LEN, sizeof(buf) - DATE_PREFIX_LEN);
ci_headers_add(req->response_header, buf);
if (req->keepalive)
ci_headers_add(req->response_header, "Connection: keep-alive");
else
ci_headers_add(req->response_header, "Connection: close");
ci_membuf_t *body = (ci_membuf_t *)req->service_data;
if (body && ci_membuf_size(body)) {
ci_headers_remove(req->response_header, "Content-Length");
snprintf(buf, sizeof(buf), "Content-Length: %d", ci_membuf_size(body));
ci_headers_add(req->response_header, buf);
}
if (!ci_headers_is_empty(req->xheaders)) {
ci_headers_addheaders(req->response_header, req->xheaders);
}
ci_headers_pack(req->response_header);
ok = 0;
ci_clock_time_get(&req->start_w_t);
len = ci_connection_write(req->connection,
req->response_header->buf, req->response_header->bufused,
TIMEOUT);
if (len <= 0)
goto http_server_response_send_done;
req->bytes_out = len;
if (body && ci_membuf_size(body)) {
len = ci_connection_write(req->connection, ci_membuf_raw(body), ci_membuf_size(body), TIMEOUT);
if (len <= 0)
goto http_server_response_send_done;
req->bytes_out += len;
req->body_bytes_out = len;
}
ok = 1;
http_server_response_send_done:
ci_clock_time_get(&req->stop_w_t);
/*We are finishing sending*/
req->status = SEND_EOF;
req->http_bytes_out = req->bytes_out;
return ok;
}
int ci_read_icap_header(ci_request_t * req, ci_headers_list_t * h, int timeout);
static int ci_read_http_header(ci_request_t * req, int timeout)
{
int ret = ci_read_icap_header(req, req->request_header, timeout);
req->http_bytes_in = req->bytes_in;
return (ret == EC_100 ? EC_200 : ret);
}
static int http_server_request_parse_first_line(ci_request_t *req)
{
const char *line = req->request_header->headers[0];
const char *str = line;
if (strncasecmp("GET", str, 3) != 0) {
ci_debug_printf(7, "Http request protocol info, method not allowed: %.15s\n", str);
return EC_405; /*Not allowed. Only GET method is allowed*/
}
req->type = CI_HTTP_METHOD_GET;
str += 3;
while(*str == ' ') ++str; /*Str point to the start of the path*/
const char *end = str;
while(*end && *end != ' ' && *end != '?') ++end;
if (!*end) {
ci_debug_printf(7, "Http request path parse error: %15s\n", str);
return EC_400; /*Bad request, expect a " HTTP/major.min" after path*/
}
if ((end - str) >= MAX_SERVICE_NAME) {
ci_debug_printf(7, "Http request protocol info path size exceed\n");
return EC_404; /*"Not found" we have only paths of size HTTP_SERVER_MAX_PATH_SIZE */
}
strncpy(req->service, str, end - str);
req->service[end-str] = '\0';
if (*end == '?') {
end++;
str = end;
while(*end && *end != ' ') ++end;
if (!*end) {
ci_debug_printf(7, "Http request path/args parse error: %15s\n", str);
return EC_400; /*Bad request, expect a " HTTP/major.min" after path*/
}
const int args_size = end - str > MAX_SERVICE_ARGS ? MAX_SERVICE_ARGS : end - str;
strncpy(req->args, str, args_size);
req->args[args_size] = '\0';
} else
req->args[0] = '\0';
str = end;
while(*str && strchr(" \t\r\n", *str) != NULL) ++str;
const int ret = sscanf(str, "HTTP/%d.%d", &req->proto_version.major, &req->proto_version.minor);
if (ret != 2) {
ci_debug_printf(7, "Http request protocol info parse error: %.15s\n", str);
return EC_400; /*Bad request*/
}
if (req->proto_version.major != 1 || req->proto_version.minor < 0 || req->proto_version.minor > 1) {
ci_debug_printf(7, "Http request protocol versions not supported: %.15s\n", str);
return EC_400; /*Bad request, protocol version not supported*/
}
return EC_200;
}
static int http_server_exec_service_handler(ci_request_t *req)
{
if (!ServiceHandlers)
return 0;
const struct http_service_handler *handler = ci_array_search(ServiceHandlers, req->service);
if (!handler)
return 0;
req->return_code = EC_200;
req->service_data = ci_membuf_new_sized(32768);
if (!handler->handler(req)) {
req->return_code = EC_500;
ci_membuf_t *body = (ci_membuf_t *)req->service_data;
ci_membuf_free(body);
req->service_data = NULL;
}
http_server_response_send(req);
return 1;
}
static int http_do_request(ci_request_t *req)
{
ci_clock_time_get(&req->start_r_t);
int status = ci_read_http_header(req, TIMEOUT);
ci_clock_time_get(&req->stop_r_t);
if (status != EC_200) {
if (req->request_header->bufused == 0)
return -1; /* Did not read anything */
goto http_do_request_bad_status;
}
if ((status = ci_headers_unpack(req->request_header)) != EC_100) {
goto http_do_request_bad_status;
}
if ((status = http_server_request_parse_first_line(req)) != EC_200)
goto http_do_request_bad_status;
int auth_status = CI_ACCESS_ALLOW;
if ((auth_status = access_check_request(req)) == CI_ACCESS_DENY) {
status = (req->auth_required ? EC_401 : EC_403);
ci_debug_printf(3, "HTTP request not allowed/authenticated, status: %d\n", auth_status);
goto http_do_request_bad_status;
}
if (http_server_exec_service_handler(req)) {
return 1;
}
req->keepalive = 0;
req->return_code = EC_404; /*Service Not found*/
http_server_response_send(req);
return 0;
http_do_request_bad_status:
req->keepalive = 0;
req->return_code = status;
http_server_response_send(req);
return 0;
}
int http_process_request(ci_request_t *req)
{
int status = 1;
status = http_do_request(req);
if (req->service_data) {
ci_membuf_t *body = (ci_membuf_t *)req->service_data;
ci_membuf_free(body);
req->service_data = NULL;
}
if (status < 0 && req->request_header->bufused == 0) /*Did not read anything*/
return CI_NO_STATUS;
ci_thread_mutex_lock(&STAT_MTX);
if (!STATS)
STATS = ci_stat_memblock_get();
assert(STATS);
STAT_INT64_INC_NL(STATS, STAT_WS_REQUESTS, 1);
if (!status)
STAT_INT64_INC_NL(STATS, STAT_WS_FAILED_REQUESTS, 1);
STAT_KBS_INC_NL(STATS, STAT_WS_BYTES_IN, req->bytes_in);
STAT_KBS_INC_NL(STATS, STAT_WS_BYTES_OUT, req->bytes_out);
STAT_KBS_INC_NL(STATS, STAT_WS_BODY_BYTES_IN, req->body_bytes_in);
STAT_KBS_INC_NL(STATS, STAT_WS_BODY_BYTES_OUT, req->body_bytes_out);
ci_thread_mutex_unlock(&STAT_MTX);
return status;
}
int main_web_service(ci_request_t *req)
{
const char Header[] =
"<!DOCTYPE HTML>\n<HTML lang=\"en\">\n"
"<HEAD>\n"
"<TITLE>C-icap web server main page</TITLE>\n"
"<STYLE>\n"
" table {border-collapse: collapse;}\n"
" th,td,table {border-style:solid; border-width:1px; }\n"
" caption {text-align:right;}\n"
"</STYLE>\n"
"</HEAD>\n"
"<BODY>\n"
"<HTML>\n"
"<H1>The available c-icap web services</H1>\n"
"<UL>"
;
const char End[] = "</UL>\n</BODY>\n</HTML>";
if (!ServiceHandlers)
return 0;
ci_http_server_response_add_header(req, "Content-Type: text/html");
ci_http_server_response_add_header(req, "Content-Language: en");
ci_membuf_t *body = ci_http_server_response_body(req);
ci_membuf_write(body, Header, sizeof(Header) - 1, 0);
int i = 0;
const ci_array_item_t *it = NULL;
for (i = 0; (it = ci_array_get_item(ServiceHandlers, i)) != NULL; i++) {
char buf[1024];
const char *path = it->name;
const struct http_service_handler *srv = it->value;
_CI_ASSERT(path);
_CI_ASSERT(srv);
int bytes = snprintf(buf, sizeof(buf), "<LI><A href='%s'>%s</A></LI>\n", path, srv->descr);
if (bytes >= sizeof(buf))
bytes = sizeof(buf) - 1;
ci_membuf_write(body, buf, bytes, 0);
}
ci_membuf_write(body, End, sizeof(End) - 1, 1);
return 1;
}