From 057d83c50bbc38fe752d9545f6ccdc894a28ac13 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Sun, 5 Jan 2020 21:34:38 -0500 Subject: [PATCH] [core] move keep_alive flag into request_st --- src/base.h | 5 +++-- src/connections-glue.c | 4 ++-- src/connections.c | 20 ++++++++++---------- src/http-header-glue.c | 8 ++++---- src/mod_accesslog.c | 2 +- src/mod_auth.c | 6 +++--- src/mod_cgi.c | 2 +- src/request.c | 10 +++++----- src/response.c | 10 +++++----- 9 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/base.h b/src/base.h index aa4756fd..ed358508 100644 --- a/src/base.h +++ b/src/base.h @@ -20,7 +20,7 @@ struct cond_match_t; /* declaration */ #define DIRECT 0 /* con->mode */ -typedef struct { +typedef struct request_st { /** HEADER */ /* the request-line */ buffer *request; @@ -41,6 +41,8 @@ typedef struct { off_t content_length; /* returned by strtoll() */ off_t te_chunked; + int keep_alive; /* only request.c can enable it, all other just disable */ + /* internal */ buffer *pathinfo; } request; @@ -181,7 +183,6 @@ struct connection { int is_writable; int is_ssl_sock; - int keep_alive; /* only request.c can enable it, all other just disable */ int keep_alive_idle; /* remember max_keep_alive_idle from config */ int file_started; diff --git a/src/connections-glue.c b/src/connections-glue.c index e53225dc..923786f8 100644 --- a/src/connections-glue.c +++ b/src/connections-glue.c @@ -110,7 +110,7 @@ static int connection_handle_read_post_chunked_crlf(chunkqueue *cq) { } handler_t connection_handle_read_post_error(connection *con, int http_status) { - con->keep_alive = 0; + con->request.keep_alive = 0; /*(do not change status if response headers already set and possibly sent)*/ if (0 != con->bytes_header) return HANDLER_ERROR; @@ -192,7 +192,7 @@ static handler_t connection_handle_read_post_chunked(connection *con, chunkqueue else { /* ignore excessively long trailers; * disable keep-alive on connection */ - con->keep_alive = 0; + con->request.keep_alive = 0; p = c->mem->ptr + buffer_string_length(c->mem) - 4; } } diff --git a/src/connections.c b/src/connections.c index b75614df..fbb28fd4 100644 --- a/src/connections.c +++ b/src/connections.c @@ -153,8 +153,8 @@ static int connection_close(connection *con) { static void connection_read_for_eos_plain(connection * const con) { /* we have to do the linger_on_close stuff regardless - * of con->keep_alive; even non-keepalive sockets may - * still have unread data, and closing before reading + * of con->request.keep_alive; even non-keepalive sockets + * may still have unread data, and closing before reading * it will make the client not see all our output. */ ssize_t len; @@ -232,10 +232,10 @@ static void connection_handle_response_end_state(connection *con) { if (con->request.content_length != con->request_content_queue->bytes_in || con->state == CON_STATE_ERROR) { /* request body is present and has not been read completely */ - con->keep_alive = 0; + con->request.keep_alive = 0; } - if (con->keep_alive) { + if (con->request.keep_alive) { connection_reset(con); #if 0 con->request_start = con->read_idle_ts = log_epoch_secs; @@ -441,7 +441,7 @@ static int connection_handle_write_prepare(connection *con) { } http_header_response_append(con, HTTP_HEADER_TRANSFER_ENCODING, CONST_STR_LEN("Transfer-Encoding"), CONST_STR_LEN("chunked")); } else { - con->keep_alive = 0; + con->request.keep_alive = 0; } } } @@ -798,7 +798,7 @@ static int connection_handle_read_state(connection * const con) { log_error(con->conf.errh, __FILE__, __LINE__, "%s", "oversized request-header -> sending Status 431"); con->http_status = 431; /* Request Header Fields Too Large */ - con->keep_alive = 0; + con->request.keep_alive = 0; return 1; } @@ -847,7 +847,7 @@ static int connection_handle_read_state(connection * const con) { con->http_status = http_request_parse(con, hdrs, hoff); if (0 != con->http_status) { - con->keep_alive = 0; + con->request.keep_alive = 0; con->request.content_length = 0; if (con->conf.log_request_header_on_error) { @@ -915,7 +915,7 @@ static handler_t connection_handle_fdevent(void *context, int revents) { con->conf.stream_request_body &= ~(FDEVENT_STREAM_REQUEST_BUFMIN|FDEVENT_STREAM_REQUEST_POLLIN); con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLRDHUP; con->is_readable = 1; /*(can read 0 for end-of-stream)*/ - if (chunkqueue_is_empty(con->read_queue)) con->keep_alive = 0; + if (chunkqueue_is_empty(con->read_queue)) con->request.keep_alive = 0; if (con->request.content_length < -1) { /*(transparent proxy mode; no more data to read)*/ con->request.content_length = con->request_content_queue->bytes_in; } @@ -1186,7 +1186,7 @@ static int connection_handle_request(connection *con) { if (con->request.content_length) { if (con->request.content_length != con->request_content_queue->bytes_in) { - con->keep_alive = 0; + con->request.keep_alive = 0; } con->request.content_length = 0; chunkqueue_reset(con->request_content_queue); @@ -1493,7 +1493,7 @@ void connection_graceful_shutdown_maint (server *srv) { changed = 1; } - con->keep_alive = 0; /* disable keep-alive */ + con->request.keep_alive = 0; /* disable keep-alive */ con->conf.bytes_per_second = 0; /* disable rate limit */ con->conf.global_bytes_per_second = 0; /* disable rate limit */ diff --git a/src/http-header-glue.c b/src/http-header-glue.c index 5574c8ff..6332c602 100644 --- a/src/http-header-glue.c +++ b/src/http-header-glue.c @@ -817,7 +817,7 @@ void http_response_backend_error (connection *con) { /*(response might have been already started, kill the connection)*/ /*(mode == DIRECT to avoid later call to http_response_backend_done())*/ con->mode = DIRECT; /*(avoid sending final chunked block)*/ - con->keep_alive = 0; /*(no keep-alive; final chunked block not sent)*/ + con->request.keep_alive = 0; con->file_finished = 1; } /*(else error status set later by http_response_backend_done())*/ } @@ -856,7 +856,7 @@ void http_response_upgrade_read_body_unknown(connection *con) { (FDEVENT_STREAM_RESPONSE_BUFMIN | FDEVENT_STREAM_RESPONSE); con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN; con->request.content_length = -2; - con->keep_alive = 0; + con->request.keep_alive = 0; } @@ -910,7 +910,7 @@ static handler_t http_response_process_local_redir(connection *con, size_t blen) if (con->request.content_length) { if (con->request.content_length != con->request_content_queue->bytes_in) { - con->keep_alive = 0; + con->request.keep_alive = 0; } con->request.content_length = 0; chunkqueue_reset(con->request_content_queue); @@ -1032,7 +1032,7 @@ static int http_response_process_headers(connection *con, http_response_opts *op /*(should parse for tokens and do case-insensitive match for "close" * but this is an imperfect though simplistic attempt to honor * backend request to close)*/ - if (NULL != strstr(value, "lose")) con->keep_alive = 0; + if (NULL != strstr(value, "lose")) con->request.keep_alive = 0; break; case HTTP_HEADER_CONTENT_LENGTH: con->response.content_length = strtoul(value, NULL, 10); diff --git a/src/mod_accesslog.c b/src/mod_accesslog.c index ccffe594..2fea60b5 100644 --- a/src/mod_accesslog.c +++ b/src/mod_accesslog.c @@ -1087,7 +1087,7 @@ REQUESTDONE_FUNC(log_access_write) { break; case FORMAT_CONNECTION_STATUS: if (con->state == CON_STATE_RESPONSE_END) { - if (0 == con->keep_alive) { + if (0 == con->request.keep_alive) { buffer_append_string_len(b, CONST_STR_LEN("-")); } else { buffer_append_string_len(b, CONST_STR_LEN("+")); diff --git a/src/mod_auth.c b/src/mod_auth.c index 8c9e4633..89caaedd 100644 --- a/src/mod_auth.c +++ b/src/mod_auth.c @@ -598,7 +598,7 @@ static handler_t mod_auth_check_basic(connection *con, void *p_d, const struct h log_error(con->conf.errh, __FILE__, __LINE__, "password doesn't match for %s username: %s IP: %s", con->uri.path->ptr, username->ptr, con->dst_addr_buf->ptr); - con->keep_alive = 0; /*(disable keep-alive if bad password)*/ + con->request.keep_alive = 0; /*(disable keep-alive if bad password)*/ rc = HANDLER_UNSET; break; } @@ -1125,7 +1125,7 @@ static handler_t mod_auth_check_digest(connection *con, void *p_d, const struct return HANDLER_FINISHED; case HANDLER_ERROR: default: - con->keep_alive = 0; /*(disable keep-alive if unknown user)*/ + con->request.keep_alive = 0; /*(disable keep-alive if unknown user)*/ buffer_free(b); return mod_auth_send_401_unauthorized_digest(con, require, 0); } @@ -1137,7 +1137,7 @@ static handler_t mod_auth_check_digest(connection *con, void *p_d, const struct log_error(con->conf.errh, __FILE__, __LINE__, "digest: auth failed for %s: wrong password, IP: %s", username, con->dst_addr_buf->ptr); - con->keep_alive = 0; /*(disable keep-alive if bad password)*/ + con->request.keep_alive = 0; /*(disable keep-alive if bad password)*/ buffer_free(b); return mod_auth_send_401_unauthorized_digest(con, require, 0); diff --git a/src/mod_cgi.c b/src/mod_cgi.c index f5964c63..1aa2e586 100644 --- a/src/mod_cgi.c +++ b/src/mod_cgi.c @@ -372,7 +372,7 @@ static handler_t cgi_handle_fdevent_send (void *ctx, int revents) { chunkqueue *cq = con->request_content_queue; chunkqueue_mark_written(cq, chunkqueue_length(cq)); if (cq->bytes_in != (off_t)con->request.content_length) { - con->keep_alive = 0; + con->request.keep_alive = 0; } } diff --git a/src/request.c b/src/request.c index f1b70f29..8874c264 100644 --- a/src/request.c +++ b/src/request.c @@ -407,11 +407,11 @@ static int http_request_parse_single_header(connection * const con, const enum h /* "Connection: close" is common case if header is present */ if ((vlen == 5 && buffer_eq_icase_ssn(v, CONST_STR_LEN("close"))) || http_header_str_contains_token(v,vlen,CONST_STR_LEN("close"))) { - con->keep_alive = 0; + con->request.keep_alive = 0; break; } if (http_header_str_contains_token(v,vlen,CONST_STR_LEN("keep-alive"))){ - con->keep_alive = 1; + con->request.keep_alive = 1; break; } break; @@ -510,7 +510,7 @@ static int http_request_parse_proto_loose(connection * const con, const char * c return http_request_header_line_invalid(con, 400, "unknown protocol -> 400"); /* keep-alive default: HTTP/1.1 -> true; HTTP/1.0 -> false */ - con->keep_alive = (HTTP_VERSION_1_0 != con->request.http_version); + con->request.keep_alive = (HTTP_VERSION_1_0 != con->request.http_version); return 0; } @@ -580,11 +580,11 @@ static int http_request_parse_reqline(connection * const con, const char * const proto8.c[4]=p[4]; proto8.c[5]=p[5]; proto8.c[6]=p[6]; proto8.c[7]=p[7]; if (p[-1] == ' ' && http_1_1.u == proto8.u) { con->request.http_version = HTTP_VERSION_1_1; - con->keep_alive = 1; /* keep-alive default: HTTP/1.1 -> true */ + con->request.keep_alive = 1; /* keep-alive default: HTTP/1.1 -> true */ } else if (p[-1] == ' ' && http_1_0.u == proto8.u) { con->request.http_version = HTTP_VERSION_1_0; - con->keep_alive = 0; /* keep-alive default: HTTP/1.0 -> false */ + con->request.keep_alive = 0; /* keep-alive default: HTTP/1.0 -> false */ } else { int status = http_request_parse_proto_loose(con, ptr, len); diff --git a/src/response.c b/src/response.c index a18af5b7..b1bca503 100644 --- a/src/response.c +++ b/src/response.c @@ -58,20 +58,20 @@ int http_response_write_header(connection *con) { /* disable keep-alive if requested */ if (con->request_count > con->conf.max_keep_alive_requests || 0 == con->conf.max_keep_alive_idle) { - con->keep_alive = 0; + con->request.keep_alive = 0; } else if (0 != con->request.content_length && con->request.content_length != con->request_content_queue->bytes_in && (con->mode == DIRECT || 0 == con->conf.stream_request_body)) { - con->keep_alive = 0; + con->request.keep_alive = 0; } else { con->keep_alive_idle = con->conf.max_keep_alive_idle; } if ((con->response.htags & HTTP_HEADER_UPGRADE) && con->request.http_version == HTTP_VERSION_1_1) { http_header_response_set(con, HTTP_HEADER_CONNECTION, CONST_STR_LEN("Connection"), CONST_STR_LEN("upgrade")); - } else if (0 == con->keep_alive) { + } else if (0 == con->request.keep_alive) { http_header_response_set(con, HTTP_HEADER_CONNECTION, CONST_STR_LEN("Connection"), CONST_STR_LEN("close")); - } else if (con->request.http_version == HTTP_VERSION_1_0) {/*(&& con->keep_alive != 0)*/ + } else if (con->request.http_version == HTTP_VERSION_1_0) {/*(&& con->request.keep_alive != 0)*/ http_header_response_set(con, HTTP_HEADER_CONNECTION, CONST_STR_LEN("Connection"), CONST_STR_LEN("keep-alive")); } @@ -286,7 +286,7 @@ static handler_t http_response_physical_path_check(connection *con) { __attribute_cold__ __attribute_noinline__ static handler_t http_status_set_error_close (connection *con, int status) { - con->keep_alive = 0; + con->request.keep_alive = 0; con->file_finished = 1; con->mode = DIRECT; con->http_status = status;