/** * Construct the response * * @param string $statusCode The response status code (e.g. 200, 404, etc) * @param ToArrayInterface|array $headers The response headers * @param string|resource|EntityBodyInterface $body The body of the response * * @throws BadResponseException if an invalid response code is given */ public function __construct($statusCode, $headers = null, $body = null) { parent::__construct(); $this->setStatus($statusCode); $this->body = EntityBody::factory($body !== null ? $body : ''); if ($headers) { if (is_array($headers)) { $this->setHeaders($headers); } elseif ($headers instanceof ToArrayInterface) { $this->setHeaders($headers->toArray()); } else { throw new BadResponseException('Invalid headers argument received'); } } }
public function __construct($method, $url, $headers = array()) { parent::__construct(); $this->method = strtoupper($method); $this->curlOptions = new Collection(); $this->setUrl($url); if ($headers) { foreach ($headers as $key => $value) { if ($key == 'host' || $key == 'Host') { $this->setHeader($key, $value); } elseif ($value instanceof HeaderInterface) { $this->addHeader($key, $value); } else { foreach ((array) $value as $v) { $this->addHeader($key, $v); } } } } $this->setState(self::STATE_NEW); }
/** * @param string $method HTTP method * @param string|Url $url HTTP URL to connect to. The URI scheme, host header, and URI are parsed from the * full URL. If query string parameters are present they will be parsed as well. * @param array|Collection $headers HTTP headers */ public function __construct($method, $url, $headers = array()) { parent::__construct(); $this->method = strtoupper($method); $this->curlOptions = new Collection(); $this->setUrl($url); if ($headers) { // Special handling for multi-value headers foreach ($headers as $key => $value) { // Deal with collisions with Host and Authorization if ($key == 'host' || $key == 'Host') { $this->setHeader($key, $value); } elseif ($value instanceof HeaderInterface) { $this->addHeader($key, $value); } else { foreach ((array) $value as $v) { $this->addHeader($key, $v); } } } } $this->setState(self::STATE_NEW); }
/** * {@inheritdoc} * Adds a check for Host header changes */ public function addHeader($header, $value) { parent::addHeader($header, $value); if ($header == 'host' || $header == 'Host') { $this->setHost((string) $this->getHeader('Host')); } return $this; }
/** * {@inheritdoc} */ protected function changedHeader($header) { parent::changedHeader($header); if ($header == 'host') { // If the Host header was changed, be sure to update the internal URL $this->setHost((string) $this->getHeader('Host')); } }
/** * {@inheritdoc} */ protected function changedHeader($action, $header) { parent::changedHeader($action, $header); if ($header === 'host') { // If the Host header was changed, be sure to update the internal URL $this->setHost((string) $this->getHeader('Host')); } elseif ($header === 'cookie') { // Be sure to get an cookie updates and update the internal Cookie if ($action === 'set') { $this->cookie = Cookie::factory($this->getHeader('Cookie')); } else { if ($this->cookie) { $this->cookie->clear(); } } } }