aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/connection.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/connection.c')
-rw-r--r--src/daemon/connection.c167
1 files changed, 81 insertions, 86 deletions
diff --git a/src/daemon/connection.c b/src/daemon/connection.c
index 9c37a5c0..c0e3f4b5 100644
--- a/src/daemon/connection.c
+++ b/src/daemon/connection.c
@@ -286,8 +286,9 @@ connection_close_error (struct MHD_Connection *connection)
286 connection->socket_fd = -1; 286 connection->socket_fd = -1;
287 connection->state = MHD_CONNECTION_CLOSED; 287 connection->state = MHD_CONNECTION_CLOSED;
288 if (connection->daemon->notify_completed != NULL) 288 if (connection->daemon->notify_completed != NULL)
289 connection->daemon->notify_completed (connection->daemon-> 289 connection->daemon->notify_completed (connection->
290 notify_completed_cls, connection, 290 daemon->notify_completed_cls,
291 connection,
291 &connection->client_context, 292 &connection->client_context,
292 MHD_REQUEST_TERMINATED_WITH_ERROR); 293 MHD_REQUEST_TERMINATED_WITH_ERROR);
293} 294}
@@ -316,9 +317,9 @@ try_ready_normal_body (struct MHD_Connection *connection)
316 MHD_MIN (response->data_buffer_size, 317 MHD_MIN (response->data_buffer_size,
317 response->total_size - 318 response->total_size -
318 connection->response_write_position)); 319 connection->response_write_position));
319 if ( (ret == 0) && 320 if ((ret == 0) &&
320 (0 != (connection->daemon->options & MHD_USE_SELECT_INTERNALLY)) ) 321 (0 != (connection->daemon->options & MHD_USE_SELECT_INTERNALLY)))
321 abort(); /* serious client API violation */ 322 abort (); /* serious client API violation */
322 if (ret == -1) 323 if (ret == -1)
323 { 324 {
324 /* either error or http 1.0 transfer, close 325 /* either error or http 1.0 transfer, close
@@ -405,7 +406,7 @@ try_ready_chunked_body (struct MHD_Connection *connection)
405 if (ret > 0xFFFFFF) 406 if (ret > 0xFFFFFF)
406 ret = 0xFFFFFF; 407 ret = 0xFFFFFF;
407 SPRINTF (cbuf, "%X\r\n", ret); 408 SPRINTF (cbuf, "%X\r\n", ret);
408 cblen = strlen(cbuf); 409 cblen = strlen (cbuf);
409 EXTRA_CHECK (cblen <= sizeof (cbuf)); 410 EXTRA_CHECK (cblen <= sizeof (cbuf));
410 memcpy (&connection->write_buffer[sizeof (cbuf) - cblen], cbuf, cblen); 411 memcpy (&connection->write_buffer[sizeof (cbuf) - cblen], cbuf, cblen);
411 memcpy (&connection->write_buffer[sizeof (cbuf) + ret], "\r\n", 2); 412 memcpy (&connection->write_buffer[sizeof (cbuf) + ret], "\r\n", 2);
@@ -454,8 +455,7 @@ add_extra_headers (struct MHD_Connection *connection)
454 MHD_HTTP_HEADER_CONTENT_LENGTH)) 455 MHD_HTTP_HEADER_CONTENT_LENGTH))
455 { 456 {
456 SPRINTF (buf, 457 SPRINTF (buf,
457 "%llu", 458 "%llu", (unsigned long long) connection->response->total_size);
458 (unsigned long long) connection->response->total_size);
459 MHD_add_response_header (connection->response, 459 MHD_add_response_header (connection->response,
460 MHD_HTTP_HEADER_CONTENT_LENGTH, buf); 460 MHD_HTTP_HEADER_CONTENT_LENGTH, buf);
461 } 461 }
@@ -482,11 +482,11 @@ get_date_string (char *date)
482 time (&t); 482 time (&t);
483 gmtime_r (&t, &now); 483 gmtime_r (&t, &now);
484 SPRINTF (date, 484 SPRINTF (date,
485 "Date: %3s, %02u %3s %04u %02u:%02u:%02u GMT\r\n", 485 "Date: %3s, %02u %3s %04u %02u:%02u:%02u GMT\r\n",
486 days[now.tm_wday % 7], 486 days[now.tm_wday % 7],
487 now.tm_mday, 487 now.tm_mday,
488 mons[now.tm_mon % 12], 488 mons[now.tm_mon % 12],
489 1900 + now.tm_year, now.tm_hour, now.tm_min, now.tm_sec); 489 1900 + now.tm_year, now.tm_hour, now.tm_min, now.tm_sec);
490} 490}
491 491
492/** 492/**
@@ -536,10 +536,8 @@ build_header_response (struct MHD_Connection *connection)
536 add_extra_headers (connection); 536 add_extra_headers (connection);
537 reason_phrase = MHD_get_reason_phrase_for (connection->responseCode); 537 reason_phrase = MHD_get_reason_phrase_for (connection->responseCode);
538 SPRINTF (code, 538 SPRINTF (code,
539 "%s %u %s\r\n", 539 "%s %u %s\r\n",
540 MHD_HTTP_VERSION_1_1, 540 MHD_HTTP_VERSION_1_1, connection->responseCode, reason_phrase);
541 connection->responseCode,
542 reason_phrase);
543 off = strlen (code); 541 off = strlen (code);
544 /* estimate size */ 542 /* estimate size */
545 size = off + 2; /* extra \r\n at the end */ 543 size = off + 2; /* extra \r\n at the end */
@@ -1033,7 +1031,7 @@ parse_initial_message_line (struct MHD_Connection *connection, char *line)
1033 args++; 1031 args++;
1034 parse_arguments (MHD_GET_ARGUMENT_KIND, connection, args); 1032 parse_arguments (MHD_GET_ARGUMENT_KIND, connection, args);
1035 } 1033 }
1036 MHD_http_unescape(uri); 1034 MHD_http_unescape (uri);
1037 connection->url = uri; 1035 connection->url = uri;
1038 if (httpVersion == NULL) 1036 if (httpVersion == NULL)
1039 connection->version = ""; 1037 connection->version = "";
@@ -1056,7 +1054,7 @@ call_connection_handler (struct MHD_Connection *connection)
1056 int instant_retry; 1054 int instant_retry;
1057 unsigned int i; 1055 unsigned int i;
1058 int malformed; 1056 int malformed;
1059 char * buffer_head; 1057 char *buffer_head;
1060 1058
1061 if (connection->response != NULL) 1059 if (connection->response != NULL)
1062 return; /* already queued a response */ 1060 return; /* already queued a response */
@@ -1069,17 +1067,15 @@ call_connection_handler (struct MHD_Connection *connection)
1069 if ((connection->have_chunked_upload == MHD_YES) && 1067 if ((connection->have_chunked_upload == MHD_YES) &&
1070 (connection->remaining_upload_size == -1)) 1068 (connection->remaining_upload_size == -1))
1071 { 1069 {
1072 if ((connection->current_chunk_offset == connection->current_chunk_size) && 1070 if ((connection->current_chunk_offset ==
1073 (connection->current_chunk_offset != 0) && 1071 connection->current_chunk_size)
1074 (available >= 2)) 1072 && (connection->current_chunk_offset != 0) && (available >= 2))
1075 { 1073 {
1076 /* skip new line at the *end* of a chunk */ 1074 /* skip new line at the *end* of a chunk */
1077 i = 0; 1075 i = 0;
1078 if ((buffer_head[i] == '\r') || 1076 if ((buffer_head[i] == '\r') || (buffer_head[i] == '\n'))
1079 (buffer_head[i] == '\n'))
1080 i++; /* skip 1st part of line feed */ 1077 i++; /* skip 1st part of line feed */
1081 if ((buffer_head[i] == '\r') || 1078 if ((buffer_head[i] == '\r') || (buffer_head[i] == '\n'))
1082 (buffer_head[i] == '\n'))
1083 i++; /* skip 2nd part of line feed */ 1079 i++; /* skip 2nd part of line feed */
1084 if (i == 0) 1080 if (i == 0)
1085 { 1081 {
@@ -1092,7 +1088,7 @@ call_connection_handler (struct MHD_Connection *connection)
1092 return; 1088 return;
1093 } 1089 }
1094 available -= i; 1090 available -= i;
1095 buffer_head += i; 1091 buffer_head += i;
1096 connection->current_chunk_offset = 0; 1092 connection->current_chunk_offset = 0;
1097 connection->current_chunk_size = 0; 1093 connection->current_chunk_size = 0;
1098 } 1094 }
@@ -1116,31 +1112,28 @@ call_connection_handler (struct MHD_Connection *connection)
1116 i = 0; 1112 i = 0;
1117 while (i < available) 1113 while (i < available)
1118 { 1114 {
1119 if ((buffer_head[i] == '\r') || 1115 if ((buffer_head[i] == '\r') || (buffer_head[i] == '\n'))
1120 (buffer_head[i] == '\n'))
1121 break; 1116 break;
1122 i++; 1117 i++;
1123 if (i >= 6) 1118 if (i >= 6)
1124 break; 1119 break;
1125 } 1120 }
1126 /* take '\n' into account; if '\n' 1121 /* take '\n' into account; if '\n'
1127 is the unavailable character, we 1122 is the unavailable character, we
1128 will need to wait until we have it 1123 will need to wait until we have it
1129 before going further */ 1124 before going further */
1130 if ( (i+1 >= available) && 1125 if ((i + 1 >= available) &&
1131 ! ( (i == 1) && 1126 !((i == 1) && (available == 2) && (buffer_head[0] == '0')))
1132 (available == 2) && 1127 break; /* need more data... */
1133 (buffer_head[0] == '0') ) )
1134 break; /* need more data... */
1135 malformed = (i >= 6); 1128 malformed = (i >= 6);
1136 if (!malformed) 1129 if (!malformed)
1137 { 1130 {
1138 buffer_head[i] = '\0'; 1131 buffer_head[i] = '\0';
1139 malformed = 1132 malformed =
1140 (1 != SSCANF (buffer_head, "%X", 1133 (1 != SSCANF (buffer_head, "%X",
1141 &connection->current_chunk_size)) && 1134 &connection->current_chunk_size)) &&
1142 (1 != SSCANF (buffer_head, "%x", 1135 (1 != SSCANF (buffer_head, "%x",
1143 &connection->current_chunk_size)); 1136 &connection->current_chunk_size));
1144 } 1137 }
1145 if (malformed) 1138 if (malformed)
1146 { 1139 {
@@ -1153,17 +1146,16 @@ call_connection_handler (struct MHD_Connection *connection)
1153 return; 1146 return;
1154 } 1147 }
1155 i++; 1148 i++;
1156 if ((i<available) && 1149 if ((i < available) &&
1157 ( (buffer_head[i] == '\r') || 1150 ((buffer_head[i] == '\r') || (buffer_head[i] == '\n')))
1158 (buffer_head[i] == '\n')) )
1159 i++; /* skip 2nd part of line feed */ 1151 i++; /* skip 2nd part of line feed */
1160 1152
1161 buffer_head += i; 1153 buffer_head += i;
1162 available -= i; 1154 available -= i;
1163 connection->current_chunk_offset = 0; 1155 connection->current_chunk_offset = 0;
1164 1156
1165 if (available > 0) 1157 if (available > 0)
1166 instant_retry = MHD_YES; 1158 instant_retry = MHD_YES;
1167 if (connection->current_chunk_size == 0) 1159 if (connection->current_chunk_size == 0)
1168 { 1160 {
1169 connection->remaining_upload_size = 0; 1161 connection->remaining_upload_size = 0;
@@ -1179,13 +1171,12 @@ call_connection_handler (struct MHD_Connection *connection)
1179 } 1171 }
1180 used = processed; 1172 used = processed;
1181 if (MHD_NO == 1173 if (MHD_NO ==
1182 connection->daemon->default_handler (connection->daemon-> 1174 connection->daemon->default_handler (connection->
1183 default_handler_cls, 1175 daemon->default_handler_cls,
1184 connection, connection->url, 1176 connection, connection->url,
1185 connection->method, 1177 connection->method,
1186 connection->version, 1178 connection->version,
1187 buffer_head, 1179 buffer_head, &processed,
1188 &processed,
1189 &connection->client_context)) 1180 &connection->client_context))
1190 { 1181 {
1191 /* serious internal error, close connection */ 1182 /* serious internal error, close connection */
@@ -1202,7 +1193,7 @@ call_connection_handler (struct MHD_Connection *connection)
1202 instant_retry = MHD_NO; /* client did not process everything */ 1193 instant_retry = MHD_NO; /* client did not process everything */
1203 used -= processed; 1194 used -= processed;
1204 if (connection->have_chunked_upload == MHD_YES) 1195 if (connection->have_chunked_upload == MHD_YES)
1205 connection->current_chunk_offset += used; 1196 connection->current_chunk_offset += used;
1206 /* dh left "processed" bytes in buffer for next time... */ 1197 /* dh left "processed" bytes in buffer for next time... */
1207 buffer_head += used; 1198 buffer_head += used;
1208 available -= used; 1199 available -= used;
@@ -1211,9 +1202,7 @@ call_connection_handler (struct MHD_Connection *connection)
1211 } 1202 }
1212 while (instant_retry == MHD_YES); 1203 while (instant_retry == MHD_YES);
1213 if (available > 0) 1204 if (available > 0)
1214 memmove(connection->read_buffer, 1205 memmove (connection->read_buffer, buffer_head, available);
1215 buffer_head,
1216 available);
1217 connection->read_buffer_offset = available; 1206 connection->read_buffer_offset = available;
1218} 1207}
1219 1208
@@ -1234,8 +1223,10 @@ do_read (struct MHD_Connection *connection)
1234 return MHD_NO; 1223 return MHD_NO;
1235 1224
1236 bytes_read = connection->recv_cls (connection, 1225 bytes_read = connection->recv_cls (connection,
1237 &connection->read_buffer[connection->read_buffer_offset], 1226 &connection->read_buffer[connection->
1238 connection->read_buffer_size - connection->read_buffer_offset); 1227 read_buffer_offset],
1228 connection->read_buffer_size -
1229 connection->read_buffer_offset);
1239 if (bytes_read < 0) 1230 if (bytes_read < 0)
1240 { 1231 {
1241 if (errno == EINTR) 1232 if (errno == EINTR)
@@ -1271,10 +1262,10 @@ do_write (struct MHD_Connection *connection)
1271 int ret; 1262 int ret;
1272 1263
1273 ret = connection->send_cls (connection, 1264 ret = connection->send_cls (connection,
1274 &connection->write_buffer 1265 &connection->write_buffer
1275 [connection->write_buffer_send_offset], 1266 [connection->write_buffer_send_offset],
1276 connection->write_buffer_append_offset 1267 connection->write_buffer_append_offset
1277 - connection->write_buffer_send_offset); 1268 - connection->write_buffer_send_offset);
1278 1269
1279 if (ret < 0) 1270 if (ret < 0)
1280 { 1271 {
@@ -1581,10 +1572,12 @@ MHD_connection_handle_write (struct MHD_Connection *connection)
1581 break; 1572 break;
1582 case MHD_CONNECTION_CONTINUE_SENDING: 1573 case MHD_CONNECTION_CONTINUE_SENDING:
1583 ret = connection->send_cls (connection, 1574 ret = connection->send_cls (connection,
1584 &HTTP_100_CONTINUE 1575 &HTTP_100_CONTINUE
1585 [connection->continue_message_write_offset], 1576 [connection->
1586 strlen (HTTP_100_CONTINUE) - 1577 continue_message_write_offset],
1587 connection->continue_message_write_offset); 1578 strlen (HTTP_100_CONTINUE) -
1579 connection->
1580 continue_message_write_offset);
1588 if (ret < 0) 1581 if (ret < 0)
1589 { 1582 {
1590 if (errno == EINTR) 1583 if (errno == EINTR)
@@ -1633,24 +1626,26 @@ MHD_connection_handle_write (struct MHD_Connection *connection)
1633 if (connection->daemon->options & MHD_USE_SSL) 1626 if (connection->daemon->options & MHD_USE_SSL)
1634 { 1627 {
1635 ret = MHD__gnutls_record_send (connection->tls_session, 1628 ret = MHD__gnutls_record_send (connection->tls_session,
1636 &connection->response->data 1629 &connection->response->data
1637 [connection-> 1630 [connection->response_write_position
1638 response_write_position - 1631 - response->data_start],
1639 response->data_start], 1632 response->data_size -
1640 response->data_size - 1633 (connection->
1641 (connection->response_write_position 1634 response_write_position -
1642 - response->data_start)); 1635 response->data_start));
1643 } 1636 }
1644 else 1637 else
1645#endif 1638#endif
1646 { 1639 {
1647 ret = connection->send_cls (connection, 1640 ret = connection->send_cls (connection,
1648 &response->data[connection-> 1641 &response->
1649 response_write_position - 1642 data
1650 response->data_start], 1643 [connection->response_write_position
1651 response->data_size - 1644 - response->data_start],
1652 (connection->response_write_position - 1645 response->data_size -
1653 response->data_start)); 1646 (connection->
1647 response_write_position -
1648 response->data_start));
1654 } 1649 }
1655#if DEBUG_SEND_DATA 1650#if DEBUG_SEND_DATA
1656 if (ret > 0) 1651 if (ret > 0)
@@ -1705,11 +1700,11 @@ MHD_connection_handle_write (struct MHD_Connection *connection)
1705 connection_close_error (connection); 1700 connection_close_error (connection);
1706 return MHD_NO; 1701 return MHD_NO;
1707 case MHD_TLS_CONNECTION_INIT: 1702 case MHD_TLS_CONNECTION_INIT:
1708 case MHD_TLS_HELLO_REQUEST: 1703 case MHD_TLS_HELLO_REQUEST:
1709 case MHD_TLS_HANDSHAKE_FAILED: 1704 case MHD_TLS_HANDSHAKE_FAILED:
1710 case MHD_TLS_HANDSHAKE_COMPLETE: 1705 case MHD_TLS_HANDSHAKE_COMPLETE:
1711 EXTRA_CHECK(0); 1706 EXTRA_CHECK (0);
1712 break; 1707 break;
1713 } 1708 }
1714 break; 1709 break;
1715 } 1710 }
@@ -1983,8 +1978,8 @@ MHD_connection_handle_idle (struct MHD_Connection *connection)
1983 case MHD_CONNECTION_FOOTERS_SENT: 1978 case MHD_CONNECTION_FOOTERS_SENT:
1984 MHD_destroy_response (connection->response); 1979 MHD_destroy_response (connection->response);
1985 if (connection->daemon->notify_completed != NULL) 1980 if (connection->daemon->notify_completed != NULL)
1986 connection->daemon->notify_completed (connection->daemon-> 1981 connection->daemon->notify_completed (connection->
1987 notify_completed_cls, 1982 daemon->notify_completed_cls,
1988 connection, 1983 connection,
1989 &connection->client_context, 1984 &connection->client_context,
1990 MHD_REQUEST_TERMINATED_COMPLETED_OK); 1985 MHD_REQUEST_TERMINATED_COMPLETED_OK);