15 static int request_check_hostname(server
*srv
, connection
*con
, buffer
*host
) {
16 enum { DOMAINLABEL
, TOPLABEL
} stage
= TOPLABEL
;
21 int is_ip
= -1; /* -1 don't know yet, 0 no, 1 yes */
28 * hostport = host [ ":" port ]
29 * host = hostname | IPv4address | IPv6address
30 * hostname = *( domainlabel "." ) toplabel [ "." ]
31 * domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
32 * toplabel = alpha | alpha *( alphanum | "-" ) alphanum
33 * IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
34 * IPv6address = "[" ... "]"
39 if (buffer_is_empty(host
)) return 0;
41 host_len
= buffer_string_length(host
);
44 if (host
->ptr
[0] == '[') {
45 char *c
= host
->ptr
+ 1;
48 /* check the address inside [...] */
49 for (; *c
&& *c
!= ']'; c
++) {
51 if (++colon_cnt
> 7) {
54 } else if (!light_isxdigit(*c
) && '.' != *c
) {
66 for (c
+= 2; *c
; c
++) {
67 if (!light_isdigit(*c
)) {
72 else if ('\0' != *(c
+1)) {
73 /* only a port is allowed to follow [...] */
79 if (NULL
!= (colon
= memchr(host
->ptr
, ':', host_len
))) {
82 /* check portnumber */
84 if (!light_isdigit(*c
)) return -1;
87 /* remove the port from the host-len */
88 host_len
= colon
- host
->ptr
;
92 if (host_len
== 0) return -1;
94 /* if the hostname ends in a "." strip it */
95 if (host
->ptr
[host_len
-1] == '.') {
96 /* shift port info one left */
97 if (NULL
!= colon
) memmove(colon
-1, colon
, buffer_string_length(host
) - host_len
);
98 buffer_string_set_length(host
, buffer_string_length(host
) - 1);
102 if (host_len
== 0) return -1;
104 /* scan from the right and skip the \0 */
105 for (i
= host_len
; i
-- > 0; ) {
106 const char c
= host
->ptr
[i
];
111 /* only switch stage, if this is not the last character */
112 if (i
!= host_len
- 1) {
113 if (label_len
== 0) {
117 /* check the first character at right of the dot */
119 if (!light_isalnum(host
->ptr
[i
+1])) {
122 } else if (!light_isdigit(host
->ptr
[i
+1])) {
124 } else if ('-' == host
->ptr
[i
+1]) {
136 /* just a dot and nothing else is evil */
140 /* the first character of the hostname */
141 if (!light_isalnum(c
)) {
146 if (c
!= '-' && !light_isalnum(c
)) {
150 if (!light_isdigit(c
)) is_ip
= 0;
159 if (label_len
== 0) {
165 } else if (!light_isdigit(c
)) {
172 if (label_len
== 0) {
176 /* c is either - or alphanum here */
177 if ('-' == host
->ptr
[i
+1]) {
184 if (!light_isalnum(c
)) {
189 if (c
!= '-' && !light_isalnum(c
)) {
200 /* a IP has to consist of 4 parts */
201 if (is_ip
== 1 && level
!= 3) {
205 if (label_len
== 0) {
216 static int http_request_split_value(array
*vals
, buffer
*b
) {
221 const char *token_start
= NULL
, *token_end
= NULL
;
225 * val1, val2, val3, val4
227 * into a array (more or less a explode() incl. striping of whitespaces
230 if (buffer_string_is_empty(b
)) return 0;
233 len
= buffer_string_length(b
);
234 for (i
= 0; i
<= len
; ++i
, ++current
) {
238 case 0: /* find start of a token */
241 case '\t': /* skip white space */
242 case ',': /* skip empty token */
244 case '\0': /* end of string */
247 /* found real data, switch to state 1 to find the end of the token */
248 token_start
= token_end
= current
;
253 case 1: /* find end of token and last non white space character */
257 /* space - don't update token_end */
260 case '\0': /* end of string also marks the end of a token */
261 if (NULL
== (ds
= (data_string
*)array_get_unused_element(vals
, TYPE_STRING
))) {
262 ds
= data_string_init();
265 buffer_copy_string_len(ds
->value
, token_start
, token_end
-token_start
+1);
266 array_insert_unique(vals
, (data_unset
*)ds
);
271 /* no white space, update token_end to include current character */
282 static int request_uri_is_valid_char(unsigned char c
) {
283 if (c
<= 32) return 0;
284 if (c
== 127) return 0;
285 if (c
== 255) return 0;
290 int http_request_parse(server
*srv
, connection
*con
) {
291 char *uri
= NULL
, *proto
= NULL
, *method
= NULL
, con_length_set
;
292 int is_key
= 1, key_len
= 0, is_ws_after_key
= 0, in_folding
;
293 char *value
= NULL
, *key
= NULL
;
294 char *reqline_host
= NULL
;
295 int reqline_hostlen
= 0;
297 enum { HTTP_CONNECTION_UNSET
, HTTP_CONNECTION_KEEPALIVE
, HTTP_CONNECTION_CLOSE
} keep_alive_set
= HTTP_CONNECTION_UNSET
;
301 int request_line_stage
= 0;
302 size_t i
, first
, ilen
;
307 * Request: "^(GET|POST|HEAD) ([^ ]+(\\?[^ ]+|)) (HTTP/1\\.[01])$"
308 * Option : "^([-a-zA-Z]+): (.+)$"
312 if (con
->conf
.log_request_header
) {
313 log_error_write(srv
, __FILE__
, __LINE__
, "sdsdSb",
315 "request-len:", buffer_string_length(con
->request
.request
),
316 "\n", con
->request
.request
);
319 if (con
->request_count
> 1 &&
320 con
->request
.request
->ptr
[0] == '\r' &&
321 con
->request
.request
->ptr
[1] == '\n') {
322 /* we are in keep-alive and might get \r\n after a previous POST request.*/
324 buffer_copy_string_len(con
->parse_request
, con
->request
.request
->ptr
+ 2, buffer_string_length(con
->request
.request
) - 2);
326 /* fill the local request buffer */
327 buffer_copy_buffer(con
->parse_request
, con
->request
.request
);
333 /* parse the first line of the request
337 * <method> <uri> <protocol>\r\n
339 ilen
= buffer_string_length(con
->parse_request
);
340 for (i
= 0, first
= 0; i
< ilen
&& line
== 0; i
++) {
341 switch(con
->parse_request
->ptr
[i
]) {
343 if (con
->parse_request
->ptr
[i
+1] == '\n') {
349 con
->parse_request
->ptr
[i
] = '\0';
350 con
->parse_request
->ptr
[i
+1] = '\0';
352 buffer_copy_string_len(con
->request
.request_line
, con
->parse_request
->ptr
, i
);
354 if (request_line_stage
!= 2) {
355 con
->http_status
= 400;
356 con
->response
.keep_alive
= 0;
359 if (srv
->srvconf
.log_request_header_on_error
) {
360 log_error_write(srv
, __FILE__
, __LINE__
, "s", "incomplete request line -> 400");
361 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
363 con
->request
.request
);
368 proto
= con
->parse_request
->ptr
+ first
;
373 /* we got the first one :) */
374 if (HTTP_METHOD_UNSET
== (r
= get_http_method_key(method
))) {
375 con
->http_status
= 501;
376 con
->response
.keep_alive
= 0;
379 if (srv
->srvconf
.log_request_header_on_error
) {
380 log_error_write(srv
, __FILE__
, __LINE__
, "s", "unknown http-method -> 501");
381 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
383 con
->request
.request
);
389 con
->request
.http_method
= r
;
394 * HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
397 if (0 == strncmp(proto
, "HTTP/", sizeof("HTTP/") - 1)) {
398 char * major
= proto
+ sizeof("HTTP/") - 1;
399 char * minor
= strchr(major
, '.');
401 int major_num
= 0, minor_num
= 0;
403 int invalid_version
= 0;
405 if (NULL
== minor
|| /* no dot */
406 minor
== major
|| /* no major */
407 *(minor
+ 1) == '\0' /* no minor */) {
411 major_num
= strtol(major
, &err
, 10);
413 if (*err
!= '\0') invalid_version
= 1;
416 minor_num
= strtol(minor
, &err
, 10);
418 if (*err
!= '\0') invalid_version
= 1;
421 if (invalid_version
) {
422 con
->http_status
= 400;
425 if (srv
->srvconf
.log_request_header_on_error
) {
426 log_error_write(srv
, __FILE__
, __LINE__
, "s", "unknown protocol -> 400");
427 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
429 con
->request
.request
);
434 if (major_num
== 1 && minor_num
== 1) {
435 con
->request
.http_version
= con
->conf
.allow_http11
? HTTP_VERSION_1_1
: HTTP_VERSION_1_0
;
436 } else if (major_num
== 1 && minor_num
== 0) {
437 con
->request
.http_version
= HTTP_VERSION_1_0
;
439 con
->http_status
= 505;
441 if (srv
->srvconf
.log_request_header_on_error
) {
442 log_error_write(srv
, __FILE__
, __LINE__
, "s", "unknown HTTP version -> 505");
443 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
445 con
->request
.request
);
450 con
->http_status
= 400;
453 if (srv
->srvconf
.log_request_header_on_error
) {
454 log_error_write(srv
, __FILE__
, __LINE__
, "s", "unknown protocol -> 400");
455 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
457 con
->request
.request
);
462 if (0 == strncmp(uri
, "http://", 7) &&
463 NULL
!= (nuri
= strchr(uri
+ 7, '/'))) {
464 reqline_host
= uri
+ 7;
465 reqline_hostlen
= nuri
- reqline_host
;
467 buffer_copy_string_len(con
->request
.uri
, nuri
, proto
- nuri
- 1);
468 } else if (0 == strncmp(uri
, "https://", 8) &&
469 NULL
!= (nuri
= strchr(uri
+ 8, '/'))) {
470 reqline_host
= uri
+ 8;
471 reqline_hostlen
= nuri
- reqline_host
;
473 buffer_copy_string_len(con
->request
.uri
, nuri
, proto
- nuri
- 1);
475 /* everything looks good so far */
476 buffer_copy_string_len(con
->request
.uri
, uri
, proto
- uri
- 1);
479 /* check uri for invalid characters */
480 jlen
= buffer_string_length(con
->request
.uri
);
481 for (j
= 0; j
< jlen
; j
++) {
482 if (!request_uri_is_valid_char(con
->request
.uri
->ptr
[j
])) {
483 unsigned char buf
[2];
484 con
->http_status
= 400;
487 if (srv
->srvconf
.log_request_header_on_error
) {
488 buf
[0] = con
->request
.uri
->ptr
[j
];
491 if (con
->request
.uri
->ptr
[j
] > 32 &&
492 con
->request
.uri
->ptr
[j
] != 127) {
493 /* the character is printable -> print it */
494 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
495 "invalid character in URI -> 400",
498 /* a control-character, print ascii-code */
499 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
500 "invalid character in URI -> 400",
501 con
->request
.uri
->ptr
[j
]);
504 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
506 con
->request
.request
);
513 buffer_copy_buffer(con
->request
.orig_uri
, con
->request
.uri
);
515 con
->http_status
= 0;
523 switch(request_line_stage
) {
526 method
= con
->parse_request
->ptr
+ first
;
531 uri
= con
->parse_request
->ptr
+ first
;
535 /* ERROR, one space to much */
536 con
->http_status
= 400;
537 con
->response
.keep_alive
= 0;
540 if (srv
->srvconf
.log_request_header_on_error
) {
541 log_error_write(srv
, __FILE__
, __LINE__
, "s", "overlong request line -> 400");
542 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
544 con
->request
.request
);
549 request_line_stage
++;
556 if (buffer_string_is_empty(con
->request
.uri
)) {
557 con
->http_status
= 400;
558 con
->response
.keep_alive
= 0;
561 if (srv
->srvconf
.log_request_header_on_error
) {
562 log_error_write(srv
, __FILE__
, __LINE__
, "s", "no uri specified -> 400");
563 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
565 con
->request
.request
);
571 /* Insert as host header */
574 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->request
.headers
, TYPE_STRING
))) {
575 ds
= data_string_init();
578 buffer_copy_string_len(ds
->key
, CONST_STR_LEN("Host"));
579 buffer_copy_string_len(ds
->value
, reqline_host
, reqline_hostlen
);
580 array_insert_unique(con
->request
.headers
, (data_unset
*)ds
);
581 con
->request
.http_host
= ds
->value
;
584 for (; i
<= ilen
&& !done
; i
++) {
585 char *cur
= con
->parse_request
->ptr
+ i
;
592 * 1*<any CHAR except CTLs or separators>
593 * CTLs == 0-31 + 127, CHAR = 7-bit ascii (0..127)
602 if (is_ws_after_key
== 0) {
624 con
->http_status
= 400;
626 con
->response
.keep_alive
= 0;
628 if (srv
->srvconf
.log_request_header_on_error
) {
629 log_error_write(srv
, __FILE__
, __LINE__
, "sbsds",
630 "invalid character in key", con
->request
.request
, cur
, *cur
, "-> 400");
632 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
634 con
->request
.request
);
650 /* skip every thing up to the : */
651 for (j
= 1; !got_colon
; j
++) {
652 switch(con
->parse_request
->ptr
[j
+ i
]) {
658 /* ok, done; handle the colon the usual way */
662 is_ws_after_key
= 1; /* we already know the key length */
668 if (srv
->srvconf
.log_request_header_on_error
) {
669 log_error_write(srv
, __FILE__
, __LINE__
, "s", "WS character in key -> 400");
670 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
672 con
->request
.request
);
675 con
->http_status
= 400;
676 con
->response
.keep_alive
= 0;
685 if (con
->parse_request
->ptr
[i
+1] == '\n' && i
== first
) {
687 con
->parse_request
->ptr
[i
] = '\0';
688 con
->parse_request
->ptr
[i
+1] = '\0';
694 if (srv
->srvconf
.log_request_header_on_error
) {
695 log_error_write(srv
, __FILE__
, __LINE__
, "s", "CR without LF -> 400");
696 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
698 con
->request
.request
);
701 con
->http_status
= 400;
703 con
->response
.keep_alive
= 0;
708 if (*cur
< 32 || ((unsigned char)*cur
) >= 127) {
709 con
->http_status
= 400;
711 con
->response
.keep_alive
= 0;
713 if (srv
->srvconf
.log_request_header_on_error
) {
714 log_error_write(srv
, __FILE__
, __LINE__
, "sbsds",
715 "invalid character in key", con
->request
.request
, cur
, *cur
, "-> 400");
717 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
719 con
->request
.request
);
730 if (con
->parse_request
->ptr
[i
+1] == '\n') {
731 data_string
*ds
= NULL
;
733 /* End of Headerline */
734 con
->parse_request
->ptr
[i
] = '\0';
735 con
->parse_request
->ptr
[i
+1] = '\0';
740 * we use a evil hack to handle the line-folding
742 * As array_insert_unique() deletes 'ds' in the case of a duplicate
743 * ds points somewhere and we get a evil crash. As a solution we keep the old
744 * "key" and get the current value from the hash and append us
748 if (!key
|| !key_len
) {
751 if (srv
->srvconf
.log_request_header_on_error
) {
752 log_error_write(srv
, __FILE__
, __LINE__
, "s", "WS at the start of first line -> 400");
754 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
756 con
->request
.request
);
760 con
->http_status
= 400;
762 con
->response
.keep_alive
= 0;
766 key_b
= buffer_init();
767 buffer_copy_string_len(key_b
, key
, key_len
);
769 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, key_b
->ptr
))) {
770 buffer_append_string(ds
->value
, value
);
776 key
= con
->parse_request
->ptr
+ first
;
780 /* strip trailing white-spaces */
782 (value
[s_len
- 1] == ' ' ||
783 value
[s_len
- 1] == '\t'); s_len
--);
789 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->request
.headers
, TYPE_STRING
))) {
790 ds
= data_string_init();
792 buffer_copy_string_len(ds
->key
, key
, key_len
);
793 buffer_copy_string_len(ds
->value
, value
, s_len
);
798 * the list of options is sorted to simplify the search
801 if (0 == (cmp
= buffer_caseless_compare(CONST_BUF_LEN(ds
->key
), CONST_STR_LEN("Connection")))) {
807 vals
= srv
->split_vals
;
811 http_request_split_value(vals
, ds
->value
);
813 for (vi
= 0; vi
< vals
->used
; vi
++) {
814 data_string
*dsv
= (data_string
*)vals
->data
[vi
];
816 if (0 == buffer_caseless_compare(CONST_BUF_LEN(dsv
->value
), CONST_STR_LEN("keep-alive"))) {
817 keep_alive_set
= HTTP_CONNECTION_KEEPALIVE
;
820 } else if (0 == buffer_caseless_compare(CONST_BUF_LEN(dsv
->value
), CONST_STR_LEN("close"))) {
821 keep_alive_set
= HTTP_CONNECTION_CLOSE
;
827 } else if (cmp
> 0 && 0 == (cmp
= buffer_caseless_compare(CONST_BUF_LEN(ds
->key
), CONST_STR_LEN("Content-Length")))) {
832 if (con_length_set
) {
833 con
->http_status
= 400;
836 if (srv
->srvconf
.log_request_header_on_error
) {
837 log_error_write(srv
, __FILE__
, __LINE__
, "s",
838 "duplicate Content-Length-header -> 400");
839 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
841 con
->request
.request
);
843 array_insert_unique(con
->request
.headers
, (data_unset
*)ds
);
847 jlen
= buffer_string_length(ds
->value
);
848 for (j
= 0; j
< jlen
; j
++) {
849 char c
= ds
->value
->ptr
[j
];
850 if (!isdigit((unsigned char)c
)) {
851 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
852 "content-length broken:", ds
->value
, "-> 400");
854 con
->http_status
= 400;
857 array_insert_unique(con
->request
.headers
, (data_unset
*)ds
);
862 r
= strtoul(ds
->value
->ptr
, &err
, 10);
866 con
->request
.content_length
= r
;
868 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
869 "content-length broken:", ds
->value
, "-> 400");
871 con
->http_status
= 400;
874 array_insert_unique(con
->request
.headers
, (data_unset
*)ds
);
877 } else if (cmp
> 0 && 0 == (cmp
= buffer_caseless_compare(CONST_BUF_LEN(ds
->key
), CONST_STR_LEN("Content-Type")))) {
878 /* if dup, only the first one will survive */
879 if (!con
->request
.http_content_type
) {
880 con
->request
.http_content_type
= ds
->value
->ptr
;
882 con
->http_status
= 400;
885 if (srv
->srvconf
.log_request_header_on_error
) {
886 log_error_write(srv
, __FILE__
, __LINE__
, "s",
887 "duplicate Content-Type-header -> 400");
888 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
890 con
->request
.request
);
892 array_insert_unique(con
->request
.headers
, (data_unset
*)ds
);
895 } else if (cmp
> 0 && 0 == (cmp
= buffer_caseless_compare(CONST_BUF_LEN(ds
->key
), CONST_STR_LEN("Expect")))) {
897 * Expect: 100-continue
899 * -> (10.1.1) 100 (read content, process request, send final status-code)
900 * -> (10.4.18) 417 (close)
902 * (not handled at all yet, we always send 417 here)
904 * What has to be added ?
905 * 1. handling of chunked request body
906 * 2. out-of-order sending from the HTTP/1.1 100 Continue
911 if (srv
->srvconf
.reject_expect_100_with_417
&& 0 == buffer_caseless_compare(CONST_BUF_LEN(ds
->value
), CONST_STR_LEN("100-continue"))) {
912 con
->http_status
= 417;
914 array_insert_unique(con
->request
.headers
, (data_unset
*)ds
);
917 } else if (cmp
> 0 && 0 == (cmp
= buffer_caseless_compare(CONST_BUF_LEN(ds
->key
), CONST_STR_LEN("Host")))) {
919 /* ignore all host: headers as we got the host in the request line */
920 ds
->free((data_unset
*) ds
);
922 } else if (!con
->request
.http_host
) {
923 con
->request
.http_host
= ds
->value
;
925 con
->http_status
= 400;
928 if (srv
->srvconf
.log_request_header_on_error
) {
929 log_error_write(srv
, __FILE__
, __LINE__
, "s",
930 "duplicate Host-header -> 400");
931 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
933 con
->request
.request
);
935 array_insert_unique(con
->request
.headers
, (data_unset
*)ds
);
938 } else if (cmp
> 0 && 0 == (cmp
= buffer_caseless_compare(CONST_BUF_LEN(ds
->key
), CONST_STR_LEN("If-Modified-Since")))) {
939 /* Proxies sometimes send dup headers
940 * if they are the same we ignore the second
941 * if not, we raise an error */
942 if (!con
->request
.http_if_modified_since
) {
943 con
->request
.http_if_modified_since
= ds
->value
->ptr
;
944 } else if (0 == strcasecmp(con
->request
.http_if_modified_since
,
946 /* ignore it if they are the same */
948 ds
->free((data_unset
*)ds
);
951 con
->http_status
= 400;
954 if (srv
->srvconf
.log_request_header_on_error
) {
955 log_error_write(srv
, __FILE__
, __LINE__
, "s",
956 "duplicate If-Modified-Since header -> 400");
957 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
959 con
->request
.request
);
961 array_insert_unique(con
->request
.headers
, (data_unset
*)ds
);
964 } else if (cmp
> 0 && 0 == (cmp
= buffer_caseless_compare(CONST_BUF_LEN(ds
->key
), CONST_STR_LEN("If-None-Match")))) {
965 /* if dup, only the first one will survive */
966 if (!con
->request
.http_if_none_match
) {
967 con
->request
.http_if_none_match
= ds
->value
->ptr
;
969 ds
->free((data_unset
*) ds
);
972 } else if (cmp
> 0 && 0 == (cmp
= buffer_caseless_compare(CONST_BUF_LEN(ds
->key
), CONST_STR_LEN("Range")))) {
973 if (!con
->request
.http_range
) {
976 if (0 == strncasecmp(ds
->value
->ptr
, "bytes=", 6) &&
977 NULL
!= strchr(ds
->value
->ptr
+6, '-')) {
979 /* if dup, only the first one will survive */
980 con
->request
.http_range
= ds
->value
->ptr
+ 6;
983 con
->http_status
= 400;
986 if (srv
->srvconf
.log_request_header_on_error
) {
987 log_error_write(srv
, __FILE__
, __LINE__
, "s",
988 "duplicate Range-header -> 400");
989 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
991 con
->request
.request
);
993 array_insert_unique(con
->request
.headers
, (data_unset
*)ds
);
998 if (ds
) array_insert_unique(con
->request
.headers
, (data_unset
*)ds
);
1000 /* empty header-fields are not allowed by HTTP-RFC, we just ignore them */
1010 * for Bug 1230 keep the key_len a live
1016 if (srv
->srvconf
.log_request_header_on_error
) {
1017 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1018 "CR without LF", con
->request
.request
, "-> 400");
1021 con
->http_status
= 400;
1022 con
->keep_alive
= 0;
1023 con
->response
.keep_alive
= 0;
1029 /* strip leading WS */
1030 if (value
== cur
) value
= cur
+1;
1033 if (*cur
>= 0 && *cur
< 32 && *cur
!= '\t') {
1034 if (srv
->srvconf
.log_request_header_on_error
) {
1035 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1036 "invalid char in header", (int)*cur
, "-> 400");
1039 con
->http_status
= 400;
1040 con
->keep_alive
= 0;
1049 con
->header_len
= i
;
1051 /* do some post-processing */
1053 if (con
->request
.http_version
== HTTP_VERSION_1_1
) {
1054 if (keep_alive_set
!= HTTP_CONNECTION_CLOSE
) {
1055 /* no Connection-Header sent */
1057 /* HTTP/1.1 -> keep-alive default TRUE */
1058 con
->keep_alive
= 1;
1060 con
->keep_alive
= 0;
1063 /* RFC 2616, 14.23 */
1064 if (con
->request
.http_host
== NULL
||
1065 buffer_string_is_empty(con
->request
.http_host
)) {
1066 con
->http_status
= 400;
1067 con
->response
.keep_alive
= 0;
1068 con
->keep_alive
= 0;
1070 if (srv
->srvconf
.log_request_header_on_error
) {
1071 log_error_write(srv
, __FILE__
, __LINE__
, "s", "HTTP/1.1 but Host missing -> 400");
1072 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
1073 "request-header:\n",
1074 con
->request
.request
);
1079 if (keep_alive_set
== HTTP_CONNECTION_KEEPALIVE
) {
1080 /* no Connection-Header sent */
1082 /* HTTP/1.0 -> keep-alive default FALSE */
1083 con
->keep_alive
= 1;
1085 con
->keep_alive
= 0;
1089 /* check hostname field if it is set */
1090 if (NULL
!= con
->request
.http_host
&&
1091 0 != request_check_hostname(srv
, con
, con
->request
.http_host
)) {
1093 if (srv
->srvconf
.log_request_header_on_error
) {
1094 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1095 "Invalid Hostname -> 400");
1096 log_error_write(srv
, __FILE__
, __LINE__
, "Sb",
1097 "request-header:\n",
1098 con
->request
.request
);
1101 con
->http_status
= 400;
1102 con
->response
.keep_alive
= 0;
1103 con
->keep_alive
= 0;
1108 switch(con
->request
.http_method
) {
1109 case HTTP_METHOD_GET
:
1110 case HTTP_METHOD_HEAD
:
1111 /* content-length is forbidden for those */
1112 if (con_length_set
&& con
->request
.content_length
!= 0) {
1113 /* content-length is missing */
1114 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1115 "GET/HEAD with content-length -> 400");
1117 con
->keep_alive
= 0;
1118 con
->http_status
= 400;
1122 case HTTP_METHOD_POST
:
1123 /* content-length is required for them */
1124 if (!con_length_set
) {
1125 /* content-length is missing */
1126 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1127 "POST-request, but content-length missing -> 411");
1129 con
->keep_alive
= 0;
1130 con
->http_status
= 411;
1136 /* require Content-Length if request contains request body */
1137 if (array_get_element(con
->request
.headers
, "Transfer-Encoding")) {
1138 /* presence of Transfer-Encoding in request headers requires "chunked"
1139 * be final encoding in HTTP/1.1. Return 411 Length Required as
1140 * lighttpd does not support request input transfer-encodings */
1141 con
->keep_alive
= 0;
1142 con
->http_status
= 411; /* 411 Length Required */
1149 /* check if we have read post data */
1150 if (con_length_set
) {
1151 /* don't handle more the SSIZE_MAX bytes in content-length */
1152 if (con
->request
.content_length
> SSIZE_MAX
) {
1153 con
->http_status
= 413;
1154 con
->keep_alive
= 0;
1156 log_error_write(srv
, __FILE__
, __LINE__
, "sos",
1157 "request-size too long:", (off_t
) con
->request
.content_length
, "-> 413");
1161 /* divide by 1024 as srvconf.max_request_size is in kBytes */
1162 if (srv
->srvconf
.max_request_size
!= 0 &&
1163 (con
->request
.content_length
>> 10) > srv
->srvconf
.max_request_size
) {
1164 /* the request body itself is larger then
1165 * our our max_request_size
1168 con
->http_status
= 413;
1169 con
->keep_alive
= 0;
1171 log_error_write(srv
, __FILE__
, __LINE__
, "sos",
1172 "request-size too long:", (off_t
) con
->request
.content_length
, "-> 413");
1177 /* we have content */
1178 if (con
->request
.content_length
!= 0) {
1186 int http_request_header_finished(server
*srv
, connection
*con
) {
1189 if (buffer_string_length(con
->request
.request
) < 4) return 0;
1191 if (0 == memcmp(con
->request
.request
->ptr
+ buffer_string_length(con
->request
.request
) - 4, CONST_STR_LEN("\r\n\r\n"))) return 1;
1192 if (NULL
!= strstr(con
->request
.request
->ptr
, "\r\n\r\n")) return 1;