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 buffer_reset(con
->physical
.path
);
173 array_reset(con
->response
.headers
);
174 chunkqueue_reset(con
->write_queue
);
176 if (NULL
!= www_auth
) {
177 response_header_insert(srv
, con
, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(www_auth
));
178 buffer_free(www_auth
);
182 static int connection_handle_write_prepare(server
*srv
, connection
*con
) {
183 if (con
->mode
== DIRECT
) {
185 switch(con
->request
.http_method
) {
186 case HTTP_METHOD_GET
:
187 case HTTP_METHOD_POST
:
188 case HTTP_METHOD_HEAD
:
190 case HTTP_METHOD_OPTIONS
:
192 * 400 is coming from the request-parser BEFORE uri.path is set
193 * 403 is from the response handler when noone else catched it
196 if ((!con
->http_status
|| con
->http_status
== 200) && !buffer_string_is_empty(con
->uri
.path
) &&
197 con
->uri
.path
->ptr
[0] != '*') {
198 response_header_insert(srv
, con
, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
200 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
201 con
->parsed_response
&= ~HTTP_CONTENT_LENGTH
;
203 con
->http_status
= 200;
204 con
->file_finished
= 1;
206 chunkqueue_reset(con
->write_queue
);
210 if (0 == con
->http_status
) {
211 con
->http_status
= 501;
217 if (con
->http_status
== 0) {
218 con
->http_status
= 403;
221 switch(con
->http_status
) {
222 case 204: /* class: header only */
225 /* disable chunked encoding again as we have no body */
226 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
227 con
->parsed_response
&= ~HTTP_CONTENT_LENGTH
;
228 chunkqueue_reset(con
->write_queue
);
230 con
->file_finished
= 1;
232 default: /* class: header + body */
233 if (con
->mode
!= DIRECT
) break;
235 /* only custom body for 4xx and 5xx */
236 if (con
->http_status
< 400 || con
->http_status
>= 600) break;
238 con
->file_finished
= 0;
240 connection_handle_errdoc_init(srv
, con
);
242 /* try to send static errorfile */
243 if (!buffer_string_is_empty(con
->conf
.errorfile_prefix
)) {
244 stat_cache_entry
*sce
= NULL
;
246 buffer_copy_buffer(con
->physical
.path
, con
->conf
.errorfile_prefix
);
247 buffer_append_int(con
->physical
.path
, con
->http_status
);
248 buffer_append_string_len(con
->physical
.path
, CONST_STR_LEN(".html"));
250 if (0 == http_chunk_append_file(srv
, con
, con
->physical
.path
)) {
251 con
->file_finished
= 1;
252 if (HANDLER_ERROR
!= stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
253 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce
->content_type
));
258 if (!con
->file_finished
) {
261 buffer_reset(con
->physical
.path
);
263 con
->file_finished
= 1;
266 /* build default error-page */
267 buffer_copy_string_len(b
, CONST_STR_LEN(
268 "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
269 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
270 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
271 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
274 buffer_append_int(b
, con
->http_status
);
275 buffer_append_string_len(b
, CONST_STR_LEN(" - "));
276 buffer_append_string(b
, get_http_status_name(con
->http_status
));
278 buffer_append_string_len(b
, CONST_STR_LEN(
283 buffer_append_int(b
, con
->http_status
);
284 buffer_append_string_len(b
, CONST_STR_LEN(" - "));
285 buffer_append_string(b
, get_http_status_name(con
->http_status
));
287 buffer_append_string_len(b
, CONST_STR_LEN("</h1>\n"
292 http_chunk_append_buffer(srv
, con
, b
);
294 http_chunk_close(srv
, con
);
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)) {
348 * if the backend sent a Connection: close, follow the wish
350 * NOTE: if the backend sent Connection: Keep-Alive, but no Content-Length, we
351 * will close the connection. That's fine. We can always decide the close
354 * FIXME: to be nice we should remove the Connection: ...
356 if (con
->parsed_response
& HTTP_CONNECTION
) {
357 /* a subrequest disable keep-alive although the client wanted it */
358 if (con
->keep_alive
&& !con
->response
.keep_alive
) {
364 if (con
->request
.content_length
365 && (off_t
)con
->request
.content_length
> con
->request_content_queue
->bytes_in
) {
366 /* request body is present and has not been read completely */
370 if (con
->request
.http_method
== HTTP_METHOD_HEAD
) {
372 * a HEAD request has the same as a GET
373 * without the content
375 con
->file_finished
= 1;
377 chunkqueue_reset(con
->write_queue
);
378 con
->response
.transfer_encoding
&= ~HTTP_TRANSFER_ENCODING_CHUNKED
;
381 http_response_write_header(srv
, con
);
386 static int connection_handle_write(server
*srv
, connection
*con
) {
387 switch(network_write_chunkqueue(srv
, con
, con
->write_queue
, MAX_WRITE_LIMIT
)) {
389 con
->write_request_ts
= srv
->cur_ts
;
390 if (con
->file_finished
) {
391 connection_set_state(srv
, con
, CON_STATE_RESPONSE_END
);
392 joblist_append(srv
, con
);
395 case -1: /* error on our side */
396 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
397 "connection closed: write failed on fd", con
->fd
);
398 connection_set_state(srv
, con
, CON_STATE_ERROR
);
399 joblist_append(srv
, con
);
401 case -2: /* remote close */
402 connection_set_state(srv
, con
, CON_STATE_ERROR
);
403 joblist_append(srv
, con
);
406 con
->write_request_ts
= srv
->cur_ts
;
407 con
->is_writable
= 0;
409 /* not finished yet -> WRITE */
418 connection
*connection_init(server
*srv
) {
423 con
= calloc(1, sizeof(*con
));
424 force_assert(NULL
!= con
);
429 con
->bytes_written
= 0;
431 con
->bytes_header
= 0;
432 con
->loops_per_request
= 0;
435 con->x = buffer_init();
438 CLEAN(request
.request_line
);
439 CLEAN(request
.request
);
440 CLEAN(request
.pathinfo
);
442 CLEAN(request
.orig_uri
);
445 CLEAN(uri
.authority
);
450 CLEAN(physical
.doc_root
);
451 CLEAN(physical
.path
);
452 CLEAN(physical
.basedir
);
453 CLEAN(physical
.rel_path
);
454 CLEAN(physical
.etag
);
455 CLEAN(parse_request
);
459 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
460 CLEAN(tlsext_server_name
);
464 con
->write_queue
= chunkqueue_init();
465 con
->read_queue
= chunkqueue_init();
466 con
->request_content_queue
= chunkqueue_init();
467 chunkqueue_set_tempdirs(
468 con
->request_content_queue
,
469 srv
->srvconf
.upload_tempdirs
,
470 srv
->srvconf
.upload_temp_file_size
);
472 con
->request
.headers
= array_init();
473 con
->response
.headers
= array_init();
474 con
->environment
= array_init();
476 /* init plugin specific connection structures */
478 con
->plugin_ctx
= calloc(1, (srv
->plugins
.used
+ 1) * sizeof(void *));
479 force_assert(NULL
!= con
->plugin_ctx
);
481 con
->cond_cache
= calloc(srv
->config_context
->used
, sizeof(cond_cache_t
));
482 force_assert(NULL
!= con
->cond_cache
);
483 config_setup_connection(srv
, con
);
488 void connections_free(server
*srv
) {
489 connections
*conns
= srv
->conns
;
492 for (i
= 0; i
< conns
->size
; i
++) {
493 connection
*con
= conns
->ptr
[i
];
495 connection_reset(srv
, con
);
497 chunkqueue_free(con
->write_queue
);
498 chunkqueue_free(con
->read_queue
);
499 chunkqueue_free(con
->request_content_queue
);
500 array_free(con
->request
.headers
);
501 array_free(con
->response
.headers
);
502 array_free(con
->environment
);
508 CLEAN(request
.request_line
);
509 CLEAN(request
.request
);
510 CLEAN(request
.pathinfo
);
512 CLEAN(request
.orig_uri
);
515 CLEAN(uri
.authority
);
520 CLEAN(physical
.doc_root
);
521 CLEAN(physical
.path
);
522 CLEAN(physical
.basedir
);
523 CLEAN(physical
.etag
);
524 CLEAN(physical
.rel_path
);
525 CLEAN(parse_request
);
529 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
530 CLEAN(tlsext_server_name
);
533 free(con
->plugin_ctx
);
534 free(con
->cond_cache
);
543 int connection_reset(server
*srv
, connection
*con
) {
546 plugins_call_connection_reset(srv
, con
);
548 con
->is_readable
= 1;
549 con
->is_writable
= 1;
550 con
->http_status
= 0;
551 con
->file_finished
= 0;
552 con
->file_started
= 0;
553 con
->got_response
= 0;
555 con
->parsed_response
= 0;
557 con
->bytes_written
= 0;
558 con
->bytes_written_cur_second
= 0;
560 con
->bytes_header
= 0;
561 con
->loops_per_request
= 0;
563 con
->request
.http_method
= HTTP_METHOD_UNSET
;
564 con
->request
.http_version
= HTTP_VERSION_UNSET
;
566 con
->request
.http_if_modified_since
= NULL
;
567 con
->request
.http_if_none_match
= NULL
;
569 con
->response
.keep_alive
= 0;
570 con
->response
.content_length
= -1;
571 con
->response
.transfer_encoding
= 0;
576 if (con->x) buffer_reset(con->x);
579 CLEAN(request
.request_line
);
580 CLEAN(request
.pathinfo
);
581 CLEAN(request
.request
);
583 /* CLEAN(request.orig_uri); */
586 /* CLEAN(uri.authority); */
587 /* CLEAN(uri.path); */
589 /* CLEAN(uri.query); */
591 CLEAN(physical
.doc_root
);
592 CLEAN(physical
.path
);
593 CLEAN(physical
.basedir
);
594 CLEAN(physical
.rel_path
);
595 CLEAN(physical
.etag
);
597 CLEAN(parse_request
);
600 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
601 CLEAN(tlsext_server_name
);
606 if (con->x) con->x->used = 0;
611 con->request.x = NULL;
615 CLEAN(http_content_type
);
617 con
->request
.content_length
= 0;
619 array_reset(con
->request
.headers
);
620 array_reset(con
->response
.headers
);
621 array_reset(con
->environment
);
623 chunkqueue_reset(con
->write_queue
);
624 chunkqueue_reset(con
->request_content_queue
);
626 /* the plugins should cleanup themself */
627 for (i
= 0; i
< srv
->plugins
.used
; i
++) {
628 plugin
*p
= ((plugin
**)(srv
->plugins
.ptr
))[i
];
629 plugin_data
*pd
= p
->data
;
633 if (con
->plugin_ctx
[pd
->id
] != NULL
) {
634 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "missing cleanup in", p
->name
);
637 con
->plugin_ctx
[pd
->id
] = NULL
;
640 /* The cond_cache gets reset in response.c */
641 /* config_cond_cache_reset(srv, con); */
644 con
->error_handler_saved_status
= 0;
645 /*con->error_handler_saved_method = HTTP_METHOD_UNSET;*/
646 /*(error_handler_saved_method value is not valid unless error_handler_saved_status is set)*/
648 config_setup_connection(srv
, con
);
654 * handle all header and content read
656 * we get called by the state-engine and by the fdevent-handler
658 static int connection_handle_read_state(server
*srv
, connection
*con
) {
659 chunk
*c
, *last_chunk
;
661 chunkqueue
*cq
= con
->read_queue
;
662 int is_closed
= 0; /* the connection got closed, if we don't have a complete header, -> error */
663 /* when in CON_STATE_READ: about to receive first byte for a request: */
664 int is_request_start
= chunkqueue_is_empty(cq
);
666 if (con
->is_readable
) {
667 con
->read_idle_ts
= srv
->cur_ts
;
669 switch(connection_handle_read(srv
, con
)) {
680 chunkqueue_remove_finished_chunks(cq
);
682 /* we might have got several packets at once
685 /* update request_start timestamp when first byte of
686 * next request is received on a keep-alive connection */
687 if (con
->request_count
> 1 && is_request_start
) con
->request_start
= srv
->cur_ts
;
689 /* if there is a \r\n\r\n in the chunkqueue
691 * scan the chunk-queue twice
692 * 1. to find the \r\n\r\n
693 * 2. to copy the header-packet
700 for (c
= cq
->first
; c
; c
= c
->next
) {
702 size_t len
= buffer_string_length(c
->mem
) - c
->offset
;
703 const char *b
= c
->mem
->ptr
+ c
->offset
;
705 for (i
= 0; i
< len
; ++i
) {
709 /* chec if \n\r\n follows */
712 const char header_end
[] = "\r\n\r\n";
713 int header_end_match_pos
= 1;
715 for ( ; cc
; cc
= cc
->next
, j
= 0 ) {
716 size_t bblen
= buffer_string_length(cc
->mem
) - cc
->offset
;
717 const char *bb
= cc
->mem
->ptr
+ cc
->offset
;
719 for ( ; j
< bblen
; j
++) {
722 if (ch
== header_end
[header_end_match_pos
]) {
723 header_end_match_pos
++;
724 if (4 == header_end_match_pos
) {
727 goto found_header_end
;
742 buffer_reset(con
->request
.request
);
744 for (c
= cq
->first
; c
; c
= c
->next
) {
745 size_t len
= buffer_string_length(c
->mem
) - c
->offset
;
747 if (c
== last_chunk
) {
751 buffer_append_string_len(con
->request
.request
, c
->mem
->ptr
+ c
->offset
, len
);
753 cq
->bytes_out
+= len
;
755 if (c
== last_chunk
) break;
758 connection_set_state(srv
, con
, CON_STATE_REQUEST_END
);
759 } else if (chunkqueue_length(cq
) > 64 * 1024) {
760 log_error_write(srv
, __FILE__
, __LINE__
, "s", "oversized request-header -> sending Status 414");
762 con
->http_status
= 414; /* Request-URI too large */
764 connection_set_state(srv
, con
, CON_STATE_HANDLE_REQUEST
);
765 } else if (is_closed
) {
766 /* the connection got closed and we didn't got enough data to leave CON_STATE_READ;
767 * the only way is to leave here */
768 connection_set_state(srv
, con
, CON_STATE_ERROR
);
771 chunkqueue_remove_finished_chunks(cq
);
776 static handler_t
connection_handle_fdevent(server
*srv
, void *context
, int revents
) {
777 connection
*con
= context
;
779 joblist_append(srv
, con
);
781 if (con
->srv_socket
->is_ssl
) {
782 /* ssl may read and write for both reads and writes */
783 if (revents
& (FDEVENT_IN
| FDEVENT_OUT
)) {
784 con
->is_readable
= 1;
785 con
->is_writable
= 1;
788 if (revents
& FDEVENT_IN
) {
789 con
->is_readable
= 1;
791 if (revents
& FDEVENT_OUT
) {
792 con
->is_writable
= 1;
793 /* we don't need the event twice */
798 if (revents
& ~(FDEVENT_IN
| FDEVENT_OUT
)) {
799 /* looks like an error */
801 /* FIXME: revents = 0x19 still means that we should read from the queue */
802 if (revents
& FDEVENT_HUP
) {
803 if (con
->state
== CON_STATE_CLOSE
) {
804 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
806 /* sigio reports the wrong event here
808 * there was no HUP at all
810 #ifdef USE_LINUX_SIGIO
811 if (srv
->ev
->in_sigio
== 1) {
812 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
813 "connection closed: poll() -> HUP", con
->fd
);
815 connection_set_state(srv
, con
, CON_STATE_ERROR
);
818 connection_set_state(srv
, con
, CON_STATE_ERROR
);
822 } else if (revents
& FDEVENT_ERR
) {
823 /* error, connection reset, whatever... we don't want to spam the logfile */
825 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
826 "connection closed: poll() -> ERR", con
->fd
);
828 connection_set_state(srv
, con
, CON_STATE_ERROR
);
830 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
831 "connection closed: poll() -> ???", revents
);
835 if (con
->state
== CON_STATE_READ
) {
836 connection_handle_read_state(srv
, con
);
839 if (con
->state
== CON_STATE_WRITE
&&
840 !chunkqueue_is_empty(con
->write_queue
) &&
843 if (-1 == connection_handle_write(srv
, con
)) {
844 connection_set_state(srv
, con
, CON_STATE_ERROR
);
846 log_error_write(srv
, __FILE__
, __LINE__
, "ds",
848 "handle write failed.");
852 if (con
->state
== CON_STATE_CLOSE
) {
853 /* flush the read buffers */
857 len
= read(con
->fd
, buf
, sizeof(buf
));
858 if (len
== 0 || (len
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
) ) {
859 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
863 return HANDLER_FINISHED
;
867 connection
*connection_accept(server
*srv
, server_socket
*srv_socket
) {
868 /* accept everything */
870 /* search an empty place */
874 /* accept it and register the fd */
877 * check if we can still open a new connections
882 if (srv
->conns
->used
>= srv
->max_conns
) {
886 cnt_len
= sizeof(cnt_addr
);
888 if (-1 == (cnt
= accept(srv_socket
->fd
, (struct sockaddr
*) &cnt_addr
, &cnt_len
))) {
891 #if EWOULDBLOCK != EAGAIN
895 /* we were stopped _before_ we had a connection */
896 case ECONNABORTED
: /* this is a FreeBSD thingy */
897 /* we were stopped _after_ we had a connection */
903 log_error_write(srv
, __FILE__
, __LINE__
, "ssd", "accept failed:", strerror(errno
), errno
);
907 return connection_accepted(srv
, srv_socket
, &cnt_addr
, cnt
);
911 connection
*connection_accepted(server
*srv
, server_socket
*srv_socket
, sock_addr
*cnt_addr
, int cnt
) {
916 /* ok, we have the connection, register it */
918 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
923 con
= connections_get_new_connection(srv
);
928 gettimeofday(&(con
->start_tv
), NULL
);
930 fdevent_register(srv
->ev
, con
->fd
, connection_handle_fdevent
, con
);
932 connection_set_state(srv
, con
, CON_STATE_REQUEST_START
);
934 con
->connection_start
= srv
->cur_ts
;
935 con
->dst_addr
= *cnt_addr
;
936 buffer_copy_string(con
->dst_addr_buf
, inet_ntop_cache_get_ip(srv
, &(con
->dst_addr
)));
937 con
->srv_socket
= srv_socket
;
939 if (-1 == (fdevent_fcntl_set(srv
->ev
, con
->fd
))) {
940 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed: ", strerror(errno
));
944 /* connect FD to SSL */
945 if (srv_socket
->is_ssl
) {
946 if (NULL
== (con
->ssl
= SSL_new(srv_socket
->ssl_ctx
))) {
947 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "SSL:",
948 ERR_error_string(ERR_get_error(), NULL
));
953 con
->renegotiations
= 0;
954 SSL_set_app_data(con
->ssl
, con
);
955 SSL_set_accept_state(con
->ssl
);
957 if (1 != (SSL_set_fd(con
->ssl
, cnt
))) {
958 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "SSL:",
959 ERR_error_string(ERR_get_error(), NULL
));
968 int connection_state_machine(server
*srv
, connection
*con
) {
971 server_socket
*srv_sock
= con
->srv_socket
;
974 if (srv
->srvconf
.log_state_handling
) {
975 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
978 connection_get_state(con
->state
));
982 size_t ostate
= con
->state
;
984 switch (con
->state
) {
985 case CON_STATE_REQUEST_START
: /* transient */
986 if (srv
->srvconf
.log_state_handling
) {
987 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
988 "state for fd", con
->fd
, connection_get_state(con
->state
));
991 con
->request_start
= srv
->cur_ts
;
992 con
->read_idle_ts
= srv
->cur_ts
;
994 con
->request_count
++;
995 con
->loops_per_request
= 0;
997 connection_set_state(srv
, con
, CON_STATE_READ
);
1000 case CON_STATE_REQUEST_END
: /* transient */
1001 if (srv
->srvconf
.log_state_handling
) {
1002 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1003 "state for fd", con
->fd
, connection_get_state(con
->state
));
1006 buffer_reset(con
->uri
.authority
);
1007 buffer_reset(con
->uri
.path
);
1008 buffer_reset(con
->uri
.query
);
1009 buffer_reset(con
->request
.orig_uri
);
1011 if (http_request_parse(srv
, con
)) {
1012 /* we have to read some data from the POST request */
1014 connection_set_state(srv
, con
, CON_STATE_READ_POST
);
1019 connection_set_state(srv
, con
, CON_STATE_HANDLE_REQUEST
);
1022 case CON_STATE_READ_POST
:
1023 case CON_STATE_HANDLE_REQUEST
:
1025 * the request is parsed
1027 * decided what to do with the request
1033 if (srv
->srvconf
.log_state_handling
) {
1034 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1035 "state for fd", con
->fd
, connection_get_state(con
->state
));
1038 switch (r
= http_response_prepare(srv
, con
)) {
1039 case HANDLER_FINISHED
:
1040 if (con
->error_handler_saved_status
> 0) {
1041 con
->request
.http_method
= con
->error_handler_saved_method
;
1043 if (con
->mode
== DIRECT
) {
1044 if (con
->error_handler_saved_status
) {
1045 if (con
->error_handler_saved_status
> 0) {
1046 con
->http_status
= con
->error_handler_saved_status
;
1047 } else if (con
->http_status
== 404 || con
->http_status
== 403) {
1048 /* error-handler-404 is a 404 */
1049 con
->http_status
= -con
->error_handler_saved_status
;
1051 /* error-handler-404 is back and has generated content */
1052 /* if Status: was set, take it otherwise use 200 */
1054 } else if (con
->http_status
>= 400) {
1055 buffer
*error_handler
= NULL
;
1056 if (!buffer_string_is_empty(con
->conf
.error_handler
)) {
1057 error_handler
= con
->conf
.error_handler
;
1058 } else if ((con
->http_status
== 404 || con
->http_status
== 403)
1059 && !buffer_string_is_empty(con
->conf
.error_handler_404
)) {
1060 error_handler
= con
->conf
.error_handler_404
;
1063 if (error_handler
) {
1064 /* call error-handler */
1066 /* set REDIRECT_STATUS to save current HTTP status code
1067 * for access by dynamic handlers
1068 * https://redmine.lighttpd.net/issues/1828 */
1070 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
1071 ds
= data_string_init();
1073 buffer_copy_string_len(ds
->key
, CONST_STR_LEN("REDIRECT_STATUS"));
1074 buffer_append_int(ds
->value
, con
->http_status
);
1075 array_insert_unique(con
->environment
, (data_unset
*)ds
);
1077 if (error_handler
== con
->conf
.error_handler
) {
1078 plugins_call_connection_reset(srv
, con
);
1080 if (con
->request
.content_length
) {
1081 if ((off_t
)con
->request
.content_length
!= chunkqueue_length(con
->request_content_queue
)) {
1082 con
->keep_alive
= 0;
1084 con
->request
.content_length
= 0;
1085 chunkqueue_reset(con
->request_content_queue
);
1088 con
->is_writable
= 1;
1089 con
->file_finished
= 0;
1090 con
->file_started
= 0;
1091 con
->got_response
= 0;
1092 con
->parsed_response
= 0;
1093 con
->response
.keep_alive
= 0;
1094 con
->response
.content_length
= -1;
1095 con
->response
.transfer_encoding
= 0;
1097 array_set_key_value(con
->environment
, CONST_STR_LEN("REDIRECT_URI"), CONST_BUF_LEN(con
->request
.orig_uri
));
1098 con
->error_handler_saved_status
= con
->http_status
;
1099 con
->error_handler_saved_method
= con
->request
.http_method
;
1101 con
->request
.http_method
= HTTP_METHOD_GET
;
1102 } else { /*(preserve behavior for server.error-handler-404)*/
1103 array_set_key_value(con
->environment
, CONST_STR_LEN("REDIRECT_URI"), CONST_BUF_LEN(error_handler
));
1104 con
->error_handler_saved_status
= -con
->http_status
; /*(negative to flag old behavior)*/
1107 buffer_copy_buffer(con
->request
.uri
, error_handler
);
1108 connection_handle_errdoc_init(srv
, con
);
1109 con
->http_status
= 0; /*(after connection_handle_errdoc_init())*/
1116 if (con
->http_status
== 0) con
->http_status
= 200;
1118 /* we have something to send, go on */
1119 connection_set_state(srv
, con
, CON_STATE_RESPONSE_START
);
1121 case HANDLER_WAIT_FOR_FD
:
1124 fdwaitqueue_append(srv
, con
);
1127 case HANDLER_COMEBACK
:
1130 case HANDLER_WAIT_FOR_EVENT
:
1131 /* come back here */
1134 /* something went wrong */
1135 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1138 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", "unknown ret-value: ", con
->fd
, r
);
1143 case CON_STATE_RESPONSE_START
:
1145 * the decision is done
1146 * - create the HTTP-Response-Header
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 if (-1 == connection_handle_write_prepare(srv
, con
)) {
1156 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1161 connection_set_state(srv
, con
, CON_STATE_WRITE
);
1163 case CON_STATE_RESPONSE_END
: /* transient */
1164 /* log the request */
1166 if (srv
->srvconf
.log_state_handling
) {
1167 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1168 "state for fd", con
->fd
, connection_get_state(con
->state
));
1171 plugins_call_handle_request_done(srv
, con
);
1175 if (con
->keep_alive
) {
1176 connection_set_state(srv
, con
, CON_STATE_REQUEST_START
);
1179 con
->request_start
= srv
->cur_ts
;
1180 con
->read_idle_ts
= srv
->cur_ts
;
1183 switch(r
= plugins_call_handle_connection_close(srv
, con
)) {
1185 case HANDLER_FINISHED
:
1188 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "unhandling return value", r
);
1193 if (srv_sock
->is_ssl
) {
1194 switch (SSL_shutdown(con
->ssl
)) {
1199 /* wait for fd-event
1201 * FIXME: wait for fdevent and call SSL_shutdown again
1207 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "SSL:",
1208 ERR_error_string(ERR_get_error(), NULL
));
1212 if ((0 == shutdown(con
->fd
, SHUT_WR
))) {
1213 con
->close_timeout_ts
= srv
->cur_ts
;
1214 connection_set_state(srv
, con
, CON_STATE_CLOSE
);
1216 connection_close(srv
, con
);
1222 connection_reset(srv
, con
);
1225 case CON_STATE_CONNECT
:
1226 if (srv
->srvconf
.log_state_handling
) {
1227 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1228 "state for fd", con
->fd
, connection_get_state(con
->state
));
1231 chunkqueue_reset(con
->read_queue
);
1233 con
->request_count
= 0;
1236 case CON_STATE_CLOSE
:
1237 if (srv
->srvconf
.log_state_handling
) {
1238 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1239 "state for fd", con
->fd
, connection_get_state(con
->state
));
1242 /* we have to do the linger_on_close stuff regardless
1243 * of con->keep_alive; even non-keepalive sockets may
1244 * still have unread data, and closing before reading
1245 * it will make the client not see all our output.
1251 len
= read(con
->fd
, buf
, sizeof(buf
));
1252 if (len
== 0 || (len
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
) ) {
1253 con
->close_timeout_ts
= srv
->cur_ts
- (HTTP_LINGER_TIMEOUT
+1);
1257 if (srv
->cur_ts
- con
->close_timeout_ts
> HTTP_LINGER_TIMEOUT
) {
1258 connection_close(srv
, con
);
1260 if (srv
->srvconf
.log_state_handling
) {
1261 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1262 "connection closed for fd", con
->fd
);
1267 case CON_STATE_READ
:
1268 if (srv
->srvconf
.log_state_handling
) {
1269 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1270 "state for fd", con
->fd
, connection_get_state(con
->state
));
1273 connection_handle_read_state(srv
, con
);
1275 case CON_STATE_WRITE
:
1276 if (srv
->srvconf
.log_state_handling
) {
1277 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1278 "state for fd", con
->fd
, connection_get_state(con
->state
));
1281 /* only try to write if we have something in the queue */
1282 if (!chunkqueue_is_empty(con
->write_queue
)) {
1283 if (con
->is_writable
) {
1284 if (-1 == connection_handle_write(srv
, con
)) {
1285 log_error_write(srv
, __FILE__
, __LINE__
, "ds",
1287 "handle write failed.");
1288 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1291 } else if (con
->file_finished
) {
1292 connection_set_state(srv
, con
, CON_STATE_RESPONSE_END
);
1296 case CON_STATE_ERROR
: /* transient */
1298 /* even if the connection was drop we still have to write it to the access log */
1299 if (con
->http_status
) {
1300 plugins_call_handle_request_done(srv
, con
);
1303 if (srv_sock
->is_ssl
) {
1307 switch ((ret
= SSL_shutdown(con
->ssl
))) {
1313 if (-1 != (ret
= SSL_shutdown(con
->ssl
))) break;
1318 switch ((ssl_r
= SSL_get_error(con
->ssl
, ret
))) {
1319 case SSL_ERROR_WANT_WRITE
:
1320 case SSL_ERROR_WANT_READ
:
1322 case SSL_ERROR_SYSCALL
:
1323 /* perhaps we have error waiting in our error-queue */
1324 if (0 != (err
= ERR_get_error())) {
1326 log_error_write(srv
, __FILE__
, __LINE__
, "sdds", "SSL:",
1328 ERR_error_string(err
, NULL
));
1329 } while((err
= ERR_get_error()));
1330 } else if (errno
!= 0) { /* ssl bug (see lighttpd ticket #2213): sometimes errno == 0 */
1336 log_error_write(srv
, __FILE__
, __LINE__
, "sddds", "SSL (error):",
1345 while((err
= ERR_get_error())) {
1346 log_error_write(srv
, __FILE__
, __LINE__
, "sdds", "SSL:",
1348 ERR_error_string(err
, NULL
));
1361 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1362 "emergency exit: direct",
1367 switch(r
= plugins_call_handle_connection_close(srv
, con
)) {
1369 case HANDLER_FINISHED
:
1372 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "unhandling return value", r
);
1378 connection_reset(srv
, con
);
1380 /* close the connection */
1381 if ((0 == shutdown(con
->fd
, SHUT_WR
))) {
1382 con
->close_timeout_ts
= srv
->cur_ts
;
1383 connection_set_state(srv
, con
, CON_STATE_CLOSE
);
1385 if (srv
->srvconf
.log_state_handling
) {
1386 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1387 "shutdown for fd", con
->fd
);
1390 connection_close(srv
, con
);
1393 con
->keep_alive
= 0;
1399 log_error_write(srv
, __FILE__
, __LINE__
, "sdd",
1400 "unknown state:", con
->fd
, con
->state
);
1407 } else if (ostate
== con
->state
) {
1412 if (srv
->srvconf
.log_state_handling
) {
1413 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
1416 connection_get_state(con
->state
));
1419 switch(con
->state
) {
1420 case CON_STATE_READ_POST
:
1421 case CON_STATE_READ
:
1422 case CON_STATE_CLOSE
:
1423 fdevent_event_set(srv
->ev
, &(con
->fde_ndx
), con
->fd
, FDEVENT_IN
);
1425 case CON_STATE_WRITE
:
1426 /* request write-fdevent only if we really need it
1427 * - if we have data to write
1428 * - if the socket is not writable yet
1430 if (!chunkqueue_is_empty(con
->write_queue
) &&
1431 (con
->is_writable
== 0) &&
1432 (con
->traffic_limit_reached
== 0)) {
1433 fdevent_event_set(srv
->ev
, &(con
->fde_ndx
), con
->fd
, FDEVENT_OUT
);
1435 fdevent_event_set(srv
->ev
, &(con
->fde_ndx
), con
->fd
, 0);
1439 fdevent_event_set(srv
->ev
, &(con
->fde_ndx
), con
->fd
, 0);