private function add_debug(array $request, &$options, $value, &$params) { if ($value === false) { return; } static $map = array(STREAM_NOTIFY_CONNECT => 'CONNECT', STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED', STREAM_NOTIFY_AUTH_RESULT => 'AUTH_RESULT', STREAM_NOTIFY_MIME_TYPE_IS => 'MIME_TYPE_IS', STREAM_NOTIFY_FILE_SIZE_IS => 'FILE_SIZE_IS', STREAM_NOTIFY_REDIRECTED => 'REDIRECTED', STREAM_NOTIFY_PROGRESS => 'PROGRESS', STREAM_NOTIFY_FAILURE => 'FAILURE', STREAM_NOTIFY_COMPLETED => 'COMPLETED', STREAM_NOTIFY_RESOLVE => 'RESOLVE'); static $args = array('severity', 'message', 'message_code', 'bytes_transferred', 'bytes_max'); $value = Core::getDebugResource($value); $ident = $request['http_method'] . ' ' . Core::url($request); $fn = function () use($ident, $value, $map, $args) { $passed = func_get_args(); $code = array_shift($passed); fprintf($value, '<%s> [%s] ', $ident, $map[$code]); foreach (array_filter($passed) as $i => $v) { fwrite($value, $args[$i] . ': "' . $v . '" '); } fwrite($value, "\n"); }; // Wrap the existing function if needed. $params['notification'] = isset($params['notification']) ? Core::callArray([$params['notification'], $fn]) : $fn; }
/** * Applies an array of request client options to a the options array. * * This method uses a large switch rather than double-dispatch to save on * high overhead of calling functions in PHP. */ private function applyHandlerOptions(array $request, array &$options) { foreach ($request['client'] as $key => $value) { switch ($key) { // Violating PSR-4 to provide more room. case 'verify': if ($value === false) { unset($options[CURLOPT_CAINFO]); $options[CURLOPT_SSL_VERIFYHOST] = 0; $options[CURLOPT_SSL_VERIFYPEER] = false; continue; } $options[CURLOPT_SSL_VERIFYHOST] = 2; $options[CURLOPT_SSL_VERIFYPEER] = true; if (is_string($value)) { $options[CURLOPT_CAINFO] = $value; if (!file_exists($value)) { throw new \InvalidArgumentException("SSL CA bundle not found: {$value}"); } } break; case 'decode_content': if ($value === false) { continue; } $accept = Core::firstHeader($request, 'Accept-Encoding'); if ($accept) { $options[CURLOPT_ENCODING] = $accept; } else { $options[CURLOPT_ENCODING] = ''; // Don't let curl send the header over the wire $options[CURLOPT_HTTPHEADER][] = 'Accept-Encoding:'; } break; case 'save_to': if (is_string($value)) { if (!is_dir(dirname($value))) { throw new \RuntimeException(sprintf('Directory %s does not exist for save_to value of %s', dirname($value), $value)); } $value = new LazyOpenStream($value, 'w+'); } if ($value instanceof StreamInterface) { $options[CURLOPT_WRITEFUNCTION] = function ($ch, $write) use($value) { return $value->write($write); }; } elseif (is_resource($value)) { $options[CURLOPT_FILE] = $value; } else { throw new \InvalidArgumentException('save_to must be a ' . 'GuzzleHttp\\Stream\\StreamInterface or resource'); } break; case 'timeout': if (defined('CURLOPT_TIMEOUT_MS')) { $options[CURLOPT_TIMEOUT_MS] = $value * 1000; } else { $options[CURLOPT_TIMEOUT] = $value; } break; case 'connect_timeout': if (defined('CURLOPT_CONNECTTIMEOUT_MS')) { $options[CURLOPT_CONNECTTIMEOUT_MS] = $value * 1000; } else { $options[CURLOPT_CONNECTTIMEOUT] = $value; } break; case 'proxy': if (!is_array($value)) { $options[CURLOPT_PROXY] = $value; } elseif (isset($request['scheme'])) { $scheme = $request['scheme']; if (isset($value[$scheme])) { $options[CURLOPT_PROXY] = $value[$scheme]; } } break; case 'cert': if (is_array($value)) { $options[CURLOPT_SSLCERTPASSWD] = $value[1]; $value = $value[0]; } if (!file_exists($value)) { throw new \InvalidArgumentException("SSL certificate not found: {$value}"); } $options[CURLOPT_SSLCERT] = $value; break; case 'ssl_key': if (is_array($value)) { $options[CURLOPT_SSLKEYPASSWD] = $value[1]; $value = $value[0]; } if (!file_exists($value)) { throw new \InvalidArgumentException("SSL private key not found: {$value}"); } $options[CURLOPT_SSLKEY] = $value; break; case 'progress': if (!is_callable($value)) { throw new \InvalidArgumentException('progress client option must be callable'); } $options[CURLOPT_NOPROGRESS] = false; $options[CURLOPT_PROGRESSFUNCTION] = function () use($value) { $args = func_get_args(); // PHP 5.5 pushed the handle onto the start of the args if (is_resource($args[0])) { array_shift($args); } call_user_func_array($value, $args); }; break; case 'debug': if ($value) { $options[CURLOPT_STDERR] = Core::getDebugResource($value); $options[CURLOPT_VERBOSE] = true; } break; } } }