1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16:
17: class Horde_Http_Request_Peclhttp extends Horde_Http_Request_Base
18: {
19: 20: 21: 22:
23: protected $_httpAuthSchemes = array(
24: Horde_Http::AUTH_ANY => HTTP_AUTH_ANY,
25: Horde_Http::AUTH_BASIC => HTTP_AUTH_BASIC,
26: Horde_Http::AUTH_DIGEST => HTTP_AUTH_DIGEST,
27: Horde_Http::AUTH_GSSNEGOTIATE => HTTP_AUTH_GSSNEG,
28: Horde_Http::AUTH_NTLM => HTTP_AUTH_NTLM,
29: );
30:
31: 32: 33: 34: 35:
36: public function __construct($args = array())
37: {
38: if (!class_exists('HttpRequest', false)) {
39: throw new Horde_Http_Exception('The pecl_http extension is not installed. See http://php.net/http.install');
40: }
41:
42: parent::__construct($args);
43: }
44:
45: 46: 47: 48: 49: 50:
51: public function send()
52: {
53: if (!defined('HTTP_METH_' . $this->method)) {
54: throw new Horde_Http_Exception('Method ' . $this->method . ' not supported.');
55: }
56:
57: $httpRequest = new HttpRequest($this->uri, constant('HTTP_METH_' . $this->method));
58:
59: $data = $this->data;
60: if (is_array($data)) {
61: $httpRequest->setPostFields($data);
62: } else {
63: if ($this->method == 'PUT') {
64: $httpRequest->setPutData($data);
65: } else {
66: $httpRequest->setBody($data);
67: }
68: }
69:
70: $httpOptions = array('timeout' => $this->timeout);
71:
72:
73: if ($this->proxyServer) {
74: $httpOptions['proxyhost'] = $this->proxyServer;
75: if ($this->proxyPort) {
76: $httpOptions['proxyport'] = $this->proxyPort;
77: }
78: if ($this->proxyUsername && $this->proxyPassword) {
79: $httpOptions['proxyauth'] = $this->proxyUsername . ':' . $this->proxyPassword;
80: $httpOptions['proxyauthtype'] = $this->_httpAuthScheme($this->proxyAuthenticationScheme);
81: }
82: if ($this->proxyType == Horde_Http::PROXY_SOCKS4) {
83: $httpOptions['proxytype'] = HTTP_PROXY_SOCKS4;
84: } else if ($this->proxyType == Horde_Http::PROXY_SOCKS5) {
85: $httpOptions['proxytype'] = HTTP_PROXY_SOCKS5;
86: } else if ($this->proxyType != Horde_Http::PROXY_HTTP) {
87: throw new Horde_Http_Exception(sprintf('Proxy type %s not supported by this request type!', $this->proxyType));
88: }
89: }
90:
91:
92: if ($this->username) {
93: $httpOptions['httpauth'] = $this->username . ':' . $this->password;
94: $httpOptions['httpauthtype'] = $this->_httpAuthScheme($this->authenticationScheme);
95: }
96:
97:
98: $httpOptions['headers'] = $this->headers;
99:
100:
101: $httpOptions['redirect'] = (int)$this->redirects;
102:
103:
104: $httpRequest->setOptions($httpOptions);
105:
106: try {
107: $httpResponse = $httpRequest->send();
108: } catch (HttpException $e) {
109: throw new Horde_Http_Exception($e->getMessage(), $e->getCode(), $e);
110: }
111:
112: return new Horde_Http_Response_Peclhttp($this->uri, $httpResponse);
113: }
114:
115: 116: 117: 118: 119: 120: 121:
122: protected function _httpAuthScheme($httpAuthScheme)
123: {
124: if (!isset($this->_httpAuthSchemes[$httpAuthScheme])) {
125: throw new Horde_Http_Exception('Unsupported authentication scheme (' . $httpAuthScheme . ')');
126: }
127: return $this->_httpAuthSchemes[$httpAuthScheme];
128: }
129: }
130: