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(connection
*con
) {
161 buffer_reset(con
->physical
.path
);
162 array_reset(con
->response
.headers
);
163 chunkqueue_reset(con
->write_queue
);
166 static int connection_handle_write_prepare(server
*srv
, connection
*con
) {
167 if (con
->mode
== DIRECT
) {
169 switch(con
->request
.http_method
) {
170 case HTTP_METHOD_GET
:
171 case HTTP_METHOD_POST
:
172 case HTTP_METHOD_HEAD
:
174 case HTTP_METHOD_OPTIONS
:
176 * 400 is coming from the request-parser BEFORE uri.path is set
177 * 403 is from the response handler when noone else catched it
180 if ((!con
->http_status
|| con
->http_status
== 200) && !buffer_string_is_empty(con
->uri
.path
) &&
181 con
->uri
.path
->ptr
[0] != '*') {
182 response_header_insert(srv
, con
, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
184 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
185 con
->parsed_response
&= ~HTTP_CONTENT_LENGTH
;
187 con
->http_status
= 200;
188 con
->file_finished
= 1;
190 chunkqueue_reset(con
->write_queue
);
194 if (0 == con
->http_status
) {
195 con
->http_status
= 501;
201 if (con
->http_status
== 0) {
202 con
->http_status
= 403;
205 switch(con
->http_status
) {
206 case 204: /* class: header only */
209 /* disable chunked encoding again as we have no body */
210 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
211 con
->parsed_response
&= ~HTTP_CONTENT_LENGTH
;
212 chunkqueue_reset(con
->write_queue
);
214 con
->file_finished
= 1;
216 default: /* class: header + body */
217 if (con
->mode
!= DIRECT
) break;
219 /* only custom body for 4xx and 5xx */
220 if (con
->http_status
< 400 || con
->http_status
>= 600) break;
222 con
->file_finished
= 0;
224 connection_handle_errdoc_init(con
);
226 /* try to send static errorfile */
227 if (!buffer_string_is_empty(con
->conf
.errorfile_prefix
)) {
228 stat_cache_entry
*sce
= NULL
;
230 buffer_copy_buffer(con
->physical
.path
, con
->conf
.errorfile_prefix
);
231 buffer_append_int(con
->physical
.path
, con
->http_status
);
232 buffer_append_string_len(con
->physical
.path
, CONST_STR_LEN(".html"));
234 if (0 == http_chunk_append_file(srv
, con
, con
->physical
.path
)) {
235 con
->file_finished
= 1;
236 if (HANDLER_ERROR
!= stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
237 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce
->content_type
));
242 if (!con
->file_finished
) {
245 buffer_reset(con
->physical
.path
);
247 con
->file_finished
= 1;
250 /* build default error-page */
251 buffer_copy_string_len(b
, CONST_STR_LEN(
252 "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
253 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
254 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
255 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
258 buffer_append_int(b
, con
->http_status
);
259 buffer_append_string_len(b
, CONST_STR_LEN(" - "));
260 buffer_append_string(b
, get_http_status_name(con
->http_status
));
262 buffer_append_string_len(b
, CONST_STR_LEN(
267 buffer_append_int(b
, con
->http_status
);
268 buffer_append_string_len(b
, CONST_STR_LEN(" - "));
269 buffer_append_string(b
, get_http_status_name(con
->http_status
));
271 buffer_append_string_len(b
, CONST_STR_LEN("</h1>\n"
276 http_chunk_append_buffer(srv
, con
, b
);
278 http_chunk_close(srv
, con
);
280 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
285 if (con
->file_finished
) {
286 /* we have all the content and chunked encoding is not used, set a content-length */
288 if ((!(con
->parsed_response
& HTTP_CONTENT_LENGTH
)) &&
289 (con
->response
.transfer_encoding
& HTTP_TRANSFER_ENCODING_CHUNKED
) == 0) {
290 off_t qlen
= chunkqueue_length(con
->write_queue
);
293 * The Content-Length header only can be sent if we have content:
294 * - HEAD doesn't have a content-body (but have a content-length)
295 * - 1xx, 204 and 304 don't have a content-body (RFC 2616 Section 4.3)
297 * Otherwise generate a Content-Length header as chunked encoding is not
300 if ((con
->http_status
>= 100 && con
->http_status
< 200) ||
301 con
->http_status
== 204 ||
302 con
->http_status
== 304) {
304 /* no Content-Body, no Content-Length */
305 if (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "Content-Length"))) {
306 buffer_reset(ds
->value
); /* Headers with empty values are ignored for output */
308 } else if (qlen
> 0 || con
->request
.http_method
!= HTTP_METHOD_HEAD
) {
309 /* qlen = 0 is important for Redirects (301, ...) as they MAY have
310 * a content. Browsers are waiting for a Content otherwise
312 buffer_copy_int(srv
->tmp_buf
, qlen
);
314 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Length"), CONST_BUF_LEN(srv
->tmp_buf
));
319 * the file isn't finished yet, but we have all headers
321 * to get keep-alive we either need:
322 * - Content-Length: ... (HTTP/1.0 and HTTP/1.0) or
323 * - Transfer-Encoding: chunked (HTTP/1.1)
326 if (((con
->parsed_response
& HTTP_CONTENT_LENGTH
) == 0) &&
327 ((con
->response
.transfer_encoding
& HTTP_TRANSFER_ENCODING_CHUNKED
) == 0)) {
332 * if the backend sent a Connection: close, follow the wish
334 * NOTE: if the backend sent Connection: Keep-Alive, but no Content-Length, we
335 * will close the connection. That's fine. We can always decide the close
338 * FIXME: to be nice we should remove the Connection: ...
340 if (con
->parsed_response
& HTTP_CONNECTION
) {
341 /* a subrequest disable keep-alive although the client wanted it */
342 if (con
->keep_alive
&& !con
->response
.keep_alive
) {
348 if (con
->request
.content_length
349 && (off_t
)con
->request
.content_length
> con
->request_content_queue
->bytes_in
) {
350 /* request body is present and has not been read completely */
354 if (con
->request
.http_method
== HTTP_METHOD_HEAD
) {
356 * a HEAD request has the same as a GET
357 * without the content
359 con
->file_finished
= 1;
361 chunkqueue_reset(con
->write_queue
);
362 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
365 http_response_write_header(srv
, con
);
370 static int connection_handle_write(server
*srv
, connection
*con
) {
371 switch(network_write_chunkqueue(srv
, con
, con
->write_queue
, MAX_WRITE_LIMIT
)) {
373 con
->write_request_ts
= srv
->cur_ts
;
374 if (con
->file_finished
) {
375 connection_set_state(srv
, con
, CON_STATE_RESPONSE_END
);
376 joblist_append(srv
, con
);
379 case -1: /* error on our side */
380 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
381 "connection closed: write failed on fd", con
->fd
);
382 connection_set_state(srv
, con
, CON_STATE_ERROR
);
383 joblist_append(srv
, con
);
385 case -2: /* remote close */
386 connection_set_state(srv
, con
, CON_STATE_ERROR
);
387 joblist_append(srv
, con
);
390 con
->write_request_ts
= srv
->cur_ts
;
391 con
->is_writable
= 0;
393 /* not finished yet -> WRITE */
402 connection
*connection_init(server
*srv
) {
407 con
= calloc(1, sizeof(*con
));
408 force_assert(NULL
!= con
);
413 con
->bytes_written
= 0;
415 con
->bytes_header
= 0;
416 con
->loops_per_request
= 0;
419 con->x = buffer_init();
422 CLEAN(request
.request_line
);
423 CLEAN(request
.request
);
424 CLEAN(request
.pathinfo
);
426 CLEAN(request
.orig_uri
);
429 CLEAN(uri
.authority
);
434 CLEAN(physical
.doc_root
);
435 CLEAN(physical
.path
);
436 CLEAN(physical
.basedir
);
437 CLEAN(physical
.rel_path
);
438 CLEAN(physical
.etag
);
439 CLEAN(parse_request
);
443 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
444 CLEAN(tlsext_server_name
);
448 con
->write_queue
= chunkqueue_init();
449 con
->read_queue
= chunkqueue_init();
450 con
->request_content_queue
= chunkqueue_init();
451 chunkqueue_set_tempdirs(
452 con
->request_content_queue
,
453 srv
->srvconf
.upload_tempdirs
,
454 srv
->srvconf
.upload_temp_file_size
);
456 con
->request
.headers
= array_init();
457 con
->response
.headers
= array_init();
458 con
->environment
= array_init();
460 /* init plugin specific connection structures */
462 con
->plugin_ctx
= calloc(1, (srv
->plugins
.used
+ 1) * sizeof(void *));
463 force_assert(NULL
!= con
->plugin_ctx
);
465 con
->cond_cache
= calloc(srv
->config_context
->used
, sizeof(cond_cache_t
));
466 force_assert(NULL
!= con
->cond_cache
);
467 config_setup_connection(srv
, con
);
472 void connections_free(server
*srv
) {
473 connections
*conns
= srv
->conns
;
476 for (i
= 0; i
< conns
->size
; i
++) {
477 connection
*con
= conns
->ptr
[i
];
479 connection_reset(srv
, con
);
481 chunkqueue_free(con
->write_queue
);
482 chunkqueue_free(con
->read_queue
);
483 chunkqueue_free(con
->request_content_queue
);
484 array_free(con
->request
.headers
);
485 array_free(con
->response
.headers
);
486 array_free(con
->environment
);
492 CLEAN(request
.request_line
);
493 CLEAN(request
.request
);
494 CLEAN(request
.pathinfo
);
496 CLEAN(request
.orig_uri
);
499 CLEAN(uri
.authority
);
504 CLEAN(physical
.doc_root
);
505 CLEAN(physical
.path
);
506 CLEAN(physical
.basedir
);
507 CLEAN(physical
.etag
);
508 CLEAN(physical
.rel_path
);
509 CLEAN(parse_request
);
513 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
514 CLEAN(tlsext_server_name
);
517 free(con
->plugin_ctx
);
518 free(con
->cond_cache
);
527 int connection_reset(server
*srv
, connection
*con
) {
530 plugins_call_connection_reset(srv
, con
);
532 con
->is_readable
= 1;
533 con
->is_writable
= 1;
534 con
->http_status
= 0;
535 con
->file_finished
= 0;
536 con
->file_started
= 0;
537 con
->got_response
= 0;
539 con
->parsed_response
= 0;
541 con
->bytes_written
= 0;
542 con
->bytes_written_cur_second
= 0;
544 con
->bytes_header
= 0;
545 con
->loops_per_request
= 0;
547 con
->request
.http_method
= HTTP_METHOD_UNSET
;
548 con
->request
.http_version
= HTTP_VERSION_UNSET
;
550 con
->request
.http_if_modified_since
= NULL
;
551 con
->request
.http_if_none_match
= NULL
;
553 con
->response
.keep_alive
= 0;
554 con
->response
.content_length
= -1;
555 con
->response
.transfer_encoding
= 0;
560 if (con->x) buffer_reset(con->x);
563 CLEAN(request
.request_line
);
564 CLEAN(request
.pathinfo
);
565 CLEAN(request
.request
);
567 /* CLEAN(request.orig_uri); */
570 /* CLEAN(uri.authority); */
571 /* CLEAN(uri.path); */
573 /* CLEAN(uri.query); */
575 CLEAN(physical
.doc_root
);
576 CLEAN(physical
.path
);
577 CLEAN(physical
.basedir
);
578 CLEAN(physical
.rel_path
);
579 CLEAN(physical
.etag
);
581 CLEAN(parse_request
);
584 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
585 CLEAN(tlsext_server_name
);
590 if (con->x) con->x->used = 0;
595 con->request.x = NULL;
599 CLEAN(http_content_type
);
601 con
->request
.content_length
= 0;
603 array_reset(con
->request
.headers
);
604 array_reset(con
->response
.headers
);
605 array_reset(con
->environment
);
607 chunkqueue_reset(con
->write_queue
);
608 chunkqueue_reset(con
->request_content_queue
);
610 /* the plugins should cleanup themself */
611 for (i
= 0; i
< srv
->plugins
.used
; i
++) {
612 plugin
*p
= ((plugin
**)(srv
->plugins
.ptr
))[i
];
613 plugin_data
*pd
= p
->data
;
617 if (con
->plugin_ctx
[pd
->id
] != NULL
) {
618 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "missing cleanup in", p
->name
);
621 con
->plugin_ctx
[pd
->id
] = NULL
;
624 /* The cond_cache gets reset in response.c */
625 /* config_cond_cache_reset(srv, con); */
628 con
->error_handler_saved_status
= 0;
629 /*con->error_handler_saved_method = HTTP_METHOD_UNSET;*/
630 /*(error_handler_saved_method value is not valid unless error_handler_saved_status is set)*/
632 config_setup_connection(srv
, con
);
638 * handle all header and content read
640 * we get called by the state-engine and by the fdevent-handler
642 static int connection_handle_read_state(server
*srv
, connection
*con
) {
643 chunk
*c
, *last_chunk
;
645 chunkqueue
*cq
= con
->read_queue
;
646 int is_closed
= 0; /* the connection got closed, if we don't have a complete header, -> error */
647 /* when in CON_STATE_READ: about to receive first byte for a request: */
648 int is_request_start
= chunkqueue_is_empty(cq
);
650 if (con
->is_readable
) {
651 con
->read_idle_ts
= srv
->cur_ts
;
653 switch(connection_handle_read(srv
, con
)) {
664 chunkqueue_remove_finished_chunks(cq
);
666 /* we might have got several packets at once
669 /* update request_start timestamp when first byte of
670 * next request is received on a keep-alive connection */
671 if (con
->request_count
> 1 && is_request_start
) con
->request_start
= srv
->cur_ts
;
673 /* if there is a \r\n\r\n in the chunkqueue
675 * scan the chunk-queue twice
676 * 1. to find the \r\n\r\n
677 * 2. to copy the header-packet
684 for (c
= cq
->first
; c
; c
= c
->next
) {
686 size_t len
= buffer_string_length(c
->mem
) - c
->offset
;
687 const char *b
= c
->mem
->ptr
+ c
->offset
;
689 for (i
= 0; i
< len
; ++i
) {
693 /* chec if \n\r\n follows */
696 const char header_end
[] = "\r\n\r\n";
697 int header_end_match_pos
= 1;
699 for ( ; cc
; cc
= cc
->next
, j
= 0 ) {
700 size_t bblen
= buffer_string_length(cc
->mem
) - cc
->offset
;
701 const char *bb
= cc
->mem
->ptr
+ cc
->offset
;
703 for ( ; j
< bblen
; j
++) {
706 if (ch
== header_end
[header_end_match_pos
]) {
707 header_end_match_pos
++;
708 if (4 == header_end_match_pos
) {
711 goto found_header_end
;
726 buffer_reset(con
->request
.request
);
728 for (c
= cq
->first
; c
; c
= c
->next
) {
729 size_t len
= buffer_string_length(c
->mem
) - c
->offset
;
731 if (c
== last_chunk
) {
735 buffer_append_string_len(con
->request
.request
, c
->mem
->ptr
+ c
->offset
, len
);
737 cq
->bytes_out
+= len
;
739 if (c
== last_chunk
) break;
742 connection_set_state(srv
, con
, CON_STATE_REQUEST_END
);
743 } else if (chunkqueue_length(cq
) > 64 * 1024) {
744 log_error_write(srv
, __FILE__
, __LINE__
, "s", "oversized request-header -> sending Status 414");
746 con
->http_status
= 414; /* Request-URI too large */
748 connection_set_state(srv
, con
, CON_STATE_HANDLE_REQUEST
);
749 } else if (is_closed
) {
750 /* the connection got closed and we didn't got enough data to leave CON_STATE_READ;
751 * the only way is to leave here */
752 connection_set_state(srv
, con
, CON_STATE_ERROR
);
755 chunkqueue_remove_finished_chunks(cq
);
760 static handler_t
connection_handle_fdevent(server
*srv
, void *context
, int revents
) {
761 connection
*con
= context
;
763 joblist_append(srv
, con
);
765 if (con
->srv_socket
->is_ssl
) {
766 /* ssl may read and write for both reads and writes */
767 if (revents
& (FDEVENT_IN
| FDEVENT_OUT
)) {
768 con
->is_readable
= 1;
769 con
->is_writable
= 1;
772 if (revents
& FDEVENT_IN
) {
773 con
->is_readable
= 1;
775 if (revents
& FDEVENT_OUT
) {
776 con
->is_writable
= 1;
777 /* we don't need the event twice */
782 if (revents
& ~(FDEVENT_IN
| FDEVENT_OUT
)) {
783 /* looks like an error */
785 /* FIXME: revents = 0x19 still means that we should read from the queue */
786 if (revents
& FDEVENT_HUP
) {
787 if (con
->state
== CON_STATE_CLOSE
) {
788 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
790 /* sigio reports the wrong event here
792 * there was no HUP at all
794 #ifdef USE_LINUX_SIGIO
795 if (srv
->ev
->in_sigio
== 1) {
796 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
797 "connection closed: poll() -> HUP", con
->fd
);
799 connection_set_state(srv
, con
, CON_STATE_ERROR
);
802 connection_set_state(srv
, con
, CON_STATE_ERROR
);
806 } else if (revents
& FDEVENT_ERR
) {
807 /* error, connection reset, whatever... we don't want to spam the logfile */
809 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
810 "connection closed: poll() -> ERR", con
->fd
);
812 connection_set_state(srv
, con
, CON_STATE_ERROR
);
814 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
815 "connection closed: poll() -> ???", revents
);
819 if (con
->state
== CON_STATE_READ
) {
820 connection_handle_read_state(srv
, con
);
823 if (con
->state
== CON_STATE_WRITE
&&
824 !chunkqueue_is_empty(con
->write_queue
) &&
827 if (-1 == connection_handle_write(srv
, con
)) {
828 connection_set_state(srv
, con
, CON_STATE_ERROR
);
830 log_error_write(srv
, __FILE__
, __LINE__
, "ds",
832 "handle write failed.");
836 if (con
->state
== CON_STATE_CLOSE
) {
837 /* flush the read buffers */
841 len
= read(con
->fd
, buf
, sizeof(buf
));
842 if (len
== 0 || (len
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
) ) {
843 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
847 return HANDLER_FINISHED
;
851 connection
*connection_accept(server
*srv
, server_socket
*srv_socket
) {
852 /* accept everything */
854 /* search an empty place */
858 /* accept it and register the fd */
861 * check if we can still open a new connections
866 if (srv
->conns
->used
>= srv
->max_conns
) {
870 cnt_len
= sizeof(cnt_addr
);
872 if (-1 == (cnt
= accept(srv_socket
->fd
, (struct sockaddr
*) &cnt_addr
, &cnt_len
))) {
875 #if EWOULDBLOCK != EAGAIN
879 /* we were stopped _before_ we had a connection */
880 case ECONNABORTED
: /* this is a FreeBSD thingy */
881 /* we were stopped _after_ we had a connection */
887 log_error_write(srv
, __FILE__
, __LINE__
, "ssd", "accept failed:", strerror(errno
), errno
);
891 return connection_accepted(srv
, srv_socket
, &cnt_addr
, cnt
);
895 connection
*connection_accepted(server
*srv
, server_socket
*srv_socket
, sock_addr
*cnt_addr
, int cnt
) {
900 /* ok, we have the connection, register it */
902 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
907 con
= connections_get_new_connection(srv
);
912 gettimeofday(&(con
->start_tv
), NULL
);
914 fdevent_register(srv
->ev
, con
->fd
, connection_handle_fdevent
, con
);
916 connection_set_state(srv
, con
, CON_STATE_REQUEST_START
);
918 con
->connection_start
= srv
->cur_ts
;
919 con
->dst_addr
= *cnt_addr
;
920 buffer_copy_string(con
->dst_addr_buf
, inet_ntop_cache_get_ip(srv
, &(con
->dst_addr
)));
921 con
->srv_socket
= srv_socket
;
923 if (-1 == (fdevent_fcntl_set(srv
->ev
, con
->fd
))) {
924 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed: ", strerror(errno
));
928 /* connect FD to SSL */
929 if (srv_socket
->is_ssl
) {
930 if (NULL
== (con
->ssl
= SSL_new(srv_socket
->ssl_ctx
))) {
931 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "SSL:",
932 ERR_error_string(ERR_get_error(), NULL
));
937 con
->renegotiations
= 0;
938 SSL_set_app_data(con
->ssl
, con
);
939 SSL_set_accept_state(con
->ssl
);
941 if (1 != (SSL_set_fd(con
->ssl
, cnt
))) {
942 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "SSL:",
943 ERR_error_string(ERR_get_error(), NULL
));
952 int connection_state_machine(server
*srv
, connection
*con
) {
955 server_socket
*srv_sock
= con
->srv_socket
;
958 if (srv
->srvconf
.log_state_handling
) {
959 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
962 connection_get_state(con
->state
));
966 size_t ostate
= con
->state
;
968 switch (con
->state
) {
969 case CON_STATE_REQUEST_START
: /* transient */
970 if (srv
->srvconf
.log_state_handling
) {
971 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
972 "state for fd", con
->fd
, connection_get_state(con
->state
));
975 con
->request_start
= srv
->cur_ts
;
976 con
->read_idle_ts
= srv
->cur_ts
;
978 con
->request_count
++;
979 con
->loops_per_request
= 0;
981 connection_set_state(srv
, con
, CON_STATE_READ
);
984 case CON_STATE_REQUEST_END
: /* transient */
985 if (srv
->srvconf
.log_state_handling
) {
986 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
987 "state for fd", con
->fd
, connection_get_state(con
->state
));
990 buffer_reset(con
->uri
.authority
);
991 buffer_reset(con
->uri
.path
);
992 buffer_reset(con
->uri
.query
);
993 buffer_reset(con
->request
.orig_uri
);
995 if (http_request_parse(srv
, con
)) {
996 /* we have to read some data from the POST request */
998 connection_set_state(srv
, con
, CON_STATE_READ_POST
);
1003 connection_set_state(srv
, con
, CON_STATE_HANDLE_REQUEST
);
1006 case CON_STATE_READ_POST
:
1007 case CON_STATE_HANDLE_REQUEST
:
1009 * the request is parsed
1011 * decided what to do with the request
1017 if (srv
->srvconf
.log_state_handling
) {
1018 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1019 "state for fd", con
->fd
, connection_get_state(con
->state
));
1022 switch (r
= http_response_prepare(srv
, con
)) {
1023 case HANDLER_FINISHED
:
1024 if (con
->error_handler_saved_status
> 0) {
1025 con
->request
.http_method
= con
->error_handler_saved_method
;
1027 if (con
->mode
== DIRECT
) {
1028 if (con
->error_handler_saved_status
) {
1029 if (con
->error_handler_saved_status
> 0) {
1030 con
->http_status
= con
->error_handler_saved_status
;
1031 } else if (con
->http_status
== 404 || con
->http_status
== 403) {
1032 /* error-handler-404 is a 404 */
1033 con
->http_status
= -con
->error_handler_saved_status
;
1035 /* error-handler-404 is back and has generated content */
1036 /* if Status: was set, take it otherwise use 200 */
1038 } else if (con
->http_status
>= 400) {
1039 buffer
*error_handler
= NULL
;
1040 if (!buffer_string_is_empty(con
->conf
.error_handler
)) {
1041 error_handler
= con
->conf
.error_handler
;
1042 } else if ((con
->http_status
== 404 || con
->http_status
== 403)
1043 && !buffer_string_is_empty(con
->conf
.error_handler_404
)) {
1044 error_handler
= con
->conf
.error_handler_404
;
1047 if (error_handler
) {
1048 /* call error-handler */
1050 /* set REDIRECT_STATUS to save current HTTP status code
1051 * for access by dynamic handlers
1052 * https://redmine.lighttpd.net/issues/1828 */
1054 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
1055 ds
= data_string_init();
1057 buffer_copy_string_len(ds
->key
, CONST_STR_LEN("REDIRECT_STATUS"));
1058 buffer_append_int(ds
->value
, con
->http_status
);
1059 array_insert_unique(con
->environment
, (data_unset
*)ds
);
1061 if (error_handler
== con
->conf
.error_handler
) {
1062 plugins_call_connection_reset(srv
, con
);
1064 if (con
->request
.content_length
) {
1065 if ((off_t
)con
->request
.content_length
!= chunkqueue_length(con
->request_content_queue
)) {
1066 con
->keep_alive
= 0;
1068 con
->request
.content_length
= 0;
1069 chunkqueue_reset(con
->request_content_queue
);
1072 con
->is_writable
= 1;
1073 con
->file_finished
= 0;
1074 con
->file_started
= 0;
1075 con
->got_response
= 0;
1076 con
->parsed_response
= 0;
1077 con
->response
.keep_alive
= 0;
1078 con
->response
.content_length
= -1;
1079 con
->response
.transfer_encoding
= 0;
1081 array_set_key_value(con
->environment
, CONST_STR_LEN("REDIRECT_URI"), CONST_BUF_LEN(con
->request
.orig_uri
));
1082 con
->error_handler_saved_status
= con
->http_status
;
1083 con
->error_handler_saved_method
= con
->request
.http_method
;
1085 con
->request
.http_method
= HTTP_METHOD_GET
;
1086 } else { /*(preserve behavior for server.error-handler-404)*/
1087 array_set_key_value(con
->environment
, CONST_STR_LEN("REDIRECT_URI"), CONST_BUF_LEN(error_handler
));
1088 con
->error_handler_saved_status
= -con
->http_status
; /*(negative to flag old behavior)*/
1090 con
->http_status
= 0;
1092 buffer_copy_buffer(con
->request
.uri
, error_handler
);
1093 connection_handle_errdoc_init(con
);
1100 if (con
->http_status
== 0) con
->http_status
= 200;
1102 /* we have something to send, go on */
1103 connection_set_state(srv
, con
, CON_STATE_RESPONSE_START
);
1105 case HANDLER_WAIT_FOR_FD
:
1108 fdwaitqueue_append(srv
, con
);
1111 case HANDLER_COMEBACK
:
1114 case HANDLER_WAIT_FOR_EVENT
:
1115 /* come back here */
1118 /* something went wrong */
1119 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1122 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", "unknown ret-value: ", con
->fd
, r
);
1127 case CON_STATE_RESPONSE_START
:
1129 * the decision is done
1130 * - create the HTTP-Response-Header
1134 if (srv
->srvconf
.log_state_handling
) {
1135 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1136 "state for fd", con
->fd
, connection_get_state(con
->state
));
1139 if (-1 == connection_handle_write_prepare(srv
, con
)) {
1140 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1145 connection_set_state(srv
, con
, CON_STATE_WRITE
);
1147 case CON_STATE_RESPONSE_END
: /* transient */
1148 /* log the request */
1150 if (srv
->srvconf
.log_state_handling
) {
1151 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1152 "state for fd", con
->fd
, connection_get_state(con
->state
));
1155 plugins_call_handle_request_done(srv
, con
);
1159 if (con
->keep_alive
) {
1160 connection_set_state(srv
, con
, CON_STATE_REQUEST_START
);
1163 con
->request_start
= srv
->cur_ts
;
1164 con
->read_idle_ts
= srv
->cur_ts
;
1167 switch(r
= plugins_call_handle_connection_close(srv
, con
)) {
1169 case HANDLER_FINISHED
:
1172 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "unhandling return value", r
);
1177 if (srv_sock
->is_ssl
) {
1178 switch (SSL_shutdown(con
->ssl
)) {
1183 /* wait for fd-event
1185 * FIXME: wait for fdevent and call SSL_shutdown again
1191 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "SSL:",
1192 ERR_error_string(ERR_get_error(), NULL
));
1196 if ((0 == shutdown(con
->fd
, SHUT_WR
))) {
1197 con
->close_timeout_ts
= srv
->cur_ts
;
1198 connection_set_state(srv
, con
, CON_STATE_CLOSE
);
1200 connection_close(srv
, con
);
1206 connection_reset(srv
, con
);
1209 case CON_STATE_CONNECT
:
1210 if (srv
->srvconf
.log_state_handling
) {
1211 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1212 "state for fd", con
->fd
, connection_get_state(con
->state
));
1215 chunkqueue_reset(con
->read_queue
);
1217 con
->request_count
= 0;
1220 case CON_STATE_CLOSE
:
1221 if (srv
->srvconf
.log_state_handling
) {
1222 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1223 "state for fd", con
->fd
, connection_get_state(con
->state
));
1226 /* we have to do the linger_on_close stuff regardless
1227 * of con->keep_alive; even non-keepalive sockets may
1228 * still have unread data, and closing before reading
1229 * it will make the client not see all our output.
1235 len
= read(con
->fd
, buf
, sizeof(buf
));
1236 if (len
== 0 || (len
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
) ) {
1237 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
1241 if (srv
->cur_ts
- con
->close_timeout_ts
> HTTP_LINGER_TIMEOUT
) {
1242 connection_close(srv
, con
);
1244 if (srv
->srvconf
.log_state_handling
) {
1245 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1246 "connection closed for fd", con
->fd
);
1251 case CON_STATE_READ
:
1252 if (srv
->srvconf
.log_state_handling
) {
1253 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1254 "state for fd", con
->fd
, connection_get_state(con
->state
));
1257 connection_handle_read_state(srv
, con
);
1259 case CON_STATE_WRITE
:
1260 if (srv
->srvconf
.log_state_handling
) {
1261 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1262 "state for fd", con
->fd
, connection_get_state(con
->state
));
1265 /* only try to write if we have something in the queue */
1266 if (!chunkqueue_is_empty(con
->write_queue
)) {
1267 if (con
->is_writable
) {
1268 if (-1 == connection_handle_write(srv
, con
)) {
1269 log_error_write(srv
, __FILE__
, __LINE__
, "ds",
1271 "handle write failed.");
1272 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1275 } else if (con
->file_finished
) {
1276 connection_set_state(srv
, con
, CON_STATE_RESPONSE_END
);
1280 case CON_STATE_ERROR
: /* transient */
1282 /* even if the connection was drop we still have to write it to the access log */
1283 if (con
->http_status
) {
1284 plugins_call_handle_request_done(srv
, con
);
1287 if (srv_sock
->is_ssl
) {
1291 switch ((ret
= SSL_shutdown(con
->ssl
))) {
1297 if (-1 != (ret
= SSL_shutdown(con
->ssl
))) break;
1302 switch ((ssl_r
= SSL_get_error(con
->ssl
, ret
))) {
1303 case SSL_ERROR_WANT_WRITE
:
1304 case SSL_ERROR_WANT_READ
:
1306 case SSL_ERROR_SYSCALL
:
1307 /* perhaps we have error waiting in our error-queue */
1308 if (0 != (err
= ERR_get_error())) {
1310 log_error_write(srv
, __FILE__
, __LINE__
, "sdds", "SSL:",
1312 ERR_error_string(err
, NULL
));
1313 } while((err
= ERR_get_error()));
1314 } else if (errno
!= 0) { /* ssl bug (see lighttpd ticket #2213): sometimes errno == 0 */
1320 log_error_write(srv
, __FILE__
, __LINE__
, "sddds", "SSL (error):",
1329 while((err
= ERR_get_error())) {
1330 log_error_write(srv
, __FILE__
, __LINE__
, "sdds", "SSL:",
1332 ERR_error_string(err
, NULL
));
1345 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1346 "emergency exit: direct",
1351 switch(r
= plugins_call_handle_connection_close(srv
, con
)) {
1353 case HANDLER_FINISHED
:
1356 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "unhandling return value", r
);
1362 connection_reset(srv
, con
);
1364 /* close the connection */
1365 if ((0 == shutdown(con
->fd
, SHUT_WR
))) {
1366 con
->close_timeout_ts
= srv
->cur_ts
;
1367 connection_set_state(srv
, con
, CON_STATE_CLOSE
);
1369 if (srv
->srvconf
.log_state_handling
) {
1370 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1371 "shutdown for fd", con
->fd
);
1374 connection_close(srv
, con
);
1377 con
->keep_alive
= 0;
1383 log_error_write(srv
, __FILE__
, __LINE__
, "sdd",
1384 "unknown state:", con
->fd
, con
->state
);
1391 } else if (ostate
== con
->state
) {
1396 if (srv
->srvconf
.log_state_handling
) {
1397 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1400 connection_get_state(con
->state
));
1403 switch(con
->state
) {
1404 case CON_STATE_READ_POST
:
1405 case CON_STATE_READ
:
1406 case CON_STATE_CLOSE
:
1407 fdevent_event_set(srv
->ev
, &(con
->fde_ndx
), con
->fd
, FDEVENT_IN
);
1409 case CON_STATE_WRITE
:
1410 /* request write-fdevent only if we really need it
1411 * - if we have data to write
1412 * - if the socket is not writable yet
1414 if (!chunkqueue_is_empty(con
->write_queue
) &&
1415 (con
->is_writable
== 0) &&
1416 (con
->traffic_limit_reached
== 0)) {
1417 fdevent_event_set(srv
->ev
, &(con
->fde_ndx
), con
->fd
, FDEVENT_OUT
);
1419 fdevent_event_set(srv
->ev
, &(con
->fde_ndx
), con
->fd
, 0);
1423 fdevent_event_set(srv
->ev
, &(con
->fde_ndx
), con
->fd
, 0);