Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Curl.php « Request « Common « OpenCloud « lib « php-opencloud « 3rdparty « files_external « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bb829afc5f6bd47ab978bed905fce2ebb00df764 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php

namespace OpenCloud\Common\Request;

use OpenCloud\Common\Base;
use OpenCloud\Common\Lang;
use OpenCloud\Common\Exceptions\HttpRetryError;
use OpenCloud\Common\Exceptions\HttpUrlError;
use OpenCloud\Common\Exceptions\HttpTimeoutError;
use OpenCloud\Common\Exceptions\HttpError;

/**
 * The CurlRequest class is a simple wrapper to CURL functions. Not only does
 * this permit stubbing of the interface as described under the HttpRequest
 * interface, it could potentially allow us to replace the interface methods
 * with other function calls in the future.
 *
 * @api
 * @author Glen Campbell <glen.campbell@rackspace.com>
 */
class Curl extends Base implements HttpRequestInterface
{

    private $url;
    private $method;
    private $handle;
    private $retries = 0;
    private $headers = array();
    private $returnheaders = array();

    /**
     * Initializes the CURL handle and HTTP method
     *
     * The constructor also sets a number of default values for options.
     *
     * @param string $url the URL to connect to
     * @param string $method the HTTP method (default "GET")
     * @param array $options optional hashed array of options => value pairs
     */
    public function __construct($url, $method = 'GET', array $options = array())
    {
        $this->url = $url;
        $this->method = $method;
        $this->handle = curl_init($url);

        // set our options
        $this->setOption(CURLOPT_CUSTOMREQUEST, $method);

        foreach($options as $opt => $value) {
            $this->getLogger()->info(Lang::translate('Setting option {key}={val}'), array(
                'key' => $opt, 
                'val' => $value
            ));
            $this->setOption($opt, $value);
        }

        // @codeCoverageIgnoreStart
        if (RAXSDK_SSL_VERIFYHOST != 2) {
            $this->getLogger()->warning("WARNING: RAXSDK_SSL_VERIFYHOST has reduced security, value [{value}]", array(
                'value' => RAXSDK_SSL_VERIFYHOST
            ));
        }

        if (RAXSDK_SSL_VERIFYPEER !== true) {
            $this->getLogger()->warning("WARNING: RAXSDK_SSL_VERIFYPEER has reduced security");
        }
        // @codeCoverageIgnoreEnd

        $this->setOption(CURLOPT_SSL_VERIFYHOST, RAXSDK_SSL_VERIFYHOST);
        $this->setOption(CURLOPT_SSL_VERIFYPEER, RAXSDK_SSL_VERIFYPEER);

        if (defined('RAXSDK_CACERTPEM') && file_exists(RAXSDK_CACERTPEM)) {
            $this->setOption(CURLOPT_CAINFO, RAXSDK_CACERTPEM);
        }

        //  curl code [18]
        //  message [transfer closed with x bytes remaining to read]
        if ($method === 'HEAD') {
            $this->setOption(CURLOPT_NOBODY, true);
        }

        // follow redirects
        $this->setOption(CURLOPT_FOLLOWLOCATION, true);

        // don't return the headers in the request
        $this->setOption(CURLOPT_HEADER, false);

        // retrieve headers via callback
        $this->setOption(CURLOPT_HEADERFUNCTION, array($this, '_get_header_cb'));

        // return the entire request on curl_exec()
        $this->setOption(CURLOPT_RETURNTRANSFER, true);

        // set default timeouts
        $this->setConnectTimeout(RAXSDK_CONNECTTIMEOUT);
        $this->setHttpTimeout(RAXSDK_TIMEOUT);
    }

    /**
     * Sets a CURL option
     *
     * @param const $name - a CURL named constant; e.g. CURLOPT_TIMEOUT
     * @param mixed $value - the value for the option
     */
    public function setOption($name, $value)
    {
        return curl_setopt($this->handle, $name, $value);
    }

    /**
     * Explicit method for setting the connect timeout
     *
     * The connect timeout is the time it takes for the initial connection
     * request to be established. It is different than the HTTP timeout, which
     * is the time for the entire request to be serviced.
     *
     * @param integer $value The connection timeout in seconds.
     *      Use 0 to wait indefinitely (NOT recommended)
     */
    public function setConnectTimeout($value)
    {
        $this->setOption(CURLOPT_CONNECTTIMEOUT, $value);
    }

    /**
     * Explicit method for setting the HTTP timeout
     *
     * The HTTP timeout is the time it takes for the HTTP request to be
     * serviced. This value is usually larger than the connect timeout
     * value.
     *
     * @param integer $value - the number of seconds to wait before timing out
     *      the HTTP request.
     */
    public function setHttpTimeout($value)
    {
        $this->setOption(CURLOPT_TIMEOUT, $value);
    }

    /**
     * Sets the number of retries
     *
     * If you set this to a non-zero value, then it will repeat the request
     * up to that number.
     */
    public function setRetries($value)
    {
        $this->retries = $value;
    }

    /**
     * Simplified method for setting lots of headers at once
     *
     * This method takes an associative array of header/value pairs and calls
     * the setheader() method on each of them.
     *
     * @param array $arr an associative array of headers
     */
    public function setheaders($array)
    {
        if (!is_array($array)) {
            throw new HttpError(Lang::translate(
                'Value passed to CurlRequest::setheaders() must be array'
            ));
        }

        foreach ($array as $name => $value) {
            $this->setHeader($name, $value);
        }
    }

    /**
     * Sets a single header
     *
     * For example, to set the content type to JSON:
     * `$request->SetHeader('Content-Type','application/json');`
     *
     * @param string $name The name of the header
     * @param mixed $value The value of the header
     */
    public function setHeader($name, $value)
    {
        $this->headers[$name] = $value;
    }

    /**
     * Executes the current request
     *
     * This method actually performs the request using the values set
     * previously. It throws a OpenCloud\HttpError exception on
     * any CURL error.
     *
     * @return OpenCloud\HttpResponse
     * @throws OpenCloud\HttpError
     * 
     * @codeCoverageIgnore
     */
    public function execute()
    {
        // set all the headers
        $headarr = array();

        foreach ($this->headers as $name => $value) {
            $headarr[] = $name.': '.$value;
        }

        $this->setOption(CURLOPT_HTTPHEADER, $headarr);

        // set up to retry if necessary
        $try_counter = 0;

        do {
            $data = curl_exec($this->handle);
            if (curl_errno($this->handle) && ($try_counter<$this->retries)) {
                $this->getLogger()->info(Lang::translate('Curl error [%d]; retrying [%s]'), array(
                    'error' => curl_errno($this->handle), 
                    'url'   => $this->url
                ));
            }

        } while((++$try_counter <= $this->retries) && (curl_errno($this->handle) != 0));

        // log retries error
        if ($this->retries && curl_errno($this->handle)) {
            throw new HttpRetryError(sprintf(
                Lang::translate('No more retries available, last error [%d]'), 
                curl_errno($this->handle)
            ));
        }

        // check for CURL errors
        switch(curl_errno($this->handle)) {
            case 0:
                // everything's ok
                break;
            case 3:
                throw new HttpUrlError(sprintf(Lang::translate('Malformed URL [%s]'), $this->url));
                break;
            case 28:
                // timeout
                throw new HttpTimeoutError(Lang::translate('Operation timed out; check RAXSDK_TIMEOUT value'));
                break;
            default:
                throw new HttpError(sprintf(
                    Lang::translate('HTTP error on [%s], curl code [%d] message [%s]'),
                    $this->url,
                    curl_errno($this->handle),
                    curl_error($this->handle)
                ));
        }

        // otherwise, return the HttpResponse
        return new Response\Http($this, $data);
    }

    /**
     * returns an array of information about the request
     */
    public function info()
    {
        return curl_getinfo($this->handle);
    }

    /**
     * returns the most recent CURL error number
     */
    public function errno()
    {
        return curl_errno($this->handle);
    }

    /**
     * returns the most recent CURL error string
     */
    public function error()
    {
        return curl_error($this->handle);
    }

    /**
     * Closes the HTTP request
     */
    public function close()
    {
        return curl_close($this->handle);
    }

    /**
     * Returns the headers as an array
     */
    public function returnHeaders()
    {
        return $this->returnheaders;
    }

    /**
     * This is a callback method used to handle the returned HTTP headers
     *
     * @param mixed $ch a CURL handle
     * @param string $header the header string in its entirety
     */
    public function _get_header_cb($ch, $header)
    {
        $this->returnheaders[] = $header;
        return strlen($header);
    }

}