aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/connection.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/connection.c')
-rw-r--r--src/microhttpd/connection.c44
1 files changed, 42 insertions, 2 deletions
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index cd4e8257..542c5a40 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -32,7 +32,9 @@
32#include "response.h" 32#include "response.h"
33#include "mhd_mono_clock.h" 33#include "mhd_mono_clock.h"
34#include "mhd_str.h" 34#include "mhd_str.h"
35#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
35#include "mhd_locks.h" 36#include "mhd_locks.h"
37#endif
36#include "mhd_sockets.h" 38#include "mhd_sockets.h"
37#include "mhd_compat.h" 39#include "mhd_compat.h"
38#include "mhd_itc.h" 40#include "mhd_itc.h"
@@ -1104,7 +1106,9 @@ try_ready_normal_body (struct MHD_Connection *connection)
1104 { 1106 {
1105 /* either error or http 1.0 transfer, close socket! */ 1107 /* either error or http 1.0 transfer, close socket! */
1106 response->total_size = connection->response_write_position; 1108 response->total_size = connection->response_write_position;
1109#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1107 MHD_mutex_unlock_chk_ (&response->mutex); 1110 MHD_mutex_unlock_chk_ (&response->mutex);
1111#endif
1108 if ( ((ssize_t)MHD_CONTENT_READER_END_OF_STREAM) == ret) 1112 if ( ((ssize_t)MHD_CONTENT_READER_END_OF_STREAM) == ret)
1109 MHD_connection_close_ (connection, 1113 MHD_connection_close_ (connection,
1110 MHD_REQUEST_TERMINATED_COMPLETED_OK); 1114 MHD_REQUEST_TERMINATED_COMPLETED_OK);
@@ -1118,7 +1122,9 @@ try_ready_normal_body (struct MHD_Connection *connection)
1118 if (0 == ret) 1122 if (0 == ret)
1119 { 1123 {
1120 connection->state = MHD_CONNECTION_NORMAL_BODY_UNREADY; 1124 connection->state = MHD_CONNECTION_NORMAL_BODY_UNREADY;
1125#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1121 MHD_mutex_unlock_chk_ (&response->mutex); 1126 MHD_mutex_unlock_chk_ (&response->mutex);
1127#endif
1122 return MHD_NO; 1128 return MHD_NO;
1123 } 1129 }
1124 return MHD_YES; 1130 return MHD_YES;
@@ -1156,7 +1162,9 @@ try_ready_chunked_body (struct MHD_Connection *connection)
1156 size /= 2; 1162 size /= 2;
1157 if (size < 128) 1163 if (size < 128)
1158 { 1164 {
1165#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1159 MHD_mutex_unlock_chk_ (&response->mutex); 1166 MHD_mutex_unlock_chk_ (&response->mutex);
1167#endif
1160 /* not enough memory */ 1168 /* not enough memory */
1161 CONNECTION_CLOSE_ERROR (connection, 1169 CONNECTION_CLOSE_ERROR (connection,
1162 _("Closing connection (out of memory)\n")); 1170 _("Closing connection (out of memory)\n"));
@@ -1202,7 +1210,9 @@ try_ready_chunked_body (struct MHD_Connection *connection)
1202 { 1210 {
1203 /* error, close socket! */ 1211 /* error, close socket! */
1204 response->total_size = connection->response_write_position; 1212 response->total_size = connection->response_write_position;
1213#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1205 MHD_mutex_unlock_chk_ (&response->mutex); 1214 MHD_mutex_unlock_chk_ (&response->mutex);
1215#endif
1206 CONNECTION_CLOSE_ERROR (connection, 1216 CONNECTION_CLOSE_ERROR (connection,
1207 _("Closing connection (application error generating response)\n")); 1217 _("Closing connection (application error generating response)\n"));
1208 return MHD_NO; 1218 return MHD_NO;
@@ -1222,7 +1232,9 @@ try_ready_chunked_body (struct MHD_Connection *connection)
1222 if (0 == ret) 1232 if (0 == ret)
1223 { 1233 {
1224 connection->state = MHD_CONNECTION_CHUNKED_BODY_UNREADY; 1234 connection->state = MHD_CONNECTION_CHUNKED_BODY_UNREADY;
1235#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1225 MHD_mutex_unlock_chk_ (&response->mutex); 1236 MHD_mutex_unlock_chk_ (&response->mutex);
1237#endif
1226 return MHD_NO; 1238 return MHD_NO;
1227 } 1239 }
1228 if (ret > 0xFFFFFF) 1240 if (ret > 0xFFFFFF)
@@ -2861,8 +2873,9 @@ MHD_update_last_activity_ (struct MHD_Connection *connection)
2861 2873
2862 if (connection->connection_timeout != daemon->connection_timeout) 2874 if (connection->connection_timeout != daemon->connection_timeout)
2863 return; /* custom timeout, no need to move it in "normal" DLL */ 2875 return; /* custom timeout, no need to move it in "normal" DLL */
2864 2876#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
2865 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); 2877 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2878#endif
2866 /* move connection to head of timeout list (by remove + add operation) */ 2879 /* move connection to head of timeout list (by remove + add operation) */
2867 XDLL_remove (daemon->normal_timeout_head, 2880 XDLL_remove (daemon->normal_timeout_head,
2868 daemon->normal_timeout_tail, 2881 daemon->normal_timeout_tail,
@@ -2870,7 +2883,9 @@ MHD_update_last_activity_ (struct MHD_Connection *connection)
2870 XDLL_insert (daemon->normal_timeout_head, 2883 XDLL_insert (daemon->normal_timeout_head,
2871 daemon->normal_timeout_tail, 2884 daemon->normal_timeout_tail,
2872 connection); 2885 connection);
2886#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
2873 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex); 2887 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2888#endif
2874} 2889}
2875 2890
2876 2891
@@ -3088,8 +3103,10 @@ MHD_connection_handle_write (struct MHD_Connection *connection)
3088 { 3103 {
3089 uint64_t data_write_offset; 3104 uint64_t data_write_offset;
3090 3105
3106#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3091 if (NULL != response->crc) 3107 if (NULL != response->crc)
3092 MHD_mutex_lock_chk_ (&response->mutex); 3108 MHD_mutex_lock_chk_ (&response->mutex);
3109#endif
3093 if (MHD_YES != try_ready_normal_body (connection)) 3110 if (MHD_YES != try_ready_normal_body (connection))
3094 { 3111 {
3095 /* mutex was already unlocked by try_ready_normal_body */ 3112 /* mutex was already unlocked by try_ready_normal_body */
@@ -3124,8 +3141,10 @@ MHD_connection_handle_write (struct MHD_Connection *connection)
3124 response->data_start]); 3141 response->data_start]);
3125#endif 3142#endif
3126 } 3143 }
3144#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3127 if (NULL != response->crc) 3145 if (NULL != response->crc)
3128 MHD_mutex_unlock_chk_ (&response->mutex); 3146 MHD_mutex_unlock_chk_ (&response->mutex);
3147#endif
3129 if (ret < 0) 3148 if (ret < 0)
3130 { 3149 {
3131 if (MHD_ERR_AGAIN_ == ret) 3150 if (MHD_ERR_AGAIN_ == ret)
@@ -3239,7 +3258,9 @@ cleanup_connection (struct MHD_Connection *connection)
3239 MHD_destroy_response (connection->response); 3258 MHD_destroy_response (connection->response);
3240 connection->response = NULL; 3259 connection->response = NULL;
3241 } 3260 }
3261#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3242 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); 3262 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
3263#endif
3243 if (connection->suspended) 3264 if (connection->suspended)
3244 { 3265 {
3245 DLL_remove (daemon->suspended_connections_head, 3266 DLL_remove (daemon->suspended_connections_head,
@@ -3269,7 +3290,9 @@ cleanup_connection (struct MHD_Connection *connection)
3269 connection); 3290 connection);
3270 connection->resuming = false; 3291 connection->resuming = false;
3271 connection->in_idle = false; 3292 connection->in_idle = false;
3293#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3272 MHD_mutex_unlock_chk_(&daemon->cleanup_connection_mutex); 3294 MHD_mutex_unlock_chk_(&daemon->cleanup_connection_mutex);
3295#endif
3273 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 3296 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
3274 { 3297 {
3275 /* if we were at the connection limit before and are in 3298 /* if we were at the connection limit before and are in
@@ -3610,19 +3633,25 @@ MHD_connection_handle_idle (struct MHD_Connection *connection)
3610 /* nothing to do here */ 3633 /* nothing to do here */
3611 break; 3634 break;
3612 case MHD_CONNECTION_NORMAL_BODY_UNREADY: 3635 case MHD_CONNECTION_NORMAL_BODY_UNREADY:
3636#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3613 if (NULL != connection->response->crc) 3637 if (NULL != connection->response->crc)
3614 MHD_mutex_lock_chk_ (&connection->response->mutex); 3638 MHD_mutex_lock_chk_ (&connection->response->mutex);
3639#endif
3615 if (0 == connection->response->total_size) 3640 if (0 == connection->response->total_size)
3616 { 3641 {
3642#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3617 if (NULL != connection->response->crc) 3643 if (NULL != connection->response->crc)
3618 MHD_mutex_unlock_chk_ (&connection->response->mutex); 3644 MHD_mutex_unlock_chk_ (&connection->response->mutex);
3645#endif
3619 connection->state = MHD_CONNECTION_BODY_SENT; 3646 connection->state = MHD_CONNECTION_BODY_SENT;
3620 continue; 3647 continue;
3621 } 3648 }
3622 if (MHD_YES == try_ready_normal_body (connection)) 3649 if (MHD_YES == try_ready_normal_body (connection))
3623 { 3650 {
3651#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3624 if (NULL != connection->response->crc) 3652 if (NULL != connection->response->crc)
3625 MHD_mutex_unlock_chk_ (&connection->response->mutex); 3653 MHD_mutex_unlock_chk_ (&connection->response->mutex);
3654#endif
3626 connection->state = MHD_CONNECTION_NORMAL_BODY_READY; 3655 connection->state = MHD_CONNECTION_NORMAL_BODY_READY;
3627 /* Buffering for flushable socket was already enabled*/ 3656 /* Buffering for flushable socket was already enabled*/
3628 if (MHD_NO == socket_flush_possible (connection)) 3657 if (MHD_NO == socket_flush_possible (connection))
@@ -3636,21 +3665,27 @@ MHD_connection_handle_idle (struct MHD_Connection *connection)
3636 /* nothing to do here */ 3665 /* nothing to do here */
3637 break; 3666 break;
3638 case MHD_CONNECTION_CHUNKED_BODY_UNREADY: 3667 case MHD_CONNECTION_CHUNKED_BODY_UNREADY:
3668#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3639 if (NULL != connection->response->crc) 3669 if (NULL != connection->response->crc)
3640 MHD_mutex_lock_chk_ (&connection->response->mutex); 3670 MHD_mutex_lock_chk_ (&connection->response->mutex);
3671#endif
3641 if ( (0 == connection->response->total_size) || 3672 if ( (0 == connection->response->total_size) ||
3642 (connection->response_write_position == 3673 (connection->response_write_position ==
3643 connection->response->total_size) ) 3674 connection->response->total_size) )
3644 { 3675 {
3676#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3645 if (NULL != connection->response->crc) 3677 if (NULL != connection->response->crc)
3646 MHD_mutex_unlock_chk_ (&connection->response->mutex); 3678 MHD_mutex_unlock_chk_ (&connection->response->mutex);
3679#endif
3647 connection->state = MHD_CONNECTION_BODY_SENT; 3680 connection->state = MHD_CONNECTION_BODY_SENT;
3648 continue; 3681 continue;
3649 } 3682 }
3650 if (MHD_YES == try_ready_chunked_body (connection)) 3683 if (MHD_YES == try_ready_chunked_body (connection))
3651 { 3684 {
3685#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3652 if (NULL != connection->response->crc) 3686 if (NULL != connection->response->crc)
3653 MHD_mutex_unlock_chk_ (&connection->response->mutex); 3687 MHD_mutex_unlock_chk_ (&connection->response->mutex);
3688#endif
3654 connection->state = MHD_CONNECTION_CHUNKED_BODY_READY; 3689 connection->state = MHD_CONNECTION_CHUNKED_BODY_READY;
3655 /* Buffering for flushable socket was already enabled */ 3690 /* Buffering for flushable socket was already enabled */
3656 if (MHD_NO == socket_flush_possible (connection)) 3691 if (MHD_NO == socket_flush_possible (connection))
@@ -3937,8 +3972,9 @@ MHD_set_connection_option (struct MHD_Connection *connection,
3937 case MHD_CONNECTION_OPTION_TIMEOUT: 3972 case MHD_CONNECTION_OPTION_TIMEOUT:
3938 if (0 == connection->connection_timeout) 3973 if (0 == connection->connection_timeout)
3939 connection->last_activity = MHD_monotonic_sec_counter(); 3974 connection->last_activity = MHD_monotonic_sec_counter();
3940 3975#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3941 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); 3976 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
3977#endif
3942 if ( (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 3978 if ( (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
3943 (! connection->suspended) ) 3979 (! connection->suspended) )
3944 { 3980 {
@@ -3967,7 +4003,9 @@ MHD_set_connection_option (struct MHD_Connection *connection,
3967 daemon->manual_timeout_tail, 4003 daemon->manual_timeout_tail,
3968 connection); 4004 connection);
3969 } 4005 }
4006#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3970 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex); 4007 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
4008#endif
3971 return MHD_YES; 4009 return MHD_YES;
3972 default: 4010 default:
3973 return MHD_NO; 4011 return MHD_NO;
@@ -4005,6 +4043,7 @@ MHD_queue_response (struct MHD_Connection *connection,
4005 return MHD_YES; /* If daemon was shut down in parallel, 4043 return MHD_YES; /* If daemon was shut down in parallel,
4006 * response will be aborted now or on later stage. */ 4044 * response will be aborted now or on later stage. */
4007 4045
4046#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
4008 if ( (!connection->suspended) && 4047 if ( (!connection->suspended) &&
4009 (0 != (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) && 4048 (0 != (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) &&
4010 (!MHD_thread_ID_match_current_(connection->pid.ID)) ) 4049 (!MHD_thread_ID_match_current_(connection->pid.ID)) )
@@ -4015,6 +4054,7 @@ MHD_queue_response (struct MHD_Connection *connection,
4015#endif 4054#endif
4016 return MHD_NO; 4055 return MHD_NO;
4017 } 4056 }
4057#endif
4018#ifdef UPGRADE_SUPPORT 4058#ifdef UPGRADE_SUPPORT
4019 if ( (NULL != response->upgrade_handler) && 4059 if ( (NULL != response->upgrade_handler) &&
4020 (0 == (daemon->options & MHD_ALLOW_UPGRADE)) ) 4060 (0 == (daemon->options & MHD_ALLOW_UPGRADE)) )