update HTTP_Request2 to 2.0.0RC1
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
<?php
|
||||
/**
|
||||
* Class representing a HTTP request
|
||||
* Class representing a HTTP request message
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* LICENSE:
|
||||
*
|
||||
* Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>
|
||||
* Copyright (c) 2008-2011, Alexey Borzov <avb@php.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -37,7 +37,7 @@
|
||||
* @package HTTP_Request2
|
||||
* @author Alexey Borzov <avb@php.net>
|
||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||
* @version CVS: $Id: Request2.php 278226 2009-04-03 21:32:48Z avb $
|
||||
* @version SVN: $Id: Request2.php 308735 2011-02-27 20:31:28Z avb $
|
||||
* @link http://pear.php.net/package/HTTP_Request2
|
||||
*/
|
||||
|
||||
@@ -48,16 +48,16 @@ require_once 'Net/URL2.php';
|
||||
|
||||
/**
|
||||
* Exception class for HTTP_Request2 package
|
||||
*/
|
||||
*/
|
||||
require_once 'HTTP/Request2/Exception.php';
|
||||
|
||||
/**
|
||||
* Class representing a HTTP request
|
||||
* Class representing a HTTP request message
|
||||
*
|
||||
* @category HTTP
|
||||
* @package HTTP_Request2
|
||||
* @author Alexey Borzov <avb@php.net>
|
||||
* @version Release: 0.4.1
|
||||
* @version Release: 2.0.0RC1
|
||||
* @link http://tools.ietf.org/html/rfc2616#section-5
|
||||
*/
|
||||
class HTTP_Request2 implements SplSubject
|
||||
@@ -78,7 +78,7 @@ class HTTP_Request2 implements SplSubject
|
||||
/**#@-*/
|
||||
|
||||
/**#@+
|
||||
* Constants for HTTP authentication schemes
|
||||
* Constants for HTTP authentication schemes
|
||||
*
|
||||
* @link http://tools.ietf.org/html/rfc2617
|
||||
*/
|
||||
@@ -95,7 +95,7 @@ class HTTP_Request2 implements SplSubject
|
||||
/**
|
||||
* Regular expression used to check for invalid symbols in cookie strings
|
||||
* @link http://pear.php.net/bugs/bug.php?id=15630
|
||||
* @link http://cgi.netscape.com/newsref/std/cookie_spec.html
|
||||
* @link http://web.archive.org/web/20080331104521/http://cgi.netscape.com/newsref/std/cookie_spec.html
|
||||
*/
|
||||
const REGEXP_INVALID_COOKIE = '/[\s,;]/';
|
||||
|
||||
@@ -164,7 +164,11 @@ class HTTP_Request2 implements SplSubject
|
||||
'ssl_local_cert' => null,
|
||||
'ssl_passphrase' => null,
|
||||
|
||||
'digest_compat_ie' => false
|
||||
'digest_compat_ie' => false,
|
||||
|
||||
'follow_redirects' => false,
|
||||
'max_redirects' => 5,
|
||||
'strict_redirects' => false
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -191,7 +195,7 @@ class HTTP_Request2 implements SplSubject
|
||||
protected $postParams = array();
|
||||
|
||||
/**
|
||||
* Array of file uploads (for multipart/form-data POST requests)
|
||||
* Array of file uploads (for multipart/form-data POST requests)
|
||||
* @var array
|
||||
*/
|
||||
protected $uploads = array();
|
||||
@@ -202,11 +206,16 @@ class HTTP_Request2 implements SplSubject
|
||||
*/
|
||||
protected $adapter;
|
||||
|
||||
/**
|
||||
* Cookie jar to persist cookies between requests
|
||||
* @var HTTP_Request2_CookieJar
|
||||
*/
|
||||
protected $cookieJar = null;
|
||||
|
||||
/**
|
||||
* Constructor. Can set request URL, method and configuration array.
|
||||
*
|
||||
* Also sets a default value for User-Agent header.
|
||||
* Also sets a default value for User-Agent header.
|
||||
*
|
||||
* @param string|Net_Url2 Request URL
|
||||
* @param string Request method
|
||||
@@ -214,14 +223,14 @@ class HTTP_Request2 implements SplSubject
|
||||
*/
|
||||
public function __construct($url = null, $method = self::METHOD_GET, array $config = array())
|
||||
{
|
||||
$this->setConfig($config);
|
||||
if (!empty($url)) {
|
||||
$this->setUrl($url);
|
||||
}
|
||||
if (!empty($method)) {
|
||||
$this->setMethod($method);
|
||||
}
|
||||
$this->setConfig($config);
|
||||
$this->setHeader('user-agent', 'HTTP_Request2/0.4.1 ' .
|
||||
$this->setHeader('user-agent', 'HTTP_Request2/2.0.0RC1 ' .
|
||||
'(http://pear.php.net/package/http_request2) ' .
|
||||
'PHP/' . phpversion());
|
||||
}
|
||||
@@ -235,15 +244,20 @@ class HTTP_Request2 implements SplSubject
|
||||
*
|
||||
* @param string|Net_URL2 Request URL
|
||||
* @return HTTP_Request2
|
||||
* @throws HTTP_Request2_Exception
|
||||
* @throws HTTP_Request2_LogicException
|
||||
*/
|
||||
public function setUrl($url)
|
||||
{
|
||||
if (is_string($url)) {
|
||||
$url = new Net_URL2($url);
|
||||
$url = new Net_URL2(
|
||||
$url, array(Net_URL2::OPTION_USE_BRACKETS => $this->config['use_brackets'])
|
||||
);
|
||||
}
|
||||
if (!$url instanceof Net_URL2) {
|
||||
throw new HTTP_Request2_Exception('Parameter is not a valid HTTP URL');
|
||||
throw new HTTP_Request2_LogicException(
|
||||
'Parameter is not a valid HTTP URL',
|
||||
HTTP_Request2_Exception::INVALID_ARGUMENT
|
||||
);
|
||||
}
|
||||
// URL contains username / password?
|
||||
if ($url->getUserinfo()) {
|
||||
@@ -275,13 +289,16 @@ class HTTP_Request2 implements SplSubject
|
||||
*
|
||||
* @param string
|
||||
* @return HTTP_Request2
|
||||
* @throws HTTP_Request2_Exception if the method name is invalid
|
||||
* @throws HTTP_Request2_LogicException if the method name is invalid
|
||||
*/
|
||||
public function setMethod($method)
|
||||
{
|
||||
// Method name should be a token: http://tools.ietf.org/html/rfc2616#section-5.1.1
|
||||
if (preg_match(self::REGEXP_INVALID_TOKEN, $method)) {
|
||||
throw new HTTP_Request2_Exception("Invalid request method '{$method}'");
|
||||
throw new HTTP_Request2_LogicException(
|
||||
"Invalid request method '{$method}'",
|
||||
HTTP_Request2_Exception::INVALID_ARGUMENT
|
||||
);
|
||||
}
|
||||
$this->method = $method;
|
||||
|
||||
@@ -306,7 +323,7 @@ class HTTP_Request2 implements SplSubject
|
||||
* <li> 'adapter' - adapter to use (string)</li>
|
||||
* <li> 'connect_timeout' - Connection timeout in seconds (integer)</li>
|
||||
* <li> 'timeout' - Total number of seconds a request can take.
|
||||
* Use 0 for no limit, should be greater than
|
||||
* Use 0 for no limit, should be greater than
|
||||
* 'connect_timeout' if set (integer)</li>
|
||||
* <li> 'use_brackets' - Whether to append [] to array variable names (bool)</li>
|
||||
* <li> 'protocol_version' - HTTP Version to use, '1.0' or '1.1' (string)</li>
|
||||
@@ -324,7 +341,7 @@ class HTTP_Request2 implements SplSubject
|
||||
* certificate matches host name (bool)</li>
|
||||
* <li> 'ssl_cafile' - Cerificate Authority file to verify the peer
|
||||
* with (use with 'ssl_verify_peer') (string)</li>
|
||||
* <li> 'ssl_capath' - Directory holding multiple Certificate
|
||||
* <li> 'ssl_capath' - Directory holding multiple Certificate
|
||||
* Authority files (string)</li>
|
||||
* <li> 'ssl_local_cert' - Name of a file containing local cerificate (string)</li>
|
||||
* <li> 'ssl_passphrase' - Passphrase with which local certificate
|
||||
@@ -332,13 +349,19 @@ class HTTP_Request2 implements SplSubject
|
||||
* <li> 'digest_compat_ie' - Whether to imitate behaviour of MSIE 5 and 6
|
||||
* in using URL without query string in digest
|
||||
* authentication (boolean)</li>
|
||||
* <li> 'follow_redirects' - Whether to automatically follow HTTP Redirects (boolean)</li>
|
||||
* <li> 'max_redirects' - Maximum number of redirects to follow (integer)</li>
|
||||
* <li> 'strict_redirects' - Whether to keep request method on redirects via status 301 and
|
||||
* 302 (true, needed for compatibility with RFC 2616)
|
||||
* or switch to GET (false, needed for compatibility with most
|
||||
* browsers) (boolean)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param string|array configuration parameter name or array
|
||||
* ('parameter name' => 'parameter value')
|
||||
* @param mixed parameter value if $nameOrConfig is not an array
|
||||
* @return HTTP_Request2
|
||||
* @throws HTTP_Request2_Exception If the parameter is unknown
|
||||
* @throws HTTP_Request2_LogicException If the parameter is unknown
|
||||
*/
|
||||
public function setConfig($nameOrConfig, $value = null)
|
||||
{
|
||||
@@ -349,8 +372,9 @@ class HTTP_Request2 implements SplSubject
|
||||
|
||||
} else {
|
||||
if (!array_key_exists($nameOrConfig, $this->config)) {
|
||||
throw new HTTP_Request2_Exception(
|
||||
"Unknown configuration parameter '{$nameOrConfig}'"
|
||||
throw new HTTP_Request2_LogicException(
|
||||
"Unknown configuration parameter '{$nameOrConfig}'",
|
||||
HTTP_Request2_Exception::INVALID_ARGUMENT
|
||||
);
|
||||
}
|
||||
$this->config[$nameOrConfig] = $value;
|
||||
@@ -363,17 +387,18 @@ class HTTP_Request2 implements SplSubject
|
||||
* Returns the value(s) of the configuration parameter(s)
|
||||
*
|
||||
* @param string parameter name
|
||||
* @return mixed value of $name parameter, array of all configuration
|
||||
* @return mixed value of $name parameter, array of all configuration
|
||||
* parameters if $name is not given
|
||||
* @throws HTTP_Request2_Exception If the parameter is unknown
|
||||
* @throws HTTP_Request2_LogicException If the parameter is unknown
|
||||
*/
|
||||
public function getConfig($name = null)
|
||||
{
|
||||
if (null === $name) {
|
||||
return $this->config;
|
||||
} elseif (!array_key_exists($name, $this->config)) {
|
||||
throw new HTTP_Request2_Exception(
|
||||
"Unknown configuration parameter '{$name}'"
|
||||
throw new HTTP_Request2_LogicException(
|
||||
"Unknown configuration parameter '{$name}'",
|
||||
HTTP_Request2_Exception::INVALID_ARGUMENT
|
||||
);
|
||||
}
|
||||
return $this->config[$name];
|
||||
@@ -386,7 +411,7 @@ class HTTP_Request2 implements SplSubject
|
||||
* @param string password
|
||||
* @param string authentication scheme
|
||||
* @return HTTP_Request2
|
||||
*/
|
||||
*/
|
||||
public function setAuth($user, $password = '', $scheme = self::AUTH_BASIC)
|
||||
{
|
||||
if (empty($user)) {
|
||||
@@ -419,13 +444,13 @@ class HTTP_Request2 implements SplSubject
|
||||
* Sets request header(s)
|
||||
*
|
||||
* The first parameter may be either a full header string 'header: value' or
|
||||
* header name. In the former case $value parameter is ignored, in the latter
|
||||
* header name. In the former case $value parameter is ignored, in the latter
|
||||
* the header's value will either be set to $value or the header will be
|
||||
* removed if $value is null. The first parameter can also be an array of
|
||||
* headers, in that case method will be called recursively.
|
||||
*
|
||||
* Note that headers are treated case insensitively as per RFC 2616.
|
||||
*
|
||||
*
|
||||
* <code>
|
||||
* $req->setHeader('Foo: Bar'); // sets the value of 'Foo' header to 'Bar'
|
||||
* $req->setHeader('FoO', 'Baz'); // sets the value of 'Foo' header to 'Baz'
|
||||
@@ -435,18 +460,21 @@ class HTTP_Request2 implements SplSubject
|
||||
*
|
||||
* @param string|array header name, header string ('Header: value')
|
||||
* or an array of headers
|
||||
* @param string|null header value, header will be removed if null
|
||||
* @param string|array|null header value if $name is not an array,
|
||||
* header will be removed if value is null
|
||||
* @param bool whether to replace previous header with the
|
||||
* same name or append to its value
|
||||
* @return HTTP_Request2
|
||||
* @throws HTTP_Request2_Exception
|
||||
* @throws HTTP_Request2_LogicException
|
||||
*/
|
||||
public function setHeader($name, $value = null)
|
||||
public function setHeader($name, $value = null, $replace = true)
|
||||
{
|
||||
if (is_array($name)) {
|
||||
foreach ($name as $k => $v) {
|
||||
if (is_string($k)) {
|
||||
$this->setHeader($k, $v);
|
||||
$this->setHeader($k, $v, $replace);
|
||||
} else {
|
||||
$this->setHeader($v);
|
||||
$this->setHeader($v, null, $replace);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -455,17 +483,30 @@ class HTTP_Request2 implements SplSubject
|
||||
}
|
||||
// Header name should be a token: http://tools.ietf.org/html/rfc2616#section-4.2
|
||||
if (preg_match(self::REGEXP_INVALID_TOKEN, $name)) {
|
||||
throw new HTTP_Request2_Exception("Invalid header name '{$name}'");
|
||||
throw new HTTP_Request2_LogicException(
|
||||
"Invalid header name '{$name}'",
|
||||
HTTP_Request2_Exception::INVALID_ARGUMENT
|
||||
);
|
||||
}
|
||||
// Header names are case insensitive anyway
|
||||
$name = strtolower($name);
|
||||
if (null === $value) {
|
||||
unset($this->headers[$name]);
|
||||
|
||||
} else {
|
||||
$this->headers[$name] = $value;
|
||||
if (is_array($value)) {
|
||||
$value = implode(', ', array_map('trim', $value));
|
||||
} elseif (is_string($value)) {
|
||||
$value = trim($value);
|
||||
}
|
||||
if (!isset($this->headers[$name]) || $replace) {
|
||||
$this->headers[$name] = $value;
|
||||
} else {
|
||||
$this->headers[$name] .= ', ' . $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -483,21 +524,39 @@ class HTTP_Request2 implements SplSubject
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a cookie to "Cookie:" header
|
||||
* Adds a cookie to the request
|
||||
*
|
||||
* If the request does not have a CookieJar object set, this method simply
|
||||
* appends a cookie to "Cookie:" header.
|
||||
*
|
||||
* If a CookieJar object is available, the cookie is stored in that object.
|
||||
* Data from request URL will be used for setting its 'domain' and 'path'
|
||||
* parameters, 'expires' and 'secure' will be set to null and false,
|
||||
* respectively. If you need further control, use CookieJar's methods.
|
||||
*
|
||||
* @param string cookie name
|
||||
* @param string cookie value
|
||||
* @return HTTP_Request2
|
||||
* @throws HTTP_Request2_Exception
|
||||
* @throws HTTP_Request2_LogicException
|
||||
* @see setCookieJar()
|
||||
*/
|
||||
public function addCookie($name, $value)
|
||||
{
|
||||
$cookie = $name . '=' . $value;
|
||||
if (preg_match(self::REGEXP_INVALID_COOKIE, $cookie)) {
|
||||
throw new HTTP_Request2_Exception("Invalid cookie: '{$cookie}'");
|
||||
if (!empty($this->cookieJar)) {
|
||||
$this->cookieJar->store(array('name' => $name, 'value' => $value),
|
||||
$this->url);
|
||||
|
||||
} else {
|
||||
$cookie = $name . '=' . $value;
|
||||
if (preg_match(self::REGEXP_INVALID_COOKIE, $cookie)) {
|
||||
throw new HTTP_Request2_LogicException(
|
||||
"Invalid cookie: '{$cookie}'",
|
||||
HTTP_Request2_Exception::INVALID_ARGUMENT
|
||||
);
|
||||
}
|
||||
$cookies = empty($this->headers['cookie'])? '': $this->headers['cookie'] . '; ';
|
||||
$this->setHeader('cookie', $cookies . $cookie);
|
||||
}
|
||||
$cookies = empty($this->headers['cookie'])? '': $this->headers['cookie'] . '; ';
|
||||
$this->setHeader('cookie', $cookies . $cookie);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -505,24 +564,32 @@ class HTTP_Request2 implements SplSubject
|
||||
/**
|
||||
* Sets the request body
|
||||
*
|
||||
* @param string Either a string with the body or filename containing body
|
||||
* If you provide file pointer rather than file name, it should support
|
||||
* fstat() and rewind() operations.
|
||||
*
|
||||
* @param string|resource|HTTP_Request2_MultipartBody Either a string
|
||||
* with the body or filename containing body or pointer to
|
||||
* an open file or object with multipart body data
|
||||
* @param bool Whether first parameter is a filename
|
||||
* @return HTTP_Request2
|
||||
* @throws HTTP_Request2_Exception
|
||||
* @throws HTTP_Request2_LogicException
|
||||
*/
|
||||
public function setBody($body, $isFilename = false)
|
||||
{
|
||||
if (!$isFilename) {
|
||||
$this->body = (string)$body;
|
||||
} else {
|
||||
if (!($fp = @fopen($body, 'rb'))) {
|
||||
throw new HTTP_Request2_Exception("Cannot open file {$body}");
|
||||
if (!$isFilename && !is_resource($body)) {
|
||||
if (!$body instanceof HTTP_Request2_MultipartBody) {
|
||||
$this->body = (string)$body;
|
||||
} else {
|
||||
$this->body = $body;
|
||||
}
|
||||
$this->body = $fp;
|
||||
} else {
|
||||
$fileData = $this->fopenWrapper($body, empty($this->headers['content-type']));
|
||||
$this->body = $fileData['fp'];
|
||||
if (empty($this->headers['content-type'])) {
|
||||
$this->setHeader('content-type', self::detectMimeType($body));
|
||||
$this->setHeader('content-type', $fileData['type']);
|
||||
}
|
||||
}
|
||||
$this->postParams = $this->uploads = array();
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -534,10 +601,10 @@ class HTTP_Request2 implements SplSubject
|
||||
*/
|
||||
public function getBody()
|
||||
{
|
||||
if (self::METHOD_POST == $this->method &&
|
||||
if (self::METHOD_POST == $this->method &&
|
||||
(!empty($this->postParams) || !empty($this->uploads))
|
||||
) {
|
||||
if ('application/x-www-form-urlencoded' == $this->headers['content-type']) {
|
||||
if (0 === strpos($this->headers['content-type'], 'application/x-www-form-urlencoded')) {
|
||||
$body = http_build_query($this->postParams, '', '&');
|
||||
if (!$this->getConfig('use_brackets')) {
|
||||
$body = preg_replace('/%5B\d+%5D=/', '=', $body);
|
||||
@@ -545,7 +612,7 @@ class HTTP_Request2 implements SplSubject
|
||||
// support RFC 3986 by not encoding '~' symbol (request #15368)
|
||||
return str_replace('%7E', '~', $body);
|
||||
|
||||
} elseif ('multipart/form-data' == $this->headers['content-type']) {
|
||||
} elseif (0 === strpos($this->headers['content-type'], 'multipart/form-data')) {
|
||||
require_once 'HTTP/Request2/MultipartBody.php';
|
||||
return new HTTP_Request2_MultipartBody(
|
||||
$this->postParams, $this->uploads, $this->getConfig('use_brackets')
|
||||
@@ -564,25 +631,28 @@ class HTTP_Request2 implements SplSubject
|
||||
* If you just want to send the contents of a file as the body of HTTP
|
||||
* request you should use setBody() method.
|
||||
*
|
||||
* If you provide file pointers rather than file names, they should support
|
||||
* fstat() and rewind() operations.
|
||||
*
|
||||
* @param string name of file-upload field
|
||||
* @param mixed full name of local file
|
||||
* @param string filename to send in the request
|
||||
* @param string|resource|array full name of local file, pointer to
|
||||
* open file or an array of files
|
||||
* @param string filename to send in the request
|
||||
* @param string content-type of file being uploaded
|
||||
* @return HTTP_Request2
|
||||
* @throws HTTP_Request2_Exception
|
||||
* @throws HTTP_Request2_LogicException
|
||||
*/
|
||||
public function addUpload($fieldName, $filename, $sendFilename = null,
|
||||
$contentType = null)
|
||||
{
|
||||
if (!is_array($filename)) {
|
||||
if (!($fp = @fopen($filename, 'rb'))) {
|
||||
throw new HTTP_Request2_Exception("Cannot open file {$filename}");
|
||||
}
|
||||
$fileData = $this->fopenWrapper($filename, empty($contentType));
|
||||
$this->uploads[$fieldName] = array(
|
||||
'fp' => $fp,
|
||||
'filename' => empty($sendFilename)? basename($filename): $sendFilename,
|
||||
'size' => filesize($filename),
|
||||
'type' => empty($contentType)? self::detectMimeType($filename): $contentType
|
||||
'fp' => $fileData['fp'],
|
||||
'filename' => !empty($sendFilename)? $sendFilename
|
||||
:(is_string($filename)? basename($filename): 'anonymous.blob') ,
|
||||
'size' => $fileData['size'],
|
||||
'type' => empty($contentType)? $fileData['type']: $contentType
|
||||
);
|
||||
} else {
|
||||
$fps = $names = $sizes = $types = array();
|
||||
@@ -590,13 +660,12 @@ class HTTP_Request2 implements SplSubject
|
||||
if (!is_array($f)) {
|
||||
$f = array($f);
|
||||
}
|
||||
if (!($fp = @fopen($f[0], 'rb'))) {
|
||||
throw new HTTP_Request2_Exception("Cannot open file {$f[0]}");
|
||||
}
|
||||
$fps[] = $fp;
|
||||
$names[] = empty($f[1])? basename($f[0]): $f[1];
|
||||
$sizes[] = filesize($f[0]);
|
||||
$types[] = empty($f[2])? self::detectMimeType($f[0]): $f[2];
|
||||
$fileData = $this->fopenWrapper($f[0], empty($f[2]));
|
||||
$fps[] = $fileData['fp'];
|
||||
$names[] = !empty($f[1])? $f[1]
|
||||
:(is_string($f[0])? basename($f[0]): 'anonymous.blob');
|
||||
$sizes[] = $fileData['size'];
|
||||
$types[] = empty($f[2])? $fileData['type']: $f[2];
|
||||
}
|
||||
$this->uploads[$fieldName] = array(
|
||||
'fp' => $fps, 'filename' => $names, 'size' => $sizes, 'type' => $types
|
||||
@@ -703,8 +772,10 @@ class HTTP_Request2 implements SplSubject
|
||||
* <li>'disconnect' - after disconnection from server</li>
|
||||
* <li>'sentHeaders' - after sending the request headers,
|
||||
* data is the headers sent (string)</li>
|
||||
* <li>'sentBodyPart' - after sending a part of the request body,
|
||||
* <li>'sentBodyPart' - after sending a part of the request body,
|
||||
* data is the length of that part (int)</li>
|
||||
* <li>'sentBody' - after sending the whole request body,
|
||||
* data is request body length (int)</li>
|
||||
* <li>'receivedHeaders' - after receiving the response headers,
|
||||
* data is HTTP_Request2_Response object</li>
|
||||
* <li>'receivedBodyPart' - after receiving a part of the response
|
||||
@@ -738,7 +809,7 @@ class HTTP_Request2 implements SplSubject
|
||||
*
|
||||
* @param string|HTTP_Request2_Adapter
|
||||
* @return HTTP_Request2
|
||||
* @throws HTTP_Request2_Exception
|
||||
* @throws HTTP_Request2_LogicException
|
||||
*/
|
||||
public function setAdapter($adapter)
|
||||
{
|
||||
@@ -751,19 +822,67 @@ class HTTP_Request2 implements SplSubject
|
||||
include_once str_replace('_', DIRECTORY_SEPARATOR, $adapter) . '.php';
|
||||
}
|
||||
if (!class_exists($adapter, false)) {
|
||||
throw new HTTP_Request2_Exception("Class {$adapter} not found");
|
||||
throw new HTTP_Request2_LogicException(
|
||||
"Class {$adapter} not found",
|
||||
HTTP_Request2_Exception::MISSING_VALUE
|
||||
);
|
||||
}
|
||||
}
|
||||
$adapter = new $adapter;
|
||||
}
|
||||
if (!$adapter instanceof HTTP_Request2_Adapter) {
|
||||
throw new HTTP_Request2_Exception('Parameter is not a HTTP request adapter');
|
||||
throw new HTTP_Request2_LogicException(
|
||||
'Parameter is not a HTTP request adapter',
|
||||
HTTP_Request2_Exception::INVALID_ARGUMENT
|
||||
);
|
||||
}
|
||||
$this->adapter = $adapter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the cookie jar
|
||||
*
|
||||
* A cookie jar is used to maintain cookies across HTTP requests and
|
||||
* responses. Cookies from jar will be automatically added to the request
|
||||
* headers based on request URL.
|
||||
*
|
||||
* @param HTTP_Request2_CookieJar|bool Existing CookieJar object, true to
|
||||
* create a new one, false to remove
|
||||
*/
|
||||
public function setCookieJar($jar = true)
|
||||
{
|
||||
if (!class_exists('HTTP_Request2_CookieJar', false)) {
|
||||
require_once 'HTTP/Request2/CookieJar.php';
|
||||
}
|
||||
|
||||
if ($jar instanceof HTTP_Request2_CookieJar) {
|
||||
$this->cookieJar = $jar;
|
||||
} elseif (true === $jar) {
|
||||
$this->cookieJar = new HTTP_Request2_CookieJar();
|
||||
} elseif (!$jar) {
|
||||
$this->cookieJar = null;
|
||||
} else {
|
||||
throw new HTTP_Request2_LogicException(
|
||||
'Invalid parameter passed to setCookieJar()',
|
||||
HTTP_Request2_Exception::INVALID_ARGUMENT
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current CookieJar object or null if none
|
||||
*
|
||||
* @return HTTP_Request2_CookieJar|null
|
||||
*/
|
||||
public function getCookieJar()
|
||||
{
|
||||
return $this->cookieJar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the request and returns the response
|
||||
*
|
||||
@@ -773,20 +892,25 @@ class HTTP_Request2 implements SplSubject
|
||||
public function send()
|
||||
{
|
||||
// Sanity check for URL
|
||||
if (!$this->url instanceof Net_URL2) {
|
||||
throw new HTTP_Request2_Exception('No URL given');
|
||||
} elseif (!$this->url->isAbsolute()) {
|
||||
throw new HTTP_Request2_Exception('Absolute URL required');
|
||||
} elseif (!in_array(strtolower($this->url->getScheme()), array('https', 'http'))) {
|
||||
throw new HTTP_Request2_Exception('Not a HTTP URL');
|
||||
if (!$this->url instanceof Net_URL2
|
||||
|| !$this->url->isAbsolute()
|
||||
|| !in_array(strtolower($this->url->getScheme()), array('https', 'http'))
|
||||
) {
|
||||
throw new HTTP_Request2_LogicException(
|
||||
'HTTP_Request2 needs an absolute HTTP(S) request URL, '
|
||||
. ($this->url instanceof Net_URL2
|
||||
? 'none' : "'" . $this->url->__toString() . "'")
|
||||
. ' given',
|
||||
HTTP_Request2_Exception::INVALID_ARGUMENT
|
||||
);
|
||||
}
|
||||
if (empty($this->adapter)) {
|
||||
$this->setAdapter($this->getConfig('adapter'));
|
||||
}
|
||||
// magic_quotes_runtime may break file uploads and chunked response
|
||||
// processing; see bug #4543
|
||||
if ($magicQuotes = ini_get('magic_quotes_runtime')) {
|
||||
ini_set('magic_quotes_runtime', false);
|
||||
// processing; see bug #4543. Don't use ini_get() here; see bug #16440.
|
||||
if ($magicQuotes = get_magic_quotes_runtime()) {
|
||||
set_magic_quotes_runtime(false);
|
||||
}
|
||||
// force using single byte encoding if mbstring extension overloads
|
||||
// strlen() and substr(); see bug #1781, bug #10605
|
||||
@@ -801,7 +925,7 @@ class HTTP_Request2 implements SplSubject
|
||||
}
|
||||
// cleanup in either case (poor man's "finally" clause)
|
||||
if ($magicQuotes) {
|
||||
ini_set('magic_quotes_runtime', true);
|
||||
set_magic_quotes_runtime(true);
|
||||
}
|
||||
if (!empty($oldEncoding)) {
|
||||
mb_internal_encoding($oldEncoding);
|
||||
@@ -813,6 +937,53 @@ class HTTP_Request2 implements SplSubject
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper around fopen()/fstat() used by setBody() and addUpload()
|
||||
*
|
||||
* @param string|resource file name or pointer to open file
|
||||
* @param bool whether to try autodetecting MIME type of file,
|
||||
* will only work if $file is a filename, not pointer
|
||||
* @return array array('fp' => file pointer, 'size' => file size, 'type' => MIME type)
|
||||
* @throws HTTP_Request2_LogicException
|
||||
*/
|
||||
protected function fopenWrapper($file, $detectType = false)
|
||||
{
|
||||
if (!is_string($file) && !is_resource($file)) {
|
||||
throw new HTTP_Request2_LogicException(
|
||||
"Filename or file pointer resource expected",
|
||||
HTTP_Request2_Exception::INVALID_ARGUMENT
|
||||
);
|
||||
}
|
||||
$fileData = array(
|
||||
'fp' => is_string($file)? null: $file,
|
||||
'type' => 'application/octet-stream',
|
||||
'size' => 0
|
||||
);
|
||||
if (is_string($file)) {
|
||||
$track = @ini_set('track_errors', 1);
|
||||
if (!($fileData['fp'] = @fopen($file, 'rb'))) {
|
||||
$e = new HTTP_Request2_LogicException(
|
||||
$php_errormsg, HTTP_Request2_Exception::READ_ERROR
|
||||
);
|
||||
}
|
||||
@ini_set('track_errors', $track);
|
||||
if (isset($e)) {
|
||||
throw $e;
|
||||
}
|
||||
if ($detectType) {
|
||||
$fileData['type'] = self::detectMimeType($file);
|
||||
}
|
||||
}
|
||||
if (!($stat = fstat($fileData['fp']))) {
|
||||
throw new HTTP_Request2_LogicException(
|
||||
"fstat() call failed", HTTP_Request2_Exception::READ_ERROR
|
||||
);
|
||||
}
|
||||
$fileData['size'] = $stat['size'];
|
||||
|
||||
return $fileData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to detect MIME type of a file
|
||||
*
|
||||
@@ -825,12 +996,12 @@ class HTTP_Request2 implements SplSubject
|
||||
*/
|
||||
protected static function detectMimeType($filename)
|
||||
{
|
||||
// finfo extension from PECL available
|
||||
// finfo extension from PECL available
|
||||
if (function_exists('finfo_open')) {
|
||||
if (!isset(self::$_fileinfoDb)) {
|
||||
self::$_fileinfoDb = @finfo_open(FILEINFO_MIME);
|
||||
}
|
||||
if (self::$_fileinfoDb) {
|
||||
if (self::$_fileinfoDb) {
|
||||
$info = finfo_file(self::$_fileinfoDb, $filename);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user