class-cachemiddleware.php
6 days ago
class-httpmiddleware.php
6 days ago
class-redirectionmiddleware.php
6 days ago
middleware-interface.php
6 days ago
class-httpmiddleware.php
161 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\HttpClient\Middleware; |
| 4 | |
| 5 | use WordPress\HttpClient\Client; |
| 6 | use WordPress\HttpClient\ClientState; |
| 7 | use WordPress\HttpClient\HttpClientException; |
| 8 | use WordPress\HttpClient\Request; |
| 9 | use WordPress\HttpClient\Connection; |
| 10 | use WordPress\HttpClient\Transport\CurlTransport; |
| 11 | use WordPress\HttpClient\Transport\SocketTransport; |
| 12 | use WordPress\HttpClient\Transport\TransportInterface; |
| 13 | |
| 14 | class HttpMiddleware implements MiddlewareInterface { |
| 15 | |
| 16 | const EVENT_GOT_HEADERS = 'EVENT_GOT_HEADERS'; |
| 17 | const EVENT_BODY_CHUNK_AVAILABLE = 'EVENT_BODY_CHUNK_AVAILABLE'; |
| 18 | const EVENT_FAILED = 'EVENT_FAILED'; |
| 19 | const EVENT_FINISHED = 'EVENT_FINISHED'; |
| 20 | |
| 21 | /** |
| 22 | * @var ClientState |
| 23 | */ |
| 24 | private $state; |
| 25 | /** |
| 26 | * @var TransportInterface |
| 27 | */ |
| 28 | private $transport; |
| 29 | |
| 30 | public function __construct( $client_state, $options = array() ) { |
| 31 | $this->state = $client_state; |
| 32 | $this->transport = $options['transport']; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Enqueues one or multiple HTTP requests for asynchronous processing. |
| 37 | * It does not open the network sockets, only adds the Request objects to |
| 38 | * an internal queue. Network transmission is delayed until one of the returned |
| 39 | * streams is read from. |
| 40 | * |
| 41 | * @param Request $request The HTTP request to enqueue. |
| 42 | */ |
| 43 | public function enqueue( Request $request ) { |
| 44 | $request->state = Request::STATE_ENQUEUED; |
| 45 | $this->state->requests[] = apply_filters( 'wp_http_client_request', $request ); |
| 46 | $this->state->events[ $request->id ] = array(); |
| 47 | $this->state->connections[ $request->id ] = new Connection( $request ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the next event related to any of the HTTP |
| 52 | * requests enqueued in this client. |
| 53 | * |
| 54 | * ## Events |
| 55 | * |
| 56 | * The returned event is a ClientEvent with $event->name |
| 57 | * being one of the following: |
| 58 | * |
| 59 | * * `Client::EVENT_GOT_HEADERS` |
| 60 | * * `Client::EVENT_BODY_CHUNK_AVAILABLE` |
| 61 | * * `Client::EVENT_FAILED` |
| 62 | * * `Client::EVENT_FINISHED` |
| 63 | * |
| 64 | * See the ClientEvent class for details on each event. |
| 65 | * |
| 66 | * Once an event is consumed, it is removed from the |
| 67 | * event queue and will not be returned again. |
| 68 | * |
| 69 | * When there are no events available, this function |
| 70 | * blocks and waits for the next one. If all requests |
| 71 | * have already finished, and we are not waiting for |
| 72 | * any more events, it returns false. |
| 73 | * |
| 74 | * ## Filtering |
| 75 | * |
| 76 | * The $query parameter can be used to filter the events |
| 77 | * that are returned. It can contain the following keys: |
| 78 | * |
| 79 | * * `request_id` – The ID of the request to consider. |
| 80 | * |
| 81 | * For example, to only consider the next `EVENT_GOT_HEADERS` |
| 82 | * event for a specific request, you can use: |
| 83 | * |
| 84 | * ```php |
| 85 | * $request = new Request( "https://w.org" ); |
| 86 | * |
| 87 | * $client = new HttpClientClient(); |
| 88 | * $client->enqueue( $request ); |
| 89 | * $event = $client->await_next_event( [ |
| 90 | * 'request_id' => $request->id, |
| 91 | * ] ); |
| 92 | * ``` |
| 93 | * |
| 94 | * Importantly, filtering does not consume unrelated events. |
| 95 | * You can await all the events for a request #2, and |
| 96 | * then await the next event for request #1 even if the |
| 97 | * request #1 has finished before you started awaiting |
| 98 | * events for request #2. |
| 99 | * |
| 100 | * @param $query |
| 101 | * |
| 102 | * @return bool |
| 103 | */ |
| 104 | public function await_next_event( $requests_ids ): bool { |
| 105 | $ordered_events = array( |
| 106 | Client::EVENT_GOT_HEADERS, |
| 107 | Client::EVENT_BODY_CHUNK_AVAILABLE, |
| 108 | Client::EVENT_FAILED, |
| 109 | Client::EVENT_FINISHED, |
| 110 | ); |
| 111 | $this->state->event = null; |
| 112 | $this->state->request = null; |
| 113 | $this->state->response_body_chunk = null; |
| 114 | |
| 115 | // Give the requests an opportunity to time out; 10% more, but at least 300ms. |
| 116 | $timeout_ms = $this->state->request_timeout_ms + max( 300, $this->state->request_timeout_ms * 0.1 ); |
| 117 | $start_time = microtime( true ); |
| 118 | |
| 119 | do { |
| 120 | foreach ( $requests_ids as $request_id ) { |
| 121 | foreach ( $ordered_events as $considered_event ) { |
| 122 | $needs_emitting = $this->state->events[ $request_id ][ $considered_event ] ?? false; |
| 123 | if ( ! $needs_emitting ) { |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | $this->state->events[ $request_id ][ $considered_event ] = false; |
| 128 | $this->state->event = $considered_event; |
| 129 | $this->state->request = $this->state->get_request_by_id( $request_id ); |
| 130 | switch ( $this->state->event ) { |
| 131 | case Client::EVENT_BODY_CHUNK_AVAILABLE: |
| 132 | $this->state->response_body_chunk = $this->state->consume_buffered_response_body( $request_id ); |
| 133 | break; |
| 134 | case Client::EVENT_FAILED: |
| 135 | case Client::EVENT_FINISHED: |
| 136 | // We don't need the response buffer anymore. It's |
| 137 | // safe to clean up the connection object now. The |
| 138 | // HTTP resource have been closed by now via the |
| 139 | // close_connection() method. |
| 140 | unset( $this->state->connections[ $request_id ] ); |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // After we've checked for any available events, see if we've run out of time. |
| 149 | // This way, we always return any events that were ready before worrying about the timeout |
| 150 | // If we checked the timeout first, we might miss events that were already waiting for us |
| 151 | // when the timeout is set to zero. |
| 152 | $time_elapsed_ms = ( microtime( true ) - $start_time ) * 1000; |
| 153 | if ( $timeout_ms && $time_elapsed_ms >= $timeout_ms ) { |
| 154 | return false; |
| 155 | } |
| 156 | } while ( $this->transport->event_loop_tick() ); |
| 157 | |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 |