From 4a8a5f54fb3480ad8a7e961be296e09cade42b15 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Thu, 1 Oct 2009 14:42:58 -0400 Subject: [PATCH] - make constructors consistent across Request types - consistently try to set proxy settings across Request types - condense code in Curl and Peclhttp requests for translating between our constants and theirs --- framework/Http/lib/Horde/Http/Request/Curl.php | 65 +++++++++++------- framework/Http/lib/Horde/Http/Request/Fopen.php | 8 ++- framework/Http/lib/Horde/Http/Request/Peclhttp.php | 79 ++++++++++++++-------- 3 files changed, 94 insertions(+), 58 deletions(-) diff --git a/framework/Http/lib/Horde/Http/Request/Curl.php b/framework/Http/lib/Horde/Http/Request/Curl.php index ca02b9f09..44528ba31 100644 --- a/framework/Http/lib/Horde/Http/Request/Curl.php +++ b/framework/Http/lib/Horde/Http/Request/Curl.php @@ -16,6 +16,21 @@ */ class Horde_Http_Request_Curl extends Horde_Http_Request_Base { + /** + * Map of HTTP authentication schemes from Horde_Http constants to HTTP_AUTH constants. + * @var array + */ + protected $_httpAuthSchemes = array( + Horde_Http::AUTH_ANY => CURLAUTH_ANY, + Horde_Http::AUTH_BASIC => CURLAUTH_BASIC, + Horde_Http::AUTH_DIGEST => CURLAUTH_DIGEST, + Horde_Http::AUTH_GSSNEGOTIATE => CURLAUTH_GSSNEGOTIATE, + Horde_Http::AUTH_NTLM => CURLAUTH_NTLM, + ); + + /** + * Constructor + */ public function __construct($args = array()) { if (!extension_loaded('curl')) { @@ -47,35 +62,19 @@ class Horde_Http_Request_Curl extends Horde_Http_Request_Base } if ($data) { curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } - // Proxy + // Proxy settings + if ($this->proxyServer) { + curl_setopt($curl, CURLOPT_PROXY, $this->proxyServer); + if ($this->proxyUsername && $this->proxyPassword) { + curl_setopt($curl, CURLOPT_PROXYUSERPWD, $this->proxyUsername . ':' . $this->proxyPassword); + curl_setopt($curl, CURLOPT_PROXYAUTH, $this->_httpAuthScheme($this->proxyAuthenticationScheme)); + } + } - // Set authentication data + // Authentication settings if ($this->username) { curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':' . $this->password); - switch ($this->authenticationScheme) { - case Horde_Http::AUTH_ANY: - curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - break; - - case Horde_Http::AUTH_BASIC: - curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - break; - - case Horde_Http::AUTH_DIGEST: - curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); - break; - - case Horde_Http::AUTH_GSSNEGOTIATE: - curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE); - break; - - case Horde_Http::AUTH_NTLM: - curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); - break; - - default: - throw new Horde_Http_Exception('Unsupported authentication scheme (' . $this->authenticationScheme . ')'); - } + curl_setopt($curl, CURLOPT_HTTPAUTH, $this->_httpAuthScheme($this->authenticationScheme)); } // Concatenate the headers @@ -92,4 +91,18 @@ class Horde_Http_Request_Curl extends Horde_Http_Request_Base $info = curl_getinfo($curl); return new Horde_Http_Response_Curl($this->uri, $result, $info); } + + /** + * Translate a Horde_Http::AUTH_* constant to CURLAUTH_* + * + * @param const + * @return const + */ + protected function _httpAuthScheme($httpAuthScheme) + { + if (!isset($this->_httpAuthSchemes[$httpAuthScheme])) { + throw new Horde_Http_Exception('Unsupported authentication scheme (' . $httpAuthScheme . ')'); + } + return $this->_httpAuthSchemes[$httpAuthScheme]; + } } diff --git a/framework/Http/lib/Horde/Http/Request/Fopen.php b/framework/Http/lib/Horde/Http/Request/Fopen.php index 9f3eea543..7b53dc45c 100644 --- a/framework/Http/lib/Horde/Http/Request/Fopen.php +++ b/framework/Http/lib/Horde/Http/Request/Fopen.php @@ -16,11 +16,13 @@ */ class Horde_Http_Request_Fopen extends Horde_Http_Request_Base { - public function __construct() + public function __construct($args = array()) { if (!ini_get('allow_url_fopen')) { throw new Horde_Http_Exception('allow_url_fopen must be enabled'); } + + parent::__construct($args); } /** @@ -40,7 +42,7 @@ class Horde_Http_Request_Fopen extends Horde_Http_Request_Base $opts = array('http' => array()); - // Proxy settings - check first, so we can include the correct headers + // Proxy settings if ($this->proxyServer) { $opts['http']['proxy'] = 'tcp://' . $this->proxyServer; $opts['http']['request_fulluri'] = true; @@ -50,7 +52,7 @@ class Horde_Http_Request_Fopen extends Horde_Http_Request_Base } } - // Set authentication data + // Authentication settings if ($this->username) { switch ($this->authenticationScheme) { case Horde_Http::AUTH_BASIC: diff --git a/framework/Http/lib/Horde/Http/Request/Peclhttp.php b/framework/Http/lib/Horde/Http/Request/Peclhttp.php index b9f7c4623..fbf053c9d 100644 --- a/framework/Http/lib/Horde/Http/Request/Peclhttp.php +++ b/framework/Http/lib/Horde/Http/Request/Peclhttp.php @@ -16,7 +16,11 @@ */ class Horde_Http_Request_Peclhttp extends Horde_Http_Request_Base { - public static $methods = array( + /** + * Map of HTTP methods to HTTP_METH_* constants + * @var array + */ + protected $_httpMethods = array( 'GET' => HTTP_METH_GET, 'HEAD' => HTTP_METH_HEAD, 'POST' => HTTP_METH_POST, @@ -24,11 +28,30 @@ class Horde_Http_Request_Peclhttp extends Horde_Http_Request_Base 'DELETE' => HTTP_METH_DELETE, ); - public function __construct() + /** + * Map of HTTP authentication schemes from Horde_Http constants to HTTP_AUTH constants. + * @var array + */ + protected $_httpAuthSchemes = array( + Horde_Http::AUTH_ANY => HTTP_AUTH_ANY, + Horde_Http::AUTH_BASIC => HTTP_AUTH_BASIC, + Horde_Http::AUTH_DIGEST => HTTP_AUTH_DIGEST, + Horde_Http::AUTH_GSSNEGOTIATE => HTTP_AUTH_GSSNEG, + Horde_Http::AUTH_NTLM => HTTP_AUTH_NTLM, + ); + + /** + * Constructor + * + * @throws Horde_Http_Exception + */ + public function __construct($args = array()) { if (!class_exists('HttpRequest', false)) { throw new Horde_Http_Exception('The pecl_http extension is not installed. See http://php.net/http.install'); } + + parent::__construct($args); } /** @@ -38,7 +61,7 @@ class Horde_Http_Request_Peclhttp extends Horde_Http_Request_Base */ public function send() { - $httpRequest = new HttpRequest($this->uri, self::$methods[$this->method]); + $httpRequest = new HttpRequest($this->uri, $this->_httpMethods[$this->method]); $httpRequest->setHeaders($this->headers); $data = $this->data; @@ -50,35 +73,19 @@ class Horde_Http_Request_Peclhttp extends Horde_Http_Request_Base $httpOptions = array(); - // Proxy + // Proxy settings + if ($this->proxyServer) { + $httpOptions['proxyhost'] = $this->proxyServer; + if ($this->proxyUsername && $this->proxyPassword) { + $httpOptions['proxyauth'] = $this->proxyUsername . ':' . $this->proxyPassword; + $httpOptions['proxyauthtype'] = $this->_httpAuthScheme($this->proxyAuthenticationScheme); + } + } - // Set authentication data + // Authentication settings if ($this->username) { $httpOptions['httpauth'] = $this->username . ':' . $this->password; - switch ($this->authenticationScheme) { - case Horde_Http::AUTH_ANY: - $httpOptions['httpauthtype'] = HTTP_AUTH_ANY; - break; - - case Horde_Http::AUTH_BASIC: - $httpOptions['httpauthtype'] = HTTP_AUTH_BASIC; - break; - - case Horde_Http::AUTH_DIGEST: - $httpOptions['httpauthtype'] = HTTP_AUTH_DIGEST; - break; - - case Horde_Http::AUTH_GSSNEGOTIATE: - $httpOptions['httpauthtype'] = HTTP_AUTH_GSSNEG; - break; - - case Horde_Http::AUTH_NTLM: - $httpOptions['httpauthtype'] = HTTP_AUTH_NTLM; - break; - - default: - throw new Horde_Http_Exception('Unsupported authentication scheme (' . $this->authenticationScheme . ')'); - } + $httpOptions['httpauthtype'] = $this->_httpAuthScheme($this->authenticationScheme); } // Set options @@ -92,4 +99,18 @@ class Horde_Http_Request_Peclhttp extends Horde_Http_Request_Base return new Horde_Http_Response_Peclhttp($this->uri, $httpResponse); } + + /** + * Translate a Horde_Http::AUTH_* constant to HTTP_AUTH_* + * + * @param const + * @return const + */ + protected function _httpAuthScheme($httpAuthScheme) + { + if (!isset($this->_httpAuthSchemes[$httpAuthScheme])) { + throw new Horde_Http_Exception('Unsupported authentication scheme (' . $httpAuthScheme . ')'); + } + return $this->_httpAuthSchemes[$httpAuthScheme]; + } } -- 2.11.0