http.php

来自「一款可以和GOOGLE媲美的开源统计系统,运用AJAX.功能强大. 无色提示:」· PHP 代码 · 共 624 行 · 第 1/2 页

PHP
624
字号
<?php    /**     *	base include file for SimpleTest     *	@package	SimpleTest     *	@subpackage	WebTester     *	@version	$Id: http.php 163 2008-01-14 04:40:16Z matt $     */    /**#@+     *	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     *    a HTTP request.	 *    @package SimpleTest	 *    @subpackage WebTester     */    class SimpleRoute {        var $_url;                /**         *    Sets the target URL.         *    @param SimpleUrl $url   URL as object.         *    @access public         */        function SimpleRoute($url) {            $this->_url = $url;        }                /**         *    Resource name.         *    @return SimpleUrl        Current url.         *    @access protected         */        function getUrl() {            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';        }                /**         *    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();            }            return $line;        }                /**         *    Opens a socket to the route.         *    @param string $method      HTTP request method, usually GET.         *    @param integer $timeout    Connection timeout.         *    @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,                    $timeout);            if (! $socket->isError()) {                $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.         *    @param string $host                     Hostname to connect to.         *    @param integer $port                    Remote port.         *    @param integer $timeout                 Connection timeout.         *    @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);            } else {                $socket = &new SimpleSocket($host, $port, $timeout);            }            return $socket;        }    }        /**     *    Creates HTTP headers for the end point of     *    a HTTP request via a proxy server.	 *    @package SimpleTest	 *    @subpackage WebTester     */    class SimpleProxyRoute extends SimpleRoute {        var $_proxy;        var $_username;        var $_password;                /**         *    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.         *    @access public         */        function SimpleProxyRoute($url, $proxy, $username = false, $password = false) {            $this->SimpleRoute($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.         *    @param SimpleUrl $url   URL as object.         *    @return string          Request line content.         *    @access protected         */        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;            return "$host:$port";        }                /**         *    Opens a socket to the route.         *    @param string $method       HTTP request method, usually GET.         *    @param integer $timeout     Connection timeout.         *    @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,                    $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('Proxy-Authorization: Basic ' .                        base64_encode($this->_username . ':' . $this->_password) .                        "\r\n");            }            $socket->write("Connection: close\r\n");            return $socket;        }    }    /**     *    HTTP request for a web page. Factory for     *    HttpResponse object.	 *    @package SimpleTest	 *    @subpackage WebTester     */    class SimpleHttpRequest {        var $_route;        var $_encoding;        var $_headers;        var $_cookies;                /**         *    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         *                                           request.         *    @access public         */        function SimpleHttpRequest(&$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.         *    @return SimpleHttpResponse   A response which may only have         *                                 an error, but hopefully has a         *                                 complete web page.         *    @access public         */        function &fetch($timeout) {            $socket = &$this->_route->createConnection($this->_encoding->getMethod(), $timeout);            if (! $socket->isError()) {                $this->_dispatchRequest($socket, $this->_encoding);            }            $response = &$this->_createResponse($socket);            return $response;        }                /**         *    Sends the headers.         *    @param SimpleSocket $socket           Open socket.         *    @param string $method                 HTTP request method,         *                                          usually GET.         *    @param SimpleFormEncoding $encoding   Content to send with request.         *    @access private         */        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");            }            $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;        }                /**         *    Reads all the relevant cookies from the         *    cookie jar.         *    @param SimpleCookieJar $jar     Jar to read         *    @param SimpleUrl $url           Url to use for scope.         *    @access public         */        function readCookiesFromJar($jar, $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) {            $response = &new SimpleHttpResponse(                    $socket,                    $this->_route->getUrl(),                    $this->_encoding);            return $response;        }    }        /**     *    Collection of header lines in the response.	 *    @package SimpleTest	 *    @subpackage WebTester     */    class SimpleHttpHeaders {        var $_raw_headers;        var $_response_code;        var $_http_version;        var $_mime_type;        var $_location;        var $_cookies;        var $_authentication;        var $_realm;                /**         *    Parses the incoming header block.         *    @param string $headers     Header block.         *    @access public         */        function SimpleHttpHeaders($headers) {            $this->_raw_headers = $headers;            $this->_response_code = false;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?