/** * Initialize the Response * * @return void */ protected function _init() { parent::_init(); if ($this->_config['message']) { $this->body = $this->_parseMessage($this->_config['message']); } if (isset($this->headers['Transfer-Encoding'])) { $this->body = $this->_httpChunkedDecode($this->body); } if (isset($this->headers['Content-Type'])) { $pattern = '/([-\\w\\/\\.+]+)(;\\s*?charset=(.+))?/i'; preg_match($pattern, $this->headers['Content-Type'], $match); if (isset($match[1])) { $this->type = trim($match[1]); $this->body = $this->_decode($this->body); } if (isset($match[3])) { $this->encoding = strtoupper(trim($match[3])); } } }
protected function _init() { parent::_init(); $body = $this->_config['body']; if ($this->_config['body'] && !$this->_config['message']) { $this->body = $this->_config['body']; } elseif (($body = $this->_config['message']) && !$this->_config['body']) { $body = $this->_parseMessage($body); } if (isset($this->headers['Content-Type'])) { preg_match('/^(.*?);\\s*?charset=(.+)/i', $this->headers['Content-Type'], $match); if ($match) { $this->type = trim($match[1]); $this->encoding = strtoupper(trim($match[2])); } } if (isset($this->headers['Transfer-Encoding'])) { $body = $this->_decode($body); } $this->body = $this->body ?: $body; }
/** * Sets/Gets the content type. If `'type'` is null, the method will attempt to determine the * type first, from the params, then from the environment setting * * @param string $type a full content type i.e. `'application/json'` or simple name `'json'` * @return string A simple content type name, i.e. `'html'`, `'xml'`, `'json'`, etc., depending * on the content type of the request. */ public function type($type = null) { if ($type === null) { $type = $this->type; if (!$type) { $type = $this->env('CONTENT_TYPE'); } } return parent::type($type); }
/** * Converts the data in the record set to a different format, i.e. an array. Available * options: array, URL, stream context configuration, or string. * * @see lithium\net\Message::to() * @param string $format Format to convert to. Should be either `'url'`, which returns a string * representation of the URL that this request points to, or `'context'`, which returns an * array usable with PHP's `stream_context_create()` function. For more available formats, * see the parent method, `lithium\net\Message::to()`. * @param array $options Allows overriding of specific portions of the URL, as follows. These * options should only be specified if you intend to replace the values that are already in * the `Request` object. * - `'scheme'` _string_: The protocol scheme of the URL. * - `'method'` _string_: If applicable, the HTTP method to use in the request. Mainly * applies to the `'context'` format. * - `'host'` _string_: The host name the request is pointing at. * - `'port'` _string_: The host port, if any. * - `'path'` _string_: The URL path. * - `'query'` _mixed_: The query string of the URL as a string or array. * - `'auth'` _string_: Authentication information. See the constructor for details. * - `'content'` _string_: The body of the request. * - `'headers'` _array_: The request headers. * - `'version'` _string_: The HTTP version of the request, where applicable. * @return mixed Varies; see the `$format` parameter for possible return values. */ public function to($format, array $options = array()) { $defaults = array('method' => $this->method, 'scheme' => $this->scheme, 'host' => $this->host, 'port' => $this->port, 'path' => $this->path, 'query' => null, 'auth' => $this->auth, 'username' => $this->username, 'password' => $this->password, 'headers' => array(), 'cookies' => array(), 'proxy' => $this->_config['proxy'], 'body' => null, 'version' => $this->version, 'ignore_errors' => $this->_config['ignoreErrors'], 'follow_location' => $this->_config['followLocation'], 'request_fulluri' => (bool) $this->_config['proxy']); $options += $defaults; if (is_string($options['query'])) { $options['query'] = "?" . $options['query']; } elseif ($options['query']) { $options['query'] = "?" . http_build_query($options['query']); } elseif ($options['query'] === null) { $options['query'] = $this->queryString(); } if ($options['auth']) { $data = array(); if (is_array($options['auth']) && !empty($options['auth']['nonce'])) { $data = array('method' => $options['method'], 'uri' => $options['path']); $data += $options['auth']; } $auth = $this->_classes['auth']; $data = $auth::encode($options['username'], $options['password'], $data); $this->headers('Authorization', $auth::header($data)); } if ($this->cookies($options['cookies'])) { $this->headers('Cookie', $this->_cookies()); } $body = $this->body($options['body']); if ($body || !in_array($options['method'], array('GET', 'HEAD', 'DELETE'))) { $this->headers('Content-Length', strlen($body)); } $conv = isset($this->_formats[$format]) ? $this->_formats[$format] : null; return $conv ? $conv($this, $options, $defaults) : parent::to($format, $options); }
/** * Converts the data in the record set to a different format, i.e. an array. Available * options: array, url, context, or string. * * @param string $format Format to convert to. * @param array $options * @return mixed */ public function to($format, array $options = array()) { switch ($format) { case 'url': $query = $this->queryString(); $host = $this->host . ($this->port ? ":{$this->port}" : ''); return "{$this->scheme}://{$host}{$this->path}{$query}"; case 'context': if ($this->_config['auth']) { $auth = base64_encode("{$this->username}:{$this->password}"); $this->headers('Authorization', "{$this->_config['auth']} {$auth}"); } $content = $this->body(); $this->headers('Content-Length', strlen($content)); $defaults = array('method' => $this->method, 'header' => $this->headers(), 'content' => $content, 'protocol_version' => $this->version, 'ignore_errors' => true); return array('http' => $options + $defaults); default: return parent::to($format, $options); } }
/** * Converts the data in the record set to a different format, i.e. an array. Available * options: array, URL, stream context configuration, or string. * * @see lithium\net\Message::to() * @param string $format Format to convert to. Should be either `'url'`, which returns a string * representation of the URL that this request points to, or `'context'`, which * returns an array usable with PHP's `stream_context_create()` function. For * more available formats, see the parent method, `lithium\net\Message::to()`. * @param array $options Allows overriding of specific portions of the URL, as follows. These * options should only be specified if you intend to replace the values that are * already in the `Request` object. * - `'scheme'` _string_: The protocol scheme of the URL. * - `'method'` _string_: If applicable, the HTTP method to use in the request. * Mainly applies to the `'context'` format. * - `'host'` _string_: The host name the request is pointing at. * - `'port'` _string_: The host port, if any. If specified, should be prefixed * with `':'`. * - `'path'` _string_: The URL path. * - `'query'` _mixed_: The query string of the URL as a string or array. If passed * as a string, should be prefixed with `'?'`. * - `'auth'` _string_: Authentication information. See the constructor for * details. * - `'content'` _string_: The body of the request. * - `'headers'` _array_: The request headers. * - `'version'` _string_: The HTTP version of the request, where applicable. * @return mixed Varies; see the `$format` parameter for possible return values. */ public function to($format, array $options = array()) { $defaults = array('method' => $this->method, 'scheme' => $this->scheme, 'host' => $this->host, 'port' => $this->port ? ":{$this->port}" : '', 'path' => $this->path, 'query' => $this->queryString(), 'auth' => $this->_config['auth'], 'content' => $this->body(), 'version' => $this->version); $options += $defaults; if ($options['query'] && is_array($options['query'])) { $options['query'] = $this->queryString($options['query']); } switch ($format) { case 'url': return String::insert("{:scheme}://{:host}{:port}{:path}{:query}", $options); case 'context': if ($options['auth']) { $auth = base64_encode("{$this->username}:{$this->password}"); $this->headers('Authorization', "{$options['auth']} {$auth}"); } $this->headers('Content-Length', strlen($options['content'])); $base = array('content' => $options['content'], 'method' => $options['method'], 'header' => $this->headers(), 'protocol_version' => $options['version'], 'ignore_errors' => true); return array('http' => array_diff_key($options, $defaults) + $base); default: return parent::to($format, $options); } }
/** * Add data to or compile and return the HTTP message body, optionally decoding its parts * according to content type. * * @see lithium\net\Message::body() * @see lithium\net\http\Message::_decode() * @param mixed $data * @param array $options * - `'buffer'` _integer_: split the body string * - `'encode'` _boolean_: encode the body based on the content type * - `'decode'` _boolean_: decode the body based on the content type * @return array */ public function body($data = null, $options = array()) { $defaults = array('decode' => true); return parent::body($data, $options + $defaults); }
/** * Converts the data in the record set to a different format, i.e. an array. Available * options: array, URL, stream context configuration, or string. * * @see lithium\net\Message::to() * @param string $format Format to convert to. Should be either `'url'`, which returns a string * representation of the URL that this request points to, or `'context'`, which * returns an array usable with PHP's `stream_context_create()` function. For * more available formats, see the parent method, `lithium\net\Message::to()`. * @param array $options Allows overriding of specific portions of the URL, as follows. These * options should only be specified if you intend to replace the values that are * already in the `Request` object. * - `'scheme'` _string_: The protocol scheme of the URL. * - `'method'` _string_: If applicable, the HTTP method to use in the request. * Mainly applies to the `'context'` format. * - `'host'` _string_: The host name the request is pointing at. * - `'port'` _string_: The host port, if any. If specified, should be prefixed * with `':'`. * - `'path'` _string_: The URL path. * - `'query'` _mixed_: The query string of the URL as a string or array. If passed * as a string, should be prefixed with `'?'`. * - `'auth'` _string_: Authentication information. See the constructor for * details. * - `'content'` _string_: The body of the request. * - `'headers'` _array_: The request headers. * - `'version'` _string_: The HTTP version of the request, where applicable. * @return mixed Varies; see the `$format` parameter for possible return values. */ public function to($format, array $options = array()) { $defaults = array('method' => $this->method, 'scheme' => $this->scheme, 'host' => $this->host, 'port' => $this->port ? ":{$this->port}" : '', 'path' => $this->path, 'query' => null, 'auth' => $this->auth, 'username' => $this->username, 'password' => $this->password, 'headers' => array(), 'body' => null, 'version' => $this->version, 'ignore_errors' => isset($this->_config['ignore_errors']) ? $this->_config['ignore_errors'] : true, 'follow_location' => isset($this->_config['follow_location']) ? $this->_config['follow_location'] : true); $options += $defaults; if (!empty($options['auth'])) { $data = array(); if (is_array($options['auth']) && !empty($options['auth']['nonce'])) { $data = array('method' => $options['method'], 'uri' => $options['path']); $data += $options['auth']; } $auth = $this->_classes['auth']; $data = $auth::encode($options['username'], $options['password'], $data); $this->headers('Authorization', $auth::header($data)); } if (in_array($options['method'], array('POST', 'PUT'))) { $media = $this->_classes['media']; if ($type = $media::type($this->_type)) { $this->headers('Content-Type', $type['content'][0]); } } $body = $this->body($options['body']); $this->headers('Content-Length', strlen($body)); switch ($format) { case 'url': $options['query'] = $this->queryString($options['query']); $options['path'] = str_replace('//', '/', $options['path']); return String::insert("{:scheme}://{:host}{:port}{:path}{:query}", $options); case 'context': $base = array('content' => $body, 'method' => $options['method'], 'header' => $this->headers($options['headers']), 'protocol_version' => $options['version'], 'ignore_errors' => $options['ignore_errors'], 'follow_location' => $options['follow_location']); return array('http' => array_diff_key($options, $defaults) + $base); case 'string': $path = str_replace('//', '/', $this->path) . $this->queryString($options['query']); $status = "{$this->method} {$path} {$this->protocol}"; return join("\r\n", array($status, join("\r\n", $this->headers()), "", $body)); default: return parent::to($format, $options); } }