MDL-32250 import latest SimpleTest 1.1.0

This commit is contained in:
Petr Skoda
2012-03-29 15:07:53 +02:00
parent a280078197
commit 0bb48cc2a1
88 changed files with 17451 additions and 5860 deletions
+184 -180
View File
@@ -3,7 +3,7 @@
* base include file for SimpleTest
* @package SimpleTest
* @subpackage WebTester
* @version $Id$
* @version $Id: http.php 2011 2011-04-29 08:22:48Z pp11 $
*/
/**#@+
@@ -21,50 +21,50 @@ require_once(dirname(__FILE__) . '/url.php');
* @subpackage WebTester
*/
class SimpleRoute {
var $_url;
private $url;
/**
* Sets the target URL.
* @param SimpleUrl $url URL as object.
* @access public
*/
function SimpleRoute($url) {
$this->_url = $url;
function __construct($url) {
$this->url = $url;
}
/**
* Resource name.
* @return SimpleUrl Current url.
* @access protected
*/
function getUrl() {
return $this->_url;
return $this->url;
}
/**
* Creates the first line which is the actual request.
* @param string $method HTTP request method, usually GET.
* @return string Request line content.
* @access protected
*/
function _getRequestLine($method) {
return $method . ' ' . $this->_url->getPath() .
$this->_url->getEncodedRequest() . ' HTTP/1.0';
protected function getRequestLine($method) {
return $method . ' ' . $this->url->getPath() .
$this->url->getEncodedRequest() . ' HTTP/1.0';
}
/**
* Creates the host part of the request.
* @return string Host line content.
* @access protected
*/
function _getHostLine() {
$line = 'Host: ' . $this->_url->getHost();
if ($this->_url->getPort()) {
$line .= ':' . $this->_url->getPort();
protected function getHostLine() {
$line = 'Host: ' . $this->url->getHost();
if ($this->url->getPort()) {
$line .= ':' . $this->url->getPort();
}
return $line;
}
/**
* Opens a socket to the route.
* @param string $method HTTP request method, usually GET.
@@ -72,21 +72,21 @@ class SimpleRoute {
* @return SimpleSocket New socket.
* @access public
*/
function &createConnection($method, $timeout) {
$default_port = ('https' == $this->_url->getScheme()) ? 443 : 80;
$socket = &$this->_createSocket(
$this->_url->getScheme() ? $this->_url->getScheme() : 'http',
$this->_url->getHost(),
$this->_url->getPort() ? $this->_url->getPort() : $default_port,
function createConnection($method, $timeout) {
$default_port = ('https' == $this->url->getScheme()) ? 443 : 80;
$socket = $this->createSocket(
$this->url->getScheme() ? $this->url->getScheme() : 'http',
$this->url->getHost(),
$this->url->getPort() ? $this->url->getPort() : $default_port,
$timeout);
if (! $socket->isError()) {
$socket->write($this->_getRequestLine($method) . "\r\n");
$socket->write($this->_getHostLine() . "\r\n");
$socket->write($this->getRequestLine($method) . "\r\n");
$socket->write($this->getHostLine() . "\r\n");
$socket->write("Connection: close\r\n");
}
return $socket;
}
/**
* Factory for socket.
* @param string $scheme Protocol to use.
@@ -96,13 +96,14 @@ class SimpleRoute {
* @return SimpleSocket/SimpleSecureSocket New socket.
* @access protected
*/
function &_createSocket($scheme, $host, $port, $timeout) {
if (in_array($scheme, array('https'))) {
$socket = new SimpleSecureSocket($host, $port, $timeout);
protected function createSocket($scheme, $host, $port, $timeout) {
if (in_array($scheme, array('file'))) {
return new SimpleFileSocket($this->url);
} elseif (in_array($scheme, array('https'))) {
return new SimpleSecureSocket($host, $port, $timeout);
} else {
$socket = new SimpleSocket($host, $port, $timeout);
return new SimpleSocket($host, $port, $timeout);
}
return $socket;
}
}
@@ -113,10 +114,10 @@ class SimpleRoute {
* @subpackage WebTester
*/
class SimpleProxyRoute extends SimpleRoute {
var $_proxy;
var $_username;
var $_password;
private $proxy;
private $username;
private $password;
/**
* Stashes the proxy address.
* @param SimpleUrl $url URL as object.
@@ -125,13 +126,13 @@ class SimpleProxyRoute extends SimpleRoute {
* @param string $password Password for autentication.
* @access public
*/
function SimpleProxyRoute($url, $proxy, $username = false, $password = false) {
$this->SimpleRoute($url);
$this->_proxy = $proxy;
$this->_username = $username;
$this->_password = $password;
function __construct($url, $proxy, $username = false, $password = false) {
parent::__construct($url);
$this->proxy = $proxy;
$this->username = $username;
$this->password = $password;
}
/**
* Creates the first line which is the actual request.
* @param string $method HTTP request method, usually GET.
@@ -139,26 +140,26 @@ class SimpleProxyRoute extends SimpleRoute {
* @return string Request line content.
* @access protected
*/
function _getRequestLine($method) {
function getRequestLine($method) {
$url = $this->getUrl();
$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.
* @access protected
*/
function _getHostLine() {
$host = 'Host: ' . $this->_proxy->getHost();
$port = $this->_proxy->getPort() ? $this->_proxy->getPort() : 8080;
function getHostLine() {
$host = 'Host: ' . $this->proxy->getHost();
$port = $this->proxy->getPort() ? $this->proxy->getPort() : 8080;
return "$host:$port";
}
/**
* Opens a socket to the route.
* @param string $method HTTP request method, usually GET.
@@ -166,20 +167,20 @@ class SimpleProxyRoute extends SimpleRoute {
* @return SimpleSocket New socket.
* @access public
*/
function &createConnection($method, $timeout) {
$socket = &$this->_createSocket(
$this->_proxy->getScheme() ? $this->_proxy->getScheme() : 'http',
$this->_proxy->getHost(),
$this->_proxy->getPort() ? $this->_proxy->getPort() : 8080,
function createConnection($method, $timeout) {
$socket = $this->createSocket(
$this->proxy->getScheme() ? $this->proxy->getScheme() : 'http',
$this->proxy->getHost(),
$this->proxy->getPort() ? $this->proxy->getPort() : 8080,
$timeout);
if ($socket->isError()) {
return $socket;
}
$socket->write($this->_getRequestLine($method) . "\r\n");
$socket->write($this->_getHostLine() . "\r\n");
if ($this->_username && $this->_password) {
$socket->write($this->getRequestLine($method) . "\r\n");
$socket->write($this->getHostLine() . "\r\n");
if ($this->username && $this->password) {
$socket->write('Proxy-Authorization: Basic ' .
base64_encode($this->_username . ':' . $this->_password) .
base64_encode($this->username . ':' . $this->password) .
"\r\n");
}
$socket->write("Connection: close\r\n");
@@ -194,11 +195,11 @@ class SimpleProxyRoute extends SimpleRoute {
* @subpackage WebTester
*/
class SimpleHttpRequest {
var $_route;
var $_encoding;
var $_headers;
var $_cookies;
private $route;
private $encoding;
private $headers;
private $cookies;
/**
* Builds the socket request from the different pieces.
* These include proxy information, URL, cookies, headers,
@@ -208,13 +209,13 @@ class SimpleHttpRequest {
* request.
* @access public
*/
function SimpleHttpRequest(&$route, $encoding) {
$this->_route = &$route;
$this->_encoding = $encoding;
$this->_headers = array();
$this->_cookies = array();
function __construct($route, $encoding) {
$this->route = $route;
$this->encoding = $encoding;
$this->headers = array();
$this->cookies = array();
}
/**
* Dispatches the content to the route's socket.
* @param integer $timeout Connection timeout.
@@ -223,15 +224,14 @@ class SimpleHttpRequest {
* complete web page.
* @access public
*/
function &fetch($timeout) {
$socket = &$this->_route->createConnection($this->_encoding->getMethod(), $timeout);
function fetch($timeout) {
$socket = $this->route->createConnection($this->encoding->getMethod(), $timeout);
if (! $socket->isError()) {
$this->_dispatchRequest($socket, $this->_encoding);
$this->dispatchRequest($socket, $this->encoding);
}
$response = &$this->_createResponse($socket);
return $response;
return $this->createResponse($socket);
}
/**
* Sends the headers.
* @param SimpleSocket $socket Open socket.
@@ -240,27 +240,27 @@ class SimpleHttpRequest {
* @param SimpleFormEncoding $encoding Content to send with request.
* @access private
*/
function _dispatchRequest(&$socket, $encoding) {
foreach ($this->_headers as $header_line) {
protected function dispatchRequest($socket, $encoding) {
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");
if (count($this->cookies) > 0) {
$socket->write("Cookie: " . implode(";", $this->cookies) . "\r\n");
}
$encoding->writeHeadersTo($socket);
$socket->write("\r\n");
$encoding->writeTo($socket);
}
/**
* Adds a header line to the request.
* @param string $header_line Text of full header line.
* @access public
*/
function addHeaderLine($header_line) {
$this->_headers[] = $header_line;
$this->headers[] = $header_line;
}
/**
* Reads all the relevant cookies from the
* cookie jar.
@@ -269,20 +269,21 @@ class SimpleHttpRequest {
* @access public
*/
function readCookiesFromJar($jar, $url) {
$this->_cookies = $jar->selectAsPairs($url);
$this->cookies = $jar->selectAsPairs($url);
}
/**
* Wraps the socket in a response parser.
* @param SimpleSocket $socket Responding socket.
* @return SimpleHttpResponse Parsed response object.
* @access protected
*/
function &_createResponse(&$socket) {
protected function createResponse($socket) {
$response = new SimpleHttpResponse(
$socket,
$this->_route->getUrl(),
$this->_encoding);
$this->route->getUrl(),
$this->encoding);
$socket->close();
return $response;
}
}
@@ -293,61 +294,61 @@ class SimpleHttpRequest {
* @subpackage WebTester
*/
class SimpleHttpHeaders {
var $_raw_headers;
var $_response_code;
var $_http_version;
var $_mime_type;
var $_location;
var $_cookies;
var $_authentication;
var $_realm;
private $raw_headers;
private $response_code;
private $http_version;
private $mime_type;
private $location;
private $cookies;
private $authentication;
private $realm;
/**
* Parses the incoming header block.
* @param string $headers Header block.
* @access public
*/
function SimpleHttpHeaders($headers) {
$this->_raw_headers = $headers;
$this->_response_code = false;
$this->_http_version = false;
$this->_mime_type = '';
$this->_location = false;
$this->_cookies = array();
$this->_authentication = false;
$this->_realm = false;
function __construct($headers) {
$this->raw_headers = $headers;
$this->response_code = false;
$this->http_version = false;
$this->mime_type = '';
$this->location = false;
$this->cookies = array();
$this->authentication = false;
$this->realm = false;
foreach (explode("\r\n", $headers) as $header_line) {
$this->_parseHeaderLine($header_line);
$this->parseHeaderLine($header_line);
}
}
/**
* Accessor for parsed HTTP protocol version.
* @return integer HTTP error code.
* @access public
*/
function getHttpVersion() {
return $this->_http_version;
return $this->http_version;
}
/**
* Accessor for raw header block.
* @return string All headers as raw string.
* @access public
*/
function getRaw() {
return $this->_raw_headers;
return $this->raw_headers;
}
/**
* Accessor for parsed HTTP error code.
* @return integer HTTP error code.
* @access public
*/
function getResponseCode() {
return (integer)$this->_response_code;
return (integer)$this->response_code;
}
/**
* Returns the redirected URL or false if
* no redirection.
@@ -355,19 +356,19 @@ class SimpleHttpHeaders {
* @access public
*/
function getLocation() {
return $this->_location;
return $this->location;
}
/**
* Test to see if the response is a valid redirect.
* @return boolean True if valid redirect.
* @access public
*/
function isRedirect() {
return in_array($this->_response_code, array(301, 302, 303, 307)) &&
return in_array($this->response_code, array(301, 302, 303, 307)) &&
(boolean)$this->getLocation();
}
/**
* Test to see if the response is an authentication
* challenge.
@@ -375,46 +376,46 @@ class SimpleHttpHeaders {
* @access public
*/
function isChallenge() {
return ($this->_response_code == 401) &&
(boolean)$this->_authentication &&
(boolean)$this->_realm;
return ($this->response_code == 401) &&
(boolean)$this->authentication &&
(boolean)$this->realm;
}
/**
* Accessor for MIME type header information.
* @return string MIME type.
* @access public
*/
function getMimeType() {
return $this->_mime_type;
return $this->mime_type;
}
/**
* Accessor for authentication type.
* @return string Type.
* @access public
*/
function getAuthentication() {
return $this->_authentication;
return $this->authentication;
}
/**
* Accessor for security realm.
* @return string Realm.
* @access public
*/
function getRealm() {
return $this->_realm;
return $this->realm;
}
/**
* Writes new cookies to the cookie jar.
* @param SimpleCookieJar $jar Jar to write to.
* @param SimpleUrl $url Host and path to write under.
* @access public
*/
function writeCookiesToJar(&$jar, $url) {
foreach ($this->_cookies as $cookie) {
function writeCookiesToJar($jar, $url) {
foreach ($this->cookies as $cookie) {
$jar->setCookie(
$cookie->getName(),
$cookie->getValue(),
@@ -430,33 +431,33 @@ class SimpleHttpHeaders {
* @param string $header_line One line of header.
* @access protected
*/
function _parseHeaderLine($header_line) {
protected function parseHeaderLine($header_line) {
if (preg_match('/HTTP\/(\d+\.\d+)\s+(\d+)/i', $header_line, $matches)) {
$this->_http_version = $matches[1];
$this->_response_code = $matches[2];
$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]);
$this->mime_type = trim($matches[1]);
}
if (preg_match('/Location:\s*(.*)/i', $header_line, $matches)) {
$this->_location = trim($matches[1]);
$this->location = trim($matches[1]);
}
if (preg_match('/Set-cookie:(.*)/i', $header_line, $matches)) {
$this->_cookies[] = $this->_parseCookie($matches[1]);
$this->cookies[] = $this->parseCookie($matches[1]);
}
if (preg_match('/WWW-Authenticate:\s+(\S+)\s+realm=\"(.*?)\"/i', $header_line, $matches)) {
$this->_authentication = $matches[1];
$this->_realm = trim($matches[2]);
$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.
* @access private
*/
function _parseCookie($cookie_line) {
protected function parseCookie($cookie_line) {
$parts = explode(";", $cookie_line);
$cookie = array();
preg_match('/\s*(.*?)\s*=(.*)/', array_shift($parts), $cookie);
@@ -479,12 +480,12 @@ class SimpleHttpHeaders {
* @subpackage WebTester
*/
class SimpleHttpResponse extends SimpleStickyError {
var $_url;
var $_encoding;
var $_sent;
var $_content;
var $_headers;
private $url;
private $encoding;
private $sent;
private $content;
private $headers;
/**
* Constructor. Reads and parses the incoming
* content and headers.
@@ -494,74 +495,77 @@ class SimpleHttpResponse extends SimpleStickyError {
* @param mixed $encoding Record of content sent.
* @access public
*/
function SimpleHttpResponse(&$socket, $url, $encoding) {
$this->SimpleStickyError();
$this->_url = $url;
$this->_encoding = $encoding;
$this->_sent = $socket->getSent();
$this->_content = false;
$raw = $this->_readAll($socket);
function __construct($socket, $url, $encoding) {
parent::__construct();
$this->url = $url;
$this->encoding = $encoding;
$this->sent = $socket->getSent();
$this->content = false;
$raw = $this->readAll($socket);
if ($socket->isError()) {
$this->_setError('Error reading socket [' . $socket->getError() . ']');
$this->setError('Error reading socket [' . $socket->getError() . ']');
return;
}
$this->_parse($raw);
$this->parse($raw);
}
/**
* Splits up the headers and the rest of the content.
* @param string $raw Content to parse.
* @access private
*/
function _parse($raw) {
protected function parse($raw) {
if (! $raw) {
$this->_setError('Nothing fetched');
$this->_headers = new SimpleHttpHeaders('');
$this->setError('Nothing fetched');
$this->headers = new SimpleHttpHeaders('');
} elseif ('file' == $this->url->getScheme()) {
$this->headers = new SimpleHttpHeaders('');
$this->content = $raw;
} elseif (! strstr($raw, "\r\n\r\n")) {
$this->_setError('Could not split headers from content');
$this->_headers = new SimpleHttpHeaders($raw);
$this->setError('Could not split headers from content');
$this->headers = new SimpleHttpHeaders($raw);
} else {
list($headers, $this->_content) = explode("\r\n\r\n", $raw, 2);
$this->_headers = new SimpleHttpHeaders($headers);
list($headers, $this->content) = explode("\r\n\r\n", $raw, 2);
$this->headers = new SimpleHttpHeaders($headers);
}
}
/**
* Original request method.
* @return string GET, POST or HEAD.
* @access public
*/
function getMethod() {
return $this->_encoding->getMethod();
return $this->encoding->getMethod();
}
/**
* Resource name.
* @return SimpleUrl Current url.
* @access public
*/
function getUrl() {
return $this->_url;
return $this->url;
}
/**
* Original request data.
* @return mixed Sent content.
* @access public
*/
function getRequestData() {
return $this->_encoding;
return $this->encoding;
}
/**
* Raw request that was sent down the wire.
* @return string Bytes actually sent.
* @access public
*/
function getSent() {
return $this->_sent;
return $this->sent;
}
/**
* Accessor for the content after the last
* header line.
@@ -569,9 +573,9 @@ class SimpleHttpResponse extends SimpleStickyError {
* @access public
*/
function getContent() {
return $this->_content;
return $this->content;
}
/**
* Accessor for header block. The response is the
* combination of this and the content.
@@ -579,18 +583,18 @@ class SimpleHttpResponse extends SimpleStickyError {
* @access public
*/
function getHeaders() {
return $this->_headers;
return $this->headers;
}
/**
* Accessor for any new cookies.
* @return array List of new cookies.
* @access public
*/
function getNewCookies() {
return $this->_headers->getNewCookies();
return $this->headers->getNewCookies();
}
/**
* Reads the whole of the socket output into a
* single string.
@@ -599,14 +603,14 @@ class SimpleHttpResponse extends SimpleStickyError {
* else false.
* @access private
*/
function _readAll(&$socket) {
protected function readAll($socket) {
$all = '';
while (! $this->_isLastPacket($next = $socket->read())) {
while (! $this->isLastPacket($next = $socket->read())) {
$all .= $next;
}
return $all;
}
/**
* Test to see if the packet from the socket is the
* last one.
@@ -614,11 +618,11 @@ class SimpleHttpResponse extends SimpleStickyError {
* @return boolean True if empty or EOF.
* @access private
*/
function _isLastPacket($packet) {
protected function isLastPacket($packet) {
if (is_string($packet)) {
return $packet === '';
}
return ! $packet;
}
}
?>
?>