_httpVersion; $this->_httpVersion = $version; return $old; } /** * Get request HTTP version. * * @return float */ public function getHttpVersion() { return $this->_httpVersion; } /** * Parse a HTTP packet. * * @param string $packet HTTP packet. * @return void * @throws \Hoa\Http\Exception */ abstract public function parse($packet); /** * Helper to parse HTTP headers and distribute them in array accesses. * * @param array $headers Headers to parse and distribute. * @return array */ protected function _parse(array $headers) { unset($this->_headers); $this->_headers = []; foreach ($headers as $header) { list($name, $value) = explode(':', $header, 2); $this->_headers[strtolower($name)] = trim($value); } return $this->_headers; } /** * Get headers. * * @return array */ public function getHeaders() { return $this->_headers; } /** * Get headers (formatted). * * @return array */ public function getHeadersFormatted() { $out = []; foreach ($this->getHeaders() as $header => $value) { if ('x-' == strtolower(substr($header, 0, 2))) { $header = 'http_' . $header; } $out[strtoupper(str_replace('-', '_', $header))] = $value; } return $out; } /** * Check if header exists. * * @param string $offset Header. * @return bool */ public function offsetExists($offset) { return array_key_exists($offset, $this->_headers); } /** * Get a header's value. * * @param string $offset Header. * @return string */ public function offsetGet($offset) { if (false === $this->offsetExists($offset)) { return null; } return $this->_headers[$offset]; } /** * Set a value to a header. * * @param string $offset Header. * @param string $value Value. * @return void */ public function offsetSet($offset, $value) { $this->_headers[$offset] = $value; return; } /** * Unset a header. * * @param string $offset Header. * @return void */ public function offsetUnset($offset) { unset($this->_headers[$offset]); return; } /** * Get iterator. * * @return \ArrayIterator */ public function getIterator() { return new \ArrayIterator($this->getHeaders()); } /** * Count number of headers. * * @return int */ public function count() { return count($this->getHeaders()); } /** * Set request body. * * @param string $body Body. * @return string */ public function setBody($body) { $old = $this->_body; $this->_body = $body; return $old; } /** * Get request body. * * @return string */ public function getBody() { return $this->_body; } /** * Dump (parse^-1). * * @return string */ public function __toString() { $out = null; foreach ($this->getHeaders() as $key => $value) { $out .= $key . ': ' . $value . CRLF; } return $out; } } /** * Flex entity. */ Consistency::flexEntity('Hoa\Http\Http');