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
;
127 if (srv_sock
->is_ssl
) {
128 if (con
->ssl
) SSL_free(con
->ssl
);
133 fdevent_event_del(srv
->ev
, &(con
->fde_ndx
), con
->fd
);
134 fdevent_unregister(srv
->ev
, con
->fd
);
136 if (closesocket(con
->fd
)) {
137 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
138 "(warning) close:", con
->fd
, strerror(errno
));
141 if (close(con
->fd
)) {
142 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
143 "(warning) close:", con
->fd
, strerror(errno
));
150 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
151 "closed()", con
->fd
);
154 connection_del(srv
, con
);
155 connection_set_state(srv
, con
, CON_STATE_CONNECT
);
160 static void connection_handle_errdoc_init(server
*srv
, connection
*con
) {
161 /* modules that produce headers required with error response should
162 * typically also produce an error document. Make an exception for
163 * mod_auth WWW-Authenticate response header. */
164 buffer
*www_auth
= NULL
;
165 if (401 == con
->http_status
) {
166 data_string
*ds
= (data_string
*)array_get_element(con
->response
.headers
, "WWW-Authenticate");
168 www_auth
= buffer_init_buffer(ds
->value
);
172 con
->response
.transfer_encoding
= 0;
173 buffer_reset(con
->physical
.path
);
174 array_reset(con
->response
.headers
);
175 chunkqueue_reset(con
->write_queue
);
177 if (NULL
!= www_auth
) {
178 response_header_insert(srv
, con
, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(www_auth
));
179 buffer_free(www_auth
);
183 static int connection_handle_write_prepare(server
*srv
, connection
*con
) {
184 if (con
->mode
== DIRECT
) {
186 switch(con
->request
.http_method
) {
187 case HTTP_METHOD_GET
:
188 case HTTP_METHOD_POST
:
189 case HTTP_METHOD_HEAD
:
191 case HTTP_METHOD_OPTIONS
:
193 * 400 is coming from the request-parser BEFORE uri.path is set
194 * 403 is from the response handler when noone else catched it
197 if ((!con
->http_status
|| con
->http_status
== 200) && !buffer_string_is_empty(con
->uri
.path
) &&
198 con
->uri
.path
->ptr
[0] != '*') {
199 response_header_insert(srv
, con
, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
201 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
202 con
->parsed_response
&= ~HTTP_CONTENT_LENGTH
;
204 con
->http_status
= 200;
205 con
->file_finished
= 1;
207 chunkqueue_reset(con
->write_queue
);
211 if (0 == con
->http_status
) {
212 con
->http_status
= 501;
218 if (con
->http_status
== 0) {
219 con
->http_status
= 403;
222 switch(con
->http_status
) {
223 case 204: /* class: header only */
226 /* disable chunked encoding again as we have no body */
227 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
228 con
->parsed_response
&= ~HTTP_CONTENT_LENGTH
;
229 chunkqueue_reset(con
->write_queue
);
231 con
->file_finished
= 1;
233 default: /* class: header + body */
234 if (con
->mode
!= DIRECT
) break;
236 /* only custom body for 4xx and 5xx */
237 if (con
->http_status
< 400 || con
->http_status
>= 600) break;
239 con
->file_finished
= 0;
241 connection_handle_errdoc_init(srv
, con
);
243 /* try to send static errorfile */
244 if (!buffer_string_is_empty(con
->conf
.errorfile_prefix
)) {
245 stat_cache_entry
*sce
= NULL
;
247 buffer_copy_buffer(con
->physical
.path
, con
->conf
.errorfile_prefix
);
248 buffer_append_int(con
->physical
.path
, con
->http_status
);
249 buffer_append_string_len(con
->physical
.path
, CONST_STR_LEN(".html"));
251 if (0 == http_chunk_append_file(srv
, con
, con
->physical
.path
)) {
252 con
->file_finished
= 1;
253 if (HANDLER_ERROR
!= stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
254 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce
->content_type
));
259 if (!con
->file_finished
) {
262 buffer_reset(con
->physical
.path
);
264 con
->file_finished
= 1;
267 /* build default error-page */
268 buffer_copy_string_len(b
, CONST_STR_LEN(
269 "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
270 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
271 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
272 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
275 buffer_append_int(b
, con
->http_status
);
276 buffer_append_string_len(b
, CONST_STR_LEN(" - "));
277 buffer_append_string(b
, get_http_status_name(con
->http_status
));
279 buffer_append_string_len(b
, CONST_STR_LEN(
284 buffer_append_int(b
, con
->http_status
);
285 buffer_append_string_len(b
, CONST_STR_LEN(" - "));
286 buffer_append_string(b
, get_http_status_name(con
->http_status
));
288 buffer_append_string_len(b
, CONST_STR_LEN("</h1>\n"
293 (void)http_chunk_append_buffer(srv
, con
, b
);
296 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
301 if (con
->file_finished
) {
302 /* we have all the content and chunked encoding is not used, set a content-length */
304 if ((!(con
->parsed_response
& HTTP_CONTENT_LENGTH
)) &&
305 (con
->response
.transfer_encoding
& HTTP_TRANSFER_ENCODING_CHUNKED
) == 0) {
306 off_t qlen
= chunkqueue_length(con
->write_queue
);
309 * The Content-Length header only can be sent if we have content:
310 * - HEAD doesn't have a content-body (but have a content-length)
311 * - 1xx, 204 and 304 don't have a content-body (RFC 2616 Section 4.3)
313 * Otherwise generate a Content-Length header as chunked encoding is not
316 if ((con
->http_status
>= 100 && con
->http_status
< 200) ||
317 con
->http_status
== 204 ||
318 con
->http_status
== 304) {
320 /* no Content-Body, no Content-Length */
321 if (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "Content-Length"))) {
322 buffer_reset(ds
->value
); /* Headers with empty values are ignored for output */
324 } else if (qlen
> 0 || con
->request
.http_method
!= HTTP_METHOD_HEAD
) {
325 /* qlen = 0 is important for Redirects (301, ...) as they MAY have
326 * a content. Browsers are waiting for a Content otherwise
328 buffer_copy_int(srv
->tmp_buf
, qlen
);
330 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Length"), CONST_BUF_LEN(srv
->tmp_buf
));
335 * the file isn't finished yet, but we have all headers
337 * to get keep-alive we either need:
338 * - Content-Length: ... (HTTP/1.0 and HTTP/1.0) or
339 * - Transfer-Encoding: chunked (HTTP/1.1)
342 if (((con
->parsed_response
& HTTP_CONTENT_LENGTH
) == 0) &&
343 ((con
->response
.transfer_encoding
& HTTP_TRANSFER_ENCODING_CHUNKED
) == 0)) {
344 if (con
->request
.http_version
== HTTP_VERSION_1_1
) {
345 off_t qlen
= chunkqueue_length(con
->write_queue
);
346 con
->response
.transfer_encoding
= HTTP_TRANSFER_ENCODING_CHUNKED
;
348 /* create initial Transfer-Encoding: chunked segment */
349 buffer
*b
= srv
->tmp_chunk_len
;
350 buffer_string_set_length(b
, 0);
351 buffer_append_uint_hex(b
, (uintmax_t)qlen
);
352 buffer_append_string_len(b
, CONST_STR_LEN("\r\n"));
353 chunkqueue_prepend_buffer(con
->write_queue
, b
);
354 chunkqueue_append_mem(con
->write_queue
, CONST_STR_LEN("\r\n"));
362 * if the backend sent a Connection: close, follow the wish
364 * NOTE: if the backend sent Connection: Keep-Alive, but no Content-Length, we
365 * will close the connection. That's fine. We can always decide the close
368 * FIXME: to be nice we should remove the Connection: ...
370 if (con
->parsed_response
& HTTP_CONNECTION
) {
371 /* a subrequest disable keep-alive although the client wanted it */
372 if (con
->keep_alive
&& !con
->response
.keep_alive
) {
378 if (con
->request
.http_method
== HTTP_METHOD_HEAD
) {
380 * a HEAD request has the same as a GET
381 * without the content
383 con
->file_finished
= 1;
385 chunkqueue_reset(con
->write_queue
);
386 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
389 http_response_write_header(srv
, con
);
394 static int connection_handle_write(server
*srv
, connection
*con
) {
395 switch(network_write_chunkqueue(srv
, con
, con
->write_queue
, MAX_WRITE_LIMIT
)) {
397 con
->write_request_ts
= srv
->cur_ts
;
398 if (con
->file_finished
) {
399 connection_set_state(srv
, con
, CON_STATE_RESPONSE_END
);
402 case -1: /* error on our side */
403 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
404 "connection closed: write failed on fd", con
->fd
);
405 connection_set_state(srv
, con
, CON_STATE_ERROR
);
407 case -2: /* remote close */
408 connection_set_state(srv
, con
, CON_STATE_ERROR
);
411 con
->write_request_ts
= srv
->cur_ts
;
412 con
->is_writable
= 0;
414 /* not finished yet -> WRITE */
423 connection
*connection_init(server
*srv
) {
428 con
= calloc(1, sizeof(*con
));
429 force_assert(NULL
!= con
);
434 con
->bytes_written
= 0;
436 con
->bytes_header
= 0;
437 con
->loops_per_request
= 0;
440 con->x = buffer_init();
443 CLEAN(request
.request_line
);
444 CLEAN(request
.request
);
445 CLEAN(request
.pathinfo
);
447 CLEAN(request
.orig_uri
);
450 CLEAN(uri
.authority
);
455 CLEAN(physical
.doc_root
);
456 CLEAN(physical
.path
);
457 CLEAN(physical
.basedir
);
458 CLEAN(physical
.rel_path
);
459 CLEAN(physical
.etag
);
460 CLEAN(parse_request
);
464 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
465 CLEAN(tlsext_server_name
);
469 con
->write_queue
= chunkqueue_init();
470 con
->read_queue
= chunkqueue_init();
471 con
->request_content_queue
= chunkqueue_init();
472 chunkqueue_set_tempdirs(
473 con
->request_content_queue
,
474 srv
->srvconf
.upload_tempdirs
,
475 srv
->srvconf
.upload_temp_file_size
);
477 con
->request
.headers
= array_init();
478 con
->response
.headers
= array_init();
479 con
->environment
= array_init();
481 /* init plugin specific connection structures */
483 con
->plugin_ctx
= calloc(1, (srv
->plugins
.used
+ 1) * sizeof(void *));
484 force_assert(NULL
!= con
->plugin_ctx
);
486 con
->cond_cache
= calloc(srv
->config_context
->used
, sizeof(cond_cache_t
));
487 force_assert(NULL
!= con
->cond_cache
);
488 config_setup_connection(srv
, con
);
493 void connections_free(server
*srv
) {
494 connections
*conns
= srv
->conns
;
497 for (i
= 0; i
< conns
->size
; i
++) {
498 connection
*con
= conns
->ptr
[i
];
500 connection_reset(srv
, con
);
502 chunkqueue_free(con
->write_queue
);
503 chunkqueue_free(con
->read_queue
);
504 chunkqueue_free(con
->request_content_queue
);
505 array_free(con
->request
.headers
);
506 array_free(con
->response
.headers
);
507 array_free(con
->environment
);
513 CLEAN(request
.request_line
);
514 CLEAN(request
.request
);
515 CLEAN(request
.pathinfo
);
517 CLEAN(request
.orig_uri
);
520 CLEAN(uri
.authority
);
525 CLEAN(physical
.doc_root
);
526 CLEAN(physical
.path
);
527 CLEAN(physical
.basedir
);
528 CLEAN(physical
.etag
);
529 CLEAN(physical
.rel_path
);
530 CLEAN(parse_request
);
534 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
535 CLEAN(tlsext_server_name
);
538 free(con
->plugin_ctx
);
539 free(con
->cond_cache
);
548 int connection_reset(server
*srv
, connection
*con
) {
551 plugins_call_connection_reset(srv
, con
);
553 con
->is_readable
= 1;
554 con
->is_writable
= 1;
555 con
->http_status
= 0;
556 con
->file_finished
= 0;
557 con
->file_started
= 0;
558 con
->got_response
= 0;
560 con
->parsed_response
= 0;
562 con
->bytes_written
= 0;
563 con
->bytes_written_cur_second
= 0;
565 con
->bytes_header
= 0;
566 con
->loops_per_request
= 0;
568 con
->request
.http_method
= HTTP_METHOD_UNSET
;
569 con
->request
.http_version
= HTTP_VERSION_UNSET
;
571 con
->request
.http_if_modified_since
= NULL
;
572 con
->request
.http_if_none_match
= NULL
;
574 con
->response
.keep_alive
= 0;
575 con
->response
.content_length
= -1;
576 con
->response
.transfer_encoding
= 0;
581 if (con->x) buffer_reset(con->x);
584 CLEAN(request
.request_line
);
585 CLEAN(request
.pathinfo
);
586 CLEAN(request
.request
);
588 /* CLEAN(request.orig_uri); */
591 /* CLEAN(uri.authority); */
592 /* CLEAN(uri.path); */
594 /* CLEAN(uri.query); */
596 CLEAN(physical
.doc_root
);
597 CLEAN(physical
.path
);
598 CLEAN(physical
.basedir
);
599 CLEAN(physical
.rel_path
);
600 CLEAN(physical
.etag
);
602 CLEAN(parse_request
);
605 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
606 CLEAN(tlsext_server_name
);
611 if (con->x) con->x->used = 0;
616 con->request.x = NULL;
620 CLEAN(http_content_type
);
622 con
->request
.content_length
= 0;
624 array_reset(con
->request
.headers
);
625 array_reset(con
->response
.headers
);
626 array_reset(con
->environment
);
628 chunkqueue_reset(con
->write_queue
);
629 chunkqueue_reset(con
->request_content_queue
);
631 /* the plugins should cleanup themself */
632 for (i
= 0; i
< srv
->plugins
.used
; i
++) {
633 plugin
*p
= ((plugin
**)(srv
->plugins
.ptr
))[i
];
634 plugin_data
*pd
= p
->data
;
638 if (con
->plugin_ctx
[pd
->id
] != NULL
) {
639 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "missing cleanup in", p
->name
);
642 con
->plugin_ctx
[pd
->id
] = NULL
;
645 /* The cond_cache gets reset in response.c */
646 /* config_cond_cache_reset(srv, con); */
649 con
->error_handler_saved_status
= 0;
650 /*con->error_handler_saved_method = HTTP_METHOD_UNSET;*/
651 /*(error_handler_saved_method value is not valid unless error_handler_saved_status is set)*/
653 config_setup_connection(srv
, con
);
659 * handle all header and content read
661 * we get called by the state-engine and by the fdevent-handler
663 static int connection_handle_read_state(server
*srv
, connection
*con
) {
664 chunk
*c
, *last_chunk
;
666 chunkqueue
*cq
= con
->read_queue
;
667 int is_closed
= 0; /* the connection got closed, if we don't have a complete header, -> error */
668 /* when in CON_STATE_READ: about to receive first byte for a request: */
669 int is_request_start
= chunkqueue_is_empty(cq
);
671 if (con
->is_readable
) {
672 con
->read_idle_ts
= srv
->cur_ts
;
674 switch(connection_handle_read(srv
, con
)) {
685 chunkqueue_remove_finished_chunks(cq
);
687 /* we might have got several packets at once
690 /* update request_start timestamp when first byte of
691 * next request is received on a keep-alive connection */
692 if (con
->request_count
> 1 && is_request_start
) {
693 con
->request_start
= srv
->cur_ts
;
694 if (con
->conf
.high_precision_timestamps
)
695 log_clock_gettime_realtime(&con
->request_start_hp
);
698 /* if there is a \r\n\r\n in the chunkqueue
700 * scan the chunk-queue twice
701 * 1. to find the \r\n\r\n
702 * 2. to copy the header-packet
709 for (c
= cq
->first
; c
; c
= c
->next
) {
711 size_t len
= buffer_string_length(c
->mem
) - c
->offset
;
712 const char *b
= c
->mem
->ptr
+ c
->offset
;
714 for (i
= 0; i
< len
; ++i
) {
718 /* chec if \n\r\n follows */
721 const char header_end
[] = "\r\n\r\n";
722 int header_end_match_pos
= 1;
724 for ( ; cc
; cc
= cc
->next
, j
= 0 ) {
725 size_t bblen
= buffer_string_length(cc
->mem
) - cc
->offset
;
726 const char *bb
= cc
->mem
->ptr
+ cc
->offset
;
728 for ( ; j
< bblen
; j
++) {
731 if (ch
== header_end
[header_end_match_pos
]) {
732 header_end_match_pos
++;
733 if (4 == header_end_match_pos
) {
736 goto found_header_end
;
751 buffer_reset(con
->request
.request
);
753 for (c
= cq
->first
; c
; c
= c
->next
) {
754 size_t len
= buffer_string_length(c
->mem
) - c
->offset
;
756 if (c
== last_chunk
) {
760 buffer_append_string_len(con
->request
.request
, c
->mem
->ptr
+ c
->offset
, len
);
762 cq
->bytes_out
+= len
;
764 if (c
== last_chunk
) break;
767 connection_set_state(srv
, con
, CON_STATE_REQUEST_END
);
768 } else if (chunkqueue_length(cq
) > 64 * 1024) {
769 log_error_write(srv
, __FILE__
, __LINE__
, "s", "oversized request-header -> sending Status 414");
771 con
->http_status
= 414; /* Request-URI too large */
773 connection_set_state(srv
, con
, CON_STATE_HANDLE_REQUEST
);
774 } else if (is_closed
) {
775 /* the connection got closed and we didn't got enough data to leave CON_STATE_READ;
776 * the only way is to leave here */
777 connection_set_state(srv
, con
, CON_STATE_ERROR
);
780 chunkqueue_remove_finished_chunks(cq
);
785 static handler_t
connection_handle_fdevent(server
*srv
, void *context
, int revents
) {
786 connection
*con
= context
;
788 joblist_append(srv
, con
);
790 if (con
->srv_socket
->is_ssl
) {
791 /* ssl may read and write for both reads and writes */
792 if (revents
& (FDEVENT_IN
| FDEVENT_OUT
)) {
793 con
->is_readable
= 1;
794 con
->is_writable
= 1;
797 if (revents
& FDEVENT_IN
) {
798 con
->is_readable
= 1;
800 if (revents
& FDEVENT_OUT
) {
801 con
->is_writable
= 1;
802 /* we don't need the event twice */
807 if (revents
& ~(FDEVENT_IN
| FDEVENT_OUT
)) {
808 /* looks like an error */
810 /* FIXME: revents = 0x19 still means that we should read from the queue */
811 if (revents
& FDEVENT_HUP
) {
812 if (con
->state
== CON_STATE_CLOSE
) {
813 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
815 /* sigio reports the wrong event here
817 * there was no HUP at all
819 #ifdef USE_LINUX_SIGIO
820 if (srv
->ev
->in_sigio
== 1) {
821 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
822 "connection closed: poll() -> HUP", con
->fd
);
824 connection_set_state(srv
, con
, CON_STATE_ERROR
);
827 connection_set_state(srv
, con
, CON_STATE_ERROR
);
831 } else if (revents
& FDEVENT_ERR
) {
832 /* error, connection reset, whatever... we don't want to spam the logfile */
834 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
835 "connection closed: poll() -> ERR", con
->fd
);
837 connection_set_state(srv
, con
, CON_STATE_ERROR
);
839 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
840 "connection closed: poll() -> ???", revents
);
844 if (con
->state
== CON_STATE_READ
) {
845 connection_handle_read_state(srv
, con
);
848 if (con
->state
== CON_STATE_WRITE
&&
849 !chunkqueue_is_empty(con
->write_queue
) &&
852 if (-1 == connection_handle_write(srv
, con
)) {
853 connection_set_state(srv
, con
, CON_STATE_ERROR
);
855 log_error_write(srv
, __FILE__
, __LINE__
, "ds",
857 "handle write failed.");
861 if (con
->state
== CON_STATE_CLOSE
) {
862 /* flush the read buffers */
866 len
= read(con
->fd
, buf
, sizeof(buf
));
867 if (len
== 0 || (len
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
) ) {
868 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
872 return HANDLER_FINISHED
;
876 connection
*connection_accept(server
*srv
, server_socket
*srv_socket
) {
877 /* accept everything */
879 /* search an empty place */
883 /* accept it and register the fd */
886 * check if we can still open a new connections
891 if (srv
->conns
->used
>= srv
->max_conns
) {
895 cnt_len
= sizeof(cnt_addr
);
897 if (-1 == (cnt
= accept(srv_socket
->fd
, (struct sockaddr
*) &cnt_addr
, &cnt_len
))) {
900 #if EWOULDBLOCK != EAGAIN
904 /* we were stopped _before_ we had a connection */
905 case ECONNABORTED
: /* this is a FreeBSD thingy */
906 /* we were stopped _after_ we had a connection */
912 log_error_write(srv
, __FILE__
, __LINE__
, "ssd", "accept failed:", strerror(errno
), errno
);
916 if (cnt_addr
.plain
.sa_family
!= AF_UNIX
) {
917 network_accept_tcp_nagle_disable(cnt
);
919 return connection_accepted(srv
, srv_socket
, &cnt_addr
, cnt
);
923 connection
*connection_accepted(server
*srv
, server_socket
*srv_socket
, sock_addr
*cnt_addr
, int cnt
) {
928 /* ok, we have the connection, register it */
930 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
935 con
= connections_get_new_connection(srv
);
939 fdevent_register(srv
->ev
, con
->fd
, connection_handle_fdevent
, con
);
941 connection_set_state(srv
, con
, CON_STATE_REQUEST_START
);
943 con
->connection_start
= srv
->cur_ts
;
944 con
->dst_addr
= *cnt_addr
;
945 buffer_copy_string(con
->dst_addr_buf
, inet_ntop_cache_get_ip(srv
, &(con
->dst_addr
)));
946 con
->srv_socket
= srv_socket
;
948 if (-1 == (fdevent_fcntl_set(srv
->ev
, con
->fd
))) {
949 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed: ", strerror(errno
));
953 /* connect FD to SSL */
954 if (srv_socket
->is_ssl
) {
955 if (NULL
== (con
->ssl
= SSL_new(srv_socket
->ssl_ctx
))) {
956 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "SSL:",
957 ERR_error_string(ERR_get_error(), NULL
));
962 con
->renegotiations
= 0;
963 SSL_set_app_data(con
->ssl
, con
);
964 SSL_set_accept_state(con
->ssl
);
966 if (1 != (SSL_set_fd(con
->ssl
, cnt
))) {
967 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "SSL:",
968 ERR_error_string(ERR_get_error(), NULL
));
977 int connection_state_machine(server
*srv
, connection
*con
) {
980 server_socket
*srv_sock
= con
->srv_socket
;
983 if (srv
->srvconf
.log_state_handling
) {
984 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
987 connection_get_state(con
->state
));
991 size_t ostate
= con
->state
;
993 switch (con
->state
) {
994 case CON_STATE_REQUEST_START
: /* transient */
995 if (srv
->srvconf
.log_state_handling
) {
996 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
997 "state for fd", con
->fd
, connection_get_state(con
->state
));
1000 con
->request_start
= srv
->cur_ts
;
1001 con
->read_idle_ts
= srv
->cur_ts
;
1002 if (con
->conf
.high_precision_timestamps
)
1003 log_clock_gettime_realtime(&con
->request_start_hp
);
1005 con
->request_count
++;
1006 con
->loops_per_request
= 0;
1008 connection_set_state(srv
, con
, CON_STATE_READ
);
1011 case CON_STATE_REQUEST_END
: /* transient */
1012 if (srv
->srvconf
.log_state_handling
) {
1013 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1014 "state for fd", con
->fd
, connection_get_state(con
->state
));
1017 buffer_reset(con
->uri
.authority
);
1018 buffer_reset(con
->uri
.path
);
1019 buffer_reset(con
->uri
.query
);
1020 buffer_reset(con
->request
.orig_uri
);
1022 if (http_request_parse(srv
, con
)) {
1023 /* we have to read some data from the POST request */
1025 connection_set_state(srv
, con
, CON_STATE_READ_POST
);
1030 connection_set_state(srv
, con
, CON_STATE_HANDLE_REQUEST
);
1033 case CON_STATE_READ_POST
:
1034 case CON_STATE_HANDLE_REQUEST
:
1036 * the request is parsed
1038 * decided what to do with the request
1044 if (srv
->srvconf
.log_state_handling
) {
1045 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1046 "state for fd", con
->fd
, connection_get_state(con
->state
));
1049 switch (r
= http_response_prepare(srv
, con
)) {
1050 case HANDLER_WAIT_FOR_EVENT
:
1051 if (!con
->file_finished
&& (!con
->file_started
|| 0 == con
->conf
.stream_response_body
)) {
1052 break; /* come back here */
1054 /* response headers received from backend; fall through to start response */
1055 case HANDLER_FINISHED
:
1056 if (con
->error_handler_saved_status
> 0) {
1057 con
->request
.http_method
= con
->error_handler_saved_method
;
1059 if (con
->mode
== DIRECT
) {
1060 if (con
->error_handler_saved_status
) {
1061 if (con
->error_handler_saved_status
> 0) {
1062 con
->http_status
= con
->error_handler_saved_status
;
1063 } else if (con
->http_status
== 404 || con
->http_status
== 403) {
1064 /* error-handler-404 is a 404 */
1065 con
->http_status
= -con
->error_handler_saved_status
;
1067 /* error-handler-404 is back and has generated content */
1068 /* if Status: was set, take it otherwise use 200 */
1070 } else if (con
->http_status
>= 400) {
1071 buffer
*error_handler
= NULL
;
1072 if (!buffer_string_is_empty(con
->conf
.error_handler
)) {
1073 error_handler
= con
->conf
.error_handler
;
1074 } else if ((con
->http_status
== 404 || con
->http_status
== 403)
1075 && !buffer_string_is_empty(con
->conf
.error_handler_404
)) {
1076 error_handler
= con
->conf
.error_handler_404
;
1079 if (error_handler
) {
1080 /* call error-handler */
1082 /* set REDIRECT_STATUS to save current HTTP status code
1083 * for access by dynamic handlers
1084 * https://redmine.lighttpd.net/issues/1828 */
1086 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
1087 ds
= data_string_init();
1089 buffer_copy_string_len(ds
->key
, CONST_STR_LEN("REDIRECT_STATUS"));
1090 buffer_append_int(ds
->value
, con
->http_status
);
1091 array_insert_unique(con
->environment
, (data_unset
*)ds
);
1093 if (error_handler
== con
->conf
.error_handler
) {
1094 plugins_call_connection_reset(srv
, con
);
1096 if (con
->request
.content_length
) {
1097 if ((off_t
)con
->request
.content_length
!= chunkqueue_length(con
->request_content_queue
)) {
1098 con
->keep_alive
= 0;
1100 con
->request
.content_length
= 0;
1101 chunkqueue_reset(con
->request_content_queue
);
1104 con
->is_writable
= 1;
1105 con
->file_finished
= 0;
1106 con
->file_started
= 0;
1107 con
->got_response
= 0;
1108 con
->parsed_response
= 0;
1109 con
->response
.keep_alive
= 0;
1110 con
->response
.content_length
= -1;
1111 con
->response
.transfer_encoding
= 0;
1113 array_set_key_value(con
->environment
, CONST_STR_LEN("REDIRECT_URI"), CONST_BUF_LEN(con
->request
.orig_uri
));
1114 con
->error_handler_saved_status
= con
->http_status
;
1115 con
->error_handler_saved_method
= con
->request
.http_method
;
1117 con
->request
.http_method
= HTTP_METHOD_GET
;
1118 } else { /*(preserve behavior for server.error-handler-404)*/
1119 array_set_key_value(con
->environment
, CONST_STR_LEN("REDIRECT_URI"), CONST_BUF_LEN(error_handler
));
1120 con
->error_handler_saved_status
= -con
->http_status
; /*(negative to flag old behavior)*/
1123 buffer_copy_buffer(con
->request
.uri
, error_handler
);
1124 connection_handle_errdoc_init(srv
, con
);
1125 con
->http_status
= 0; /*(after connection_handle_errdoc_init())*/
1132 if (con
->http_status
== 0) con
->http_status
= 200;
1134 /* we have something to send, go on */
1135 connection_set_state(srv
, con
, CON_STATE_RESPONSE_START
);
1137 case HANDLER_WAIT_FOR_FD
:
1140 fdwaitqueue_append(srv
, con
);
1143 case HANDLER_COMEBACK
:
1147 /* something went wrong */
1148 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1151 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", "unknown ret-value: ", con
->fd
, r
);
1156 case CON_STATE_RESPONSE_START
:
1158 * the decision is done
1159 * - create the HTTP-Response-Header
1163 if (srv
->srvconf
.log_state_handling
) {
1164 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1165 "state for fd", con
->fd
, connection_get_state(con
->state
));
1168 if (-1 == connection_handle_write_prepare(srv
, con
)) {
1169 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1174 connection_set_state(srv
, con
, CON_STATE_WRITE
);
1176 case CON_STATE_RESPONSE_END
: /* transient */
1177 /* log the request */
1179 if (srv
->srvconf
.log_state_handling
) {
1180 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1181 "state for fd", con
->fd
, connection_get_state(con
->state
));
1184 if (con
->request
.content_length
1185 && (off_t
)con
->request
.content_length
> con
->request_content_queue
->bytes_in
) {
1186 /* request body is present and has not been read completely */
1187 con
->keep_alive
= 0;
1190 plugins_call_handle_request_done(srv
, con
);
1194 if (con
->keep_alive
) {
1195 connection_set_state(srv
, con
, CON_STATE_REQUEST_START
);
1198 con
->request_start
= srv
->cur_ts
;
1199 con
->read_idle_ts
= srv
->cur_ts
;
1202 switch(r
= plugins_call_handle_connection_close(srv
, con
)) {
1204 case HANDLER_FINISHED
:
1207 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "unhandling return value", r
);
1212 if (srv_sock
->is_ssl
) {
1213 switch (SSL_shutdown(con
->ssl
)) {
1218 /* wait for fd-event
1220 * FIXME: wait for fdevent and call SSL_shutdown again
1226 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "SSL:",
1227 ERR_error_string(ERR_get_error(), NULL
));
1231 if ((0 == shutdown(con
->fd
, SHUT_WR
))) {
1232 con
->close_timeout_ts
= srv
->cur_ts
;
1233 connection_set_state(srv
, con
, CON_STATE_CLOSE
);
1235 connection_close(srv
, con
);
1241 connection_reset(srv
, con
);
1244 case CON_STATE_CONNECT
:
1245 if (srv
->srvconf
.log_state_handling
) {
1246 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1247 "state for fd", con
->fd
, connection_get_state(con
->state
));
1250 chunkqueue_reset(con
->read_queue
);
1252 con
->request_count
= 0;
1255 case CON_STATE_CLOSE
:
1256 if (srv
->srvconf
.log_state_handling
) {
1257 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1258 "state for fd", con
->fd
, connection_get_state(con
->state
));
1261 /* we have to do the linger_on_close stuff regardless
1262 * of con->keep_alive; even non-keepalive sockets may
1263 * still have unread data, and closing before reading
1264 * it will make the client not see all our output.
1270 len
= read(con
->fd
, buf
, sizeof(buf
));
1271 if (len
== 0 || (len
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
) ) {
1272 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
1276 if (srv
->cur_ts
- con
->close_timeout_ts
> HTTP_LINGER_TIMEOUT
) {
1277 connection_close(srv
, con
);
1279 if (srv
->srvconf
.log_state_handling
) {
1280 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1281 "connection closed for fd", con
->fd
);
1286 case CON_STATE_READ
:
1287 if (srv
->srvconf
.log_state_handling
) {
1288 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1289 "state for fd", con
->fd
, connection_get_state(con
->state
));
1292 connection_handle_read_state(srv
, con
);
1294 case CON_STATE_WRITE
:
1295 if (srv
->srvconf
.log_state_handling
) {
1296 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1297 "state for fd", con
->fd
, connection_get_state(con
->state
));
1301 /* only try to write if we have something in the queue */
1302 if (!chunkqueue_is_empty(con
->write_queue
)) {
1303 if (con
->is_writable
) {
1304 if (-1 == connection_handle_write(srv
, con
)) {
1305 log_error_write(srv
, __FILE__
, __LINE__
, "ds",
1307 "handle write failed.");
1308 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1311 if (con
->state
!= CON_STATE_WRITE
) break;
1313 } else if (con
->file_finished
) {
1314 connection_set_state(srv
, con
, CON_STATE_RESPONSE_END
);
1318 if (con
->mode
!= DIRECT
&& !con
->file_finished
) {
1319 switch(r
= plugins_call_handle_subrequest(srv
, con
)) {
1320 case HANDLER_WAIT_FOR_EVENT
:
1321 case HANDLER_FINISHED
:
1324 case HANDLER_WAIT_FOR_FD
:
1326 fdwaitqueue_append(srv
, con
);
1328 case HANDLER_COMEBACK
:
1330 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", "unexpected subrequest handler ret-value: ", con
->fd
, r
);
1333 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1337 } while (con
->state
== CON_STATE_WRITE
&& (!chunkqueue_is_empty(con
->write_queue
) ? con
->is_writable
: con
->file_finished
));
1340 case CON_STATE_ERROR
: /* transient */
1342 /* even if the connection was drop we still have to write it to the access log */
1343 if (con
->http_status
) {
1344 plugins_call_handle_request_done(srv
, con
);
1347 if (srv_sock
->is_ssl
) {
1351 switch ((ret
= SSL_shutdown(con
->ssl
))) {
1357 if (-1 != (ret
= SSL_shutdown(con
->ssl
))) break;
1362 switch ((ssl_r
= SSL_get_error(con
->ssl
, ret
))) {
1363 case SSL_ERROR_WANT_WRITE
:
1364 case SSL_ERROR_WANT_READ
:
1366 case SSL_ERROR_SYSCALL
:
1367 /* perhaps we have error waiting in our error-queue */
1368 if (0 != (err
= ERR_get_error())) {
1370 log_error_write(srv
, __FILE__
, __LINE__
, "sdds", "SSL:",
1372 ERR_error_string(err
, NULL
));
1373 } while((err
= ERR_get_error()));
1374 } else if (errno
!= 0) { /* ssl bug (see lighttpd ticket #2213): sometimes errno == 0 */
1380 log_error_write(srv
, __FILE__
, __LINE__
, "sddds", "SSL (error):",
1389 while((err
= ERR_get_error())) {
1390 log_error_write(srv
, __FILE__
, __LINE__
, "sdds", "SSL:",
1392 ERR_error_string(err
, NULL
));
1405 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1406 "emergency exit: direct",
1411 switch(r
= plugins_call_handle_connection_close(srv
, con
)) {
1413 case HANDLER_FINISHED
:
1416 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "unhandling return value", r
);
1422 connection_reset(srv
, con
);
1424 /* close the connection */
1425 if ((0 == shutdown(con
->fd
, SHUT_WR
))) {
1426 con
->close_timeout_ts
= srv
->cur_ts
;
1427 connection_set_state(srv
, con
, CON_STATE_CLOSE
);
1429 if (srv
->srvconf
.log_state_handling
) {
1430 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1431 "shutdown for fd", con
->fd
);
1434 connection_close(srv
, con
);
1437 con
->keep_alive
= 0;
1443 log_error_write(srv
, __FILE__
, __LINE__
, "sdd",
1444 "unknown state:", con
->fd
, con
->state
);
1451 } else if (ostate
== con
->state
) {
1456 if (srv
->srvconf
.log_state_handling
) {
1457 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1460 connection_get_state(con
->state
));
1464 switch(con
->state
) {
1465 case CON_STATE_READ
:
1466 case CON_STATE_CLOSE
:
1469 case CON_STATE_WRITE
:
1470 /* request write-fdevent only if we really need it
1471 * - if we have data to write
1472 * - if the socket is not writable yet
1474 if (!chunkqueue_is_empty(con
->write_queue
) &&
1475 (con
->is_writable
== 0) &&
1476 (con
->traffic_limit_reached
== 0)) {
1480 case CON_STATE_READ_POST
:
1481 if (con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_POLLIN
) {
1488 if (-1 != con
->fd
) {
1489 const int events
= fdevent_event_get_interest(srv
->ev
, con
->fd
);
1491 /* update timestamps when enabling interest in events */
1492 if ((r
& FDEVENT_IN
) && !(events
& FDEVENT_IN
)) {
1493 con
->read_idle_ts
= srv
->cur_ts
;
1495 if ((r
& FDEVENT_OUT
) && !(events
& FDEVENT_OUT
)) {
1496 con
->write_request_ts
= srv
->cur_ts
;
1498 fdevent_event_set(srv
->ev
, &con
->fde_ndx
, con
->fd
, r
);