-
Notifications
You must be signed in to change notification settings - Fork 32
/
simple_api.c
449 lines (379 loc) · 11.7 KB
/
simple_api.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
447
448
449
/*
* Copyright (C) 2004-2008 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 "encoding.h"
#include "request_util.h"
#include "debug.h"
#include <ctype.h>
#include <errno.h>
const char *ci_http_methods_str[] = {
"UNKNOWN",
"GET",
"POST",
"PUT",
"HEAD",
"CONNECT",
"TRACE",
"OPTIONS",
"DELETE",
NULL
};
const char *ci_http_method_string(int method)
{
if (method <= CI_HTTP_METHOD_NONE || method >= CI_HTTP_METHOD_MAX)
return "UNKNWON";
return ci_http_methods_str[method];
}
/*
int ci_resp_check_body(ci_request_t *req){
int i;
ci_encaps_entity_t **e=req->entities;
for(i = 0; e[i] != NULL; i++)
if(e[i]->type == ICAP_NULL_BODY)
return 0;
return 1;
}
*/
ci_headers_list_t * ci_http_response_headers(ci_request_t * req)
{
int i;
ci_encaps_entity_t **e_list;
e_list = req->entities;
for (i = 0; e_list[i] != NULL && i < 3; i++) { /*It is the first or second ellement */
if (e_list[i]->type == ICAP_RES_HDR)
return (ci_headers_list_t *) e_list[i]->entity;
}
return NULL;
}
ci_headers_list_t *ci_http_request_headers(ci_request_t * req)
{
ci_encaps_entity_t **e_list;
e_list = req->entities;
if (e_list[0] != NULL && e_list[0]->type == ICAP_REQ_HDR) /*It is always the first ellement */
return (ci_headers_list_t *) e_list[0]->entity;
/*We did not found the request headers but maybe there are in req->trash objects
Trying to retrieve hoping that it were not desstroyed yet
*/
if (req->trash_entities[ICAP_REQ_HDR] &&
req->trash_entities[ICAP_REQ_HDR]->entity &&
((ci_headers_list_t *) req->trash_entities[ICAP_REQ_HDR]->entity)->used)
return (ci_headers_list_t *) req->trash_entities[ICAP_REQ_HDR]->entity;
return NULL;
}
int ci_http_response_reset_headers(ci_request_t * req)
{
ci_headers_list_t *heads;
if (!(heads = ci_http_response_headers(req)))
return 0;
ci_headers_reset(heads);
return 1;
}
int ci_http_request_reset_headers(ci_request_t * req)
{
ci_headers_list_t *heads;
if (!(heads = ci_http_request_headers(req)))
return 0;
ci_headers_reset(heads);
return 1;
}
/*
This function will be used when we want to responce with an error message
to an reqmod request or respmod request.
ICAP rfc says that we must responce as:
REQMOD response encapsulated_list: {[reshdr] resbody}
RESPMOD response encapsulated_list: [reshdr] resbody
*/
int ci_http_response_create(ci_request_t * req, int has_reshdr, int has_body)
{
int i = 0;
for (i = 0; i < 4; i++) {
if (req->entities[i]) {
ci_request_release_entity(req, i);
}
}
i = 0;
if (has_reshdr)
req->entities[i++] = ci_request_alloc_entity(req, ICAP_RES_HDR, 0);
if (has_body)
req->entities[i] = ci_request_alloc_entity(req, ICAP_RES_BODY, 0);
else
req->entities[i] = ci_request_alloc_entity(req, ICAP_NULL_BODY, 0);
return 1;
}
int ci_http_request_create(ci_request_t * req, int has_body)
{
int i = 0;
for (i = 0; i < 4; i++) {
if (req->entities[i]) {
ci_request_release_entity(req, i);
}
}
i = 0;
req->entities[i++] = ci_request_alloc_entity(req, ICAP_REQ_HDR, 0);
if (has_body)
req->entities[i] = ci_request_alloc_entity(req, ICAP_REQ_BODY, 0);
else
req->entities[i] = ci_request_alloc_entity(req, ICAP_NULL_BODY, 0);
return 1;
}
const char *ci_http_response_add_header(ci_request_t * req, const char *header)
{
ci_headers_list_t *heads;
if (req->packed) /*Not in edit mode*/
return NULL;
if (!(heads = ci_http_response_headers(req)))
return NULL;
return ci_headers_add(heads, header);
}
const char *ci_http_request_add_header(ci_request_t * req, const char *header)
{
ci_headers_list_t *heads;
if (req->packed) /*Not in edit mode*/
return NULL;
if (!(heads = ci_http_request_headers(req)))
return NULL;
return ci_headers_add(heads, header);
}
int ci_http_response_remove_header(ci_request_t * req, const char *header)
{
ci_headers_list_t *heads;
if (req->packed) /*Not in edit mode*/
return 0;
if (!(heads = ci_http_response_headers(req)))
return 0;
return ci_headers_remove(heads, header);
}
int ci_http_request_remove_header(ci_request_t * req, const char *header)
{
ci_headers_list_t *heads;
if (req->packed) /*Not in edit mode*/
return 0;
if (!(heads = ci_http_request_headers(req)))
return 0;
return ci_headers_remove(heads, header);
}
const char *ci_http_response_get_header(ci_request_t * req, const char *head_name)
{
ci_headers_list_t *heads;
const char *val;
if (!(heads = ci_http_response_headers(req)))
return NULL;
if (!(val = ci_headers_value(heads, head_name)))
return NULL;
return val;
}
const char *ci_http_request_get_header(ci_request_t * req, const char *head_name)
{
ci_headers_list_t *heads;
const char *val;
if (!(heads = ci_http_request_headers(req)))
return NULL;
if (!(val = ci_headers_value(heads, head_name)))
return NULL;
return val;
}
ci_off_t ci_http_content_length(ci_request_t * req)
{
ci_headers_list_t *heads;
const char *val;
ci_off_t res = 0;
char *e;
if (!(heads = ci_http_response_headers(req))) {
/*Then maybe is a reqmod reauest, try to get request headers */
if (!(heads = ci_http_request_headers(req)))
return 0;
}
if (!(val = ci_headers_value(heads, "Content-Length")))
return -1;
errno = 0;
res = ci_strto_off_t(val, &e, 10);
if (errno == ERANGE && (res == CI_STRTO_OFF_T_MAX || res == CI_STRTO_OFF_T_MIN)) {
ci_debug_printf(4, "Content-Length: overflow\n");
return -2;
}
if (val == e) {
ci_debug_printf(4, "Content-Length: not valid value: '%s' \n", val);
return -2;
}
return res;
}
const char *ci_http_request(ci_request_t * req)
{
ci_headers_list_t *heads;
if (!(heads = ci_http_request_headers(req)))
return NULL;
if (!heads->used)
return NULL;
return heads->headers[0];
}
const char *ci_icap_add_xheader(ci_request_t * req, const char *header)
{
return ci_headers_add(req->xheaders, header);
}
int ci_icap_append_xheaders(ci_request_t *req,ci_headers_list_t *headers)
{
return ci_headers_addheaders(req->xheaders, headers);
}
const char * ci_icap_request_get_header(ci_request_t *req, const char *header)
{
if (req && req->request_header)
return ci_headers_value(req->request_header, header);
return NULL;
}
const char * ci_icap_response_get_header(ci_request_t *req, const char *header)
{
if (req && req->response_header)
return ci_headers_value(req->response_header, header);
return NULL;
}
ci_headers_list_t *ci_icap_request_headers(ci_request_t *req)
{
return req ? req->request_header : NULL;
}
ci_headers_list_t *ci_icap_response_headers(ci_request_t *req)
{
return req ? req->response_header : NULL;
}
void ci_req_lock_data_non_inline(ci_request_t *req) {
return ci_req_lock_data_inline(req);
}
void ci_req_unlock_data_non_inline(ci_request_t *req)
{
return ci_req_unlock_data_inline(req);
}
int ci_req_hasbody_non_inline(const ci_request_t *req)
{
return ci_req_hasbody_inline(req);
}
int ci_req_type_non_inline(const ci_request_t *req)
{
return ci_req_type_inline(req);
}
int ci_req_preview_size_non_inline(const ci_request_t *req)
{
return ci_req_preview_size_inline(req);
}
int ci_req_allow204_non_inline(const ci_request_t *req)
{
return ci_req_allow204_inline(req);
}
int ci_req_allow206_non_inline(const ci_request_t *req)
{
return ci_req_allow206_inline(req);
}
int ci_req_allow206_outside_preview_non_inline(const ci_request_t *req)
{
return ci_req_allow206_outside_preview_inline(req);
}
int ci_req_sent_data_non_inline(const ci_request_t *req)
{
return ci_req_sent_data_inline(req);
}
int ci_req_hasalldata_non_inline(const ci_request_t *req)
{
return ci_req_hasalldata_inline(req);
}
#define header_end(e) (e == '\0' || e == '\n' || e == '\r')
int ci_http_request_url2(ci_request_t * req, char *buf, int buf_size, int flags)
{
ci_headers_list_t *heads;
const char *str, *host;
int i, bytes;
/*The request must have the form:
GET url HTTP/X.X
*/
if (!(heads = ci_http_request_headers(req)))
return 0;
if (!heads->used)
return 0;
str = heads->headers[0];
if ((str = strchr(str, ' ')) == NULL) { /*Ignore method i*/
return 0;
}
while (*str == ' ') /*ignore spaces*/
str++;
bytes = 0;
if (*str == '/' && (host = ci_headers_value(heads,"Host"))) {
/*Looks like a transparent proxy, we do not know the protocol lets try
to preserve the major part of the url....
*/
for (i = 0; (i < buf_size-1) && !header_end(host[i]) && !isspace((int)host[i]); i++) {
buf[i] = host[i];
}
buf += i;
buf_size -= i;
bytes = i;
}
/*copy the url...*/
if (flags & CI_HTTP_REQUEST_URL_ARGS)
for (i = 0; (i < buf_size-1) && !header_end(str[i]) && !isspace((int)str[i]); i++)
buf[i] = str[i];
else
for (i = 0; (i < buf_size-1) && !header_end(str[i]) && !isspace((int)str[i]) && str[i] != '?'; i++)
buf[i] = str[i];
buf[i] = '\0';
bytes += i;
return bytes;
}
int ci_http_request_url(ci_request_t * req, char *buf, int buf_size) {
return ci_http_request_url2(req, buf, buf_size, 0);
}
const ci_ip_t * ci_http_client_ip(ci_request_t * req)
{
const char *ip;
if (!req)
return NULL;
if (req->xclient_ip.family == -1) /*Already check and failed to read it*/
return NULL;
if (req->xclient_ip.family != 0) /*Already check, return cached*/
return &req->xclient_ip;
if (!(ip = ci_headers_value(req->request_header, "X-Client-IP")))
return NULL;
#ifdef HAVE_IPV6
if (strchr(ip, ':')) {
if (ci_inet_aton(AF_INET6, ip, &(req->xclient_ip.address))) {
req->xclient_ip.family = AF_INET6;
ci_ipv6_inaddr_hostnetmask(req->xclient_ip.netmask);
} else
req->xclient_ip.family = -1;
} else
#endif
{
if (ci_inet_aton(AF_INET, ip, &(req->xclient_ip.address))) {
req->xclient_ip.family = AF_INET;
ci_ipv4_inaddr_hostnetmask(req->xclient_ip.netmask);
} else
req->xclient_ip.family = -1;
}
if (req->xclient_ip.family == -1) /*Failed to read correctly*/
return NULL;
return &req->xclient_ip;
}
CI_DECLARE_FUNC(int) ci_http_response_content_encoding(ci_request_t *req)
{
ci_headers_list_t *headers = ci_http_response_headers(req);
if (headers) {
const char *content_encoding = ci_headers_value(headers, "Content-Encoding");
if (content_encoding)
return ci_encoding_method(content_encoding);
return CI_ENCODE_NONE;
}
return CI_ENCODE_UNKNOWN;
}