6 #include "connections.h"
9 #include "configfile.h"
13 #include "http_chunk.h"
14 #include "stat_cache.h"
19 #include "inet_ntop_cache.h"
32 # include <openssl/ssl.h>
33 # include <openssl/err.h>
36 #ifdef HAVE_SYS_FILIO_H
37 # include <sys/filio.h>
40 #include "sys-socket.h"
46 static connection
*connections_get_new_connection(server
*srv
) {
47 connections
*conns
= srv
->conns
;
50 if (conns
->size
== 0) {
53 conns
->ptr
= malloc(sizeof(*conns
->ptr
) * conns
->size
);
54 force_assert(NULL
!= conns
->ptr
);
55 for (i
= 0; i
< conns
->size
; i
++) {
56 conns
->ptr
[i
] = connection_init(srv
);
58 } else if (conns
->size
== conns
->used
) {
60 conns
->ptr
= realloc(conns
->ptr
, sizeof(*conns
->ptr
) * conns
->size
);
61 force_assert(NULL
!= conns
->ptr
);
63 for (i
= conns
->used
; i
< conns
->size
; i
++) {
64 conns
->ptr
[i
] = connection_init(srv
);
68 connection_reset(srv
, conns
->ptr
[conns
->used
]);
70 fprintf(stderr
, "%s.%d: add: ", __FILE__
, __LINE__
);
71 for (i
= 0; i
< conns
->used
+ 1; i
++) {
72 fprintf(stderr
, "%d ", conns
->ptr
[i
]->fd
);
74 fprintf(stderr
, "\n");
77 conns
->ptr
[conns
->used
]->ndx
= conns
->used
;
78 return conns
->ptr
[conns
->used
++];
81 static int connection_del(server
*srv
, connection
*con
) {
83 connections
*conns
= srv
->conns
;
86 if (con
== NULL
) return -1;
88 if (-1 == con
->ndx
) return -1;
90 buffer_reset(con
->uri
.authority
);
91 buffer_reset(con
->uri
.path
);
92 buffer_reset(con
->uri
.query
);
93 buffer_reset(con
->request
.orig_uri
);
97 /* not last element */
99 if (i
!= conns
->used
- 1) {
100 temp
= conns
->ptr
[i
];
101 conns
->ptr
[i
] = conns
->ptr
[conns
->used
- 1];
102 conns
->ptr
[conns
->used
- 1] = temp
;
104 conns
->ptr
[i
]->ndx
= i
;
105 conns
->ptr
[conns
->used
- 1]->ndx
= -1;
112 fprintf(stderr
, "%s.%d: del: (%d)", __FILE__
, __LINE__
, conns
->used
);
113 for (i
= 0; i
< conns
->used
; i
++) {
114 fprintf(stderr
, "%d ", conns
->ptr
[i
]->fd
);
116 fprintf(stderr
, "\n");
121 static int connection_close(server
*srv
, connection
*con
) {
123 server_socket
*srv_sock
= con
->srv_socket
;
124 if (srv_sock
->is_ssl
) {
125 if (con
->ssl
) SSL_free(con
->ssl
);
130 fdevent_event_del(srv
->ev
, &(con
->fde_ndx
), con
->fd
);
131 fdevent_unregister(srv
->ev
, con
->fd
);
133 if (closesocket(con
->fd
)) {
134 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
135 "(warning) close:", con
->fd
, strerror(errno
));
138 if (close(con
->fd
)) {
139 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
140 "(warning) close:", con
->fd
, strerror(errno
));
147 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
148 "closed()", con
->fd
);
151 connection_del(srv
, con
);
152 connection_set_state(srv
, con
, CON_STATE_CONNECT
);
157 static void connection_handle_close_state(server
*srv
, connection
*con
) {
158 /* we have to do the linger_on_close stuff regardless
159 * of con->keep_alive; even non-keepalive sockets may
160 * still have unread data, and closing before reading
161 * it will make the client not see all our output.
166 len
= read(con
->fd
, buf
, sizeof(buf
));
167 if (len
== 0 || (len
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
) ) {
168 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
171 if (srv
->cur_ts
- con
->close_timeout_ts
> HTTP_LINGER_TIMEOUT
) {
172 connection_close(srv
, con
);
174 if (srv
->srvconf
.log_state_handling
) {
175 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
176 "connection closed for fd", con
->fd
);
181 static void connection_handle_shutdown(server
*srv
, connection
*con
) {
185 server_socket
*srv_sock
= con
->srv_socket
;
186 if (srv_sock
->is_ssl
) {
190 switch ((ret
= SSL_shutdown(con
->ssl
))) {
197 * FIXME: wait for fdevent and call SSL_shutdown again
201 if (-1 != (ret
= SSL_shutdown(con
->ssl
))) break;
206 switch ((ssl_r
= SSL_get_error(con
->ssl
, ret
))) {
207 case SSL_ERROR_ZERO_RETURN
:
209 case SSL_ERROR_WANT_WRITE
:
210 /*con->is_writable = -1;*//*(no effect; shutdown() called below)*/
211 case SSL_ERROR_WANT_READ
:
213 case SSL_ERROR_SYSCALL
:
214 /* perhaps we have error waiting in our error-queue */
215 if (0 != (err
= ERR_get_error())) {
217 log_error_write(srv
, __FILE__
, __LINE__
, "sdds", "SSL:",
219 ERR_error_string(err
, NULL
));
220 } while((err
= ERR_get_error()));
221 } else if (errno
!= 0) { /* ssl bug (see lighttpd ticket #2213): sometimes errno == 0 */
227 log_error_write(srv
, __FILE__
, __LINE__
, "sddds", "SSL (error):",
236 while((err
= ERR_get_error())) {
237 log_error_write(srv
, __FILE__
, __LINE__
, "sdds", "SSL:",
239 ERR_error_string(err
, NULL
));
249 switch(r
= plugins_call_handle_connection_close(srv
, con
)) {
251 case HANDLER_FINISHED
:
254 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "unhandling return value", r
);
259 connection_reset(srv
, con
);
261 /* close the connection */
262 if ((0 == shutdown(con
->fd
, SHUT_WR
))) {
263 con
->close_timeout_ts
= srv
->cur_ts
;
264 connection_set_state(srv
, con
, CON_STATE_CLOSE
);
266 if (srv
->srvconf
.log_state_handling
) {
267 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
268 "shutdown for fd", con
->fd
);
271 connection_close(srv
, con
);
275 static void connection_handle_response_end_state(server
*srv
, connection
*con
) {
276 /* log the request */
277 /* (even if error, connection dropped, still write to access log if http_status) */
278 if (con
->http_status
) {
279 plugins_call_handle_request_done(srv
, con
);
282 if (con
->state
!= CON_STATE_ERROR
) srv
->con_written
++;
284 if ((con
->request
.content_length
285 && (off_t
)con
->request
.content_length
> con
->request_content_queue
->bytes_in
)
286 || con
->state
== CON_STATE_ERROR
) {
287 /* request body is present and has not been read completely */
291 if (con
->keep_alive
) {
292 connection_reset(srv
, con
);
294 con
->request_start
= srv
->cur_ts
;
295 con
->read_idle_ts
= srv
->cur_ts
;
297 connection_set_state(srv
, con
, CON_STATE_REQUEST_START
);
299 connection_handle_shutdown(srv
, con
);
303 static void connection_handle_errdoc_init(server
*srv
, connection
*con
) {
304 /* modules that produce headers required with error response should
305 * typically also produce an error document. Make an exception for
306 * mod_auth WWW-Authenticate response header. */
307 buffer
*www_auth
= NULL
;
308 if (401 == con
->http_status
) {
309 data_string
*ds
= (data_string
*)array_get_element(con
->response
.headers
, "WWW-Authenticate");
311 www_auth
= buffer_init_buffer(ds
->value
);
315 con
->response
.transfer_encoding
= 0;
316 buffer_reset(con
->physical
.path
);
317 array_reset(con
->response
.headers
);
318 chunkqueue_reset(con
->write_queue
);
320 if (NULL
!= www_auth
) {
321 response_header_insert(srv
, con
, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(www_auth
));
322 buffer_free(www_auth
);
326 static int connection_handle_write_prepare(server
*srv
, connection
*con
) {
327 if (con
->mode
== DIRECT
) {
329 switch(con
->request
.http_method
) {
330 case HTTP_METHOD_GET
:
331 case HTTP_METHOD_POST
:
332 case HTTP_METHOD_HEAD
:
334 case HTTP_METHOD_OPTIONS
:
336 * 400 is coming from the request-parser BEFORE uri.path is set
337 * 403 is from the response handler when noone else catched it
340 if ((!con
->http_status
|| con
->http_status
== 200) && !buffer_string_is_empty(con
->uri
.path
) &&
341 con
->uri
.path
->ptr
[0] != '*') {
342 response_header_insert(srv
, con
, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
344 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
345 con
->parsed_response
&= ~HTTP_CONTENT_LENGTH
;
347 con
->http_status
= 200;
348 con
->file_finished
= 1;
350 chunkqueue_reset(con
->write_queue
);
354 if (0 == con
->http_status
) {
355 con
->http_status
= 501;
361 if (con
->http_status
== 0) {
362 con
->http_status
= 403;
365 switch(con
->http_status
) {
366 case 204: /* class: header only */
369 /* disable chunked encoding again as we have no body */
370 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
371 con
->parsed_response
&= ~HTTP_CONTENT_LENGTH
;
372 chunkqueue_reset(con
->write_queue
);
374 con
->file_finished
= 1;
376 default: /* class: header + body */
377 if (con
->mode
!= DIRECT
) break;
379 /* only custom body for 4xx and 5xx */
380 if (con
->http_status
< 400 || con
->http_status
>= 600) break;
382 con
->file_finished
= 0;
384 connection_handle_errdoc_init(srv
, con
);
386 /* try to send static errorfile */
387 if (!buffer_string_is_empty(con
->conf
.errorfile_prefix
)) {
388 stat_cache_entry
*sce
= NULL
;
390 buffer_copy_buffer(con
->physical
.path
, con
->conf
.errorfile_prefix
);
391 buffer_append_int(con
->physical
.path
, con
->http_status
);
392 buffer_append_string_len(con
->physical
.path
, CONST_STR_LEN(".html"));
394 if (0 == http_chunk_append_file(srv
, con
, con
->physical
.path
)) {
395 con
->file_finished
= 1;
396 if (HANDLER_ERROR
!= stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
397 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce
->content_type
));
402 if (!con
->file_finished
) {
405 buffer_reset(con
->physical
.path
);
407 con
->file_finished
= 1;
410 /* build default error-page */
411 buffer_copy_string_len(b
, CONST_STR_LEN(
412 "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
413 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
414 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
415 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
418 buffer_append_int(b
, con
->http_status
);
419 buffer_append_string_len(b
, CONST_STR_LEN(" - "));
420 buffer_append_string(b
, get_http_status_name(con
->http_status
));
422 buffer_append_string_len(b
, CONST_STR_LEN(
427 buffer_append_int(b
, con
->http_status
);
428 buffer_append_string_len(b
, CONST_STR_LEN(" - "));
429 buffer_append_string(b
, get_http_status_name(con
->http_status
));
431 buffer_append_string_len(b
, CONST_STR_LEN("</h1>\n"
436 (void)http_chunk_append_buffer(srv
, con
, b
);
439 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
444 if (con
->file_finished
) {
445 /* we have all the content and chunked encoding is not used, set a content-length */
447 if ((!(con
->parsed_response
& HTTP_CONTENT_LENGTH
)) &&
448 (con
->response
.transfer_encoding
& HTTP_TRANSFER_ENCODING_CHUNKED
) == 0) {
449 off_t qlen
= chunkqueue_length(con
->write_queue
);
452 * The Content-Length header only can be sent if we have content:
453 * - HEAD doesn't have a content-body (but have a content-length)
454 * - 1xx, 204 and 304 don't have a content-body (RFC 2616 Section 4.3)
456 * Otherwise generate a Content-Length header as chunked encoding is not
459 if ((con
->http_status
>= 100 && con
->http_status
< 200) ||
460 con
->http_status
== 204 ||
461 con
->http_status
== 304) {
463 /* no Content-Body, no Content-Length */
464 if (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "Content-Length"))) {
465 buffer_reset(ds
->value
); /* Headers with empty values are ignored for output */
467 } else if (qlen
> 0 || con
->request
.http_method
!= HTTP_METHOD_HEAD
) {
468 /* qlen = 0 is important for Redirects (301, ...) as they MAY have
469 * a content. Browsers are waiting for a Content otherwise
471 buffer_copy_int(srv
->tmp_buf
, qlen
);
473 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Length"), CONST_BUF_LEN(srv
->tmp_buf
));
478 * the file isn't finished yet, but we have all headers
480 * to get keep-alive we either need:
481 * - Content-Length: ... (HTTP/1.0 and HTTP/1.0) or
482 * - Transfer-Encoding: chunked (HTTP/1.1)
485 if (((con
->parsed_response
& HTTP_CONTENT_LENGTH
) == 0) &&
486 ((con
->response
.transfer_encoding
& HTTP_TRANSFER_ENCODING_CHUNKED
) == 0)) {
487 if (con
->request
.http_version
== HTTP_VERSION_1_1
) {
488 off_t qlen
= chunkqueue_length(con
->write_queue
);
489 con
->response
.transfer_encoding
= HTTP_TRANSFER_ENCODING_CHUNKED
;
491 /* create initial Transfer-Encoding: chunked segment */
492 buffer
*b
= srv
->tmp_chunk_len
;
493 buffer_string_set_length(b
, 0);
494 buffer_append_uint_hex(b
, (uintmax_t)qlen
);
495 buffer_append_string_len(b
, CONST_STR_LEN("\r\n"));
496 chunkqueue_prepend_buffer(con
->write_queue
, b
);
497 chunkqueue_append_mem(con
->write_queue
, CONST_STR_LEN("\r\n"));
505 * if the backend sent a Connection: close, follow the wish
507 * NOTE: if the backend sent Connection: Keep-Alive, but no Content-Length, we
508 * will close the connection. That's fine. We can always decide the close
511 * FIXME: to be nice we should remove the Connection: ...
513 if (con
->parsed_response
& HTTP_CONNECTION
) {
514 /* a subrequest disable keep-alive although the client wanted it */
515 if (con
->keep_alive
&& !con
->response
.keep_alive
) {
521 if (con
->request
.http_method
== HTTP_METHOD_HEAD
) {
523 * a HEAD request has the same as a GET
524 * without the content
526 con
->file_finished
= 1;
528 chunkqueue_reset(con
->write_queue
);
529 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
532 http_response_write_header(srv
, con
);
537 static int connection_handle_write(server
*srv
, connection
*con
) {
538 switch(network_write_chunkqueue(srv
, con
, con
->write_queue
, MAX_WRITE_LIMIT
)) {
540 con
->write_request_ts
= srv
->cur_ts
;
541 if (con
->file_finished
) {
542 connection_set_state(srv
, con
, CON_STATE_RESPONSE_END
);
545 case -1: /* error on our side */
546 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
547 "connection closed: write failed on fd", con
->fd
);
548 connection_set_state(srv
, con
, CON_STATE_ERROR
);
550 case -2: /* remote close */
551 connection_set_state(srv
, con
, CON_STATE_ERROR
);
554 con
->write_request_ts
= srv
->cur_ts
;
555 con
->is_writable
= 0;
557 /* not finished yet -> WRITE */
566 connection
*connection_init(server
*srv
) {
571 con
= calloc(1, sizeof(*con
));
572 force_assert(NULL
!= con
);
577 con
->bytes_written
= 0;
579 con
->bytes_header
= 0;
580 con
->loops_per_request
= 0;
583 con->x = buffer_init();
586 CLEAN(request
.request_line
);
587 CLEAN(request
.request
);
588 CLEAN(request
.pathinfo
);
590 CLEAN(request
.orig_uri
);
593 CLEAN(uri
.authority
);
598 CLEAN(physical
.doc_root
);
599 CLEAN(physical
.path
);
600 CLEAN(physical
.basedir
);
601 CLEAN(physical
.rel_path
);
602 CLEAN(physical
.etag
);
603 CLEAN(parse_request
);
607 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
608 CLEAN(tlsext_server_name
);
612 con
->write_queue
= chunkqueue_init();
613 con
->read_queue
= chunkqueue_init();
614 con
->request_content_queue
= chunkqueue_init();
616 con
->request
.headers
= array_init();
617 con
->response
.headers
= array_init();
618 con
->environment
= array_init();
620 /* init plugin specific connection structures */
622 con
->plugin_ctx
= calloc(1, (srv
->plugins
.used
+ 1) * sizeof(void *));
623 force_assert(NULL
!= con
->plugin_ctx
);
625 con
->cond_cache
= calloc(srv
->config_context
->used
, sizeof(cond_cache_t
));
626 force_assert(NULL
!= con
->cond_cache
);
627 config_setup_connection(srv
, con
);
632 void connections_free(server
*srv
) {
633 connections
*conns
= srv
->conns
;
636 for (i
= 0; i
< conns
->size
; i
++) {
637 connection
*con
= conns
->ptr
[i
];
639 connection_reset(srv
, con
);
641 chunkqueue_free(con
->write_queue
);
642 chunkqueue_free(con
->read_queue
);
643 chunkqueue_free(con
->request_content_queue
);
644 array_free(con
->request
.headers
);
645 array_free(con
->response
.headers
);
646 array_free(con
->environment
);
652 CLEAN(request
.request_line
);
653 CLEAN(request
.request
);
654 CLEAN(request
.pathinfo
);
656 CLEAN(request
.orig_uri
);
659 CLEAN(uri
.authority
);
664 CLEAN(physical
.doc_root
);
665 CLEAN(physical
.path
);
666 CLEAN(physical
.basedir
);
667 CLEAN(physical
.etag
);
668 CLEAN(physical
.rel_path
);
669 CLEAN(parse_request
);
673 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
674 CLEAN(tlsext_server_name
);
677 free(con
->plugin_ctx
);
678 free(con
->cond_cache
);
687 int connection_reset(server
*srv
, connection
*con
) {
690 plugins_call_connection_reset(srv
, con
);
692 con
->is_readable
= 1;
693 con
->is_writable
= 1;
694 con
->http_status
= 0;
695 con
->file_finished
= 0;
696 con
->file_started
= 0;
697 con
->got_response
= 0;
699 con
->parsed_response
= 0;
701 con
->bytes_written
= 0;
702 con
->bytes_written_cur_second
= 0;
704 con
->bytes_header
= 0;
705 con
->loops_per_request
= 0;
707 con
->request
.http_method
= HTTP_METHOD_UNSET
;
708 con
->request
.http_version
= HTTP_VERSION_UNSET
;
710 con
->request
.http_if_modified_since
= NULL
;
711 con
->request
.http_if_none_match
= NULL
;
713 con
->response
.keep_alive
= 0;
714 con
->response
.content_length
= -1;
715 con
->response
.transfer_encoding
= 0;
720 if (con->x) buffer_reset(con->x);
723 CLEAN(request
.request_line
);
724 CLEAN(request
.pathinfo
);
725 CLEAN(request
.request
);
727 /* CLEAN(request.orig_uri); */
730 /* CLEAN(uri.authority); */
731 /* CLEAN(uri.path); */
733 /* CLEAN(uri.query); */
735 CLEAN(physical
.doc_root
);
736 CLEAN(physical
.path
);
737 CLEAN(physical
.basedir
);
738 CLEAN(physical
.rel_path
);
739 CLEAN(physical
.etag
);
741 CLEAN(parse_request
);
744 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
745 CLEAN(tlsext_server_name
);
750 if (con->x) con->x->used = 0;
755 con->request.x = NULL;
759 CLEAN(http_content_type
);
761 con
->request
.content_length
= 0;
763 array_reset(con
->request
.headers
);
764 array_reset(con
->response
.headers
);
765 array_reset(con
->environment
);
767 chunkqueue_reset(con
->write_queue
);
768 chunkqueue_reset(con
->request_content_queue
);
770 /* the plugins should cleanup themself */
771 for (i
= 0; i
< srv
->plugins
.used
; i
++) {
772 plugin
*p
= ((plugin
**)(srv
->plugins
.ptr
))[i
];
773 plugin_data
*pd
= p
->data
;
777 if (con
->plugin_ctx
[pd
->id
] != NULL
) {
778 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "missing cleanup in", p
->name
);
781 con
->plugin_ctx
[pd
->id
] = NULL
;
784 /* The cond_cache gets reset in response.c */
785 /* config_cond_cache_reset(srv, con); */
788 con
->error_handler_saved_status
= 0;
789 /*con->error_handler_saved_method = HTTP_METHOD_UNSET;*/
790 /*(error_handler_saved_method value is not valid unless error_handler_saved_status is set)*/
792 config_setup_connection(srv
, con
);
798 * handle all header and content read
800 * we get called by the state-engine and by the fdevent-handler
802 static int connection_handle_read_state(server
*srv
, connection
*con
) {
803 chunk
*c
, *last_chunk
;
805 chunkqueue
*cq
= con
->read_queue
;
806 int is_closed
= 0; /* the connection got closed, if we don't have a complete header, -> error */
807 /* when in CON_STATE_READ: about to receive first byte for a request: */
808 int is_request_start
= chunkqueue_is_empty(cq
);
810 if (con
->is_readable
) {
811 con
->read_idle_ts
= srv
->cur_ts
;
813 switch(connection_handle_read(srv
, con
)) {
824 chunkqueue_remove_finished_chunks(cq
);
826 /* we might have got several packets at once
829 /* update request_start timestamp when first byte of
830 * next request is received on a keep-alive connection */
831 if (con
->request_count
> 1 && is_request_start
) {
832 con
->request_start
= srv
->cur_ts
;
833 if (con
->conf
.high_precision_timestamps
)
834 log_clock_gettime_realtime(&con
->request_start_hp
);
837 /* if there is a \r\n\r\n in the chunkqueue
839 * scan the chunk-queue twice
840 * 1. to find the \r\n\r\n
841 * 2. to copy the header-packet
848 for (c
= cq
->first
; c
; c
= c
->next
) {
850 size_t len
= buffer_string_length(c
->mem
) - c
->offset
;
851 const char *b
= c
->mem
->ptr
+ c
->offset
;
853 for (i
= 0; i
< len
; ++i
) {
857 /* chec if \n\r\n follows */
860 const char header_end
[] = "\r\n\r\n";
861 int header_end_match_pos
= 1;
863 for ( ; cc
; cc
= cc
->next
, j
= 0 ) {
864 size_t bblen
= buffer_string_length(cc
->mem
) - cc
->offset
;
865 const char *bb
= cc
->mem
->ptr
+ cc
->offset
;
867 for ( ; j
< bblen
; j
++) {
870 if (ch
== header_end
[header_end_match_pos
]) {
871 header_end_match_pos
++;
872 if (4 == header_end_match_pos
) {
875 goto found_header_end
;
890 buffer_reset(con
->request
.request
);
892 for (c
= cq
->first
; c
; c
= c
->next
) {
893 size_t len
= buffer_string_length(c
->mem
) - c
->offset
;
895 if (c
== last_chunk
) {
899 buffer_append_string_len(con
->request
.request
, c
->mem
->ptr
+ c
->offset
, len
);
901 cq
->bytes_out
+= len
;
903 if (c
== last_chunk
) break;
906 connection_set_state(srv
, con
, CON_STATE_REQUEST_END
);
907 } else if (chunkqueue_length(cq
) > 64 * 1024) {
908 log_error_write(srv
, __FILE__
, __LINE__
, "s", "oversized request-header -> sending Status 414");
910 con
->http_status
= 414; /* Request-URI too large */
912 connection_set_state(srv
, con
, CON_STATE_HANDLE_REQUEST
);
913 } else if (is_closed
) {
914 /* the connection got closed and we didn't got enough data to leave CON_STATE_READ;
915 * the only way is to leave here */
916 connection_set_state(srv
, con
, CON_STATE_ERROR
);
919 chunkqueue_remove_finished_chunks(cq
);
924 static handler_t
connection_handle_fdevent(server
*srv
, void *context
, int revents
) {
925 connection
*con
= context
;
927 joblist_append(srv
, con
);
929 if (con
->srv_socket
->is_ssl
) {
930 /* ssl may read and write for both reads and writes */
931 if (revents
& (FDEVENT_IN
| FDEVENT_OUT
)) {
932 con
->is_readable
= 1;
933 con
->is_writable
= 1;
936 if (revents
& FDEVENT_IN
) {
937 con
->is_readable
= 1;
939 if (revents
& FDEVENT_OUT
) {
940 con
->is_writable
= 1;
941 /* we don't need the event twice */
946 if (revents
& ~(FDEVENT_IN
| FDEVENT_OUT
)) {
947 /* looks like an error */
949 /* FIXME: revents = 0x19 still means that we should read from the queue */
950 if (revents
& FDEVENT_HUP
) {
951 if (con
->state
== CON_STATE_CLOSE
) {
952 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
954 /* sigio reports the wrong event here
956 * there was no HUP at all
958 #ifdef USE_LINUX_SIGIO
959 if (srv
->ev
->in_sigio
== 1) {
960 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
961 "connection closed: poll() -> HUP", con
->fd
);
963 connection_set_state(srv
, con
, CON_STATE_ERROR
);
966 connection_set_state(srv
, con
, CON_STATE_ERROR
);
970 } else if (revents
& FDEVENT_ERR
) {
971 /* error, connection reset, whatever... we don't want to spam the logfile */
973 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
974 "connection closed: poll() -> ERR", con
->fd
);
976 connection_set_state(srv
, con
, CON_STATE_ERROR
);
978 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
979 "connection closed: poll() -> ???", revents
);
983 if (con
->state
== CON_STATE_READ
) {
984 connection_handle_read_state(srv
, con
);
987 if (con
->state
== CON_STATE_WRITE
&&
988 !chunkqueue_is_empty(con
->write_queue
) &&
991 if (-1 == connection_handle_write(srv
, con
)) {
992 connection_set_state(srv
, con
, CON_STATE_ERROR
);
994 log_error_write(srv
, __FILE__
, __LINE__
, "ds",
996 "handle write failed.");
1000 if (con
->state
== CON_STATE_CLOSE
) {
1001 /* flush the read buffers */
1005 len
= read(con
->fd
, buf
, sizeof(buf
));
1006 if (len
== 0 || (len
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
) ) {
1007 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
1011 return HANDLER_FINISHED
;
1015 connection
*connection_accept(server
*srv
, server_socket
*srv_socket
) {
1016 /* accept everything */
1018 /* search an empty place */
1022 /* accept it and register the fd */
1025 * check if we can still open a new connections
1030 if (srv
->conns
->used
>= srv
->max_conns
) {
1034 cnt_len
= sizeof(cnt_addr
);
1036 if (-1 == (cnt
= accept(srv_socket
->fd
, (struct sockaddr
*) &cnt_addr
, &cnt_len
))) {
1039 #if EWOULDBLOCK != EAGAIN
1043 /* we were stopped _before_ we had a connection */
1044 case ECONNABORTED
: /* this is a FreeBSD thingy */
1045 /* we were stopped _after_ we had a connection */
1051 log_error_write(srv
, __FILE__
, __LINE__
, "ssd", "accept failed:", strerror(errno
), errno
);
1055 if (cnt_addr
.plain
.sa_family
!= AF_UNIX
) {
1056 network_accept_tcp_nagle_disable(cnt
);
1058 return connection_accepted(srv
, srv_socket
, &cnt_addr
, cnt
);
1062 connection
*connection_accepted(server
*srv
, server_socket
*srv_socket
, sock_addr
*cnt_addr
, int cnt
) {
1067 /* ok, we have the connection, register it */
1069 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1074 con
= connections_get_new_connection(srv
);
1078 fdevent_register(srv
->ev
, con
->fd
, connection_handle_fdevent
, con
);
1080 connection_set_state(srv
, con
, CON_STATE_REQUEST_START
);
1082 con
->connection_start
= srv
->cur_ts
;
1083 con
->dst_addr
= *cnt_addr
;
1084 buffer_copy_string(con
->dst_addr_buf
, inet_ntop_cache_get_ip(srv
, &(con
->dst_addr
)));
1085 con
->srv_socket
= srv_socket
;
1087 if (-1 == (fdevent_fcntl_set(srv
->ev
, con
->fd
))) {
1088 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed: ", strerror(errno
));
1092 /* connect FD to SSL */
1093 if (srv_socket
->is_ssl
) {
1094 if (NULL
== (con
->ssl
= SSL_new(srv_socket
->ssl_ctx
))) {
1095 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "SSL:",
1096 ERR_error_string(ERR_get_error(), NULL
));
1101 con
->renegotiations
= 0;
1102 SSL_set_app_data(con
->ssl
, con
);
1103 SSL_set_accept_state(con
->ssl
);
1105 if (1 != (SSL_set_fd(con
->ssl
, cnt
))) {
1106 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "SSL:",
1107 ERR_error_string(ERR_get_error(), NULL
));
1116 int connection_state_machine(server
*srv
, connection
*con
) {
1119 if (srv
->srvconf
.log_state_handling
) {
1120 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1123 connection_get_state(con
->state
));
1127 size_t ostate
= con
->state
;
1129 if (srv
->srvconf
.log_state_handling
) {
1130 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1131 "state for fd", con
->fd
, connection_get_state(con
->state
));
1134 switch (con
->state
) {
1135 case CON_STATE_REQUEST_START
: /* transient */
1136 con
->request_start
= srv
->cur_ts
;
1137 con
->read_idle_ts
= srv
->cur_ts
;
1138 if (con
->conf
.high_precision_timestamps
)
1139 log_clock_gettime_realtime(&con
->request_start_hp
);
1141 con
->request_count
++;
1142 con
->loops_per_request
= 0;
1144 connection_set_state(srv
, con
, CON_STATE_READ
);
1147 case CON_STATE_REQUEST_END
: /* transient */
1148 buffer_reset(con
->uri
.authority
);
1149 buffer_reset(con
->uri
.path
);
1150 buffer_reset(con
->uri
.query
);
1151 buffer_reset(con
->request
.orig_uri
);
1153 if (http_request_parse(srv
, con
)) {
1154 /* we have to read some data from the POST request */
1156 connection_set_state(srv
, con
, CON_STATE_READ_POST
);
1161 connection_set_state(srv
, con
, CON_STATE_HANDLE_REQUEST
);
1164 case CON_STATE_READ_POST
:
1165 case CON_STATE_HANDLE_REQUEST
:
1167 * the request is parsed
1169 * decided what to do with the request
1175 switch (r
= http_response_prepare(srv
, con
)) {
1176 case HANDLER_WAIT_FOR_EVENT
:
1177 if (!con
->file_finished
&& (!con
->file_started
|| 0 == con
->conf
.stream_response_body
)) {
1178 break; /* come back here */
1180 /* response headers received from backend; fall through to start response */
1181 case HANDLER_FINISHED
:
1182 if (con
->error_handler_saved_status
> 0) {
1183 con
->request
.http_method
= con
->error_handler_saved_method
;
1185 if (con
->mode
== DIRECT
) {
1186 if (con
->error_handler_saved_status
) {
1187 if (con
->error_handler_saved_status
> 0) {
1188 con
->http_status
= con
->error_handler_saved_status
;
1189 } else if (con
->http_status
== 404 || con
->http_status
== 403) {
1190 /* error-handler-404 is a 404 */
1191 con
->http_status
= -con
->error_handler_saved_status
;
1193 /* error-handler-404 is back and has generated content */
1194 /* if Status: was set, take it otherwise use 200 */
1196 } else if (con
->http_status
>= 400) {
1197 buffer
*error_handler
= NULL
;
1198 if (!buffer_string_is_empty(con
->conf
.error_handler
)) {
1199 error_handler
= con
->conf
.error_handler
;
1200 } else if ((con
->http_status
== 404 || con
->http_status
== 403)
1201 && !buffer_string_is_empty(con
->conf
.error_handler_404
)) {
1202 error_handler
= con
->conf
.error_handler_404
;
1205 if (error_handler
) {
1206 /* call error-handler */
1208 /* set REDIRECT_STATUS to save current HTTP status code
1209 * for access by dynamic handlers
1210 * https://redmine.lighttpd.net/issues/1828 */
1212 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
1213 ds
= data_string_init();
1215 buffer_copy_string_len(ds
->key
, CONST_STR_LEN("REDIRECT_STATUS"));
1216 buffer_append_int(ds
->value
, con
->http_status
);
1217 array_insert_unique(con
->environment
, (data_unset
*)ds
);
1219 if (error_handler
== con
->conf
.error_handler
) {
1220 plugins_call_connection_reset(srv
, con
);
1222 if (con
->request
.content_length
) {
1223 if ((off_t
)con
->request
.content_length
!= chunkqueue_length(con
->request_content_queue
)) {
1224 con
->keep_alive
= 0;
1226 con
->request
.content_length
= 0;
1227 chunkqueue_reset(con
->request_content_queue
);
1230 con
->is_writable
= 1;
1231 con
->file_finished
= 0;
1232 con
->file_started
= 0;
1233 con
->got_response
= 0;
1234 con
->parsed_response
= 0;
1235 con
->response
.keep_alive
= 0;
1236 con
->response
.content_length
= -1;
1237 con
->response
.transfer_encoding
= 0;
1239 con
->error_handler_saved_status
= con
->http_status
;
1240 con
->error_handler_saved_method
= con
->request
.http_method
;
1242 con
->request
.http_method
= HTTP_METHOD_GET
;
1243 } else { /*(preserve behavior for server.error-handler-404)*/
1244 con
->error_handler_saved_status
= -con
->http_status
; /*(negative to flag old behavior)*/
1247 buffer_copy_buffer(con
->request
.uri
, error_handler
);
1248 connection_handle_errdoc_init(srv
, con
);
1249 con
->http_status
= 0; /*(after connection_handle_errdoc_init())*/
1256 if (con
->http_status
== 0) con
->http_status
= 200;
1258 /* we have something to send, go on */
1259 connection_set_state(srv
, con
, CON_STATE_RESPONSE_START
);
1261 case HANDLER_WAIT_FOR_FD
:
1264 fdwaitqueue_append(srv
, con
);
1267 case HANDLER_COMEBACK
:
1271 /* something went wrong */
1272 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1275 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", "unknown ret-value: ", con
->fd
, r
);
1280 case CON_STATE_RESPONSE_START
:
1282 * the decision is done
1283 * - create the HTTP-Response-Header
1287 if (-1 == connection_handle_write_prepare(srv
, con
)) {
1288 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1293 connection_set_state(srv
, con
, CON_STATE_WRITE
);
1295 case CON_STATE_RESPONSE_END
: /* transient */
1296 case CON_STATE_ERROR
: /* transient */
1297 connection_handle_response_end_state(srv
, con
);
1299 case CON_STATE_CONNECT
:
1300 chunkqueue_reset(con
->read_queue
);
1302 con
->request_count
= 0;
1305 case CON_STATE_CLOSE
:
1306 connection_handle_close_state(srv
, con
);
1308 case CON_STATE_READ
:
1309 connection_handle_read_state(srv
, con
);
1311 case CON_STATE_WRITE
:
1313 /* only try to write if we have something in the queue */
1314 if (!chunkqueue_is_empty(con
->write_queue
)) {
1315 if (con
->is_writable
) {
1316 if (-1 == connection_handle_write(srv
, con
)) {
1317 log_error_write(srv
, __FILE__
, __LINE__
, "ds",
1319 "handle write failed.");
1320 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1323 if (con
->state
!= CON_STATE_WRITE
) break;
1325 } else if (con
->file_finished
) {
1326 connection_set_state(srv
, con
, CON_STATE_RESPONSE_END
);
1330 if (con
->mode
!= DIRECT
&& !con
->file_finished
) {
1331 switch(r
= plugins_call_handle_subrequest(srv
, con
)) {
1332 case HANDLER_WAIT_FOR_EVENT
:
1333 case HANDLER_FINISHED
:
1336 case HANDLER_WAIT_FOR_FD
:
1338 fdwaitqueue_append(srv
, con
);
1340 case HANDLER_COMEBACK
:
1342 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", "unexpected subrequest handler ret-value: ", con
->fd
, r
);
1345 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1349 } while (con
->state
== CON_STATE_WRITE
&& (!chunkqueue_is_empty(con
->write_queue
) ? con
->is_writable
: con
->file_finished
));
1353 log_error_write(srv
, __FILE__
, __LINE__
, "sdd",
1354 "unknown state:", con
->fd
, con
->state
);
1361 } else if (ostate
== con
->state
) {
1366 if (srv
->srvconf
.log_state_handling
) {
1367 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1370 connection_get_state(con
->state
));
1374 switch(con
->state
) {
1375 case CON_STATE_READ
:
1376 case CON_STATE_CLOSE
:
1379 case CON_STATE_WRITE
:
1380 /* request write-fdevent only if we really need it
1381 * - if we have data to write
1382 * - if the socket is not writable yet
1384 if (!chunkqueue_is_empty(con
->write_queue
) &&
1385 (con
->is_writable
== 0) &&
1386 (con
->traffic_limit_reached
== 0)) {
1390 case CON_STATE_READ_POST
:
1391 if (con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_POLLIN
) {
1398 if (-1 != con
->fd
) {
1399 const int events
= fdevent_event_get_interest(srv
->ev
, con
->fd
);
1400 if (con
->is_readable
< 0) {
1401 con
->is_readable
= 0;
1404 if (con
->is_writable
< 0) {
1405 con
->is_writable
= 0;
1409 /* update timestamps when enabling interest in events */
1410 if ((r
& FDEVENT_IN
) && !(events
& FDEVENT_IN
)) {
1411 con
->read_idle_ts
= srv
->cur_ts
;
1413 if ((r
& FDEVENT_OUT
) && !(events
& FDEVENT_OUT
)) {
1414 con
->write_request_ts
= srv
->cur_ts
;
1416 fdevent_event_set(srv
->ev
, &con
->fde_ndx
, con
->fd
, r
);