Source for file http.php
Documentation is available at http.php
* base include file for SimpleTest
* @version $Id: http.php 1829 2008-12-08 17:56:37Z edwardzyang $
* include other SimpleTest class files
require_once(dirname(__FILE__
) .
'/socket.php');
require_once(dirname(__FILE__
) .
'/cookies.php');
require_once(dirname(__FILE__
) .
'/url.php');
* Creates HTTP headers for the end point of
* @param SimpleUrl $url URL as object.
* @return SimpleUrl Current url.
* Creates the first line which is the actual request.
* @param string $method HTTP request method, usually GET.
* @return string Request line content.
return $method .
' ' .
$this->url->getPath() .
$this->url->getEncodedRequest() .
' HTTP/1.0';
* Creates the host part of the request.
* @return string Host line content.
$line =
'Host: ' .
$this->url->getHost();
if ($this->url->getPort()) {
$line .=
':' .
$this->url->getPort();
* Opens a socket to the route.
* @param string $method HTTP request method, usually GET.
* @param integer $timeout Connection timeout.
* @return SimpleSocket New socket.
$default_port =
('https' ==
$this->url->getScheme()) ?
443 :
80;
$this->url->getScheme() ?
$this->url->getScheme() :
'http',
$this->url->getPort() ?
$this->url->getPort() :
$default_port,
if (! $socket->isError()) {
$socket->write("Connection: close\r\n");
* @param string $scheme Protocol to use.
* @param string $host Hostname to connect to.
* @param integer $port Remote port.
* @param integer $timeout Connection timeout.
* @return SimpleSocket/SimpleSecureSocket New socket.
protected function createSocket($scheme, $host, $port, $timeout) {
} elseif (in_array($scheme, array('https'))) {
* Creates HTTP headers for the end point of
* a HTTP request via a proxy server.
* Stashes the proxy address.
* @param SimpleUrl $url URL as object.
* @param string $proxy Proxy URL.
* @param string $username Username for autentication.
* @param string $password Password for autentication.
function __construct($url, $proxy, $username =
false, $password =
false) {
$this->username =
$username;
$this->password =
$password;
* Creates the first line which is the actual request.
* @param string $method HTTP request method, usually GET.
* @param SimpleUrl $url URL as object.
* @return string Request line content.
$scheme =
$url->getScheme() ?
$url->getScheme() :
'http';
$port =
$url->getPort() ?
':' .
$url->getPort() :
'';
return $method .
' ' .
$scheme .
'://' .
$url->getHost() .
$port .
$url->getPath() .
$url->getEncodedRequest() .
' HTTP/1.0';
* Creates the host part of the request.
* @param SimpleUrl $url URL as object.
* @return string Host line content.
$host =
'Host: ' .
$this->proxy->getHost();
$port =
$this->proxy->getPort() ?
$this->proxy->getPort() :
8080;
* Opens a socket to the route.
* @param string $method HTTP request method, usually GET.
* @param integer $timeout Connection timeout.
* @return SimpleSocket New socket.
$this->proxy->getScheme() ?
$this->proxy->getScheme() :
'http',
$this->proxy->getPort() ?
$this->proxy->getPort() :
8080,
if ($socket->isError()) {
if ($this->username &&
$this->password) {
$socket->write('Proxy-Authorization: Basic ' .
$socket->write("Connection: close\r\n");
* HTTP request for a web page. Factory for
* Builds the socket request from the different pieces.
* These include proxy information, URL, cookies, headers,
* request method and choice of encoding.
* @param SimpleRoute $route Request route.
* @param SimpleFormEncoding $encoding Content to send with
$this->encoding =
$encoding;
$this->headers =
array();
$this->cookies =
array();
* Dispatches the content to the route's socket.
* @param integer $timeout Connection timeout.
* @return SimpleHttpResponse A response which may only have
* an error, but hopefully has a
function fetch($timeout) {
$socket =
$this->route->createConnection($this->encoding->getMethod(), $timeout);
if (! $socket->isError()) {
* @param SimpleSocket $socket Open socket.
* @param string $method HTTP request method,
* @param SimpleFormEncoding $encoding Content to send with request.
foreach ($this->headers as $header_line) {
$socket->write($header_line .
"\r\n");
if (count($this->cookies) >
0) {
$socket->write("Cookie: " .
implode(";", $this->cookies) .
"\r\n");
$encoding->writeHeadersTo($socket);
$encoding->writeTo($socket);
* Adds a header line to the request.
* @param string $header_line Text of full header line.
$this->headers[] =
$header_line;
* Reads all the relevant cookies from the
* @param SimpleCookieJar $jar Jar to read
* @param SimpleUrl $url Url to use for scope.
$this->cookies =
$jar->selectAsPairs($url);
* Wraps the socket in a response parser.
* @param SimpleSocket $socket Responding socket.
* @return SimpleHttpResponse Parsed response object.
* Collection of header lines in the response.
* Parses the incoming header block.
* @param string $headers Header block.
$this->raw_headers =
$headers;
$this->response_code =
false;
$this->http_version =
false;
$this->cookies =
array();
$this->authentication =
false;
foreach (explode("\r\n", $headers) as $header_line) {
* Accessor for parsed HTTP protocol version.
* @return integer HTTP error code.
return $this->http_version;
* Accessor for raw header block.
* @return string All headers as raw string.
return $this->raw_headers;
* Accessor for parsed HTTP error code.
* @return integer HTTP error code.
return (integer)
$this->response_code;
* Returns the redirected URL or false if
* @return string URL or false for none.
* Test to see if the response is a valid redirect.
* @return boolean True if valid redirect.
return in_array($this->response_code, array(301, 302, 303, 307)) &&
* Test to see if the response is an authentication
* @return boolean True if challenge.
return ($this->response_code ==
401) &&
(boolean)
$this->authentication &&
* Accessor for MIME type header information.
* @return string MIME type.
* Accessor for authentication type.
return $this->authentication;
* Accessor for security realm.
* Writes new cookies to the cookie jar.
* @param SimpleCookieJar $jar Jar to write to.
* @param SimpleUrl $url Host and path to write under.
foreach ($this->cookies as $cookie) {
* Called on each header line to accumulate the held
* @param string $header_line One line of header.
if (preg_match('/HTTP\/(\d+\.\d+)\s+(\d+)/i', $header_line, $matches)) {
$this->http_version =
$matches[1];
$this->response_code =
$matches[2];
if (preg_match('/Content-type:\s*(.*)/i', $header_line, $matches)) {
$this->mime_type =
trim($matches[1]);
if (preg_match('/Location:\s*(.*)/i', $header_line, $matches)) {
$this->location =
trim($matches[1]);
if (preg_match('/Set-cookie:(.*)/i', $header_line, $matches)) {
if (preg_match('/WWW-Authenticate:\s+(\S+)\s+realm=\"(.*?)\"/i', $header_line, $matches)) {
$this->authentication =
$matches[1];
$this->realm =
trim($matches[2]);
* Parse the Set-cookie content.
* @param string $cookie_line Text after "Set-cookie:"
* @return SimpleCookie New cookie object.
$parts =
explode(";", $cookie_line);
foreach ($parts as $part) {
if (preg_match('/\s*(.*?)\s*=(.*)/', $part, $matches)) {
$cookie[$matches[1]] =
trim($matches[2]);
isset
($cookie["path"]) ?
$cookie["path"] :
"",
isset
($cookie["expires"]) ?
$cookie["expires"] :
false);
* Constructor. Reads and parses the incoming
* @param SimpleSocket $socket Network connection to fetch
* @param SimpleUrl $url Resource name.
* @param mixed $encoding Record of content sent.
$this->encoding =
$encoding;
$this->sent =
$socket->getSent();
if ($socket->isError()) {
$this->setError('Error reading socket [' .
$socket->getError() .
']');
* Splits up the headers and the rest of the content.
* @param string $raw Content to parse.
protected function parse($raw) {
} elseif ('file' ==
$this->url->getScheme()) {
} elseif (! strstr($raw, "\r\n\r\n")) {
$this->setError('Could not split headers from content');
list
($headers, $this->content) =
explode("\r\n\r\n", $raw, 2);
* Original request method.
* @return string GET, POST or HEAD.
return $this->encoding->getMethod();
* @return SimpleUrl Current url.
* @return mixed Sent content.
* Raw request that was sent down the wire.
* @return string Bytes actually sent.
* Accessor for the content after the last
* @return string All content.
* Accessor for header block. The response is the
* combination of this and the content.
* @return SimpleHeaders Wrapped header block.
* Accessor for any new cookies.
* @return array List of new cookies.
return $this->headers->getNewCookies();
* Reads the whole of the socket output into a
* @param SimpleSocket $socket Unread socket.
* @return string Raw output if successful
protected function readAll($socket) {
* Test to see if the packet from the socket is the
* @param string $packet Chunk to interpret.
* @return boolean True if empty or EOF.
Documentation generated on Sun, 31 Oct 2010 16:31:38 -0500 by phpDocumentor 1.4.3