| version 1.1, 2005/06/05 12:14:22 |
version 1.2, 2005/11/07 20:37:40 |
| Line 128 class HTTP_Request { |
|
| Line 128 class HTTP_Request { |
|
| |
|
| /** |
/** |
| * Post data |
* Post data |
| * @var mixed |
* @var array |
| */ |
*/ |
| var $_postData; |
var $_postData; |
| |
|
| /** |
/** |
| |
* Request body |
| |
* @var string |
| |
*/ |
| |
var $_body; |
| |
|
| |
/** |
| |
* A list of methods that MUST NOT have a request body, per RFC 2616 |
| |
* @var array |
| |
*/ |
| |
var $_bodyDisallowed = array('TRACE'); |
| |
|
| |
/** |
| * Files to post |
* Files to post |
| * @var array |
* @var array |
| */ |
*/ |
| Line 229 class HTTP_Request { |
|
| Line 241 class HTTP_Request { |
|
| $this->_method = HTTP_REQUEST_METHOD_GET; |
$this->_method = HTTP_REQUEST_METHOD_GET; |
| $this->_http = HTTP_REQUEST_HTTP_VER_1_1; |
$this->_http = HTTP_REQUEST_HTTP_VER_1_1; |
| $this->_requestHeaders = array(); |
$this->_requestHeaders = array(); |
| $this->_postData = null; |
$this->_postData = array(); |
| |
$this->_body = null; |
| |
|
| $this->_user = null; |
$this->_user = null; |
| $this->_pass = null; |
$this->_pass = null; |
| Line 262 class HTTP_Request { |
|
| Line 275 class HTTP_Request { |
|
| |
|
| // Basic authentication |
// Basic authentication |
| if (!empty($this->_user)) { |
if (!empty($this->_user)) { |
| $this->_requestHeaders['Authorization'] = 'Basic ' . base64_encode($this->_user . ':' . $this->_pass); |
$this->addHeader('Authorization', 'Basic ' . base64_encode($this->_user . ':' . $this->_pass)); |
| } |
} |
| |
|
| // Use gzip encoding if possible |
// Use gzip encoding if possible |
| Line 398 class HTTP_Request { |
|
| Line 411 class HTTP_Request { |
|
| */ |
*/ |
| function addHeader($name, $value) |
function addHeader($name, $value) |
| { |
{ |
| $this->_requestHeaders[$name] = $value; |
$this->_requestHeaders[strtolower($name)] = $value; |
| } |
} |
| |
|
| /** |
/** |
| Line 409 class HTTP_Request { |
|
| Line 422 class HTTP_Request { |
|
| */ |
*/ |
| function removeHeader($name) |
function removeHeader($name) |
| { |
{ |
| if (isset($this->_requestHeaders[$name])) { |
if (isset($this->_requestHeaders[strtolower($name)])) { |
| unset($this->_requestHeaders[$name]); |
unset($this->_requestHeaders[strtolower($name)]); |
| } |
} |
| } |
} |
| |
|
| Line 509 class HTTP_Request { |
|
| Line 522 class HTTP_Request { |
|
| } |
} |
| |
|
| /** |
/** |
| * Adds raw postdata |
* Adds raw postdata (DEPRECATED) |
| * |
* |
| * @param string The data |
* @param string The data |
| * @param bool Whether data is preencoded or not, default = already encoded |
* @param bool Whether data is preencoded or not, default = already encoded |
| * @access public |
* @access public |
| |
* @deprecated deprecated since 1.3.0, method addBody() should be used instead |
| */ |
*/ |
| function addRawPostData($postdata, $preencoded = true) |
function addRawPostData($postdata, $preencoded = true) |
| { |
{ |
| $this->_postData = $preencoded ? $postdata : urlencode($postdata); |
$this->_body = $preencoded ? $postdata : urlencode($postdata); |
| |
} |
| |
|
| |
/** |
| |
* Sets the request body (for POST, PUT and similar requests) |
| |
* |
| |
* @param string Request body |
| |
* @access public |
| |
*/ |
| |
function setBody($body) |
| |
{ |
| |
$this->_body = $body; |
| } |
} |
| |
|
| /** |
/** |
| Line 542 class HTTP_Request { |
|
| Line 567 class HTTP_Request { |
|
| */ |
*/ |
| function addCookie($name, $value) |
function addCookie($name, $value) |
| { |
{ |
| $cookies = isset($this->_requestHeaders['Cookie']) ? $this->_requestHeaders['Cookie']. '; ' : ''; |
$cookies = isset($this->_requestHeaders['cookie']) ? $this->_requestHeaders['cookie']. '; ' : ''; |
| $this->addHeader('Cookie', $cookies . $name . '=' . $value); |
$this->addHeader('Cookie', $cookies . $name . '=' . $value); |
| } |
} |
| |
|
| Line 585 class HTTP_Request { |
|
| Line 610 class HTTP_Request { |
|
| $host = 'ssl://' . $host; |
$host = 'ssl://' . $host; |
| } |
} |
| |
|
| |
// magic quotes may fuck up file uploads and chunked response processing |
| |
$magicQuotes = ini_get('magic_quotes_runtime'); |
| |
ini_set('magic_quotes_runtime', false); |
| |
|
| // If this is a second request, we may get away without |
// If this is a second request, we may get away without |
| // re-connecting if they're on the same server |
// re-connecting if they're on the same server |
| if (PEAR::isError($err = $this->_sock->connect($host, $port, null, $this->_timeout, $this->_socketOptions)) || |
$err = $this->_sock->connect($host, $port, null, $this->_timeout, $this->_socketOptions); |
| PEAR::isError($err = $this->_sock->write($this->_buildRequest()))) { |
PEAR::isError($err) or $err = $this->_sock->write($this->_buildRequest()); |
| |
|
| return $err; |
if (!PEAR::isError($err)) { |
| } |
if (!empty($this->_readTimeout)) { |
| if (!empty($this->_readTimeout)) { |
$this->_sock->setTimeout($this->_readTimeout[0], $this->_readTimeout[1]); |
| $this->_sock->setTimeout($this->_readTimeout[0], $this->_readTimeout[1]); |
} |
| |
|
| |
$this->_notify('sentRequest'); |
| |
|
| |
// Read the response |
| |
$this->_response = &new HTTP_Response($this->_sock, $this->_listeners); |
| |
$err = $this->_response->process($this->_saveBody && $saveBody); |
| } |
} |
| |
|
| $this->_notify('sentRequest'); |
ini_set('magic_quotes_runtime', $magicQuotes); |
| |
|
| // Read the response |
if (PEAR::isError($err)) { |
| $this->_response = &new HTTP_Response($this->_sock, $this->_listeners); |
|
| if (PEAR::isError($err = $this->_response->process($this->_saveBody && $saveBody)) ) { |
|
| return $err; |
return $err; |
| } |
} |
| |
|
| |
|
| // Check for redirection |
// Check for redirection |
| // Bugfix (PEAR) bug #18, 6 oct 2003 by Dave Mertens (headers are also stored lowercase, so we're gonna use them here) |
|
| // some non RFC2616 compliant servers (scripts) are returning lowercase headers ('location: xxx') |
|
| if ( $this->_allowRedirects |
if ( $this->_allowRedirects |
| AND $this->_redirects <= $this->_maxRedirects |
AND $this->_redirects <= $this->_maxRedirects |
| AND $this->getResponseCode() > 300 |
AND $this->getResponseCode() > 300 |
| Line 681 class HTTP_Request { |
|
| Line 713 class HTTP_Request { |
|
| if (!isset($headername)) { |
if (!isset($headername)) { |
| return isset($this->_response->_headers)? $this->_response->_headers: array(); |
return isset($this->_response->_headers)? $this->_response->_headers: array(); |
| } else { |
} else { |
| |
$headername = strtolower($headername); |
| return isset($this->_response->_headers[$headername]) ? $this->_response->_headers[$headername] : false; |
return isset($this->_response->_headers[$headername]) ? $this->_response->_headers[$headername] : false; |
| } |
} |
| } |
} |
| Line 727 class HTTP_Request { |
|
| Line 760 class HTTP_Request { |
|
| |
|
| $request = $this->_method . ' ' . $url . ' HTTP/' . $this->_http . "\r\n"; |
$request = $this->_method . ' ' . $url . ' HTTP/' . $this->_http . "\r\n"; |
| |
|
| if (HTTP_REQUEST_METHOD_POST != $this->_method && HTTP_REQUEST_METHOD_PUT != $this->_method) { |
if (in_array($this->_method, $this->_bodyDisallowed) || |
| |
(HTTP_REQUEST_METHOD_POST != $this->_method && empty($this->_body)) || |
| |
(HTTP_REQUEST_METHOD_POST != $this->_method && empty($this->_postData) && empty($this->_postFiles))) { |
| |
|
| $this->removeHeader('Content-Type'); |
$this->removeHeader('Content-Type'); |
| } else { |
} else { |
| if (empty($this->_requestHeaders['Content-Type'])) { |
if (empty($this->_requestHeaders['content-type'])) { |
| // Add default content-type |
// Add default content-type |
| $this->addHeader('Content-Type', 'application/x-www-form-urlencoded'); |
$this->addHeader('Content-Type', 'application/x-www-form-urlencoded'); |
| } elseif ('multipart/form-data' == $this->_requestHeaders['Content-Type']) { |
} elseif ('multipart/form-data' == $this->_requestHeaders['content-type']) { |
| $boundary = 'HTTP_Request_' . md5(uniqid('request') . microtime()); |
$boundary = 'HTTP_Request_' . md5(uniqid('request') . microtime()); |
| $this->addHeader('Content-Type', 'multipart/form-data; boundary=' . $boundary); |
$this->addHeader('Content-Type', 'multipart/form-data; boundary=' . $boundary); |
| } |
} |
| Line 742 class HTTP_Request { |
|
| Line 778 class HTTP_Request { |
|
| // Request Headers |
// Request Headers |
| if (!empty($this->_requestHeaders)) { |
if (!empty($this->_requestHeaders)) { |
| foreach ($this->_requestHeaders as $name => $value) { |
foreach ($this->_requestHeaders as $name => $value) { |
| $request .= $name . ': ' . $value . "\r\n"; |
$canonicalName = implode('-', array_map('ucfirst', explode('-', $name))); |
| |
$request .= $canonicalName . ': ' . $value . "\r\n"; |
| } |
} |
| } |
} |
| |
|
| // No post data or wrong method, so simply add a final CRLF |
// No post data or wrong method, so simply add a final CRLF |
| if ((HTTP_REQUEST_METHOD_POST != $this->_method && HTTP_REQUEST_METHOD_PUT != $this->_method) || |
if (in_array($this->_method, $this->_bodyDisallowed) || |
| (empty($this->_postData) && empty($this->_postFiles))) { |
(HTTP_REQUEST_METHOD_POST != $this->_method && empty($this->_body))) { |
| |
|
| $request .= "\r\n"; |
$request .= "\r\n"; |
| |
|
| // Post data if it's an array |
// Post data if it's an array |
| } elseif ((!empty($this->_postData) && is_array($this->_postData)) || !empty($this->_postFiles)) { |
} elseif (HTTP_REQUEST_METHOD_POST == $this->_method && |
| |
(!empty($this->_postData) || !empty($this->_postFiles))) { |
| |
|
| // "normal" POST request |
// "normal" POST request |
| if (!isset($boundary)) { |
if (!isset($boundary)) { |
| $postdata = implode('&', array_map( |
$postdata = implode('&', array_map( |
| Line 791 class HTTP_Request { |
|
| Line 831 class HTTP_Request { |
|
| $postdata .= "\r\n\r\n" . $data . "\r\n"; |
$postdata .= "\r\n\r\n" . $data . "\r\n"; |
| } |
} |
| } |
} |
| $postdata .= '--' . $boundary . "\r\n"; |
$postdata .= '--' . $boundary . "--\r\n"; |
| } |
} |
| $request .= 'Content-Length: ' . strlen($postdata) . "\r\n\r\n"; |
$request .= 'Content-Length: ' . strlen($postdata) . "\r\n\r\n"; |
| $request .= $postdata; |
$request .= $postdata; |
| |
|
| // Post data if it's raw |
// Explicitly set request body |
| } elseif(!empty($this->_postData)) { |
} elseif (!empty($this->_body)) { |
| $request .= 'Content-Length: ' . strlen($this->_postData) . "\r\n\r\n"; |
|
| $request .= $this->_postData; |
$request .= 'Content-Length: ' . strlen($this->_body) . "\r\n\r\n"; |
| |
$request .= $this->_body; |
| } |
} |
| |
|
| return $request; |
return $request; |
| Line 995 class HTTP_Response |
|
| Line 1036 class HTTP_Response |
|
| $chunked = isset($this->_headers['transfer-encoding']) && ('chunked' == $this->_headers['transfer-encoding']); |
$chunked = isset($this->_headers['transfer-encoding']) && ('chunked' == $this->_headers['transfer-encoding']); |
| $gzipped = isset($this->_headers['content-encoding']) && ('gzip' == $this->_headers['content-encoding']); |
$gzipped = isset($this->_headers['content-encoding']) && ('gzip' == $this->_headers['content-encoding']); |
| $hasBody = false; |
$hasBody = false; |
| while (!$this->_sock->eof()) { |
if (!isset($this->_headers['content-length']) || 0 != $this->_headers['content-length']) { |
| if ($chunked) { |
while (!$this->_sock->eof()) { |
| $data = $this->_readChunked(); |
if ($chunked) { |
| } else { |
$data = $this->_readChunked(); |
| $data = $this->_sock->read(4096); |
} else { |
| } |
$data = $this->_sock->read(4096); |
| if ('' != $data) { |
} |
| $hasBody = true; |
if ('' == $data) { |
| if ($saveBody || $gzipped) { |
break; |
| $this->_body .= $data; |
} else { |
| |
$hasBody = true; |
| |
if ($saveBody || $gzipped) { |
| |
$this->_body .= $data; |
| |
} |
| |
$this->_notify($gzipped? 'gzTick': 'tick', $data); |
| } |
} |
| $this->_notify($gzipped? 'gzTick': 'tick', $data); |
|
| } |
} |
| } |
} |
| if ($hasBody) { |
if ($hasBody) { |
| Line 1031 class HTTP_Response |
|
| Line 1076 class HTTP_Response |
|
| function _processHeader($header) |
function _processHeader($header) |
| { |
{ |
| list($headername, $headervalue) = explode(':', $header, 2); |
list($headername, $headervalue) = explode(':', $header, 2); |
| $headername_i = strtolower($headername); |
$headername = strtolower($headername); |
| $headervalue = ltrim($headervalue); |
$headervalue = ltrim($headervalue); |
| |
|
| if ('set-cookie' != $headername_i) { |
if ('set-cookie' != $headername) { |
| $this->_headers[$headername] = $headervalue; |
if (isset($this->_headers[$headername])) { |
| $this->_headers[$headername_i] = $headervalue; |
$this->_headers[$headername] .= ',' . $headervalue; |
| |
} else { |
| |
$this->_headers[$headername] = $headervalue; |
| |
} |
| } else { |
} else { |
| $this->_parseCookie($headervalue); |
$this->_parseCookie($headervalue); |
| } |
} |
| Line 1109 class HTTP_Response |
|
| Line 1157 class HTTP_Response |
|
| $this->_chunkLength = hexdec($matches[1]); |
$this->_chunkLength = hexdec($matches[1]); |
| // Chunk with zero length indicates the end |
// Chunk with zero length indicates the end |
| if (0 == $this->_chunkLength) { |
if (0 == $this->_chunkLength) { |
| $this->_sock->readAll(); // make this an eof() |
$this->_sock->readLine(); // make this an eof() |
| return ''; |
return ''; |
| } |
} |
| } elseif ($this->_sock->eof()) { |
} else { |
| return ''; |
return ''; |
| } |
} |
| } |
} |