class-curltransport.php
3 weeks ago
class-sockettransport.php
3 weeks ago
interface-transportinterface.php
3 weeks ago
class-curltransport.php
291 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\HttpClient\Transport; |
| 4 | |
| 5 | use WordPress\HttpClient\Client; |
| 6 | use WordPress\HttpClient\ClientState; |
| 7 | use WordPress\HttpClient\HttpClientException; |
| 8 | use WordPress\HttpClient\HttpError; |
| 9 | use WordPress\HttpClient\Request; |
| 10 | use WordPress\HttpClient\Response; |
| 11 | |
| 12 | /** |
| 13 | * An HTTP client using curl multihandle. |
| 14 | * |
| 15 | * @extends Client |
| 16 | */ |
| 17 | class CurlTransport implements TransportInterface { |
| 18 | |
| 19 | /** |
| 20 | * @var ClientState |
| 21 | */ |
| 22 | protected $state; |
| 23 | |
| 24 | /** |
| 25 | * @var \CurlMultiHandle cURL multi-handle managing parallel requests |
| 26 | */ |
| 27 | protected $multi_handle; |
| 28 | |
| 29 | /** |
| 30 | * @var array Map of cURL handle resource IDs to request IDs (for callbacks). |
| 31 | */ |
| 32 | protected $handle_map = array(); |
| 33 | |
| 34 | /** |
| 35 | * Initializes a new CurlClient with optional settings. |
| 36 | * |
| 37 | * @param ClientState $state The client state containing configuration. |
| 38 | */ |
| 39 | public function __construct( ClientState $state ) { |
| 40 | $this->state = $state; |
| 41 | |
| 42 | $this->multi_handle = curl_multi_init(); |
| 43 | curl_multi_setopt( $this->multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX ); |
| 44 | curl_multi_setopt( $this->multi_handle, CURLMOPT_MAX_TOTAL_CONNECTIONS, $this->state->concurrency ); |
| 45 | curl_multi_setopt( $this->multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, $this->state->concurrency ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Destructor to clean up the curl_multi handle. |
| 50 | */ |
| 51 | public function __destruct() { |
| 52 | if ( $this->multi_handle ) { |
| 53 | curl_multi_close( $this->multi_handle ); |
| 54 | $this->multi_handle = null; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public function event_loop_tick(): bool { |
| 59 | if ( 0 === count( $this->state->get_active_requests() ) ) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | $this->open_nonblocking_curl_handles( |
| 64 | $this->state->get_active_requests( array( Request::STATE_ENQUEUED ) ) |
| 65 | ); |
| 66 | |
| 67 | if ( 0 === count( $this->handle_map ) ) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | $this->poll_active_curl_requests(); |
| 72 | |
| 73 | foreach ( $this->state->get_active_requests( array( Request::STATE_RECEIVED ) ) as $request ) { |
| 74 | $this->mark_finished( $request ); |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | private function open_nonblocking_curl_handles( $requests ) { |
| 81 | foreach ( $requests as $request ) { |
| 82 | // Initialize and add the curl handle for this request. |
| 83 | $ch = $this->init_curl_handle( $request ); |
| 84 | /** @var \CurlHandle $ch */ |
| 85 | if ( ! $ch ) { |
| 86 | // If initialization fails, immediately mark this request as failed. |
| 87 | $this->set_error( $request, new HttpError( 'Failed to initialize cURL handle', $request ) ); |
| 88 | continue; |
| 89 | } |
| 90 | if ( 0 !== curl_multi_add_handle( $this->multi_handle, $ch ) ) { |
| 91 | $this->set_error( $request, new HttpError( 'Failed to add cURL handle to multi handle', $request ) ); |
| 92 | continue; |
| 93 | } |
| 94 | $this->state->connections[ $request->id ]->http_socket = $ch; |
| 95 | $this->handle_map[ (int) $ch ] = $request->id; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private function poll_active_curl_requests() { |
| 100 | $running = 0; |
| 101 | do { |
| 102 | $mrc = curl_multi_exec( $this->multi_handle, $running ); |
| 103 | } while ( CURLM_CALL_MULTI_PERFORM === $mrc ); |
| 104 | |
| 105 | // Handle any completed requests. |
| 106 | while ( $info = curl_multi_info_read( $this->multi_handle ) ) { |
| 107 | if ( CURLMSG_DONE === $info['msg'] ) { |
| 108 | $ch = $info['handle']; |
| 109 | $id = $this->handle_map[ (int) $ch ] ?? null; |
| 110 | if ( null === $id ) { |
| 111 | throw new HttpClientException( 'Received completion event for an unknown request ' . ( $ch ? (int) $ch : 'unknown' ) ); |
| 112 | } |
| 113 | $request = $this->state->get_request_by_id( $id ); |
| 114 | if ( CURLE_OK !== $info['result'] ) { |
| 115 | $this->set_error( $request, new HttpError( sprintf( 'cURL error %d: %s', $info['result'], curl_error( $ch ) ) ) ); |
| 116 | return; |
| 117 | } |
| 118 | if ( ! $request->response ) { |
| 119 | $this->set_error( $request, new HttpError( 'Connection closed while reading response headers.', $request ) ); |
| 120 | return; |
| 121 | } |
| 122 | if ( Request::STATE_FAILED === $request->state || Request::STATE_FINISHED === $request->state ) { |
| 123 | // We've already handled errors and successes. |
| 124 | continue; |
| 125 | } |
| 126 | // We'll mark it as finished in the event_loop_tick() method. |
| 127 | $request->state = Request::STATE_RECEIVED; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // @TODO: What kind of timeout should we use here? |
| 132 | curl_multi_select( $this->multi_handle, 0.05 ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Create and configure a curl handle for the given Request. |
| 137 | * |
| 138 | * @param Request $request The HTTP request to prepare. |
| 139 | * @return resource|false Returns the configured cURL handle, or false on failure. |
| 140 | */ |
| 141 | private function init_curl_handle( $request ) { |
| 142 | $ch = curl_init(); |
| 143 | if ( ! $ch ) { |
| 144 | throw new HttpClientException( 'Failed to initialize cURL handle' ); |
| 145 | } |
| 146 | // Basic curl settings for the request. |
| 147 | curl_setopt( $ch, CURLOPT_URL, $request->url ); |
| 148 | curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, false ); |
| 149 | // Redirects are handled in the Client. |
| 150 | curl_setopt( $ch, CURLOPT_MAXREDIRS, 0 ); |
| 151 | curl_setopt( $ch, CURLOPT_TIMEOUT_MS, $this->state->request_timeout_ms ); |
| 152 | curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false ); // use callbacks for data. |
| 153 | curl_setopt( $ch, CURLOPT_HEADER, false ); // headers via callback. |
| 154 | curl_setopt( $ch, CURLOPT_ENCODING, '' ); |
| 155 | // Set HTTP method and body if needed. |
| 156 | curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $request->method ); |
| 157 | if ( ! empty( $request->upload_body_stream ) ) { |
| 158 | curl_setopt( $ch, CURLOPT_UPLOAD, true ); |
| 159 | curl_setopt( |
| 160 | $ch, |
| 161 | CURLOPT_READFUNCTION, |
| 162 | function ( $ch, $fp, $length ) use ( $request ) { |
| 163 | $stream = $request->upload_body_stream; |
| 164 | // Pull at most $length bytes until we either get some bytes |
| 165 | // or we reach the end of the stream. |
| 166 | while ( ! $stream->reached_end_of_data() ) { |
| 167 | $got_bytes = $stream->pull( $length ); |
| 168 | if ( $got_bytes > 0 ) { |
| 169 | return $stream->consume( $got_bytes ); |
| 170 | } |
| 171 | } |
| 172 | return ''; |
| 173 | } |
| 174 | ); |
| 175 | } |
| 176 | // Set headers if provided. |
| 177 | if ( ! empty( $request->headers ) ) { |
| 178 | $header_lines = array(); |
| 179 | foreach ( $request->headers as $name => $value ) { |
| 180 | $header_lines[] = "{$name}: {$value}"; |
| 181 | if ( 'content-length' === $name && is_numeric( $value ) ) { |
| 182 | curl_setopt( $ch, CURLOPT_INFILESIZE, (int) $value ); |
| 183 | } |
| 184 | } |
| 185 | curl_setopt( $ch, CURLOPT_HTTPHEADER, $header_lines ); |
| 186 | } |
| 187 | // Set callback functions for data and headers. |
| 188 | curl_setopt( |
| 189 | $ch, |
| 190 | CURLOPT_WRITEFUNCTION, |
| 191 | function ( $ch, $data ) { |
| 192 | return $this->handle_body_data( $ch, $data ); |
| 193 | } |
| 194 | ); |
| 195 | curl_setopt( |
| 196 | $ch, |
| 197 | CURLOPT_HEADERFUNCTION, |
| 198 | function ( $ch, $header ) { |
| 199 | return $this->handle_header_line( $ch, $header ); |
| 200 | } |
| 201 | ); |
| 202 | // Disable signals (required for timeout in multi). |
| 203 | curl_setopt( $ch, CURLOPT_NOSIGNAL, true ); |
| 204 | $request->state = Request::STATE_WILL_SEND_HEADERS; |
| 205 | |
| 206 | return $ch; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * `cURL` callback to handle incoming header lines. |
| 211 | * Triggers an EVENT_GOT_HEADERS event when the header section is complete. |
| 212 | * |
| 213 | * @param resource $ch The cURL handle. |
| 214 | * @param string $header_line A line from the response headers. |
| 215 | * @return int Number of bytes handled (required by cURL). |
| 216 | */ |
| 217 | private function handle_header_line( $ch, $header_line ) { |
| 218 | $request = $this->get_request_by_handle( $ch ); |
| 219 | if ( null === $request ) { |
| 220 | throw new HttpClientException( 'Received header data for an unknown request ' . ( $ch ? (int) $ch : 'unknown' ) ); |
| 221 | } |
| 222 | $connection = $this->state->connections[ $request->id ]; |
| 223 | if ( 0 === strlen( $connection->response_buffer ) ) { |
| 224 | $request->state = Request::STATE_RECEIVING_HEADERS; |
| 225 | } |
| 226 | $connection->response_buffer .= $header_line; |
| 227 | |
| 228 | // Check for the end of the header section. |
| 229 | if ( '' === trim( $header_line ) ) { |
| 230 | $request->response = Response::from_http_headers( |
| 231 | $connection->response_buffer, |
| 232 | $request |
| 233 | ); |
| 234 | $connection->response_buffer = ''; |
| 235 | if ( false === $request->response ) { |
| 236 | $request->response = new Response( $request ); |
| 237 | $this->set_error( $request, new HttpError( 'Failed to parse headers', $request ) ); |
| 238 | return strlen( $header_line ); |
| 239 | } |
| 240 | $this->state->events[ $request->id ][ Client::EVENT_GOT_HEADERS ] = true; |
| 241 | $request->state = Request::STATE_RECEIVING_BODY; |
| 242 | return strlen( $header_line ); |
| 243 | } |
| 244 | |
| 245 | return strlen( $header_line ); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * `cURL` callback to handle chunks of response body data. |
| 250 | * Triggers an EVENT_BODY_CHUNK_AVAILABLE event for each chunk received. |
| 251 | * |
| 252 | * @param resource $ch The cURL handle. |
| 253 | * @param string $data The chunk of response body data. |
| 254 | * @return int Number of bytes handled. |
| 255 | */ |
| 256 | private function handle_body_data( $ch, $data ) { |
| 257 | $request = $this->get_request_by_handle( $ch ); |
| 258 | if ( null === $request ) { |
| 259 | throw new HttpClientException( 'Received body data for an unknown request ' . ( $ch ? (int) $ch : 'unknown' ) ); |
| 260 | } |
| 261 | $this->state->connections[ $request->id ]->response_buffer .= $data; |
| 262 | $this->state->events[ $request->id ][ Client::EVENT_BODY_CHUNK_AVAILABLE ] = true; |
| 263 | |
| 264 | return strlen( $data ); |
| 265 | } |
| 266 | |
| 267 | private function get_request_by_handle( $handle ) { |
| 268 | $request_id = $this->handle_map[ (int) $handle ] ?? null; |
| 269 | return $this->state->get_request_by_id( $request_id ); |
| 270 | } |
| 271 | |
| 272 | private function mark_finished( Request $request ) { |
| 273 | $this->state->set_request_finished( $request ); |
| 274 | $this->close_connection( $request ); |
| 275 | } |
| 276 | |
| 277 | private function set_error( Request $request, $error ) { |
| 278 | $this->state->set_request_error( $request, $error ); |
| 279 | $this->close_connection( $request ); |
| 280 | } |
| 281 | |
| 282 | private function close_connection( Request $request ) { |
| 283 | $handle = $this->state->connections[ $request->id ]->http_socket; |
| 284 | if ( null !== $handle ) { |
| 285 | curl_multi_remove_handle( $this->multi_handle, $handle ); |
| 286 | curl_close( $handle ); |
| 287 | } |
| 288 | unset( $this->handle_map[ (int) $handle ] ); |
| 289 | } |
| 290 | } |
| 291 |