diff options
Diffstat (limited to 'src/microhttpd/connection.c')
-rw-r--r-- | src/microhttpd/connection.c | 82 |
1 files changed, 44 insertions, 38 deletions
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c index e4873073..8c91f468 100644 --- a/src/microhttpd/connection.c +++ b/src/microhttpd/connection.c | |||
@@ -1019,8 +1019,8 @@ build_header_response (struct MHD_Connection *connection) | |||
1019 | const char *reason_phrase; | 1019 | const char *reason_phrase; |
1020 | uint32_t rc; | 1020 | uint32_t rc; |
1021 | const char *client_requested_close; | 1021 | const char *client_requested_close; |
1022 | const char *response_has_close; | 1022 | bool response_has_close; |
1023 | const char *response_has_keepalive; | 1023 | bool response_has_keepalive; |
1024 | const char *have_encoding; | 1024 | const char *have_encoding; |
1025 | const char *have_content_length; | 1025 | const char *have_content_length; |
1026 | int must_add_close; | 1026 | int must_add_close; |
@@ -1082,21 +1082,17 @@ build_header_response (struct MHD_Connection *connection) | |||
1082 | must_add_chunked_encoding = MHD_NO; | 1082 | must_add_chunked_encoding = MHD_NO; |
1083 | must_add_keep_alive = MHD_NO; | 1083 | must_add_keep_alive = MHD_NO; |
1084 | must_add_content_length = MHD_NO; | 1084 | must_add_content_length = MHD_NO; |
1085 | response_has_keepalive = NULL; | 1085 | response_has_close = false; |
1086 | response_has_keepalive = false; | ||
1086 | switch (connection->state) | 1087 | switch (connection->state) |
1087 | { | 1088 | { |
1088 | case MHD_CONNECTION_FOOTERS_RECEIVED: | 1089 | case MHD_CONNECTION_FOOTERS_RECEIVED: |
1089 | response_has_close = MHD_get_response_header (connection->response, | 1090 | response_has_close = MHD_check_response_header_s_token_ci (connection->response, |
1090 | MHD_HTTP_HEADER_CONNECTION); | 1091 | MHD_HTTP_HEADER_CONNECTION, |
1091 | response_has_keepalive = response_has_close; | 1092 | "close"); |
1092 | if ( (NULL != response_has_close) && | 1093 | response_has_keepalive = MHD_check_response_header_s_token_ci (connection->response, |
1093 | (! MHD_str_equal_caseless_ (response_has_close, | 1094 | MHD_HTTP_HEADER_CONNECTION, |
1094 | "close")) ) | 1095 | "Keep-Alive"); |
1095 | response_has_close = NULL; | ||
1096 | if ( (NULL != response_has_keepalive) && | ||
1097 | (! MHD_str_equal_caseless_ (response_has_keepalive, | ||
1098 | "Keep-Alive")) ) | ||
1099 | response_has_keepalive = NULL; | ||
1100 | client_requested_close = MHD_lookup_connection_value (connection, | 1096 | client_requested_close = MHD_lookup_connection_value (connection, |
1101 | MHD_HEADER_KIND, | 1097 | MHD_HEADER_KIND, |
1102 | MHD_HTTP_HEADER_CONNECTION); | 1098 | MHD_HTTP_HEADER_CONNECTION); |
@@ -1112,7 +1108,7 @@ build_header_response (struct MHD_Connection *connection) | |||
1112 | connection->have_chunked_upload = false; | 1108 | connection->have_chunked_upload = false; |
1113 | 1109 | ||
1114 | if ( (MHD_SIZE_UNKNOWN == connection->response->total_size) && | 1110 | if ( (MHD_SIZE_UNKNOWN == connection->response->total_size) && |
1115 | (NULL == response_has_close) && | 1111 | (! response_has_close) && |
1116 | (NULL == client_requested_close) ) | 1112 | (NULL == client_requested_close) ) |
1117 | { | 1113 | { |
1118 | /* size is unknown, and close was not explicitly requested; | 1114 | /* size is unknown, and close was not explicitly requested; |
@@ -1146,7 +1142,7 @@ build_header_response (struct MHD_Connection *connection) | |||
1146 | { | 1142 | { |
1147 | /* Keep alive or chunking not possible | 1143 | /* Keep alive or chunking not possible |
1148 | => set close header if not present */ | 1144 | => set close header if not present */ |
1149 | if (NULL == response_has_close) | 1145 | if (! response_has_close) |
1150 | must_add_close = MHD_YES; | 1146 | must_add_close = MHD_YES; |
1151 | } | 1147 | } |
1152 | } | 1148 | } |
@@ -1155,7 +1151,7 @@ build_header_response (struct MHD_Connection *connection) | |||
1155 | if ( ( (NULL != client_requested_close) || | 1151 | if ( ( (NULL != client_requested_close) || |
1156 | (connection->read_closed) || | 1152 | (connection->read_closed) || |
1157 | (MHD_CONN_MUST_CLOSE == connection->keepalive)) && | 1153 | (MHD_CONN_MUST_CLOSE == connection->keepalive)) && |
1158 | (NULL == response_has_close) && | 1154 | (! response_has_close) && |
1159 | (0 == (connection->response->flags & MHD_RF_HTTP_VERSION_1_0_ONLY) ) ) | 1155 | (0 == (connection->response->flags & MHD_RF_HTTP_VERSION_1_0_ONLY) ) ) |
1160 | must_add_close = MHD_YES; | 1156 | must_add_close = MHD_YES; |
1161 | 1157 | ||
@@ -1199,15 +1195,15 @@ build_header_response (struct MHD_Connection *connection) | |||
1199 | } | 1195 | } |
1200 | 1196 | ||
1201 | /* check for adding keep alive */ | 1197 | /* check for adding keep alive */ |
1202 | if ( (NULL == response_has_keepalive) && | 1198 | if ( (! response_has_keepalive) && |
1203 | (NULL == response_has_close) && | 1199 | (! response_has_close) && |
1204 | (MHD_NO == must_add_close) && | 1200 | (MHD_NO == must_add_close) && |
1205 | (MHD_CONN_MUST_CLOSE != connection->keepalive) && | 1201 | (MHD_CONN_MUST_CLOSE != connection->keepalive) && |
1206 | (MHD_YES == keepalive_possible (connection)) ) | 1202 | (MHD_YES == keepalive_possible (connection)) ) |
1207 | must_add_keep_alive = MHD_YES; | 1203 | must_add_keep_alive = MHD_YES; |
1208 | break; | 1204 | break; |
1209 | case MHD_CONNECTION_BODY_SENT: | 1205 | case MHD_CONNECTION_BODY_SENT: |
1210 | response_has_keepalive = NULL; | 1206 | response_has_keepalive = false; |
1211 | break; | 1207 | break; |
1212 | default: | 1208 | default: |
1213 | EXTRA_CHECK (0); | 1209 | EXTRA_CHECK (0); |
@@ -1215,9 +1211,9 @@ build_header_response (struct MHD_Connection *connection) | |||
1215 | 1211 | ||
1216 | if (MHD_CONN_MUST_CLOSE != connection->keepalive) | 1212 | if (MHD_CONN_MUST_CLOSE != connection->keepalive) |
1217 | { | 1213 | { |
1218 | if ( (must_add_close) || (NULL != response_has_close) ) | 1214 | if ( (must_add_close) || (response_has_close) ) |
1219 | connection->keepalive = MHD_CONN_MUST_CLOSE; | 1215 | connection->keepalive = MHD_CONN_MUST_CLOSE; |
1220 | else if ( (must_add_keep_alive) || (NULL != response_has_keepalive) ) | 1216 | else if ( (must_add_keep_alive) || (response_has_keepalive) ) |
1221 | connection->keepalive = MHD_CONN_USE_KEEPALIVE; | 1217 | connection->keepalive = MHD_CONN_USE_KEEPALIVE; |
1222 | } | 1218 | } |
1223 | 1219 | ||
@@ -1233,12 +1229,17 @@ build_header_response (struct MHD_Connection *connection) | |||
1233 | EXTRA_CHECK (! (must_add_chunked_encoding && must_add_content_length) ); | 1229 | EXTRA_CHECK (! (must_add_chunked_encoding && must_add_content_length) ); |
1234 | 1230 | ||
1235 | for (pos = connection->response->first_header; NULL != pos; pos = pos->next) | 1231 | for (pos = connection->response->first_header; NULL != pos; pos = pos->next) |
1236 | if ( (pos->kind == kind) && | 1232 | { |
1237 | (! ( (MHD_YES == must_add_close) && | 1233 | /* TODO: add proper support for excluding "Keep-Alive" token. */ |
1238 | (pos->value == response_has_keepalive) && | 1234 | if ( (pos->kind == kind) && |
1239 | (MHD_str_equal_caseless_(pos->header, | 1235 | (! ( (MHD_YES == must_add_close) && |
1240 | MHD_HTTP_HEADER_CONNECTION) ) ) ) ) | 1236 | (response_has_keepalive) && |
1241 | size += strlen (pos->header) + strlen (pos->value) + 4; /* colon, space, linefeeds */ | 1237 | (MHD_str_equal_caseless_(pos->header, |
1238 | MHD_HTTP_HEADER_CONNECTION)) && | ||
1239 | (MHD_str_equal_caseless_(pos->value, | ||
1240 | "Keep-Alive")) ) ) ) | ||
1241 | size += strlen (pos->header) + strlen (pos->value) + 4; /* colon, space, linefeeds */ | ||
1242 | } | ||
1242 | /* produce data */ | 1243 | /* produce data */ |
1243 | data = MHD_pool_allocate (connection->pool, | 1244 | data = MHD_pool_allocate (connection->pool, |
1244 | size + 1, | 1245 | size + 1, |
@@ -1290,16 +1291,21 @@ build_header_response (struct MHD_Connection *connection) | |||
1290 | off += content_length_len; | 1291 | off += content_length_len; |
1291 | } | 1292 | } |
1292 | for (pos = connection->response->first_header; NULL != pos; pos = pos->next) | 1293 | for (pos = connection->response->first_header; NULL != pos; pos = pos->next) |
1293 | if ( (pos->kind == kind) && | 1294 | { |
1294 | (! ( (pos->value == response_has_keepalive) && | 1295 | /* TODO: add proper support for excluding "Keep-Alive" token. */ |
1295 | (MHD_YES == must_add_close) && | 1296 | if ( (pos->kind == kind) && |
1296 | (MHD_str_equal_caseless_(pos->header, | 1297 | (! ( (MHD_YES == must_add_close) && |
1297 | MHD_HTTP_HEADER_CONNECTION) ) ) ) ) | 1298 | (response_has_keepalive) && |
1298 | off += MHD_snprintf_ (&data[off], | 1299 | (MHD_str_equal_caseless_(pos->header, |
1299 | size - off, | 1300 | MHD_HTTP_HEADER_CONNECTION)) && |
1300 | "%s: %s\r\n", | 1301 | (MHD_str_equal_caseless_(pos->value, |
1301 | pos->header, | 1302 | "Keep-Alive")) ) ) ) |
1302 | pos->value); | 1303 | off += MHD_snprintf_ (&data[off], |
1304 | size - off, | ||
1305 | "%s: %s\r\n", | ||
1306 | pos->header, | ||
1307 | pos->value); | ||
1308 | } | ||
1303 | if (MHD_CONNECTION_FOOTERS_RECEIVED == connection->state) | 1309 | if (MHD_CONNECTION_FOOTERS_RECEIVED == connection->state) |
1304 | { | 1310 | { |
1305 | strcpy (&data[off], | 1311 | strcpy (&data[off], |