/
header.c
1221 lines (1011 loc) · 28.1 KB
/
header.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
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2014 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "common-internal.h"
#include "header.h"
#include "header-protected.h"
#include "util.h"
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <ctype.h>
#define ENTRIES "header"
#if 0
# define HEADER_INTERNAL_DEBUG
#endif
#ifdef HEADER_INTERNAL_DEBUG
# define HEADER_INTERNAL_CHECK(h) \
do { \
if (cherokee_buffer_crc32 (h->input_buffer) != h->input_buffer_crc) \
fprintf (stderr, "Header sanity check failed: %s, line %d\n", __FILE__, __LINE__); \
} while(0)
#else
# define HEADER_INTERNAL_CHECK(h)
#endif
#define cmp_str(l,s) (strncmp(l, s, sizeof(s)-1) == 0)
static void
clean_known_headers (cherokee_header_t *hdr)
{
int i;
for (i=0; i<HEADER_LENGTH; i++) {
hdr->header[i].info_off = 0;
hdr->header[i].info_len = -1;
}
}
static void
clean_unknown_headers (cherokee_header_t *hdr)
{
if (hdr->unknowns != NULL) {
free (hdr->unknowns);
hdr->unknowns = NULL;
hdr->unknowns_len = 0;
}
}
static void
clean_headers (cherokee_header_t *hdr)
{
clean_known_headers (hdr);
clean_unknown_headers (hdr);
}
ret_t
cherokee_header_init (cherokee_header_t *hdr, cherokee_header_type_t type)
{
/* Known headers
*/
hdr->type = type;
clean_known_headers (hdr);
/* Unknown headers
*/
hdr->unknowns = NULL;
hdr->unknowns_len = 0;
/* Properties
*/
hdr->method = http_unknown;
hdr->version = http_version_unknown;
hdr->response = http_unset;
/* Request
*/
hdr->request_off = 0;
hdr->request_len = 0;
hdr->request_args_len = 0;
/* Query string
*/
hdr->query_string_off = 0;
hdr->query_string_len = 0;
/* Sanity
*/
hdr->input_buffer = NULL;
hdr->input_buffer_crc = 0;
hdr->input_header_len = 0;
return ret_ok;
}
ret_t
cherokee_header_mrproper (cherokee_header_t *hdr)
{
clean_unknown_headers (hdr);
return ret_ok;
}
ret_t
cherokee_header_new (cherokee_header_t **hdr, cherokee_header_type_t type)
{
ret_t ret;
CHEROKEE_NEW_STRUCT (n, header);
ret = cherokee_header_init (n, type);
if (unlikely (ret != ret_ok)) return ret;
*hdr = n;
return ret_ok;
}
CHEROKEE_ADD_FUNC_FREE (header);
ret_t
cherokee_header_clean (cherokee_header_t *hdr)
{
clean_headers (hdr);
hdr->method = http_unknown;
hdr->version = http_version_unknown;
hdr->response = http_unset;
hdr->input_header_len = 0;
hdr->request_off = 0;
hdr->request_len = 0;
hdr->query_string_off = 0;
hdr->query_string_len = 0;
return ret_ok;
}
static ret_t
add_known_header (cherokee_header_t *hdr, cherokee_common_header_t header, off_t info_off, int info_len)
{
HEADER_INTERNAL_CHECK(hdr);
hdr->header[header].info_off = info_off;
hdr->header[header].info_len = info_len;
return ret_ok;
}
static ret_t
add_unknown_header (cherokee_header_t *hdr, off_t header_off, off_t info_off, int info_len)
{
cherokee_header_unknown_entry_t *entry;
hdr->unknowns_len++;
hdr->unknowns = (cherokee_header_unknown_entry_t *) realloc (
hdr->unknowns, sizeof(cherokee_header_unknown_entry_t) * hdr->unknowns_len);
if (hdr->unknowns == NULL) {
return ret_nomem;
}
entry = &hdr->unknowns[hdr->unknowns_len-1];
entry->header_off = header_off;
entry->header_info_off = info_off;
entry->header_info_len = info_len;
return ret_ok;
}
static ret_t
parse_response_first_line (cherokee_header_t *hdr, cherokee_buffer_t *buf, char **next_pos, cherokee_http_t *error_code)
{
ret_t ret;
char tmp[4];
char *end;
size_t len;
int n;
char *line = buf->buf;
char *begin = buf->buf;
/* NOTE: here we deal only with HTTP/1.0 or higher.
*/
len = strcspn(line, CRLF);
end = &line[len];
/* Some security checks
*/
if (len < 14 || buf->len < 14) {
return ret_error;
}
/* Return next line
*/
switch (*end) {
case CHR_CR:
if (end[1] != CHR_LF)
return ret_error;
*next_pos = end + 2;
break;
case CHR_LF:
*next_pos = end + 1;
break;
default:
return ret_error;
}
/* Example:
* HTTP/1.0 403 Forbidden
*/
if (unlikely(! cmp_str(begin, "HTTP/1."))) {
*error_code = http_version_not_supported;
return ret_error;
}
if (unlikely(! isdigit(begin[7]))) {
return ret_error;
}
/* Get the HTTP version
*/
switch (begin[7]) {
case '0':
hdr->version = http_version_10;
break;
case '1':
default:
hdr->version = http_version_11;
break;
}
/* Skip whites - there must at least one
*/
if (unlikely (CHEROKEE_CHAR_IS_WHITE (begin[8]))) {
return ret_error;
}
n = 9;
while (CHEROKEE_CHAR_IS_WHITE (begin[n])) {
n++;
}
/* Read the response code
*/
if (unlikely ((begin + n + 3 > end))) {
return ret_error;
}
memcpy (tmp, begin+n, 3);
tmp[3] = '\0';
ret = cherokee_atoi (tmp, (int *)&hdr->response);
if (unlikely (ret != ret_ok)) {
return ret_error;
}
return ret_ok;
}
static ret_t
parse_method (cherokee_header_t *hdr, char *line, char *end, char **pointer)
{
char *p;
char chr = *line;
#define detect_method(l,str,mthd) \
if (cmp_str (line, (str" "))) { \
hdr->method = http_ ## mthd; \
*pointer += sizeof(str); \
return ret_ok; \
}
/* Check the first letter of the method name, if it matches it
* can continue with the rest.
*/
switch (chr) {
case 'G':
detect_method (line, "GET", get)
break;
case 'P':
detect_method (line, "POST", post)
else
detect_method (line, "PUT", put)
else
detect_method (line, "PURGE", purge)
else
detect_method (line, "PATCH", patch)
else
detect_method (line, "POLL", poll)
else
detect_method (line, "PROPFIND", propfind)
else
detect_method (line, "PROPPATCH", proppatch)
break;
case 'H':
detect_method (line, "HEAD", head)
break;
case 'O':
detect_method (line, "OPTIONS", options)
break;
case 'D':
detect_method (line, "DELETE", delete)
break;
case 'T':
detect_method (line, "TRACE", trace)
break;
case 'C':
detect_method (line, "CHECKIN", checkin)
else
detect_method (line, "CHECKOUT", checkout)
else
detect_method (line, "CONNECT", connect)
else
detect_method (line, "COPY", copy)
break;
case 'L':
detect_method (line, "LABEL", label)
else
detect_method (line, "LOCK", lock)
break;
case 'M':
detect_method (line, "MERGE", merge)
else
detect_method (line, "MKACTIVITY", mkactivity)
else
detect_method (line, "MKCOL", mkcol)
else
detect_method (line, "MKWORKSPACE", mkworkspace)
else
detect_method (line, "MOVE", move)
break;
case 'N':
detect_method (line, "NOTIFY", notify)
break;
case 'S':
detect_method (line, "SEARCH", search)
else
detect_method (line, "SUBSCRIBE", subscribe)
break;
case 'U':
detect_method (line, "UPDATE", update)
else
detect_method (line, "UNCHECKOUT", uncheckout)
else
detect_method (line, "UNLOCK", unlock)
else
detect_method (line, "UNSUBSCRIBE", unsubscribe)
break;
case 'R':
detect_method (line, "REPORT", report)
break;
case 'V':
detect_method (line, "VERSION-CONTROL", version_control)
break;
case 'B':
detect_method (line, "BASELINE-CONTROL", baseline_control)
break;
case 'I':
detect_method (line, "INVALID", invalid)
break;
}
/* The method was not matched. Skip the first word.
*/
p = line;
while (p < end) {
if (*p == ' ') {
hdr->method = http_unknown;
*pointer += p - line;
return ret_ok;
}
p++;
}
return ret_error;
}
static ret_t
parse_request_first_line (cherokee_header_t *hdr, cherokee_buffer_t *buf, char **next_pos, cherokee_http_t *error_code)
{
ret_t ret;
char *line = buf->buf;
char *begin = buf->buf;
char *begin_ver = buf->buf;
char *end;
char *ptr;
char *restore;
char chr_end;
size_t len = 0;
size_t len_ver = 0;
/* Basic security check. The shortest possible request
* "GET / HTTP/1.0" is 14 characters long but we want
* to reply (with an error message) to HTTP/0.9 requests too
* (HTTP/0.9 don't have " HTTP/x.x" version).
*/
if (unlikely(buf->len < 6)) {
return ret_error;
}
/* Look for the end of the request line
*/
end = strchr (line, CHR_LF);
if (unlikely(end == NULL || line == end)) {
return ret_error;
}
--end;
if (unlikely(*end != CHR_CR)) {
++end;
/* Return begin of next line
*/
len = (size_t) (end - line);
*next_pos = end + 1;
} else {
/* Return begin of next line
*/
len = (size_t) (end - line);
*next_pos = end + 2;
}
chr_end = *end;
*end = '\0';
restore = end;
/* Check shortest string length "GET /"
*/
if (len < 5)
goto error;
/* Get the method
*/
ret = parse_method (hdr, line, end, &begin);
if (unlikely (ret != ret_ok)) {
*error_code = http_not_implemented;
goto error;
}
/* Skip other blank spaces between method and URI.
*/
begin += strspn (begin, LBS);
/* Length of URI.
*/
len = strcspn (begin, LBS);
if (len == 0)
goto error;
if (begin[len] == '\0') {
/* HTTP/0.9 has no HTTP version string and
* it is not supported by this server.
*/
*error_code = http_version_not_supported;
goto error;
}
begin_ver = &begin[len];
begin_ver += strspn (begin_ver, LBS);
len_ver = (size_t) (end - begin_ver);
/* Verify "HTTP/x.x" pattern.
*/
if (len_ver != 8 ||
!isdigit(begin_ver[5]) ||
begin_ver[6] != '.' ||
!isdigit(begin_ver[7]) ||
!cmp_str (begin_ver, "HTTP/")) {
goto error;
}
/* Get the protocol version.
*/
if (begin_ver[5] != '1' || begin_ver[7] > '1') {
*error_code = http_version_not_supported;
goto error;
}
hdr->version = (begin_ver[7] == '1' ? http_version_11 : http_version_10);
/* Skip the HTTP version string: " HTTP/x.y"
*/
end = &begin[len];
/* Look for the QueryString
*/
hdr->request_args_len = len;
ptr = strchr (begin, '?');
if (ptr) {
end = ptr;
hdr->query_string_off = (off_t) (++ptr - buf->buf);
hdr->query_string_len = (cint_t) (&begin[len] - ptr);
} else {
hdr->query_string_len = 0;
}
/* Get the request
*/
hdr->request_off = (off_t) (begin - buf->buf);
hdr->request_len = (cint_t) (end - begin);
/* Check if the request is a full URL
*/
begin = buf->buf + hdr->request_off;
if (cmp_str (begin, "http://")) {
char *dir;
char *host = begin + 7;
char end_chr = *end;
if (host[0] == '/' || host[0] == '.')
goto error;
*end = '\0';
dir = strchr (host, '/');
*end = end_chr;
if (dir == NULL)
goto error;
/* Add the host header
*/
add_known_header (hdr, header_host, host - buf->buf, dir - host);
/* Fix the URL
*/
hdr->request_len -= (dir - begin);
hdr->request_off = dir - buf->buf;
}
*restore = chr_end;
return ret_ok;
error:
*restore = chr_end;
return ret_error;
}
ret_t
cherokee_header_get_length (cherokee_header_t *hdr, cuint_t *len)
{
*len = hdr->input_header_len;
return ret_ok;
}
ret_t
cherokee_header_get_unknown (cherokee_header_t *hdr,
const char *name, cuint_t name_len,
char **header, cuint_t *header_len)
{
int i;
HEADER_INTERNAL_CHECK(hdr);
for (i=0; i < hdr->unknowns_len; i++) {
char *h = hdr->unknowns[i].header_off + hdr->input_buffer->buf;
if (strncasecmp (h, name, name_len) == 0) {
*header = hdr->unknowns[i].header_info_off + hdr->input_buffer->buf;
*header_len = hdr->unknowns[i].header_info_len;
return ret_ok;
}
}
return ret_not_found;
}
ret_t
cherokee_header_copy_unknown (cherokee_header_t *hdr,
const char *name,
cuint_t name_len,
cherokee_buffer_t *buf)
{
ret_t ret;
char *info;
cuint_t info_len;
ret = cherokee_header_get_unknown (hdr, name, name_len, &info, &info_len);
if (unlikely(ret != ret_ok))
return ret;
return cherokee_buffer_add (buf, info, info_len);
}
ret_t
cherokee_header_has_known (cherokee_header_t *hdr, cherokee_common_header_t header)
{
if (hdr->header[header].info_off != 0) {
return ret_ok;
}
return ret_not_found;
}
ret_t
cherokee_header_get_known (cherokee_header_t *hdr, cherokee_common_header_t header, char **info, cuint_t *info_len)
{
HEADER_INTERNAL_CHECK(hdr);
if (hdr->header[header].info_off == 0) {
return ret_not_found;
}
*info = hdr->header[header].info_off + hdr->input_buffer->buf;
*info_len = hdr->header[header].info_len;
return ret_ok;
}
ret_t
cherokee_header_copy_known (cherokee_header_t *hdr, cherokee_common_header_t header, cherokee_buffer_t *buf)
{
ret_t ret;
char *info = NULL;
cuint_t info_len = 0;
ret = cherokee_header_get_known (hdr, header, &info, &info_len);
if (unlikely(ret != ret_ok))
return ret;
return cherokee_buffer_add (buf, info, info_len);
}
static ret_t
sanitize_buffer (cherokee_buffer_t *buf)
{
cuint_t i, j;
cuint_t removed = 0;
/* Look for the ANSI CSIs (0x1B+[, and 0x9B)
*/
for (i=0,j=0; j<buf->len;) {
if (unlikely ((j+1 < buf->len) &&
(buf->buf[i] == 0x1B) &&
(buf->buf[i+1] == '[' || buf->buf[i+1] == ']')))
{
j += 2;
removed += 2;
}
if (removed) {
buf->buf[i] = buf->buf[j];
}
i++;
j++;
}
/* Fix the buffer length
*/
if (removed) {
buf->len -= removed;
buf->buf[buf->len] = 0;
}
return ret_ok;
}
ret_t
cherokee_header_copy_request (cherokee_header_t *hdr, cherokee_buffer_t *request)
{
ret_t ret;
if ((hdr->request_off == 0) ||
(hdr->request_len <= 0)) {
return ret_error;
}
ret = cherokee_buffer_add (request, hdr->input_buffer->buf + hdr->request_off, hdr->request_len);
if (unlikely(ret < ret_ok)) {
return ret;
}
ret = cherokee_buffer_unescape_uri (request);
if (unlikely (ret != ret_ok)) {
return ret_error;
}
return sanitize_buffer (request);
}
ret_t
cherokee_header_copy_query_string (cherokee_header_t *hdr, cherokee_buffer_t *query_string)
{
ret_t ret;
if ((hdr->query_string_off == 0) ||
(hdr->query_string_len <= 0)) {
return ret_not_found;
}
ret = cherokee_buffer_add (query_string, hdr->input_buffer->buf + hdr->query_string_off, hdr->query_string_len);
if (unlikely(ret < ret_ok))
return ret;
return ret_ok;
}
ret_t
cherokee_header_get_request_w_args (cherokee_header_t *hdr, char **req, int *req_len)
{
if ((hdr->request_off == 0) ||
(hdr->request_args_len <= 0)) {
return ret_error;
}
if (req != NULL)
*req = hdr->input_buffer->buf + hdr->request_off;
if (req_len != NULL)
*req_len = hdr->request_args_len;
return ret_ok;
}
ret_t
cherokee_header_copy_request_w_args (cherokee_header_t *hdr, cherokee_buffer_t *request)
{
ret_t ret;
if ((hdr->request_off == 0) ||
(hdr->request_args_len <= 0)) {
return ret_error;
}
ret = cherokee_buffer_add (request, hdr->input_buffer->buf + hdr->request_off, hdr->request_args_len);
if (unlikely(ret < ret_ok))
return ret;
return cherokee_buffer_unescape_uri (request);
}
ret_t
cherokee_header_copy_method (cherokee_header_t *hdr, cherokee_buffer_t *buf)
{
ret_t ret;
const char *tmp;
cuint_t len;
ret = cherokee_http_method_to_string (HDR_METHOD(hdr), &tmp, &len);
if (unlikely(ret != ret_ok))
return ret;
return cherokee_buffer_add (buf, tmp, len);
}
ret_t
cherokee_header_copy_version (cherokee_header_t *hdr, cherokee_buffer_t *buf)
{
ret_t ret;
const char *tmp;
cuint_t len;
ret = cherokee_http_version_to_string (HDR_VERSION(hdr), &tmp, &len);
if (unlikely(ret != ret_ok))
return ret;
return cherokee_buffer_add (buf, tmp, len);
}
static ret_t
has_header_response (cherokee_header_t *hdr, cherokee_buffer_t *buffer)
{
char *tmp;
tmp = strstr (buffer->buf, CRLF_CRLF);
if (tmp == NULL)
return ret_not_found;
/* Content until CRLF_CRLF plus two of the last line CRLF
*/
hdr->input_header_len = (tmp - buffer->buf) + 2;
return ret_ok;
}
static ret_t
has_header_request (cherokee_header_t *hdr, cherokee_buffer_t *buffer, cuint_t tail_len)
{
ret_t ret;
char *start;
char *end;
cuint_t crlf_len = 0;
/* Skip initial CRLFs:
* NOTE: they are not allowed by standard (RFC) but many
* popular HTTP clients (including MSIE, etc.) may send a
* couple of them between two requests, so every widely used
* web server has to deal with them.
*/
crlf_len = cherokee_buffer_cnt_spn (buffer, 0, CRLF);
if (unlikely (crlf_len > MAX_HEADER_CRLF)) {
/* Too many initial CRLF
*/
return ret_error;
}
if ((crlf_len > 0) && (crlf_len < buffer->len)) {
/* Found heading CRLFs and their length is less than
* buffer length so we have to move the real content
* to the beginning of the buffer.
*/
cherokee_buffer_move_to_begin (buffer, (int) crlf_len);
}
/* Do we have enough information ?
* len("GET /" LF_LF) = 7 (HTTP/0.9)
* len("GET /" CRLF_CRLF) = 9 (HTTP/0.9)
* len("GET / HTTP/1.0" CRLF_CRLF) = 18 (HTTP/1.x)
*/
if (unlikely (buffer->len < 7)) {
return ret_not_found;
}
/* Look for the starting point
*/
start = (tail_len >= buffer->len) ?
buffer->buf : buffer->buf + (buffer->len - tail_len);
/* It could be a partial header, or maybe a POST request
*/
ret = cherokee_find_header_end_cstr (start,
(buffer->buf + buffer->len) - start,
&end, &crlf_len);
if (ret != ret_ok) {
return ret_not_found;
}
hdr->input_header_len = ((int) (end - buffer->buf)) + crlf_len;
return ret_ok;
}
ret_t
cherokee_header_has_header (cherokee_header_t *hdr, cherokee_buffer_t *buffer, int tail_len)
{
switch (hdr->type) {
case header_type_request:
return has_header_request (hdr, buffer, tail_len);
case header_type_response:
case header_type_basic:
return has_header_response (hdr, buffer);
default:
SHOULDNT_HAPPEN;
}
return ret_error;
}
ret_t
cherokee_header_parse (cherokee_header_t *hdr, cherokee_buffer_t *buffer, cherokee_http_t *error_code)
{
ret_t ret;
char *val_beg;
char *val_end;
char *header_end;
char chr_header_end;
char *begin = buffer->buf;
char *end = NULL;
/* Set default error code.
*/
*error_code = http_bad_request;
/* Check the buffer content
*/
if ((buffer->buf == NULL) || (buffer->len < 5)) {
LOG_ERROR_S (CHEROKEE_ERROR_HEADER_EMPTY);
return ret_error;
}
/* Set the header buffer
*/
hdr->input_buffer = buffer;
#ifdef HEADER_INTERNAL_DEBUG
hdr->input_buffer_crc = cherokee_buffer_crc32 (buffer);
#endif
/* header_len should have already been set by a previous call
* to cherokee_header_has_header() but if not we call it now.
*/
if (unlikely (hdr->input_header_len < 1)) {
/* Strange, anyway go on and look for EOH
*/
ret = has_header_request (hdr, buffer, buffer->len);
if (ret != ret_ok) {
if (ret == ret_not_found) {
LOG_ERROR (CHEROKEE_ERROR_HEADER_NO_EOH,
buffer->len, buffer->buf);
} else {
LOG_ERROR_S (CHEROKEE_ERROR_HEADER_TOO_MANY_CRLF);
}
return ret_error;
}
}
header_end = &(buffer->buf[hdr->input_header_len]);
/* Terminate current request space (there may be other
* pipelined requests in the buffer) after the EOH.
*/
chr_header_end = *header_end;
*header_end = '\0';
TRACE(ENTRIES, "Incoming header:\n%s", buffer->buf);
/* Parse the special first line
*/
switch (hdr->type) {
case header_type_request:
/* Parse request. Something like this:
* GET /icons/compressed.png HTTP/1.1CRLF
*/
ret = parse_request_first_line (hdr, buffer, &begin, error_code);
if (unlikely(ret < ret_ok)) {
PRINT_DEBUG ("ERROR: Failed to parse header_type_request:\n===\n%s===\n", buffer->buf);
*header_end = chr_header_end;
return ret;
}
break;
case header_type_response:
ret = parse_response_first_line (hdr, buffer, &begin, error_code);
if (unlikely(ret < ret_ok)) {
PRINT_DEBUG ("ERROR: Failed to parse header_type_response:\n===\n%s===\n", buffer->buf);
*header_end = chr_header_end;
return ret;
}
break;
case header_type_basic:
/* Don't do anything
*/
break;
default:
*header_end = chr_header_end;
SHOULDNT_HAPPEN;
}
/* Parse the rest of headers
*/
while ((begin < header_end)) {
cuint_t header_len;
int val_offs;