Source for file http.php
Documentation is available at http.php
* base include file for SimpleTest
* @version $Id: http.php 1722 2008-04-07 19:30:56Z lastcraft $
* 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.
if (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.
* 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',
if ($socket->isError()) {
$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
* 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()) {
$this->_dispatchRequest($socket, $this->_encoding);
* @param SimpleSocket $socket Open socket.
* @param string $method HTTP request method,
* @param SimpleFormEncoding $encoding Content to send with request.
function _dispatchRequest(&$socket, $encoding) {
foreach ($this->_headers as $header_line) {
$socket->write($header_line .
"\r\n");
$encoding->writeHeadersTo($socket);
$encoding->writeTo($socket);
* Adds a header line to the request.
* @param string $header_line Text of full 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.
foreach (split("\r\n", $headers) as $header_line) {
* Accessor for parsed HTTP protocol version.
* @return integer HTTP error code.
* Accessor for raw header block.
* @return string All headers as raw string.
* Accessor for parsed HTTP error code.
* @return integer HTTP error 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.
* Test to see if the response is an authentication
* @return boolean True if challenge.
* Accessor for MIME type header information.
* @return string MIME type.
* Accessor for authentication type.
* 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.
* 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)) {
if (preg_match('/Content-type:\s*(.*)/i', $header_line, $matches)) {
if (preg_match('/Location:\s*(.*)/i', $header_line, $matches)) {
if (preg_match('/Set-cookie:(.*)/i', $header_line, $matches)) {
$this->_cookies[] =
$this->_parseCookie($matches[1]);
if (preg_match('/WWW-Authenticate:\s+(\S+)\s+realm=\"(.*?)\"/i', $header_line, $matches)) {
* Parse the Set-cookie content.
* @param string $cookie_line Text after "Set-cookie:"
* @return SimpleCookie New cookie object.
function _parseCookie($cookie_line) {
$parts =
split(";", $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->_sent =
$socket->getSent();
$raw =
$this->_readAll($socket);
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.
} elseif (! strstr($raw, "\r\n\r\n")) {
$this->_setError('Could not split headers from content');
* Original request method.
* @return string GET, POST or HEAD.
* @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
function _readAll(&$socket) {
while (! $this->_isLastPacket($next =
$socket->read())) {
* Test to see if the packet from the socket is the
* @param string $packet Chunk to interpret.
* @return boolean True if empty or EOF.
function _isLastPacket($packet) {
Documentation generated on Sun, 04 May 2008 09:21:41 -0500 by phpDocumentor 1.3.0