Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
4,188 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Authentication provider interface | ||
* | ||
* @package Requests | ||
* @subpackage Authentication | ||
*/ | ||
|
||
/** | ||
* Authentication provider interface | ||
* | ||
* Implement this interface to act as an authentication provider. | ||
* | ||
* Parameters should be passed via the constructor where possible, as this | ||
* makes it much easier for users to use your provider. | ||
* | ||
* @see Requests_Hooks | ||
* @package Requests | ||
* @subpackage Authentication | ||
*/ | ||
interface Requests_Auth { | ||
/** | ||
* Register hooks as needed | ||
* | ||
* This method is called in {@see Requests::request} when the user has set | ||
* an instance as the 'auth' option. Use this callback to register all the | ||
* hooks you'll need. | ||
* | ||
* @see Requests_Hooks::register | ||
* @param Requests_Hooks $hooks Hook system | ||
*/ | ||
public function register(Requests_Hooks &$hooks); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
/** | ||
* Basic Authentication provider | ||
* | ||
* @package Requests | ||
* @subpackage Authentication | ||
*/ | ||
|
||
/** | ||
* Basic Authentication provider | ||
* | ||
* Provides a handler for Basic HTTP authentication via the Authorization | ||
* header. | ||
* | ||
* @package Requests | ||
* @subpackage Authentication | ||
*/ | ||
class Requests_Auth_Basic implements Requests_Auth { | ||
/** | ||
* Username | ||
* | ||
* @var string | ||
*/ | ||
public $user; | ||
|
||
/** | ||
* Password | ||
* | ||
* @var string | ||
*/ | ||
public $pass; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`) | ||
* @param array|null $args Array of user and password. Must have exactly two elements | ||
*/ | ||
public function __construct($args = null) { | ||
if (is_array($args)) { | ||
if (count($args) !== 2) { | ||
throw new Requests_Exception('Invalid number of arguments', 'authbasicbadargs'); | ||
} | ||
|
||
list($this->user, $this->pass) = $args; | ||
} | ||
} | ||
|
||
/** | ||
* Register the necessary callbacks | ||
* | ||
* @see curl_before_send | ||
* @see fsockopen_header | ||
* @param Requests_Hooks $hooks Hook system | ||
*/ | ||
public function register(Requests_Hooks &$hooks) { | ||
$hooks->register('curl.before_send', array(&$this, 'curl_before_send')); | ||
$hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header')); | ||
} | ||
|
||
/** | ||
* Set cURL parameters before the data is sent | ||
* | ||
* @param resource $handle cURL resource | ||
*/ | ||
public function curl_before_send(&$handle) { | ||
curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | ||
curl_setopt($handle, CURLOPT_USERPWD, $this->getAuthString()); | ||
} | ||
|
||
/** | ||
* Add extra headers to the request before sending | ||
* | ||
* @param string $out HTTP header string | ||
*/ | ||
public function fsockopen_header(&$out) { | ||
$out .= "Authorization: Basic " . base64_encode($this->getAuthString()) . "\r\n"; | ||
} | ||
|
||
/** | ||
* Get the authentication string (user:pass) | ||
* | ||
* @return string | ||
*/ | ||
public function getAuthString() { | ||
return $this->user . ':' . $this->pass; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Exception for HTTP requests | ||
* | ||
* @package Requests | ||
*/ | ||
|
||
/** | ||
* Exception for HTTP requests | ||
* | ||
* @package Requests | ||
*/ | ||
class Requests_Exception extends Exception { | ||
/** | ||
* Type of exception | ||
* | ||
* @var string | ||
*/ | ||
protected $type; | ||
|
||
/** | ||
* Data associated with the exception | ||
* | ||
* @var mixed | ||
*/ | ||
protected $data; | ||
|
||
/** | ||
* Create a new exception | ||
* | ||
* @param string $message Exception message | ||
* @param string $type Exception type | ||
* @param mixed $data Associated data | ||
* @param integer $code Exception numerical code, if applicable | ||
*/ | ||
public function __construct($message, $type, $data = null, $code = 0) { | ||
parent::__construct($message, $code); | ||
|
||
$this->type = $type; | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* Like {@see getCode()}, but a string code. | ||
* | ||
* @codeCoverageIgnore | ||
* @return string | ||
*/ | ||
public function getType() { | ||
return $this->type; | ||
} | ||
|
||
/** | ||
* Gives any relevant data | ||
* | ||
* @codeCoverageIgnore | ||
* @return mixed | ||
*/ | ||
public function getData() { | ||
return $this->data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* Exception based on HTTP response | ||
* | ||
* @package Requests | ||
*/ | ||
|
||
/** | ||
* Exception based on HTTP response | ||
* | ||
* @package Requests | ||
*/ | ||
class Requests_Exception_HTTP extends Requests_Exception { | ||
/** | ||
* HTTP status code | ||
* | ||
* @var integer | ||
*/ | ||
protected $code = 0; | ||
|
||
/** | ||
* Reason phrase | ||
* | ||
* @var string | ||
*/ | ||
protected $reason = 'Unknown'; | ||
|
||
/** | ||
* Create a new exception | ||
* | ||
* There is no mechanism to pass in the status code, as this is set by the | ||
* subclass used. Reason phrases can vary, however. | ||
* | ||
* @param string $reason Reason phrase | ||
* @param mixed $data Associated data | ||
*/ | ||
public function __construct($reason = null, $data = null) { | ||
if ($reason !== null) { | ||
$this->reason = $reason; | ||
} | ||
|
||
$message = sprintf('%d %s', $this->code, $this->reason); | ||
parent::__construct($message, 'httpresponse', $data, $this->code); | ||
} | ||
|
||
/** | ||
* Get the status message | ||
*/ | ||
public function get_reason() { | ||
return $this->reason; | ||
} | ||
|
||
/** | ||
* Get the correct exception class for a given error code | ||
* | ||
* @param int $code HTTP status code | ||
* @return string Exception class name to use | ||
*/ | ||
public static function get_class($code) { | ||
$class = sprintf('Requests_Exception_HTTP_%d', $code); | ||
if (class_exists($class)) { | ||
return $class; | ||
} | ||
|
||
return 'Requests_Exception_HTTP_Unknown'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
/** | ||
* Exception for 400 Bad Request responses | ||
* | ||
* @package Requests | ||
*/ | ||
|
||
/** | ||
* Exception for 400 Bad Request responses | ||
* | ||
* @package Requests | ||
*/ | ||
class Requests_Exception_HTTP_400 extends Requests_Exception_HTTP { | ||
/** | ||
* HTTP status code | ||
* | ||
* @var integer | ||
*/ | ||
protected $code = 400; | ||
|
||
/** | ||
* Reason phrase | ||
* | ||
* @var string | ||
*/ | ||
protected $reason = 'Bad Request'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
/** | ||
* Exception for 401 Unauthorized responses | ||
* | ||
* @package Requests | ||
*/ | ||
|
||
/** | ||
* Exception for 401 Unauthorized responses | ||
* | ||
* @package Requests | ||
*/ | ||
class Requests_Exception_HTTP_401 extends Requests_Exception_HTTP { | ||
/** | ||
* HTTP status code | ||
* | ||
* @var integer | ||
*/ | ||
protected $code = 401; | ||
|
||
/** | ||
* Reason phrase | ||
* | ||
* @var string | ||
*/ | ||
protected $reason = 'Unauthorized'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
/** | ||
* Exception for 402 Payment Required responses | ||
* | ||
* @package Requests | ||
*/ | ||
|
||
/** | ||
* Exception for 402 Payment Required responses | ||
* | ||
* @package Requests | ||
*/ | ||
class Requests_Exception_HTTP_402 extends Requests_Exception_HTTP { | ||
/** | ||
* HTTP status code | ||
* | ||
* @var integer | ||
*/ | ||
protected $code = 402; | ||
|
||
/** | ||
* Reason phrase | ||
* | ||
* @var string | ||
*/ | ||
protected $reason = 'Payment Required'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
/** | ||
* Exception for 403 Forbidden responses | ||
* | ||
* @package Requests | ||
*/ | ||
|
||
/** | ||
* Exception for 403 Forbidden responses | ||
* | ||
* @package Requests | ||
*/ | ||
class Requests_Exception_HTTP_403 extends Requests_Exception_HTTP { | ||
/** | ||
* HTTP status code | ||
* | ||
* @var integer | ||
*/ | ||
protected $code = 403; | ||
|
||
/** | ||
* Reason phrase | ||
* | ||
* @var string | ||
*/ | ||
protected $reason = 'Forbidden'; | ||
} |
Oops, something went wrong.