Debian Codegraph @

vendor/react/http/src/Browser.php

raw
1 <?php
2
3 namespace React\Http;
4
5 use Psr\Http\Message\ResponseInterface;
6 use RingCentral\Psr7\Request;
7 use RingCentral\Psr7\Uri;
8 use React\EventLoop\LoopInterface;
9 use React\Http\Io\ReadableBodyStream;
10 use React\Http\Io\Sender;
11 use React\Http\Io\Transaction;
12 use React\Promise\PromiseInterface;
13 use React\Socket\ConnectorInterface;
14 use React\Stream\ReadableStreamInterface;
15 use InvalidArgumentException;
16
17 /**
18 * @final This class is final and shouldn't be extended as it is likely to be marked final in a future relase.
19 */
20 class Browser
21 {
22 private $transaction;
23 private $baseUrl;
24 private $protocolVersion = '1.1';
25
26 /**
27 * The `Browser` is responsible for sending HTTP requests to your HTTP server
28 * and keeps track of pending incoming HTTP responses.
29 * It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
30 *
31 * ```php
32 * $loop = React\EventLoop\Factory::create();
33 *
34 * $browser = new React\Http\Browser($loop);
35 * ```
36 *
37 * If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
38 * proxy servers etc.), you can explicitly pass a custom instance of the
39 * [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
40 *
41 * ```php
42 * $connector = new React\Socket\Connector($loop, array(
43 * 'dns' => '127.0.0.1',
44 * 'tcp' => array(
45 * 'bindto' => '192.168.10.1:0'
46 * ),
47 * 'tls' => array(
48 * 'verify_peer' => false,
49 * 'verify_peer_name' => false
50 * )
51 * ));
52 *
53 * $browser = new React\Http\Browser($loop, $connector);
54 * ```
55 *
56 * @param LoopInterface $loop
57 * @param ConnectorInterface|null $connector [optional] Connector to use.
58 * Should be `null` in order to use default Connector.
59 */
60 public function __construct(LoopInterface $loop, ConnectorInterface $connector = null)
61 {
62 $this->transaction = new Transaction(
63 Sender::createFromLoop($loop, $connector),
64 $loop
65 );
66 }
67
68 /**
69 * Sends an HTTP GET request
70 *
71 * ```php
72 * $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
73 * var_dump((string)$response->getBody());
74 * });
75 * ```
76 *
77 * See also [GET request client example](../examples/01-client-get-request.php).
78 *
79 * @param string $url URL for the request.
80 * @param array $headers
81 * @return PromiseInterface<ResponseInterface>
82 */
83 public function get($url, array $headers = array())
84 {
85 return $this->requestMayBeStreaming('GET', $url, $headers);
86 }
87
88 /**
89 * Sends an HTTP POST request
90 *
91 * ```php
92 * $browser->post(
93 * $url,
94 * [
95 * 'Content-Type' => 'application/json'
96 * ],
97 * json_encode($data)
98 * )->then(function (Psr\Http\Message\ResponseInterface $response) {
99 * var_dump(json_decode((string)$response->getBody()));
100 * });
101 * ```
102 *
103 * See also [POST JSON client example](../examples/04-client-post-json.php).
104 *
105 * This method is also commonly used to submit HTML form data:
106 *
107 * ```php
108 * $data = [
109 * 'user' => 'Alice',
110 * 'password' => 'secret'
111 * ];
112 *
113 * $browser->post(
114 * $url,
115 * [
116 * 'Content-Type' => 'application/x-www-form-urlencoded'
117 * ],
118 * http_build_query($data)
119 * );
120 * ```
121 *
122 * This method will automatically add a matching `Content-Length` request
123 * header if the outgoing request body is a `string`. If you're using a
124 * streaming request body (`ReadableStreamInterface`), it will default to
125 * using `Transfer-Encoding: chunked` or you have to explicitly pass in a
126 * matching `Content-Length` request header like so:
127 *
128 * ```php
129 * $body = new React\Stream\ThroughStream();
130 * $loop->addTimer(1.0, function () use ($body) {
131 * $body->end("hello world");
132 * });
133 *
134 * $browser->post($url, array('Content-Length' => '11'), $body);
135 * ```
136 *
137 * @param string $url URL for the request.
138 * @param array $headers
139 * @param string|ReadableStreamInterface $contents
140 * @return PromiseInterface<ResponseInterface>
141 */
142 public function post($url, array $headers = array(), $contents = '')
143 {
144 return $this->requestMayBeStreaming('POST', $url, $headers, $contents);
145 }
146
147 /**
148 * Sends an HTTP HEAD request
149 *
150 * ```php
151 * $browser->head($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
152 * var_dump($response->getHeaders());
153 * });
154 * ```
155 *
156 * @param string $url URL for the request.
157 * @param array $headers
158 * @return PromiseInterface<ResponseInterface>
159 */
160 public function head($url, array $headers = array())
161 {
162 return $this->requestMayBeStreaming('HEAD', $url, $headers);
163 }
164
165 /**
166 * Sends an HTTP PATCH request
167 *
168 * ```php
169 * $browser->patch(
170 * $url,
171 * [
172 * 'Content-Type' => 'application/json'
173 * ],
174 * json_encode($data)
175 * )->then(function (Psr\Http\Message\ResponseInterface $response) {
176 * var_dump(json_decode((string)$response->getBody()));
177 * });
178 * ```
179 *
180 * This method will automatically add a matching `Content-Length` request
181 * header if the outgoing request body is a `string`. If you're using a
182 * streaming request body (`ReadableStreamInterface`), it will default to
183 * using `Transfer-Encoding: chunked` or you have to explicitly pass in a
184 * matching `Content-Length` request header like so:
185 *
186 * ```php
187 * $body = new React\Stream\ThroughStream();
188 * $loop->addTimer(1.0, function () use ($body) {
189 * $body->end("hello world");
190 * });
191 *
192 * $browser->patch($url, array('Content-Length' => '11'), $body);
193 * ```
194 *
195 * @param string $url URL for the request.
196 * @param array $headers
197 * @param string|ReadableStreamInterface $contents
198 * @return PromiseInterface<ResponseInterface>
199 */
200 public function patch($url, array $headers = array(), $contents = '')
201 {
202 return $this->requestMayBeStreaming('PATCH', $url , $headers, $contents);
203 }
204
205 /**
206 * Sends an HTTP PUT request
207 *
208 * ```php
209 * $browser->put(
210 * $url,
211 * [
212 * 'Content-Type' => 'text/xml'
213 * ],
214 * $xml->asXML()
215 * )->then(function (Psr\Http\Message\ResponseInterface $response) {
216 * var_dump((string)$response->getBody());
217 * });
218 * ```
219 *
220 * See also [PUT XML client example](../examples/05-client-put-xml.php).
221 *
222 * This method will automatically add a matching `Content-Length` request
223 * header if the outgoing request body is a `string`. If you're using a
224 * streaming request body (`ReadableStreamInterface`), it will default to
225 * using `Transfer-Encoding: chunked` or you have to explicitly pass in a
226 * matching `Content-Length` request header like so:
227 *
228 * ```php
229 * $body = new React\Stream\ThroughStream();
230 * $loop->addTimer(1.0, function () use ($body) {
231 * $body->end("hello world");
232 * });
233 *
234 * $browser->put($url, array('Content-Length' => '11'), $body);
235 * ```
236 *
237 * @param string $url URL for the request.
238 * @param array $headers
239 * @param string|ReadableStreamInterface $contents
240 * @return PromiseInterface<ResponseInterface>
241 */
242 public function put($url, array $headers = array(), $contents = '')
243 {
244 return $this->requestMayBeStreaming('PUT', $url, $headers, $contents);
245 }
246
247 /**
248 * Sends an HTTP DELETE request
249 *
250 * ```php
251 * $browser->delete($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
252 * var_dump((string)$response->getBody());
253 * });
254 * ```
255 *
256 * @param string $url URL for the request.
257 * @param array $headers
258 * @param string|ReadableStreamInterface $contents
259 * @return PromiseInterface<ResponseInterface>
260 */
261 public function delete($url, array $headers = array(), $contents = '')
262 {
263 return $this->requestMayBeStreaming('DELETE', $url, $headers, $contents);
264 }
265
266 /**
267 * Sends an arbitrary HTTP request.
268 *
269 * The preferred way to send an HTTP request is by using the above
270 * [request methods](#request-methods), for example the [`get()`](#get)
271 * method to send an HTTP `GET` request.
272 *
273 * As an alternative, if you want to use a custom HTTP request method, you
274 * can use this method:
275 *
276 * ```php
277 * $browser->request('OPTIONS', $url)->then(function (Psr\Http\Message\ResponseInterface $response) {
278 * var_dump((string)$response->getBody());
279 * });
280 * ```
281 *
282 * This method will automatically add a matching `Content-Length` request
283 * header if the size of the outgoing request body is known and non-empty.
284 * For an empty request body, if will only include a `Content-Length: 0`
285 * request header if the request method usually expects a request body (only
286 * applies to `POST`, `PUT` and `PATCH`).
287 *
288 * If you're using a streaming request body (`ReadableStreamInterface`), it
289 * will default to using `Transfer-Encoding: chunked` or you have to
290 * explicitly pass in a matching `Content-Length` request header like so:
291 *
292 * ```php
293 * $body = new React\Stream\ThroughStream();
294 * $loop->addTimer(1.0, function () use ($body) {
295 * $body->end("hello world");
296 * });
297 *
298 * $browser->request('POST', $url, array('Content-Length' => '11'), $body);
299 * ```
300 *
301 * @param string $method HTTP request method, e.g. GET/HEAD/POST etc.
302 * @param string $url URL for the request
303 * @param array $headers Additional request headers
304 * @param string|ReadableStreamInterface $body HTTP request body contents
305 * @return PromiseInterface<ResponseInterface,Exception>
306 */
307 public function request($method, $url, array $headers = array(), $body = '')
308 {
309 return $this->withOptions(array('streaming' => false))->requestMayBeStreaming($method, $url, $headers, $body);
310 }
311
312 /**
313 * Sends an arbitrary HTTP request and receives a streaming response without buffering the response body.
314 *
315 * The preferred way to send an HTTP request is by using the above
316 * [request methods](#request-methods), for example the [`get()`](#get)
317 * method to send an HTTP `GET` request. Each of these methods will buffer
318 * the whole response body in memory by default. This is easy to get started
319 * and works reasonably well for smaller responses.
320 *
321 * In some situations, it's a better idea to use a streaming approach, where
322 * only small chunks have to be kept in memory. You can use this method to
323 * send an arbitrary HTTP request and receive a streaming response. It uses
324 * the same HTTP message API, but does not buffer the response body in
325 * memory. It only processes the response body in small chunks as data is
326 * received and forwards this data through [ReactPHP's Stream API](https://github.com/reactphp/stream).
327 * This works for (any number of) responses of arbitrary sizes.
328 *
329 * ```php
330 * $browser->requestStreaming('GET', $url)->then(function (Psr\Http\Message\ResponseInterface $response) {
331 * $body = $response->getBody();
332 * assert($body instanceof Psr\Http\Message\StreamInterface);
333 * assert($body instanceof React\Stream\ReadableStreamInterface);
334 *
335 * $body->on('data', function ($chunk) {
336 * echo $chunk;
337 * });
338 *
339 * $body->on('error', function (Exception $error) {
340 * echo 'Error: ' . $error->getMessage() . PHP_EOL;
341 * });
342 *
343 * $body->on('close', function () {
344 * echo '[DONE]' . PHP_EOL;
345 * });
346 * });
347 * ```
348 *
349 * See also [`ReadableStreamInterface`](https://github.com/reactphp/stream#readablestreaminterface)
350 * and the [streaming response](#streaming-response) for more details,
351 * examples and possible use-cases.
352 *
353 * This method will automatically add a matching `Content-Length` request
354 * header if the size of the outgoing request body is known and non-empty.
355 * For an empty request body, if will only include a `Content-Length: 0`
356 * request header if the request method usually expects a request body (only
357 * applies to `POST`, `PUT` and `PATCH`).
358 *
359 * If you're using a streaming request body (`ReadableStreamInterface`), it
360 * will default to using `Transfer-Encoding: chunked` or you have to
361 * explicitly pass in a matching `Content-Length` request header like so:
362 *
363 * ```php
364 * $body = new React\Stream\ThroughStream();
365 * $loop->addTimer(1.0, function () use ($body) {
366 * $body->end("hello world");
367 * });
368 *
369 * $browser->requestStreaming('POST', $url, array('Content-Length' => '11'), $body);
370 * ```
371 *
372 * @param string $method HTTP request method, e.g. GET/HEAD/POST etc.
373 * @param string $url URL for the request
374 * @param array $headers Additional request headers
375 * @param string|ReadableStreamInterface $body HTTP request body contents
376 * @return PromiseInterface<ResponseInterface,Exception>
377 */
378 public function requestStreaming($method, $url, $headers = array(), $contents = '')
379 {
380 return $this->withOptions(array('streaming' => true))->requestMayBeStreaming($method, $url, $headers, $contents);
381 }
382
383 /**
384 * Changes the maximum timeout used for waiting for pending requests.
385 *
386 * You can pass in the number of seconds to use as a new timeout value:
387 *
388 * ```php
389 * $browser = $browser->withTimeout(10.0);
390 * ```
391 *
392 * You can pass in a bool `false` to disable any timeouts. In this case,
393 * requests can stay pending forever:
394 *
395 * ```php
396 * $browser = $browser->withTimeout(false);
397 * ```
398 *
399 * You can pass in a bool `true` to re-enable default timeout handling. This
400 * will respects PHP's `default_socket_timeout` setting (default 60s):
401 *
402 * ```php
403 * $browser = $browser->withTimeout(true);
404 * ```
405 *
406 * See also [timeouts](#timeouts) for more details about timeout handling.
407 *
408 * Notice that the [`Browser`](#browser) is an immutable object, i.e. this
409 * method actually returns a *new* [`Browser`](#browser) instance with the
410 * given timeout value applied.
411 *
412 * @param bool|number $timeout
413 * @return self
414 */
415 public function withTimeout($timeout)
416 {
417 if ($timeout === true) {
418 $timeout = null;
419 } elseif ($timeout === false) {
420 $timeout = -1;
421 } elseif ($timeout < 0) {
422 $timeout = 0;
423 }
424
425 return $this->withOptions(array(
426 'timeout' => $timeout,
427 ));
428 }
429
430 /**
431 * Changes how HTTP redirects will be followed.
432 *
433 * You can pass in the maximum number of redirects to follow:
434 *
435 * ```php
436 * $browser = $browser->withFollowRedirects(5);
437 * ```
438 *
439 * The request will automatically be rejected when the number of redirects
440 * is exceeded. You can pass in a `0` to reject the request for any
441 * redirects encountered:
442 *
443 * ```php
444 * $browser = $browser->withFollowRedirects(0);
445 *
446 * $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
447 * // only non-redirected responses will now end up here
448 * var_dump($response->getHeaders());
449 * });
450 * ```
451 *
452 * You can pass in a bool `false` to disable following any redirects. In
453 * this case, requests will resolve with the redirection response instead
454 * of following the `Location` response header:
455 *
456 * ```php
457 * $browser = $browser->withFollowRedirects(false);
458 *
459 * $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
460 * // any redirects will now end up here
461 * var_dump($response->getHeaderLine('Location'));
462 * });
463 * ```
464 *
465 * You can pass in a bool `true` to re-enable default redirect handling.
466 * This defaults to following a maximum of 10 redirects:
467 *
468 * ```php
469 * $browser = $browser->withFollowRedirects(true);
470 * ```
471 *
472 * See also [redirects](#redirects) for more details about redirect handling.
473 *
474 * Notice that the [`Browser`](#browser) is an immutable object, i.e. this
475 * method actually returns a *new* [`Browser`](#browser) instance with the
476 * given redirect setting applied.
477 *
478 * @param bool|int $followRedirects
479 * @return self
480 */
481 public function withFollowRedirects($followRedirects)
482 {
483 return $this->withOptions(array(
484 'followRedirects' => $followRedirects !== false,
485 'maxRedirects' => \is_bool($followRedirects) ? null : $followRedirects
486 ));
487 }
488
489 /**
490 * Changes whether non-successful HTTP response status codes (4xx and 5xx) will be rejected.
491 *
492 * You can pass in a bool `false` to disable rejecting incoming responses
493 * that use a 4xx or 5xx response status code. In this case, requests will
494 * resolve with the response message indicating an error condition:
495 *
496 * ```php
497 * $browser = $browser->withRejectErrorResponse(false);
498 *
499 * $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
500 * // any HTTP response will now end up here
501 * var_dump($response->getStatusCode(), $response->getReasonPhrase());
502 * });
503 * ```
504 *
505 * You can pass in a bool `true` to re-enable default status code handling.
506 * This defaults to rejecting any response status codes in the 4xx or 5xx
507 * range:
508 *
509 * ```php
510 * $browser = $browser->withRejectErrorResponse(true);
511 *
512 * $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
513 * // any successful HTTP response will now end up here
514 * var_dump($response->getStatusCode(), $response->getReasonPhrase());
515 * }, function (Exception $e) {
516 * if ($e instanceof React\Http\Message\ResponseException) {
517 * // any HTTP response error message will now end up here
518 * $response = $e->getResponse();
519 * var_dump($response->getStatusCode(), $response->getReasonPhrase());
520 * } else {
521 * var_dump($e->getMessage());
522 * }
523 * });
524 * ```
525 *
526 * Notice that the [`Browser`](#browser) is an immutable object, i.e. this
527 * method actually returns a *new* [`Browser`](#browser) instance with the
528 * given setting applied.
529 *
530 * @param bool $obeySuccessCode
531 * @return self
532 */
533 public function withRejectErrorResponse($obeySuccessCode)
534 {
535 return $this->withOptions(array(
536 'obeySuccessCode' => $obeySuccessCode,
537 ));
538 }
539
540 /**
541 * Changes the base URL used to resolve relative URLs to.
542 *
543 * If you configure a base URL, any requests to relative URLs will be
544 * processed by first resolving this relative to the given absolute base
545 * URL. This supports resolving relative path references (like `../` etc.).
546 * This is particularly useful for (RESTful) API calls where all endpoints
547 * (URLs) are located under a common base URL.
548 *
549 * ```php
550 * $browser = $browser->withBase('http://api.example.com/v3/');
551 *
552 * // will request http://api.example.com/v3/users
553 * $browser->get('users')->then(…);
554 * ```
555 *
556 * You can pass in a `null` base URL to return a new instance that does not
557 * use a base URL:
558 *
559 * ```php
560 * $browser = $browser->withBase(null);
561 * ```
562 *
563 * Accordingly, any requests using relative URLs to a browser that does not
564 * use a base URL can not be completed and will be rejected without sending
565 * a request.
566 *
567 * This method will throw an `InvalidArgumentException` if the given
568 * `$baseUrl` argument is not a valid URL.
569 *
570 * Notice that the [`Browser`](#browser) is an immutable object, i.e. the `withBase()` method
571 * actually returns a *new* [`Browser`](#browser) instance with the given base URL applied.
572 *
573 * @param string|null $baseUrl absolute base URL
574 * @return self
575 * @throws InvalidArgumentException if the given $baseUrl is not a valid absolute URL
576 * @see self::withoutBase()
577 */
578 public function withBase($baseUrl)
579 {
580 $browser = clone $this;
581 if ($baseUrl === null) {
582 $browser->baseUrl = null;
583 return $browser;
584 }
585
586 $browser->baseUrl = new Uri($baseUrl);
587 if (!\in_array($browser->baseUrl->getScheme(), array('http', 'https')) || $browser->baseUrl->getHost() === '') {
588 throw new \InvalidArgumentException('Base URL must be absolute');
589 }
590
591 return $browser;
592 }
593
594 /**
595 * Changes the HTTP protocol version that will be used for all subsequent requests.
596 *
597 * All the above [request methods](#request-methods) default to sending
598 * requests as HTTP/1.1. This is the preferred HTTP protocol version which
599 * also provides decent backwards-compatibility with legacy HTTP/1.0
600 * servers. As such, there should rarely be a need to explicitly change this
601 * protocol version.
602 *
603 * If you want to explicitly use the legacy HTTP/1.0 protocol version, you
604 * can use this method:
605 *
606 * ```php
607 * $browser = $browser->withProtocolVersion('1.0');
608 *
609 * $browser->get($url)->then(…);
610 * ```
611 *
612 * Notice that the [`Browser`](#browser) is an immutable object, i.e. this
613 * method actually returns a *new* [`Browser`](#browser) instance with the
614 * new protocol version applied.
615 *
616 * @param string $protocolVersion HTTP protocol version to use, must be one of "1.1" or "1.0"
617 * @return self
618 * @throws InvalidArgumentException
619 */
620 public function withProtocolVersion($protocolVersion)
621 {
622 if (!\in_array($protocolVersion, array('1.0', '1.1'), true)) {
623 throw new InvalidArgumentException('Invalid HTTP protocol version, must be one of "1.1" or "1.0"');
624 }
625
626 $browser = clone $this;
627 $browser->protocolVersion = (string) $protocolVersion;
628
629 return $browser;
630 }
631
632 /**
633 * Changes the maximum size for buffering a response body.
634 *
635 * The preferred way to send an HTTP request is by using the above
636 * [request methods](#request-methods), for example the [`get()`](#get)
637 * method to send an HTTP `GET` request. Each of these methods will buffer
638 * the whole response body in memory by default. This is easy to get started
639 * and works reasonably well for smaller responses.
640 *
641 * By default, the response body buffer will be limited to 16 MiB. If the
642 * response body exceeds this maximum size, the request will be rejected.
643 *
644 * You can pass in the maximum number of bytes to buffer:
645 *
646 * ```php
647 * $browser = $browser->withResponseBuffer(1024 * 1024);
648 *
649 * $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
650 * // response body will not exceed 1 MiB
651 * var_dump($response->getHeaders(), (string) $response->getBody());
652 * });
653 * ```
654 *
655 * Note that the response body buffer has to be kept in memory for each
656 * pending request until its transfer is completed and it will only be freed
657 * after a pending request is fulfilled. As such, increasing this maximum
658 * buffer size to allow larger response bodies is usually not recommended.
659 * Instead, you can use the [`requestStreaming()` method](#requeststreaming)
660 * to receive responses with arbitrary sizes without buffering. Accordingly,
661 * this maximum buffer size setting has no effect on streaming responses.
662 *
663 * Notice that the [`Browser`](#browser) is an immutable object, i.e. this
664 * method actually returns a *new* [`Browser`](#browser) instance with the
665 * given setting applied.
666 *
667 * @param int $maximumSize
668 * @return self
669 * @see self::requestStreaming()
670 */
671 public function withResponseBuffer($maximumSize)
672 {
673 return $this->withOptions(array(
674 'maximumSize' => $maximumSize
675 ));
676 }
677
678 /**
679 * Changes the [options](#options) to use:
680 *
681 * The [`Browser`](#browser) class exposes several options for the handling of
682 * HTTP transactions. These options resemble some of PHP's
683 * [HTTP context options](http://php.net/manual/en/context.http.php) and
684 * can be controlled via the following API (and their defaults):
685 *
686 * ```php
687 * // deprecated
688 * $newBrowser = $browser->withOptions(array(
689 * 'timeout' => null, // see withTimeout() instead
690 * 'followRedirects' => true, // see withFollowRedirects() instead
691 * 'maxRedirects' => 10, // see withFollowRedirects() instead
692 * 'obeySuccessCode' => true, // see withRejectErrorResponse() instead
693 * 'streaming' => false, // deprecated, see requestStreaming() instead
694 * ));
695 * ```
696 *
697 * See also [timeouts](#timeouts), [redirects](#redirects) and
698 * [streaming](#streaming) for more details.
699 *
700 * Notice that the [`Browser`](#browser) is an immutable object, i.e. this
701 * method actually returns a *new* [`Browser`](#browser) instance with the
702 * options applied.
703 *
704 * @param array $options
705 * @return self
706 * @see self::withTimeout()
707 * @see self::withFollowRedirects()
708 * @see self::withRejectErrorResponse()
709 */
710 private function withOptions(array $options)
711 {
712 $browser = clone $this;
713 $browser->transaction = $this->transaction->withOptions($options);
714
715 return $browser;
716 }
717
718 /**
719 * @param string $method
720 * @param string $url
721 * @param array $headers
722 * @param string|ReadableStreamInterface $body
723 * @return PromiseInterface<ResponseInterface,Exception>
724 */
725 private function requestMayBeStreaming($method, $url, array $headers = array(), $body = '')
726 {
727 if ($this->baseUrl !== null) {
728 // ensure we're actually below the base URL
729 $url = Uri::resolve($this->baseUrl, $url);
730 }
731
732 if ($body instanceof ReadableStreamInterface) {
733 $body = new ReadableBodyStream($body);
734 }
735
736 return $this->transaction->send(
737 new Request($method, $url, $headers, $body, $this->protocolVersion)
738 );
739 }
740 }