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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
/******************************************************************
* $Id: nanohttp-common.c,v 1.7 2004/01/21 12:28:20 snowdrop Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Email: ayaz@jprogrammer.net
******************************************************************/
#include <nanohttp/nanohttp-common.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
static log_level_t loglevel = HLOG_DEBUG;
log_level_t log_set_level(log_level_t level)
{
log_level_t old = loglevel;
loglevel = level;
return old;
}
log_level_t log_get_level()
{
return loglevel;
}
static
void log_write(log_level_t level, const char *prefix,
const char* func, const char *format, va_list ap)
{
char buffer[1054];
char buffer2[1054];
if (level < loglevel) return;
sprintf(buffer, "*%s*: [%s] %s\n", prefix, func, format);
vsprintf(buffer2, buffer, ap);
printf(buffer2);
fflush(stdout);
}
void log_verbose(const char* FUNC, const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_write(HLOG_VERBOSE, "VERBOSE", FUNC, format, ap);
va_end(ap);
}
void log_debug(const char* FUNC, const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_write(HLOG_DEBUG, "DEBUG", FUNC, format, ap);
va_end(ap);
}
void log_info(const char* FUNC, const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_write(HLOG_INFO, "INFO", FUNC, format, ap);
va_end(ap);
}
void log_warn(const char* FUNC, const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_write(HLOG_WARN, "WARN", FUNC, format, ap);
va_end(ap);
}
void log_error(const char* FUNC, const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_write(HLOG_ERROR, "ERROR", FUNC, format, ap);
va_end(ap);
}
/* -----------------------------------------
FUNCTION: strcmpigcase
------------------------------------------ */
int strcmpigcase(const char *s1, const char *s2)
{
int l1, l2, i;
if (s1 == NULL && s2 == NULL) return 1;
if (s1 == NULL || s2 == NULL) return 0;
l1 = strlen(s1);
l2 = strlen(s2);
if (l1 != l2) return 0;
for (i=0;i<l1;i++)
if (toupper(s1[i]) != toupper(s2[i]))
return 0;
return 1;
}
hpair_t *hpairnode_new(const char* key, const char* value, hpair_t *next)
{
hpair_t *pair;
log_verbose3("new pair ('%s','%s')", SAVE_STR(key), SAVE_STR(value));
pair = (hpair_t*)malloc(sizeof(hpair_t));
if (key != NULL) {
pair->key = (char*)malloc(strlen(key)+1);
strcpy(pair->key, key);
} else {
pair->key = NULL;
}
if (value != NULL) {
pair->value = (char*)malloc(strlen(value)+1);
strcpy(pair->value, value);
} else {
pair->value = NULL;
}
pair->next = next;
return pair;
}
hpair_t *hpairnode_parse(const char *str, const char *delim, hpair_t *next)
{
hpair_t *pair;
char *key, *value;
int c;
pair = (hpair_t*)malloc(sizeof(hpair_t));
pair->key = "";
pair->value = "";
pair->next = next;
key = strtok_r(str, delim, &value);
if (key != NULL) {
pair->key = (char*)malloc(strlen(key)+1);
strcpy(pair->key, key);
}
if (value != NULL) {
for (c=0;value[c]==' ';c++); /* skip white space */
pair->value = (char*)malloc(strlen(&value[c])+1);
strcpy(pair->value, &value[c]);
}
return pair;
}
hpair_t* hpairnode_copy(const hpair_t *src)
{
hpair_t *pair;
if (src == NULL) return NULL;
pair = hpairnode_new(src->key, src->value, NULL);
return pair;
}
hpair_t* hpairnode_copy_deep(const hpair_t *src)
{
hpair_t *pair, *result, *next;
if (src == NULL) return NULL;
result = hpairnode_copy(src);
next = src->next;
pair = result;
while (next != NULL) {
pair->next = hpairnode_copy(next);
pair = pair->next;
next = next->next;
}
return result;
}
void hpairnode_dump(hpair_t *pair)
{
if (pair == NULL) {
log_verbose1("(NULL)[]");
return;
}
log_verbose5("(%p)['%s','%s','%p']", pair,
SAVE_STR(pair->key), SAVE_STR(pair->value),
pair->next);
}
void hpairnode_dump_deep(hpair_t *pair)
{
hpair_t *p;
p = pair;
log_verbose1("-- BEGIN dump hpairnode_t --");
while (p != NULL) {
hpairnode_dump(p);
p = p->next;
}
log_verbose1("-- END dump hpairnode_t --\n");
}
void hpairnode_free(hpair_t *pair)
{
if (pair == NULL) return;
free(pair->key);
free(pair->value);
free(pair);
}
char *hpairnode_get_ignore_case(hpair_t *pair, const char* key)
{
if (key == NULL) {
log_error1("key is NULL");
return NULL;
}
while (pair != NULL) {
if (pair->key != NULL) {
if (strcmpigcase(pair->key, key)) {
return pair->value;
}
}
pair = pair->next;
}
return NULL;
}
char *hpairnode_get(hpair_t *pair, const char* key)
{
if (key == NULL) {
log_error1("key is NULL");
return NULL;
}
while (pair != NULL) {
if (pair->key != NULL) {
if (!strcmp(pair->key, key)) {
return pair->value;
}
}
pair = pair->next;
}
return NULL;
}
static
void hurl_dump(const hurl_t *url)
{
if (url == NULL) {
log_error1("url is NULL!");
return ;
}
log_verbose2("PROTOCOL : %s", SAVE_STR(url->protocol));
log_verbose2(" HOST : %s", SAVE_STR(url->host));
log_verbose2(" PORT : %d", url->port);
log_verbose2(" CONTEXT : %s", SAVE_STR(url->context));
}
hurl_t* hurl_new(const char* urlstr)
{
int iprotocol;
int ihost;
int iport;
int len;
int size;
hurl_t *url;
char tmp[8];
iprotocol = 0;
len = strlen(urlstr);
/* find protocol */
while (urlstr[iprotocol] != ':' && urlstr[iprotocol] != '\0')
{
iprotocol++;
}
if (iprotocol == 0) {
log_error1("no protocol");
return NULL;
}
if (iprotocol + 3 >= len) {
log_error1("no host");
return NULL;
}
if ( urlstr[iprotocol] != ':'
&& urlstr[iprotocol+1] != '/'
&& urlstr[iprotocol+2] != '/')
{
log_error1("no protocol");
return NULL;
}
/* find host */
ihost = iprotocol + 3;
while (urlstr[ihost] != ':'
&& urlstr[ihost] != '/'
&& urlstr[ihost] != '\0')
{
ihost++;
}
if (ihost == iprotocol + 1) {
log_error1("no host");
return NULL;
}
/* find port */
iport = ihost;
if (ihost + 1 < len) {
if (urlstr[ihost] == ':') {
while (urlstr[iport] != '/' && urlstr[iport] != '\0') {
iport++;
}
}
}
url = (hurl_t*)malloc(sizeof(hurl_t));
url->protocol = (char*)malloc(sizeof(char)*iprotocol+1);
strncpy(url->protocol, urlstr, iprotocol);
url->protocol[iprotocol] = '\0';
size = ihost - iprotocol - 3;
url->host = (char*)malloc(sizeof(char)*size + 1);
strncpy(url->host, &urlstr[iprotocol+3], size);
url->host[size] = '\0';
if (iport > ihost)
{
size = iport - ihost;
strncpy(tmp, &urlstr[ihost+1], size);
url->port = atoi(tmp);
} else {
url->port = 80;
}
len = strlen(urlstr);
if (len > iport )
{
size = len - iport;
url->context = (char*)malloc(sizeof(char)*size+1);
strncpy(url->context, &urlstr[iport], size);
url->context[size]='\0';
} else {
url->context = NULL;
}
hurl_dump(url);
return url;
}
void hurl_free(hurl_t *url)
{
if (url == NULL) return;
free(url->protocol);
free(url->host);
free(url->context);
free(url);
}
/* request stuff */
/* -----------------------------------------------------
FUNCTION: hrequest_new_from_buffer
----------------------------------------------------- */
hrequest_t *hrequest_new_from_buffer(char *data)
{
hrequest_t *req;
hpair_t *hpair = NULL, *qpair = NULL, *tmppair = NULL;
char *tmp;
char *tmp2;
char *saveptr;
char *saveptr2;
char *saveptr3;
char *result;
char *key;
char *value;
char *opt_key;
char *opt_value;
int firstline = 1;
req = (hrequest_t*)malloc(sizeof(hrequest_t));
req->method = NULL;
req->spec = NULL;
req->path = NULL;
req->query = NULL;
req->header = NULL;
tmp = data;
for (;;) {
result = (char*)strtok_r(tmp, "\n", &saveptr);
tmp=saveptr;
if (result == NULL)
break;
if (firstline) {
firstline=0;
tmp2 = result;
/* parse [GET|POST] [PATH] [SPEC] */
key = (char*)strtok_r(tmp2," ", &saveptr2);
/* save method (get or post) */
tmp2 = saveptr2;
if (key != NULL) {
req->method = (char*)malloc(strlen(key)+1);
strcpy(req->method, key);
}
/* below is key the path and tmp2 the spec */
key = (char*)strtok_r(tmp2," ", &saveptr2);
/* save spec */
tmp2 = saveptr2;
if (tmp2 != NULL) {
req->spec = (char*)malloc(strlen(tmp2)+1);
strcpy(req->spec, tmp2);
}
/* parse and save path+query
parse: /path/of/target?key1=value1&key2=value2...
*/
if (key != NULL) {
tmp2 = key;
key = (char*)strtok_r(tmp2,"?", &saveptr2);
tmp2 = saveptr2;
/* save path */
req->path = (char*)malloc(strlen(key)+1);
strcpy(req->path, key);
/* parse options */
for (;;) {
key = (char*)strtok_r(tmp2,"&",&saveptr2);
tmp2 = saveptr2;
if (key == NULL)
break;
opt_key = (char*)strtok_r(key,"=", &saveptr3);
opt_value = saveptr3;
if (opt_value == NULL)
opt_value = "";
/* create option pair */
if (opt_key != NULL) {
tmppair = (hpair_t*)malloc(sizeof(hpair_t));
if (req->query == NULL) {
req->query = qpair = tmppair;
} else {
qpair->next = tmppair;
qpair = tmppair;
}
/* fill hpairnode_t struct */
qpair->next = NULL;
qpair->key = (char*)malloc(strlen(opt_key)+1);
qpair->value = (char*)malloc(strlen(opt_value)+1);
strcpy(qpair->key, opt_key);
strcpy(qpair->value, opt_value);
}
}
}
} else {
/* parse "key: value" */
tmp2 = result;
key = (char*)strtok_r(tmp2, ": ", &saveptr2);
value = saveptr2;
/* create pair */
tmppair = (hpair_t*)malloc(sizeof(hpair_t));
if (req->header == NULL) {
req->header = hpair = tmppair;
} else {
hpair->next = tmppair;
hpair = tmppair;
}
/* fill pairnode_t struct */
hpair->next = NULL;
hpair->key = (char*)malloc(strlen(key)+1);
hpair->value = (char*)malloc(strlen(value)+1);
strcpy(hpair->key, key);
strcpy(hpair->value, value);
}
}
return req;
}
void hrequest_free(hrequest_t *req)
{
log_warn1("hrequest_free() not implemented!");
}
/* response stuff */
/* -------------------------------------------
FUNCTION: hresponse_new
---------------------------------------------*/
hresponse_t *hresponse_new()
{
hresponse_t *res;
/* create response object */
res = (hresponse_t*)malloc(sizeof(hresponse_t));
res->spec[0] = '\0';
res->errcode = -1;
res->desc = NULL;
res->header = NULL;
res->body = NULL;
res->bodysize = 0;
return res;
}
/* -------------------------------------------
FUNCTION: hresponse_new_from_buffer
---------------------------------------------*/
hresponse_t *hresponse_new_from_buffer(const char* buffer)
{
hresponse_t *res;
char *s1, *s2, *str;
hpair_t *pair;
/* create response object */
res = hresponse_new();
/* *** parse spec *** */
/* [HTTP/1.1 | 1.2] [CODE] [DESC] */
/* stage 1: HTTP spec */
str = (char*)strtok_r(buffer, " ", &s2);
s1 = s2;
if (str == NULL) { log_error1("Parse error"); return NULL; }
strncpy(res->spec, str, 10);
/* stage 2: http code */
str = (char*)strtok_r(s1, " ", &s2);
s1 = s2;
if (str == NULL) { log_error1("Parse error"); return NULL; }
res->errcode = atoi(str);
/* stage 3: description text */
str = (char*)strtok_r(s1, "\r\n", &s2);
s1 = s2;
if (str == NULL) { log_error1("Parse error"); return NULL; }
res->desc = (char*)malloc(strlen(str)+1);
strcpy(res->desc, str);
res->desc[strlen(str)] = '\0';
/* *** parse header *** */
/* [key]: [value] */
for (;;) {
str = strtok_r(s1, "\n", &s2);
s1 = s2;
/* check if header ends without body */
if (str == NULL) {
return res;
}
/* check also for end of header */
if (!strcmp(str, "\r")) {
break;
}
str[strlen(str)-1] = '\0';
res->header = hpairnode_parse(str, ":", res->header);
}
/* *** Save body *** */
res->body = s1;
/* return response object */
return res;
}
void hresponse_free(hresponse_t *res)
{
/* not implemented yet!*/
}
|