do not set REDIRECT_URI in mod_magnet, mod_rewrite (#2738)
[lighttpd.git] / src / connections.c
blob507838c083073e975a8c528f77eb803c164a3d3d
1 #include "first.h"
3 #include "buffer.h"
4 #include "server.h"
5 #include "log.h"
6 #include "connections.h"
7 #include "fdevent.h"
9 #include "configfile.h"
10 #include "request.h"
11 #include "response.h"
12 #include "network.h"
13 #include "http_chunk.h"
14 #include "stat_cache.h"
15 #include "joblist.h"
17 #include "plugin.h"
19 #include "inet_ntop_cache.h"
21 #include <sys/stat.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <assert.h>
31 #ifdef USE_OPENSSL
32 # include <openssl/ssl.h>
33 # include <openssl/err.h>
34 #endif
36 #ifdef HAVE_SYS_FILIO_H
37 # include <sys/filio.h>
38 #endif
40 #include "sys-socket.h"
42 typedef struct {
43 PLUGIN_DATA;
44 } plugin_data;
46 static connection *connections_get_new_connection(server *srv) {
47 connections *conns = srv->conns;
48 size_t i;
50 if (conns->size == 0) {
51 conns->size = 128;
52 conns->ptr = NULL;
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) {
59 conns->size += 128;
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]);
69 #if 0
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");
75 #endif
77 conns->ptr[conns->used]->ndx = conns->used;
78 return conns->ptr[conns->used++];
81 static int connection_del(server *srv, connection *con) {
82 size_t i;
83 connections *conns = srv->conns;
84 connection *temp;
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);
95 i = con->ndx;
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;
108 conns->used--;
110 con->ndx = -1;
111 #if 0
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");
117 #endif
118 return 0;
121 static int connection_close(server *srv, connection *con) {
122 #ifdef USE_OPENSSL
123 server_socket *srv_sock = con->srv_socket;
124 #endif
126 #ifdef USE_OPENSSL
127 if (srv_sock->is_ssl) {
128 if (con->ssl) SSL_free(con->ssl);
129 con->ssl = NULL;
131 #endif
133 fdevent_event_del(srv->ev, &(con->fde_ndx), con->fd);
134 fdevent_unregister(srv->ev, con->fd);
135 #ifdef __WIN32
136 if (closesocket(con->fd)) {
137 log_error_write(srv, __FILE__, __LINE__, "sds",
138 "(warning) close:", con->fd, strerror(errno));
140 #else
141 if (close(con->fd)) {
142 log_error_write(srv, __FILE__, __LINE__, "sds",
143 "(warning) close:", con->fd, strerror(errno));
145 #endif
146 con->fd = -1;
148 srv->cur_fds--;
149 #if 0
150 log_error_write(srv, __FILE__, __LINE__, "sd",
151 "closed()", con->fd);
152 #endif
154 connection_del(srv, con);
155 connection_set_state(srv, con, CON_STATE_CONNECT);
157 return 0;
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");
167 if (NULL != ds) {
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) {
185 /* static files */
186 switch(con->request.http_method) {
187 case HTTP_METHOD_GET:
188 case HTTP_METHOD_POST:
189 case HTTP_METHOD_HEAD:
190 break;
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
196 * */
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);
209 break;
210 default:
211 if (0 == con->http_status) {
212 con->http_status = 501;
214 break;
218 if (con->http_status == 0) {
219 con->http_status = 403;
222 switch(con->http_status) {
223 case 204: /* class: header only */
224 case 205:
225 case 304:
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;
232 break;
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) {
260 buffer *b;
262 buffer_reset(con->physical.path);
264 con->file_finished = 1;
265 b = buffer_init();
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"
273 " <head>\n"
274 " <title>"));
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(
280 "</title>\n"
281 " </head>\n"
282 " <body>\n"
283 " <h1>"));
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"
289 " </body>\n"
290 "</html>\n"
293 (void)http_chunk_append_buffer(srv, con, b);
294 buffer_free(b);
296 response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
298 break;
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
314 * available
316 if ((con->http_status >= 100 && con->http_status < 200) ||
317 con->http_status == 204 ||
318 con->http_status == 304) {
319 data_string *ds;
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));
333 } else {
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;
347 if (qlen) {
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"));
356 } else {
357 con->keep_alive = 0;
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
366 * the connection
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) {
373 con->keep_alive = 0;
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);
391 return 0;
394 static int connection_handle_write(server *srv, connection *con) {
395 switch(network_write_chunkqueue(srv, con, con->write_queue, MAX_WRITE_LIMIT)) {
396 case 0:
397 con->write_request_ts = srv->cur_ts;
398 if (con->file_finished) {
399 connection_set_state(srv, con, CON_STATE_RESPONSE_END);
401 break;
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);
406 break;
407 case -2: /* remote close */
408 connection_set_state(srv, con, CON_STATE_ERROR);
409 break;
410 case 1:
411 con->write_request_ts = srv->cur_ts;
412 con->is_writable = 0;
414 /* not finished yet -> WRITE */
415 break;
418 return 0;
423 connection *connection_init(server *srv) {
424 connection *con;
426 UNUSED(srv);
428 con = calloc(1, sizeof(*con));
429 force_assert(NULL != con);
431 con->fd = 0;
432 con->ndx = -1;
433 con->fde_ndx = -1;
434 con->bytes_written = 0;
435 con->bytes_read = 0;
436 con->bytes_header = 0;
437 con->loops_per_request = 0;
439 #define CLEAN(x) \
440 con->x = buffer_init();
442 CLEAN(request.uri);
443 CLEAN(request.request_line);
444 CLEAN(request.request);
445 CLEAN(request.pathinfo);
447 CLEAN(request.orig_uri);
449 CLEAN(uri.scheme);
450 CLEAN(uri.authority);
451 CLEAN(uri.path);
452 CLEAN(uri.path_raw);
453 CLEAN(uri.query);
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);
462 CLEAN(server_name);
463 CLEAN(dst_addr_buf);
464 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
465 CLEAN(tlsext_server_name);
466 #endif
468 #undef CLEAN
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);
490 return con;
493 void connections_free(server *srv) {
494 connections *conns = srv->conns;
495 size_t i;
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);
509 #define CLEAN(x) \
510 buffer_free(con->x);
512 CLEAN(request.uri);
513 CLEAN(request.request_line);
514 CLEAN(request.request);
515 CLEAN(request.pathinfo);
517 CLEAN(request.orig_uri);
519 CLEAN(uri.scheme);
520 CLEAN(uri.authority);
521 CLEAN(uri.path);
522 CLEAN(uri.path_raw);
523 CLEAN(uri.query);
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);
532 CLEAN(server_name);
533 CLEAN(dst_addr_buf);
534 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
535 CLEAN(tlsext_server_name);
536 #endif
537 #undef CLEAN
538 free(con->plugin_ctx);
539 free(con->cond_cache);
541 free(con);
544 free(conns->ptr);
548 int connection_reset(server *srv, connection *con) {
549 size_t i;
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;
564 con->bytes_read = 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;
578 con->mode = DIRECT;
580 #define CLEAN(x) \
581 if (con->x) buffer_reset(con->x);
583 CLEAN(request.uri);
584 CLEAN(request.request_line);
585 CLEAN(request.pathinfo);
586 CLEAN(request.request);
588 /* CLEAN(request.orig_uri); */
590 CLEAN(uri.scheme);
591 /* CLEAN(uri.authority); */
592 /* CLEAN(uri.path); */
593 CLEAN(uri.path_raw);
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);
604 CLEAN(server_name);
605 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
606 CLEAN(tlsext_server_name);
607 #endif
608 #undef CLEAN
610 #define CLEAN(x) \
611 if (con->x) con->x->used = 0;
613 #undef CLEAN
615 #define CLEAN(x) \
616 con->request.x = NULL;
618 CLEAN(http_host);
619 CLEAN(http_range);
620 CLEAN(http_content_type);
621 #undef CLEAN
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;
636 if (!pd) continue;
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); */
648 con->header_len = 0;
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);
655 return 0;
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;
665 off_t last_offset;
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)) {
675 case -1:
676 return -1;
677 case -2:
678 is_closed = 1;
679 break;
680 default:
681 break;
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
706 last_chunk = NULL;
707 last_offset = 0;
709 for (c = cq->first; c; c = c->next) {
710 size_t i;
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) {
715 char ch = b[i];
717 if ('\r' == ch) {
718 /* chec if \n\r\n follows */
719 size_t j = i+1;
720 chunk *cc = c;
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++) {
729 ch = bb[j];
731 if (ch == header_end[header_end_match_pos]) {
732 header_end_match_pos++;
733 if (4 == header_end_match_pos) {
734 last_chunk = cc;
735 last_offset = j+1;
736 goto found_header_end;
738 } else {
739 goto reset_search;
744 reset_search: ;
747 found_header_end:
749 /* found */
750 if (last_chunk) {
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) {
757 len = last_offset;
760 buffer_append_string_len(con->request.request, c->mem->ptr + c->offset, len);
761 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 */
772 con->keep_alive = 0;
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);
782 return 0;
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;
796 } else {
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);
814 } else {
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);
823 } else {
824 connection_set_state(srv, con, CON_STATE_ERROR);
826 #else
827 connection_set_state(srv, con, CON_STATE_ERROR);
828 #endif
831 } else if (revents & FDEVENT_ERR) {
832 /* error, connection reset, whatever... we don't want to spam the logfile */
833 #if 0
834 log_error_write(srv, __FILE__, __LINE__, "sd",
835 "connection closed: poll() -> ERR", con->fd);
836 #endif
837 connection_set_state(srv, con, CON_STATE_ERROR);
838 } else {
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) &&
850 con->is_writable) {
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",
856 con->fd,
857 "handle write failed.");
861 if (con->state == CON_STATE_CLOSE) {
862 /* flush the read buffers */
863 int len;
864 char buf[1024];
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 */
880 int cnt;
881 sock_addr cnt_addr;
882 socklen_t cnt_len;
883 /* accept it and register the fd */
886 * check if we can still open a new connections
888 * see #1216
891 if (srv->conns->used >= srv->max_conns) {
892 return NULL;
895 cnt_len = sizeof(cnt_addr);
897 if (-1 == (cnt = accept(srv_socket->fd, (struct sockaddr *) &cnt_addr, &cnt_len))) {
898 switch (errno) {
899 case EAGAIN:
900 #if EWOULDBLOCK != EAGAIN
901 case EWOULDBLOCK:
902 #endif
903 case EINTR:
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 */
907 break;
908 case EMFILE:
909 /* out of fds */
910 break;
911 default:
912 log_error_write(srv, __FILE__, __LINE__, "ssd", "accept failed:", strerror(errno), errno);
914 return NULL;
915 } else {
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) {
924 connection *con;
926 srv->cur_fds++;
928 /* ok, we have the connection, register it */
929 #if 0
930 log_error_write(srv, __FILE__, __LINE__, "sd",
931 "appected()", cnt);
932 #endif
933 srv->con_opened++;
935 con = connections_get_new_connection(srv);
937 con->fd = cnt;
938 con->fde_ndx = -1;
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));
950 return NULL;
952 #ifdef USE_OPENSSL
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));
959 return 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));
969 return NULL;
972 #endif
973 return con;
977 int connection_state_machine(server *srv, connection *con) {
978 int done = 0, r;
979 #ifdef USE_OPENSSL
980 server_socket *srv_sock = con->srv_socket;
981 #endif
983 if (srv->srvconf.log_state_handling) {
984 log_error_write(srv, __FILE__, __LINE__, "sds",
985 "state at start",
986 con->fd,
987 connection_get_state(con->state));
990 while (done == 0) {
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);
1010 break;
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);
1027 break;
1030 connection_set_state(srv, con, CON_STATE_HANDLE_REQUEST);
1032 break;
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;
1066 } else {
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 */
1085 data_string *ds;
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())*/
1127 done = -1;
1128 break;
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);
1136 break;
1137 case HANDLER_WAIT_FOR_FD:
1138 srv->want_fds++;
1140 fdwaitqueue_append(srv, con);
1142 break;
1143 case HANDLER_COMEBACK:
1144 done = -1;
1145 break;
1146 case HANDLER_ERROR:
1147 /* something went wrong */
1148 connection_set_state(srv, con, CON_STATE_ERROR);
1149 break;
1150 default:
1151 log_error_write(srv, __FILE__, __LINE__, "sdd", "unknown ret-value: ", con->fd, r);
1152 break;
1155 break;
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);
1171 break;
1174 connection_set_state(srv, con, CON_STATE_WRITE);
1175 break;
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);
1192 srv->con_written++;
1194 if (con->keep_alive) {
1195 connection_set_state(srv, con, CON_STATE_REQUEST_START);
1197 #if 0
1198 con->request_start = srv->cur_ts;
1199 con->read_idle_ts = srv->cur_ts;
1200 #endif
1201 } else {
1202 switch(r = plugins_call_handle_connection_close(srv, con)) {
1203 case HANDLER_GO_ON:
1204 case HANDLER_FINISHED:
1205 break;
1206 default:
1207 log_error_write(srv, __FILE__, __LINE__, "sd", "unhandling return value", r);
1208 break;
1211 #ifdef USE_OPENSSL
1212 if (srv_sock->is_ssl) {
1213 switch (SSL_shutdown(con->ssl)) {
1214 case 1:
1215 /* done */
1216 break;
1217 case 0:
1218 /* wait for fd-event
1220 * FIXME: wait for fdevent and call SSL_shutdown again
1224 break;
1225 default:
1226 log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
1227 ERR_error_string(ERR_get_error(), NULL));
1230 #endif
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);
1234 } else {
1235 connection_close(srv, con);
1238 srv->con_closed++;
1241 connection_reset(srv, con);
1243 break;
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;
1254 break;
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.
1267 int len;
1268 char buf[1024];
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);
1285 break;
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);
1293 break;
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));
1300 do {
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",
1306 con->fd,
1307 "handle write failed.");
1308 connection_set_state(srv, con, CON_STATE_ERROR);
1309 break;
1311 if (con->state != CON_STATE_WRITE) break;
1313 } else if (con->file_finished) {
1314 connection_set_state(srv, con, CON_STATE_RESPONSE_END);
1315 break;
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:
1322 case HANDLER_GO_ON:
1323 break;
1324 case HANDLER_WAIT_FOR_FD:
1325 srv->want_fds++;
1326 fdwaitqueue_append(srv, con);
1327 break;
1328 case HANDLER_COMEBACK:
1329 default:
1330 log_error_write(srv, __FILE__, __LINE__, "sdd", "unexpected subrequest handler ret-value: ", con->fd, r);
1331 /* fall through */
1332 case HANDLER_ERROR:
1333 connection_set_state(srv, con, CON_STATE_ERROR);
1334 break;
1337 } while (con->state == CON_STATE_WRITE && (!chunkqueue_is_empty(con->write_queue) ? con->is_writable : con->file_finished));
1339 break;
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);
1346 #ifdef USE_OPENSSL
1347 if (srv_sock->is_ssl) {
1348 int ret, ssl_r;
1349 unsigned long err;
1350 ERR_clear_error();
1351 switch ((ret = SSL_shutdown(con->ssl))) {
1352 case 1:
1353 /* ok */
1354 break;
1355 case 0:
1356 ERR_clear_error();
1357 if (-1 != (ret = SSL_shutdown(con->ssl))) break;
1359 /* fall through */
1360 default:
1362 switch ((ssl_r = SSL_get_error(con->ssl, ret))) {
1363 case SSL_ERROR_WANT_WRITE:
1364 case SSL_ERROR_WANT_READ:
1365 break;
1366 case SSL_ERROR_SYSCALL:
1367 /* perhaps we have error waiting in our error-queue */
1368 if (0 != (err = ERR_get_error())) {
1369 do {
1370 log_error_write(srv, __FILE__, __LINE__, "sdds", "SSL:",
1371 ssl_r, ret,
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 */
1375 switch(errno) {
1376 case EPIPE:
1377 case ECONNRESET:
1378 break;
1379 default:
1380 log_error_write(srv, __FILE__, __LINE__, "sddds", "SSL (error):",
1381 ssl_r, ret, errno,
1382 strerror(errno));
1383 break;
1387 break;
1388 default:
1389 while((err = ERR_get_error())) {
1390 log_error_write(srv, __FILE__, __LINE__, "sdds", "SSL:",
1391 ssl_r, ret,
1392 ERR_error_string(err, NULL));
1395 break;
1398 ERR_clear_error();
1400 #endif
1402 switch(con->mode) {
1403 case DIRECT:
1404 #if 0
1405 log_error_write(srv, __FILE__, __LINE__, "sd",
1406 "emergency exit: direct",
1407 con->fd);
1408 #endif
1409 break;
1410 default:
1411 switch(r = plugins_call_handle_connection_close(srv, con)) {
1412 case HANDLER_GO_ON:
1413 case HANDLER_FINISHED:
1414 break;
1415 default:
1416 log_error_write(srv, __FILE__, __LINE__, "sd", "unhandling return value", r);
1417 break;
1419 break;
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);
1433 } else {
1434 connection_close(srv, con);
1437 con->keep_alive = 0;
1439 srv->con_closed++;
1441 break;
1442 default:
1443 log_error_write(srv, __FILE__, __LINE__, "sdd",
1444 "unknown state:", con->fd, con->state);
1446 break;
1449 if (done == -1) {
1450 done = 0;
1451 } else if (ostate == con->state) {
1452 done = 1;
1456 if (srv->srvconf.log_state_handling) {
1457 log_error_write(srv, __FILE__, __LINE__, "sds",
1458 "state at exit:",
1459 con->fd,
1460 connection_get_state(con->state));
1463 r = 0;
1464 switch(con->state) {
1465 case CON_STATE_READ:
1466 case CON_STATE_CLOSE:
1467 r = FDEVENT_IN;
1468 break;
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)) {
1477 r |= FDEVENT_OUT;
1479 /* fall through */
1480 case CON_STATE_READ_POST:
1481 if (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN) {
1482 r |= FDEVENT_IN;
1484 break;
1485 default:
1486 break;
1488 if (-1 != con->fd) {
1489 const int events = fdevent_event_get_interest(srv->ev, con->fd);
1490 if (r != events) {
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);
1502 return 0;