| [01a620f] | 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp;
|
|---|
| 3 |
|
|---|
| 4 | use GuzzleHttp\Handler\CurlHandler;
|
|---|
| 5 | use GuzzleHttp\Handler\CurlMultiHandler;
|
|---|
| 6 | use GuzzleHttp\Handler\Proxy;
|
|---|
| 7 | use GuzzleHttp\Handler\StreamHandler;
|
|---|
| 8 | use Psr\Http\Message\StreamInterface;
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * Expands a URI template
|
|---|
| 12 | *
|
|---|
| 13 | * @param string $template URI template
|
|---|
| 14 | * @param array $variables Template variables
|
|---|
| 15 | *
|
|---|
| 16 | * @return string
|
|---|
| 17 | */
|
|---|
| 18 | function uri_template($template, array $variables)
|
|---|
| 19 | {
|
|---|
| 20 | if (extension_loaded('uri_template')) {
|
|---|
| 21 | // @codeCoverageIgnoreStart
|
|---|
| 22 | return \uri_template($template, $variables);
|
|---|
| 23 | // @codeCoverageIgnoreEnd
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | static $uriTemplate;
|
|---|
| 27 | if (!$uriTemplate) {
|
|---|
| 28 | $uriTemplate = new UriTemplate();
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | return $uriTemplate->expand($template, $variables);
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Debug function used to describe the provided value type and class.
|
|---|
| 36 | *
|
|---|
| 37 | * @param mixed $input
|
|---|
| 38 | *
|
|---|
| 39 | * @return string Returns a string containing the type of the variable and
|
|---|
| 40 | * if a class is provided, the class name.
|
|---|
| 41 | */
|
|---|
| 42 | function describe_type($input)
|
|---|
| 43 | {
|
|---|
| 44 | switch (gettype($input)) {
|
|---|
| 45 | case 'object':
|
|---|
| 46 | return 'object(' . get_class($input) . ')';
|
|---|
| 47 | case 'array':
|
|---|
| 48 | return 'array(' . count($input) . ')';
|
|---|
| 49 | default:
|
|---|
| 50 | ob_start();
|
|---|
| 51 | var_dump($input);
|
|---|
| 52 | // normalize float vs double
|
|---|
| 53 | return str_replace('double(', 'float(', rtrim(ob_get_clean()));
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Parses an array of header lines into an associative array of headers.
|
|---|
| 59 | *
|
|---|
| 60 | * @param array $lines Header lines array of strings in the following
|
|---|
| 61 | * format: "Name: Value"
|
|---|
| 62 | * @return array
|
|---|
| 63 | */
|
|---|
| 64 | function headers_from_lines($lines)
|
|---|
| 65 | {
|
|---|
| 66 | $headers = [];
|
|---|
| 67 |
|
|---|
| 68 | foreach ($lines as $line) {
|
|---|
| 69 | $parts = explode(':', $line, 2);
|
|---|
| 70 | $headers[trim($parts[0])][] = isset($parts[1])
|
|---|
| 71 | ? trim($parts[1])
|
|---|
| 72 | : null;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | return $headers;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Returns a debug stream based on the provided variable.
|
|---|
| 80 | *
|
|---|
| 81 | * @param mixed $value Optional value
|
|---|
| 82 | *
|
|---|
| 83 | * @return resource
|
|---|
| 84 | */
|
|---|
| 85 | function debug_resource($value = null)
|
|---|
| 86 | {
|
|---|
| 87 | if (is_resource($value)) {
|
|---|
| 88 | return $value;
|
|---|
| 89 | } elseif (defined('STDOUT')) {
|
|---|
| 90 | return STDOUT;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | return fopen('php://output', 'w');
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * Chooses and creates a default handler to use based on the environment.
|
|---|
| 98 | *
|
|---|
| 99 | * The returned handler is not wrapped by any default middlewares.
|
|---|
| 100 | *
|
|---|
| 101 | * @throws \RuntimeException if no viable Handler is available.
|
|---|
| 102 | * @return callable Returns the best handler for the given system.
|
|---|
| 103 | */
|
|---|
| 104 | function choose_handler()
|
|---|
| 105 | {
|
|---|
| 106 | $handler = null;
|
|---|
| 107 | if (extension_loaded('curl')) {
|
|---|
| 108 | $handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler());
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | if (ini_get('allow_url_fopen')) {
|
|---|
| 112 | $handler = $handler
|
|---|
| 113 | ? Proxy::wrapStreaming($handler, new StreamHandler())
|
|---|
| 114 | : new StreamHandler();
|
|---|
| 115 | } elseif (!$handler) {
|
|---|
| 116 | throw new \RuntimeException('GuzzleHttp requires cURL, the '
|
|---|
| 117 | . 'allow_url_fopen ini setting, or a custom HTTP handler.');
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | return $handler;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * Get the default User-Agent string to use with Guzzle
|
|---|
| 125 | *
|
|---|
| 126 | * @return string
|
|---|
| 127 | */
|
|---|
| 128 | function default_user_agent()
|
|---|
| 129 | {
|
|---|
| 130 | static $defaultAgent = '';
|
|---|
| 131 |
|
|---|
| 132 | if (!$defaultAgent) {
|
|---|
| 133 | $defaultAgent = 'GuzzleHttp/' . Client::VERSION;
|
|---|
| 134 | if (extension_loaded('curl')) {
|
|---|
| 135 | $defaultAgent .= ' curl/' . \curl_version()['version'];
|
|---|
| 136 | }
|
|---|
| 137 | $defaultAgent .= ' PHP/' . PHP_VERSION;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | return $defaultAgent;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Returns the default cacert bundle for the current system.
|
|---|
| 145 | *
|
|---|
| 146 | * First, the openssl.cafile and curl.cainfo php.ini settings are checked.
|
|---|
| 147 | * If those settings are not configured, then the common locations for
|
|---|
| 148 | * bundles found on Red Hat, CentOS, Fedora, Ubuntu, Debian, FreeBSD, OS X
|
|---|
| 149 | * and Windows are checked. If any of these file locations are found on
|
|---|
| 150 | * disk, they will be utilized.
|
|---|
| 151 | *
|
|---|
| 152 | * Note: the result of this function is cached for subsequent calls.
|
|---|
| 153 | *
|
|---|
| 154 | * @return string
|
|---|
| 155 | * @throws \RuntimeException if no bundle can be found.
|
|---|
| 156 | */
|
|---|
| 157 | function default_ca_bundle()
|
|---|
| 158 | {
|
|---|
| 159 | static $cached = null;
|
|---|
| 160 | static $cafiles = [
|
|---|
| 161 | // Red Hat, CentOS, Fedora (provided by the ca-certificates package)
|
|---|
| 162 | '/etc/pki/tls/certs/ca-bundle.crt',
|
|---|
| 163 | // Ubuntu, Debian (provided by the ca-certificates package)
|
|---|
| 164 | '/etc/ssl/certs/ca-certificates.crt',
|
|---|
| 165 | // FreeBSD (provided by the ca_root_nss package)
|
|---|
| 166 | '/usr/local/share/certs/ca-root-nss.crt',
|
|---|
| 167 | // OS X provided by homebrew (using the default path)
|
|---|
| 168 | '/usr/local/etc/openssl/cert.pem',
|
|---|
| 169 | // Windows?
|
|---|
| 170 | 'C:\\windows\\system32\\curl-ca-bundle.crt',
|
|---|
| 171 | 'C:\\windows\\curl-ca-bundle.crt',
|
|---|
| 172 | ];
|
|---|
| 173 |
|
|---|
| 174 | if ($cached) {
|
|---|
| 175 | return $cached;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | if ($ca = ini_get('openssl.cafile')) {
|
|---|
| 179 | return $cached = $ca;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | if ($ca = ini_get('curl.cainfo')) {
|
|---|
| 183 | return $cached = $ca;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | foreach ($cafiles as $filename) {
|
|---|
| 187 | if (file_exists($filename)) {
|
|---|
| 188 | return $cached = $filename;
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | throw new \RuntimeException(<<< EOT
|
|---|
| 193 | No system CA bundle could be found in any of the the common system locations.
|
|---|
| 194 | PHP versions earlier than 5.6 are not properly configured to use the system's
|
|---|
| 195 | CA bundle by default. In order to verify peer certificates, you will need to
|
|---|
| 196 | supply the path on disk to a certificate bundle to the 'verify' request
|
|---|
| 197 | option: http://docs.guzzlephp.org/en/latest/clients.html#verify. If you do not
|
|---|
| 198 | need a specific certificate bundle, then Mozilla provides a commonly used CA
|
|---|
| 199 | bundle which can be downloaded here (provided by the maintainer of cURL):
|
|---|
| 200 | https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt. Once
|
|---|
| 201 | you have a CA bundle available on disk, you can set the 'openssl.cafile' PHP
|
|---|
| 202 | ini setting to point to the path to the file, allowing you to omit the 'verify'
|
|---|
| 203 | request option. See http://curl.haxx.se/docs/sslcerts.html for more
|
|---|
| 204 | information.
|
|---|
| 205 | EOT
|
|---|
| 206 | );
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /**
|
|---|
| 210 | * Creates an associative array of lowercase header names to the actual
|
|---|
| 211 | * header casing.
|
|---|
| 212 | *
|
|---|
| 213 | * @param array $headers
|
|---|
| 214 | *
|
|---|
| 215 | * @return array
|
|---|
| 216 | */
|
|---|
| 217 | function normalize_header_keys(array $headers)
|
|---|
| 218 | {
|
|---|
| 219 | $result = [];
|
|---|
| 220 | foreach (array_keys($headers) as $key) {
|
|---|
| 221 | $result[strtolower($key)] = $key;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | return $result;
|
|---|
| 225 | }
|
|---|