@@ -1922,6 +1922,12 @@ class ClientImpl {
19221922 std::chrono::time_point<std::chrono::steady_clock> start_time,
19231923 std::function<bool (Stream &strm)> callback);
19241924 virtual bool is_ssl () const ;
1925+
1926+ // Helper: transfer the current `socket_` ownership into a StreamHandle's
1927+ // ClientConnection. This moves `socket_.sock`/`socket_.ssl` into
1928+ // `handle.connection_` and resets `socket_` to closed. This method MUST be
1929+ // called with `socket_mutex_` held.
1930+ void transfer_socket_ownership_to_handle (StreamHandle &handle);
19251931};
19261932
19271933class Client {
@@ -9716,13 +9722,8 @@ ClientImpl::open_stream(const std::string &method, const std::string &path,
97169722#endif
97179723 }
97189724
9719- // Transfer socket ownership to StreamHandle
9720- handle.connection_ ->sock = socket_.sock ;
9721- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
9722- handle.connection_ ->ssl = socket_.ssl ;
9723- socket_.ssl = nullptr ;
9724- #endif
9725- socket_.sock = INVALID_SOCKET;
9725+ // Transfer socket ownership to StreamHandle (centralized helper)
9726+ transfer_socket_ownership_to_handle (handle);
97269727 }
97279728
97289729 // Create appropriate stream for the transferred socket
@@ -9808,6 +9809,17 @@ ClientImpl::open_stream(const std::string &method, const std::string &path,
98089809 return handle;
98099810}
98109811
9812+ inline void
9813+ ClientImpl::transfer_socket_ownership_to_handle (StreamHandle &handle) {
9814+ // Caller must hold socket_mutex_. Move socket_ contents into handle.
9815+ handle.connection_ ->sock = socket_.sock ;
9816+ #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
9817+ handle.connection_ ->ssl = socket_.ssl ;
9818+ socket_.ssl = nullptr ;
9819+ #endif
9820+ socket_.sock = INVALID_SOCKET;
9821+ }
9822+
98119823inline bool ClientImpl::handle_request (Stream &strm, Request &req,
98129824 Response &res, bool close_connection,
98139825 Error &error) {
0 commit comments