* Fixed bug #14168 (st->errorbuffer not cleared between uses of persistent handles)
[m6w6/ext-http] / http_request_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #include "php_http.h"
18
19 #ifdef HTTP_HAVE_CURL
20
21 #include "php_http_api.h"
22 #include "php_http_persistent_handle_api.h"
23 #include "php_http_request_api.h"
24 #include "php_http_url_api.h"
25
26 #ifdef ZEND_ENGINE_2
27 # include "php_http_request_object.h"
28 #endif
29
30 #include "php_http_request_int.h"
31
32 /* {{{ cruft for thread safe SSL crypto locks */
33 #ifdef HTTP_NEED_OPENSSL_TSL
34 static MUTEX_T *http_openssl_tsl = NULL;
35
36 static void http_openssl_thread_lock(int mode, int n, const char * file, int line)
37 {
38 if (mode & CRYPTO_LOCK) {
39 tsrm_mutex_lock(http_openssl_tsl[n]);
40 } else {
41 tsrm_mutex_unlock(http_openssl_tsl[n]);
42 }
43 }
44
45 static ulong http_openssl_thread_id(void)
46 {
47 return (ulong) tsrm_thread_id();
48 }
49 #endif
50 #ifdef HTTP_NEED_GNUTLS_TSL
51 static int http_gnutls_mutex_create(void **m)
52 {
53 if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
54 return SUCCESS;
55 } else {
56 return FAILURE;
57 }
58 }
59
60 static int http_gnutls_mutex_destroy(void **m)
61 {
62 tsrm_mutex_free(*((MUTEX_T *) m));
63 return SUCCESS;
64 }
65
66 static int http_gnutls_mutex_lock(void **m)
67 {
68 return tsrm_mutex_lock(*((MUTEX_T *) m));
69 }
70
71 static int http_gnutls_mutex_unlock(void **m)
72 {
73 return tsrm_mutex_unlock(*((MUTEX_T *) m));
74 }
75
76 static struct gcry_thread_cbs http_gnutls_tsl = {
77 GCRY_THREAD_OPTION_USER,
78 NULL,
79 http_gnutls_mutex_create,
80 http_gnutls_mutex_destroy,
81 http_gnutls_mutex_lock,
82 http_gnutls_mutex_unlock
83 };
84 #endif
85 /* }}} */
86
87 /* safe curl wrappers */
88 #define init_curl_storage(ch) \
89 {\
90 http_request_storage *st = pecalloc(1, sizeof(http_request_storage), 1); \
91 curl_easy_setopt(ch, CURLOPT_PRIVATE, st); \
92 curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer); \
93 }
94
95 static void *safe_curl_init(void)
96 {
97 CURL *ch;
98
99 if ((ch = curl_easy_init())) {
100 init_curl_storage(ch);
101 return ch;
102 }
103 return NULL;
104 }
105 static void *safe_curl_copy(void *p)
106 {
107 CURL *ch;
108
109 if ((ch = curl_easy_duphandle(p))) {
110 init_curl_storage(ch);
111 return ch;
112 }
113 return NULL;
114 }
115 static void safe_curl_dtor(void *p) {
116 http_request_storage *st = http_request_storage_get(p);
117
118 curl_easy_cleanup(p);
119
120 if (st) {
121 if (st->url) {
122 pefree(st->url, 1);
123 }
124 if (st->cookiestore) {
125 pefree(st->cookiestore, 1);
126 }
127 pefree(st, 1);
128 }
129 }
130 /* }}} */
131
132 /* {{{ MINIT */
133 PHP_MINIT_FUNCTION(http_request)
134 {
135 #ifdef HTTP_NEED_OPENSSL_TSL
136 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
137 if (!CRYPTO_get_id_callback()) {
138 int i, c = CRYPTO_num_locks();
139
140 http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
141
142 for (i = 0; i < c; ++i) {
143 http_openssl_tsl[i] = tsrm_mutex_alloc();
144 }
145
146 CRYPTO_set_id_callback(http_openssl_thread_id);
147 CRYPTO_set_locking_callback(http_openssl_thread_lock);
148 }
149 #endif
150 #ifdef HTTP_NEED_GNUTLS_TSL
151 gcry_control(GCRYCTL_SET_THREAD_CBS, &http_gnutls_tsl);
152 #endif
153
154 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
155 return FAILURE;
156 }
157
158 if (SUCCESS != http_persistent_handle_provide("http_request", safe_curl_init, safe_curl_dtor, safe_curl_copy)) {
159 return FAILURE;
160 }
161
162 HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC);
163 HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST);
164 HTTP_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM);
165 HTTP_LONG_CONSTANT("HTTP_AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE);
166 HTTP_LONG_CONSTANT("HTTP_AUTH_ANY", CURLAUTH_ANY);
167
168 HTTP_LONG_CONSTANT("HTTP_VERSION_NONE", CURL_HTTP_VERSION_NONE); /* to be removed */
169 HTTP_LONG_CONSTANT("HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0);
170 HTTP_LONG_CONSTANT("HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1);
171 HTTP_LONG_CONSTANT("HTTP_VERSION_ANY", CURL_HTTP_VERSION_NONE);
172
173 HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1);
174 HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2);
175 HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3);
176 HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT);
177
178 HTTP_LONG_CONSTANT("HTTP_IPRESOLVE_V4", CURL_IPRESOLVE_V4);
179 HTTP_LONG_CONSTANT("HTTP_IPRESOLVE_V6", CURL_IPRESOLVE_V6);
180 HTTP_LONG_CONSTANT("HTTP_IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER);
181
182 #if HTTP_CURL_VERSION(7,15,2)
183 HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS4", CURLPROXY_SOCKS4);
184 #endif
185 HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS5", CURLPROXY_SOCKS5);
186 HTTP_LONG_CONSTANT("HTTP_PROXY_HTTP", CURLPROXY_HTTP);
187 return SUCCESS;
188 }
189 /* }}} */
190
191 /* {{{ MSHUTDOWN */
192 PHP_MSHUTDOWN_FUNCTION(http_request)
193 {
194 curl_global_cleanup();
195 #ifdef HTTP_NEED_OPENSSL_TSL
196 if (http_openssl_tsl) {
197 int i, c = CRYPTO_num_locks();
198
199 CRYPTO_set_id_callback(NULL);
200 CRYPTO_set_locking_callback(NULL);
201
202 for (i = 0; i < c; ++i) {
203 tsrm_mutex_free(http_openssl_tsl[i]);
204 }
205
206 free(http_openssl_tsl);
207 http_openssl_tsl = NULL;
208 }
209 #endif
210 return SUCCESS;
211 }
212 /* }}} */
213
214 /* {{{ forward declarations */
215 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
216 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
217 static inline zval *_http_request_option_ex(http_request *request, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
218 #define http_request_option_cache(r, k, z) _http_request_option_cache_ex((r), (k), sizeof(k), 0, (z) TSRMLS_CC)
219 #define http_request_option_cache_ex(r, k, kl, h, z) _http_request_option_cache_ex((r), (k), (kl), (h), (z) TSRMLS_CC)
220 static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC);
221
222 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
223 static int http_curl_progress_callback(void *, double, double, double, double);
224 static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *);
225 static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { return n*l; }
226 static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *);
227 /* }}} */
228
229 /* {{{ CURL *http_curl_init(http_request *) */
230 PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, http_request *request TSRMLS_DC)
231 {
232 if (ch || (SUCCESS == http_persistent_handle_acquire("http_request", &ch))) {
233 #if defined(ZTS)
234 curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
235 #endif
236 curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
237 curl_easy_setopt(ch, CURLOPT_FILETIME, 1L);
238 curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1L);
239 curl_easy_setopt(ch, CURLOPT_VERBOSE, 1L);
240 curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, NULL);
241 curl_easy_setopt(ch, CURLOPT_DEBUGFUNCTION, http_curl_raw_callback);
242 curl_easy_setopt(ch, CURLOPT_READFUNCTION, http_curl_read_callback);
243 curl_easy_setopt(ch, CURLOPT_IOCTLFUNCTION, http_curl_ioctl_callback);
244 curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, http_curl_dummy_callback);
245
246 /* set context */
247 if (request) {
248 curl_easy_setopt(ch, CURLOPT_DEBUGDATA, request);
249
250 /* attach curl handle */
251 request->ch = ch;
252 /* set defaults (also in http_request_reset()) */
253 http_request_defaults(request);
254 }
255 }
256
257 return ch;
258 }
259 /* }}} */
260
261 /* {{{ CURL *http_curl_copy(CURL *) */
262 PHP_HTTP_API CURL *_http_curl_copy(CURL *ch TSRMLS_DC)
263 {
264 CURL *copy;
265
266 if (SUCCESS == http_persistent_handle_accrete("http_request", ch, &copy)) {
267 return copy;
268 }
269 return NULL;
270 }
271 /* }}} */
272
273 /* {{{ void http_curl_free(CURL **) */
274 PHP_HTTP_API void _http_curl_free(CURL **ch TSRMLS_DC)
275 {
276 if (*ch) {
277 curl_easy_setopt(*ch, CURLOPT_NOPROGRESS, 1L);
278 curl_easy_setopt(*ch, CURLOPT_PROGRESSFUNCTION, NULL);
279 curl_easy_setopt(*ch, CURLOPT_VERBOSE, 0L);
280 curl_easy_setopt(*ch, CURLOPT_DEBUGFUNCTION, NULL);
281
282 http_persistent_handle_release("http_request", ch);
283 }
284 }
285 /* }}} */
286
287 /* {{{ http_request *http_request_init(http_request *) */
288 PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch, http_request_method meth, const char *url ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
289 {
290 http_request *r;
291
292 if (request) {
293 r = request;
294 } else {
295 r = emalloc_rel(sizeof(http_request));
296 }
297 memset(r, 0, sizeof(http_request));
298
299 r->ch = ch;
300 r->url = (url) ? http_absolute_url(url) : NULL;
301 r->meth = (meth > 0) ? meth : HTTP_GET;
302
303 phpstr_init(&r->conv.request);
304 phpstr_init_ex(&r->conv.response, HTTP_CURLBUF_SIZE, 0);
305 phpstr_init(&r->_cache.cookies);
306 zend_hash_init(&r->_cache.options, 0, NULL, ZVAL_PTR_DTOR, 0);
307
308 TSRMLS_SET_CTX(r->tsrm_ls);
309
310 return r;
311 }
312 /* }}} */
313
314 /* {{{ void http_request_dtor(http_request *) */
315 PHP_HTTP_API void _http_request_dtor(http_request *request)
316 {
317 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
318
319 http_curl_free(&request->ch);
320 http_request_reset(request);
321
322 phpstr_dtor(&request->_cache.cookies);
323 zend_hash_destroy(&request->_cache.options);
324 if (request->_cache.headers) {
325 curl_slist_free_all(request->_cache.headers);
326 request->_cache.headers = NULL;
327 }
328 if (request->_progress_callback) {
329 zval_ptr_dtor(&request->_progress_callback);
330 request->_progress_callback = NULL;
331 }
332 }
333 /* }}} */
334
335 /* {{{ void http_request_free(http_request **) */
336 PHP_HTTP_API void _http_request_free(http_request **request)
337 {
338 if (*request) {
339 TSRMLS_FETCH_FROM_CTX((*request)->tsrm_ls);
340 http_request_body_free(&(*request)->body);
341 http_request_dtor(*request);
342 efree(*request);
343 *request = NULL;
344 }
345 }
346 /* }}} */
347
348 /* {{{ void http_request_reset(http_request *) */
349 PHP_HTTP_API void _http_request_reset(http_request *request)
350 {
351 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
352 STR_SET(request->url, NULL);
353 request->conv.last_type = 0;
354 phpstr_dtor(&request->conv.request);
355 phpstr_dtor(&request->conv.response);
356 http_request_body_dtor(request->body);
357 http_request_defaults(request);
358
359 if (request->ch) {
360 http_request_storage *st = http_request_storage_get(request->ch);
361
362 if (st) {
363 if (st->url) {
364 pefree(st->url, 1);
365 st->url = NULL;
366 }
367 if (st->cookiestore) {
368 pefree(st->cookiestore, 1);
369 st->cookiestore = NULL;
370 }
371 st->errorbuffer[0] = '\0';
372 }
373 }
374 }
375 /* }}} */
376
377 /* {{{ STATUS http_request_enable_cookies(http_request *) */
378 PHP_HTTP_API STATUS _http_request_enable_cookies(http_request *request)
379 {
380 int initialized = 1;
381 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
382
383 HTTP_CHECK_CURL_INIT(request->ch, http_curl_init_ex(request->ch, request), initialized = 0);
384 if (initialized && CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIEFILE, "")) {
385 return SUCCESS;
386 }
387 http_error(HE_WARNING, HTTP_E_REQUEST, "Could not enable cookies for this session");
388 return FAILURE;
389 }
390 /* }}} */
391
392 /* {{{ STATUS http_request_reset_cookies(http_request *, int) */
393 PHP_HTTP_API STATUS _http_request_reset_cookies(http_request *request, int session_only)
394 {
395 int initialized = 1;
396 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
397
398 HTTP_CHECK_CURL_INIT(request->ch, http_curl_init_ex(request->ch, request), initialized = 0);
399 if (session_only) {
400 #if HTTP_CURL_VERSION(7,15,4)
401 if (initialized && CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "SESS")) {
402 return SUCCESS;
403 }
404 #else
405 http_error(HE_WARNING, HTTP_E_REQUEST, "Could not reset session cookies (need libcurl >= v7.15.4)");
406 #endif
407 } else {
408 #if HTTP_CURL_VERSION(7,14,1)
409 if (initialized && CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "ALL")) {
410 return SUCCESS;
411 }
412 #else
413 http_error(HE_WARNING, HTTP_E_REQUEST, "Could not reset cookies (need libcurl >= v7.14.1)");
414 #endif
415 }
416 return FAILURE;
417 }
418 /* }}} */
419
420 PHP_HTTP_API STATUS _http_request_flush_cookies(http_request *request)
421 {
422 int initialized = 1;
423 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
424
425 HTTP_CHECK_CURL_INIT(request->ch, http_curl_init_ex(request->ch, request), initialized = 0);
426 #if HTTP_CURL_VERSION(7,17,1)
427 if (initialized && CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "FLUSH")) {
428 return SUCCESS;
429 }
430 #else
431 http_error(HE_WARNING, HTTP_E_REQUEST, "Could not flush cookies (need libcurl >= v7.17.1)");
432 #endif
433 return FAILURE;
434 }
435
436 /* {{{ void http_request_defaults(http_request *) */
437 PHP_HTTP_API void _http_request_defaults(http_request *request)
438 {
439 if (request->ch) {
440 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, NULL);
441 HTTP_CURL_OPT(CURLOPT_URL, NULL);
442 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 1L);
443 HTTP_CURL_OPT(CURLOPT_PROXY, NULL);
444 HTTP_CURL_OPT(CURLOPT_PROXYPORT, 0L);
445 HTTP_CURL_OPT(CURLOPT_PROXYTYPE, 0L);
446 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD, NULL);
447 HTTP_CURL_OPT(CURLOPT_PROXYAUTH, 0L);
448 HTTP_CURL_OPT(CURLOPT_DNS_CACHE_TIMEOUT, 60L);
449 HTTP_CURL_OPT(CURLOPT_IPRESOLVE, 0);
450 HTTP_CURL_OPT(CURLOPT_LOW_SPEED_LIMIT, 0L);
451 HTTP_CURL_OPT(CURLOPT_LOW_SPEED_TIME, 0L);
452 #if HTTP_CURL_VERSION(7,15,5)
453 /* LFS weirdance
454 HTTP_CURL_OPT(CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0);
455 HTTP_CURL_OPT(CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0);
456 */
457 #endif
458 /* crashes
459 HTTP_CURL_OPT(CURLOPT_MAXCONNECTS, 5L); */
460 HTTP_CURL_OPT(CURLOPT_FRESH_CONNECT, 0L);
461 HTTP_CURL_OPT(CURLOPT_FORBID_REUSE, 0L);
462 HTTP_CURL_OPT(CURLOPT_INTERFACE, NULL);
463 HTTP_CURL_OPT(CURLOPT_PORT, 0L);
464 #if HTTP_CURL_VERSION(7,15,2)
465 HTTP_CURL_OPT(CURLOPT_LOCALPORT, 0L);
466 HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, 0L);
467 #endif
468 HTTP_CURL_OPT(CURLOPT_USERPWD, NULL);
469 HTTP_CURL_OPT(CURLOPT_HTTPAUTH, 0L);
470 HTTP_CURL_OPT(CURLOPT_ENCODING, NULL);
471 #if HTTP_CURL_VERSION(7,16,2)
472 /* we do this ourself anyway */
473 HTTP_CURL_OPT(CURLOPT_HTTP_CONTENT_DECODING, 0L);
474 HTTP_CURL_OPT(CURLOPT_HTTP_TRANSFER_DECODING, 0L);
475 #endif
476 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION, 0L);
477 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, 0L);
478 HTTP_CURL_OPT(CURLOPT_REFERER, NULL);
479 HTTP_CURL_OPT(CURLOPT_USERAGENT, "PECL::HTTP/" PHP_HTTP_VERSION " (PHP/" PHP_VERSION ")");
480 HTTP_CURL_OPT(CURLOPT_HTTPHEADER, NULL);
481 HTTP_CURL_OPT(CURLOPT_COOKIE, NULL);
482 #if HTTP_CURL_VERSION(7,14,1)
483 HTTP_CURL_OPT(CURLOPT_COOKIELIST, NULL);
484 #endif
485 HTTP_CURL_OPT(CURLOPT_RANGE, NULL);
486 HTTP_CURL_OPT(CURLOPT_RESUME_FROM, 0L);
487 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, 0L);
488 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, 0L);
489 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, 0L);
490 HTTP_CURL_OPT(CURLOPT_TIMEOUT, 0L);
491 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT, 3);
492 HTTP_CURL_OPT(CURLOPT_SSLCERT, NULL);
493 HTTP_CURL_OPT(CURLOPT_SSLCERTTYPE, NULL);
494 HTTP_CURL_OPT(CURLOPT_SSLCERTPASSWD, NULL);
495 HTTP_CURL_OPT(CURLOPT_SSLKEY, NULL);
496 HTTP_CURL_OPT(CURLOPT_SSLKEYTYPE, NULL);
497 HTTP_CURL_OPT(CURLOPT_SSLKEYPASSWD, NULL);
498 HTTP_CURL_OPT(CURLOPT_SSLENGINE, NULL);
499 HTTP_CURL_OPT(CURLOPT_SSLVERSION, 0L);
500 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYPEER, 0L);
501 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYHOST, 0L);
502 HTTP_CURL_OPT(CURLOPT_SSL_CIPHER_LIST, NULL);
503 #ifdef HTTP_CURL_CAINFO
504 HTTP_CURL_OPT(CURLOPT_CAINFO, HTTP_CURL_CAINFO);
505 #else
506 HTTP_CURL_OPT(CURLOPT_CAINFO, NULL);
507 #endif
508 HTTP_CURL_OPT(CURLOPT_CAPATH, NULL);
509 HTTP_CURL_OPT(CURLOPT_RANDOM_FILE, NULL);
510 HTTP_CURL_OPT(CURLOPT_EGDSOCKET, NULL);
511 HTTP_CURL_OPT(CURLOPT_POSTFIELDS, NULL);
512 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, 0L);
513 HTTP_CURL_OPT(CURLOPT_HTTPPOST, NULL);
514 HTTP_CURL_OPT(CURLOPT_IOCTLDATA, NULL);
515 HTTP_CURL_OPT(CURLOPT_READDATA, NULL);
516 HTTP_CURL_OPT(CURLOPT_INFILESIZE, 0L);
517 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
518 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST, NULL);
519 HTTP_CURL_OPT(CURLOPT_NOBODY, 0L);
520 HTTP_CURL_OPT(CURLOPT_POST, 0L);
521 HTTP_CURL_OPT(CURLOPT_UPLOAD, 0L);
522 HTTP_CURL_OPT(CURLOPT_HTTPGET, 1L);
523 }
524 }
525 /* }}} */
526
527 PHP_HTTP_API void _http_request_set_progress_callback(http_request *request, zval *cb)
528 {
529 if (request->_progress_callback) {
530 zval_ptr_dtor(&request->_progress_callback);
531 }
532 if ((request->_progress_callback = cb)) {
533 ZVAL_ADDREF(cb);
534 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 0);
535 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA, request);
536 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, http_curl_progress_callback);
537 } else {
538 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 1);
539 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA, NULL);
540 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, NULL);
541 }
542 }
543
544 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
545 PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *options)
546 {
547 zval *zoption;
548 zend_bool range_req = 0;
549 http_request_storage *storage;
550
551 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
552
553 HTTP_CHECK_CURL_INIT(request->ch, http_curl_init(request), return FAILURE);
554
555 if (!(storage = http_request_storage_get(request->ch))) {
556 return FAILURE;
557 }
558 storage->errorbuffer[0] = '\0';
559 /* set options */
560 if (storage->url) {
561 pefree(storage->url, 1);
562 }
563 storage->url = pestrdup(request->url, 1);
564 HTTP_CURL_OPT(CURLOPT_URL, storage->url);
565
566 /* progress callback */
567 if ((zoption = http_request_option(request, options, "onprogress", -1))) {
568 http_request_set_progress_callback(request, zoption);
569 }
570
571 /* proxy */
572 if ((zoption = http_request_option(request, options, "proxyhost", IS_STRING))) {
573 HTTP_CURL_OPT(CURLOPT_PROXY, Z_STRVAL_P(zoption));
574 /* type */
575 if ((zoption = http_request_option(request, options, "proxytype", IS_LONG))) {
576 HTTP_CURL_OPT(CURLOPT_PROXYTYPE, Z_LVAL_P(zoption));
577 }
578 /* port */
579 if ((zoption = http_request_option(request, options, "proxyport", IS_LONG))) {
580 HTTP_CURL_OPT(CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
581 }
582 /* user:pass */
583 if ((zoption = http_request_option(request, options, "proxyauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
584 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
585 }
586 /* auth method */
587 if ((zoption = http_request_option(request, options, "proxyauthtype", IS_LONG))) {
588 HTTP_CURL_OPT(CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
589 }
590 }
591
592 /* dns */
593 if ((zoption = http_request_option(request, options, "dns_cache_timeout", IS_LONG))) {
594 HTTP_CURL_OPT(CURLOPT_DNS_CACHE_TIMEOUT, Z_LVAL_P(zoption));
595 }
596 if ((zoption = http_request_option(request, options, "ipresolve", IS_LONG)) && Z_LVAL_P(zoption)) {
597 HTTP_CURL_OPT(CURLOPT_IPRESOLVE, Z_LVAL_P(zoption));
598 }
599
600 /* limits */
601 if ((zoption = http_request_option(request, options, "low_speed_limit", IS_LONG))) {
602 HTTP_CURL_OPT(CURLOPT_LOW_SPEED_LIMIT, Z_LVAL_P(zoption));
603 }
604 if ((zoption = http_request_option(request, options, "low_speed_time", IS_LONG))) {
605 HTTP_CURL_OPT(CURLOPT_LOW_SPEED_TIME, Z_LVAL_P(zoption));
606 }
607 #if HTTP_CURL_VERSION(7,15,5)
608 /* LSF weirdance
609 if ((zoption = http_request_option(request, options, "max_send_speed", IS_LONG))) {
610 HTTP_CURL_OPT(CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
611 }
612 if ((zoption = http_request_option(request, options, "max_recv_speed", IS_LONG))) {
613 HTTP_CURL_OPT(CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
614 }
615 */
616 #endif
617 /* crashes
618 if ((zoption = http_request_option(request, options, "maxconnects", IS_LONG))) {
619 HTTP_CURL_OPT(CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption));
620 } */
621 if ((zoption = http_request_option(request, options, "fresh_connect", IS_BOOL)) && Z_BVAL_P(zoption)) {
622 HTTP_CURL_OPT(CURLOPT_FRESH_CONNECT, 1L);
623 }
624 if ((zoption = http_request_option(request, options, "forbid_reuse", IS_BOOL)) && Z_BVAL_P(zoption)) {
625 HTTP_CURL_OPT(CURLOPT_FORBID_REUSE, 1L);
626 }
627
628 /* outgoing interface */
629 if ((zoption = http_request_option(request, options, "interface", IS_STRING))) {
630 HTTP_CURL_OPT(CURLOPT_INTERFACE, Z_STRVAL_P(zoption));
631
632 #if HTTP_CURL_VERSION(7,15,2)
633 if ((zoption = http_request_option(request, options, "portrange", IS_ARRAY))) {
634 zval **prs, **pre;
635
636 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
637 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &prs)) {
638 zend_hash_move_forward(Z_ARRVAL_P(zoption));
639 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) {
640 zval *prs_cpy = *prs, *pre_cpy = *pre;
641
642 convert_to_long_ex(&prs_cpy);
643 convert_to_long_ex(&pre_cpy);
644 if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) {
645 HTTP_CURL_OPT(CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy)));
646 HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L);
647 }
648 if (prs_cpy != *prs) {
649 zval_ptr_dtor(&prs_cpy);
650 }
651 if (pre_cpy != *pre) {
652 zval_ptr_dtor(&pre_cpy);
653 }
654 }
655 }
656 }
657 #endif
658 }
659
660 /* another port */
661 if ((zoption = http_request_option(request, options, "port", IS_LONG))) {
662 HTTP_CURL_OPT(CURLOPT_PORT, Z_LVAL_P(zoption));
663 }
664
665 /* auth */
666 if ((zoption = http_request_option(request, options, "httpauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
667 HTTP_CURL_OPT(CURLOPT_USERPWD, Z_STRVAL_P(zoption));
668 }
669 if ((zoption = http_request_option(request, options, "httpauthtype", IS_LONG))) {
670 HTTP_CURL_OPT(CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
671 }
672
673 /* redirects, defaults to 0 */
674 if ((zoption = http_request_option(request, options, "redirect", IS_LONG))) {
675 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1L : 0L);
676 HTTP_CURL_OPT(CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
677 if ((zoption = http_request_option(request, options, "unrestrictedauth", IS_BOOL))) {
678 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
679 }
680 }
681
682 /* retries, defaults to 0 */
683 if ((zoption = http_request_option(request, options, "retrycount", IS_LONG))) {
684 request->_retry.count = Z_LVAL_P(zoption);
685 if ((zoption = http_request_option(request, options, "retrydelay", IS_DOUBLE))) {
686 request->_retry.delay = Z_DVAL_P(zoption);
687 } else {
688 request->_retry.delay = 0;
689 }
690 } else {
691 request->_retry.count = 0;
692 }
693
694 /* referer */
695 if ((zoption = http_request_option(request, options, "referer", IS_STRING)) && Z_STRLEN_P(zoption)) {
696 HTTP_CURL_OPT(CURLOPT_REFERER, Z_STRVAL_P(zoption));
697 }
698
699 /* useragent, default "PECL::HTTP/version (PHP/version)" */
700 if ((zoption = http_request_option(request, options, "useragent", IS_STRING))) {
701 /* allow to send no user agent, not even default one */
702 if (Z_STRLEN_P(zoption)) {
703 HTTP_CURL_OPT(CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
704 } else {
705 HTTP_CURL_OPT(CURLOPT_USERAGENT, NULL);
706 }
707 }
708
709 /* resume */
710 if ((zoption = http_request_option(request, options, "resume", IS_LONG)) && (Z_LVAL_P(zoption) > 0)) {
711 range_req = 1;
712 HTTP_CURL_OPT(CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
713 }
714 /* or range of kind array(array(0,499), array(100,1499)) */
715 else if ((zoption = http_request_option(request, options, "range", IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
716 HashPosition pos1, pos2;
717 zval **rr, **rb, **re;
718 phpstr rs;
719
720 phpstr_init(&rs);
721 FOREACH_VAL(pos1, zoption, rr) {
722 if (Z_TYPE_PP(rr) == IS_ARRAY) {
723 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2);
724 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &rb, &pos2)) {
725 zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2);
726 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &re, &pos2)) {
727 if ( ((Z_TYPE_PP(rb) == IS_LONG) || ((Z_TYPE_PP(rb) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(rb), Z_STRLEN_PP(rb), NULL, NULL, 1))) &&
728 ((Z_TYPE_PP(re) == IS_LONG) || ((Z_TYPE_PP(re) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(re), Z_STRLEN_PP(re), NULL, NULL, 1)))) {
729 zval *rbl = *rb, *rel = *re;
730
731 convert_to_long_ex(&rbl);
732 convert_to_long_ex(&rel);
733 if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) {
734 phpstr_appendf(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel));
735 }
736 if (rbl != *rb) {
737 zval_ptr_dtor(&rbl);
738 }
739 if (rel != *re) {
740 zval_ptr_dtor(&rel);
741 }
742 }
743 }
744 }
745 }
746 }
747
748 if (PHPSTR_LEN(&rs)) {
749 zval *cached_range;
750
751 /* ditch last comma */
752 PHPSTR_VAL(&rs)[PHPSTR_LEN(&rs)-- -1] = '\0';
753 /* cache string */
754 MAKE_STD_ZVAL(cached_range);
755 ZVAL_STRINGL(cached_range, PHPSTR_VAL(&rs), PHPSTR_LEN(&rs), 0);
756 HTTP_CURL_OPT(CURLOPT_RANGE, Z_STRVAL_P(http_request_option_cache(request, "range", cached_range)));
757 zval_ptr_dtor(&cached_range);
758 }
759 }
760
761 /* additional headers, array('name' => 'value') */
762 if (request->_cache.headers) {
763 curl_slist_free_all(request->_cache.headers);
764 request->_cache.headers = NULL;
765 }
766 if ((zoption = http_request_option(request, options, "headers", IS_ARRAY))) {
767 HashKey header_key = initHashKey(0);
768 zval **header_val;
769 HashPosition pos;
770
771 FOREACH_KEYVAL(pos, zoption, header_key, header_val) {
772 if (header_key.type == HASH_KEY_IS_STRING) {
773 char header[1024];
774
775 ZVAL_ADDREF(*header_val);
776 convert_to_string_ex(header_val);
777 if (!strcasecmp(header_key.str, "range")) {
778 range_req = 1;
779 }
780 snprintf(header, sizeof(header), "%s: %s", header_key.str, Z_STRVAL_PP(header_val));
781 request->_cache.headers = curl_slist_append(request->_cache.headers, header);
782 zval_ptr_dtor(header_val);
783 }
784 }
785 }
786 /* etag */
787 if ((zoption = http_request_option(request, options, "etag", IS_STRING)) && Z_STRLEN_P(zoption)) {
788 char match_header[1024], *quoted_etag = NULL;
789
790 if ((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"')) {
791 spprintf(&quoted_etag, 0, "\"%s\"", Z_STRVAL_P(zoption));
792 }
793 snprintf(match_header, sizeof(match_header), "%s: %s", range_req?"If-Match":"If-None-Match", quoted_etag?quoted_etag:Z_STRVAL_P(zoption));
794 request->_cache.headers = curl_slist_append(request->_cache.headers, match_header);
795 STR_FREE(quoted_etag);
796 }
797 /* compression */
798 if ((zoption = http_request_option(request, options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
799 request->_cache.headers = curl_slist_append(request->_cache.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
800 }
801 HTTP_CURL_OPT(CURLOPT_HTTPHEADER, request->_cache.headers);
802
803 /* lastmodified */
804 if ((zoption = http_request_option(request, options, "lastmodified", IS_LONG))) {
805 if (Z_LVAL_P(zoption)) {
806 if (Z_LVAL_P(zoption) > 0) {
807 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
808 } else {
809 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, (long) HTTP_G->request.time + Z_LVAL_P(zoption));
810 }
811 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, (long) (range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE));
812 } else {
813 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
814 }
815 }
816
817 /* cookies, array('name' => 'value') */
818 if ((zoption = http_request_option(request, options, "cookies", IS_ARRAY))) {
819 phpstr_dtor(&request->_cache.cookies);
820 if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
821 zval *urlenc_cookies = NULL;
822 /* check whether cookies should not be urlencoded; default is to urlencode them */
823 if ((!(urlenc_cookies = http_request_option(request, options, "encodecookies", IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) {
824 if (SUCCESS == http_urlencode_hash_recursive(HASH_OF(zoption), &request->_cache.cookies, "; ", lenof("; "), NULL, 0)) {
825 phpstr_fix(&request->_cache.cookies);
826 HTTP_CURL_OPT(CURLOPT_COOKIE, request->_cache.cookies.data);
827 }
828 } else {
829 HashPosition pos;
830 HashKey cookie_key = initHashKey(0);
831 zval **cookie_val;
832
833 FOREACH_KEYVAL(pos, zoption, cookie_key, cookie_val) {
834 if (cookie_key.type == HASH_KEY_IS_STRING) {
835 zval *val = *cookie_val;
836 convert_to_string_ex(&val);
837 phpstr_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(val));
838 if (val != *cookie_val) {
839 zval_ptr_dtor(&val);
840 }
841 }
842 }
843
844 phpstr_fix(&request->_cache.cookies);
845 if (PHPSTR_LEN(&request->_cache.cookies)) {
846 HTTP_CURL_OPT(CURLOPT_COOKIE, PHPSTR_VAL(&request->_cache.cookies));
847 }
848 }
849 }
850 }
851
852 /* don't load session cookies from cookiestore */
853 if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL)) && Z_BVAL_P(zoption)) {
854 HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 1L);
855 }
856
857 /* cookiestore, read initial cookies from that file and store cookies back into that file */
858 if ((zoption = http_request_option(request, options, "cookiestore", IS_STRING))) {
859 if (Z_STRLEN_P(zoption)) {
860 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE);
861 }
862 if (storage->cookiestore) {
863 pefree(storage->cookiestore, 1);
864 }
865 storage->cookiestore = pestrndup(Z_STRVAL_P(zoption), Z_STRLEN_P(zoption), 1);
866 HTTP_CURL_OPT(CURLOPT_COOKIEFILE, storage->cookiestore);
867 HTTP_CURL_OPT(CURLOPT_COOKIEJAR, storage->cookiestore);
868 }
869
870 /* maxfilesize */
871 if ((zoption = http_request_option(request, options, "maxfilesize", IS_LONG))) {
872 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
873 }
874
875 /* http protocol */
876 if ((zoption = http_request_option(request, options, "protocol", IS_LONG))) {
877 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption));
878 }
879
880 #if HTTP_CURL_VERSION(7,16,2)
881 /* timeout, defaults to 0 */
882 if ((zoption = http_request_option(request, options, "timeout", IS_DOUBLE))) {
883 HTTP_CURL_OPT(CURLOPT_TIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
884 }
885 /* connecttimeout, defaults to 0 */
886 if ((zoption = http_request_option(request, options, "connecttimeout", IS_DOUBLE))) {
887 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
888 }
889 #else
890 /* timeout, defaults to 0 */
891 if ((zoption = http_request_option(request, options, "timeout", IS_LONG))) {
892 HTTP_CURL_OPT(CURLOPT_TIMEOUT, Z_LVAL_P(zoption));
893 }
894 /* connecttimeout, defaults to 0 */
895 if ((zoption = http_request_option(request, options, "connecttimeout", IS_LONG))) {
896 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT, Z_LVAL_P(zoption));
897 }
898 #endif
899
900 /* ssl */
901 if ((zoption = http_request_option(request, options, "ssl", IS_ARRAY))) {
902 HashKey key = initHashKey(0);
903 zval **param;
904 HashPosition pos;
905
906 FOREACH_KEYVAL(pos, zoption, key, param) {
907 if (key.type == HASH_KEY_IS_STRING) {
908 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1);
909 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0);
910 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0);
911
912 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0);
913 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0);
914 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0);
915
916 HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0);
917 HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0);
918
919 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1);
920 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1);
921 HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0);
922
923 HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1);
924 HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1);
925 HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1);
926 HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1);
927 }
928 }
929 }
930
931 /* request method */
932 switch (request->meth) {
933 case HTTP_GET:
934 HTTP_CURL_OPT(CURLOPT_HTTPGET, 1L);
935 break;
936
937 case HTTP_HEAD:
938 HTTP_CURL_OPT(CURLOPT_NOBODY, 1L);
939 break;
940
941 case HTTP_POST:
942 HTTP_CURL_OPT(CURLOPT_POST, 1L);
943 break;
944
945 case HTTP_PUT:
946 HTTP_CURL_OPT(CURLOPT_UPLOAD, 1L);
947 break;
948
949 default:
950 if (http_request_method_exists(0, request->meth, NULL)) {
951 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST, http_request_method_name(request->meth));
952 } else {
953 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", request->meth, request->url);
954 return FAILURE;
955 }
956 break;
957 }
958
959 /* attach request body */
960 if (request->body && (request->meth != HTTP_GET) && (request->meth != HTTP_HEAD) && (request->meth != HTTP_OPTIONS)) {
961 switch (request->body->type) {
962 case HTTP_REQUEST_BODY_EMPTY:
963 /* nothing */
964 break;
965
966 case HTTP_REQUEST_BODY_CURLPOST:
967 HTTP_CURL_OPT(CURLOPT_HTTPPOST, (struct curl_httppost *) request->body->data);
968 break;
969
970 case HTTP_REQUEST_BODY_CSTRING:
971 if (request->meth != HTTP_PUT) {
972 HTTP_CURL_OPT(CURLOPT_POSTFIELDS, request->body->data);
973 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, request->body->size);
974 break;
975 }
976 /* fallthrough, PUT/UPLOAD _needs_ READDATA */
977 case HTTP_REQUEST_BODY_UPLOADFILE:
978 HTTP_CURL_OPT(CURLOPT_IOCTLDATA, request);
979 HTTP_CURL_OPT(CURLOPT_READDATA, request);
980 HTTP_CURL_OPT(CURLOPT_INFILESIZE, request->body->size);
981 break;
982
983 default:
984 /* shouldn't ever happen */
985 http_error_ex(HE_ERROR, 0, "Unknown request body type: %d (%s)", request->body->type, request->url);
986 return FAILURE;
987 }
988 }
989
990 return SUCCESS;
991 }
992 /* }}} */
993
994 /* {{{ void http_request_exec(http_request *) */
995 PHP_HTTP_API void _http_request_exec(http_request *request)
996 {
997 uint tries = 0;
998 CURLcode result;
999 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
1000
1001 retry:
1002 if (CURLE_OK != (result = curl_easy_perform(request->ch))) {
1003 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), http_request_storage_get(request->ch)->errorbuffer, request->url);
1004
1005 if (request->_retry.count > tries++) {
1006 switch (result) {
1007 case CURLE_COULDNT_RESOLVE_PROXY:
1008 case CURLE_COULDNT_RESOLVE_HOST:
1009 case CURLE_COULDNT_CONNECT:
1010 case CURLE_WRITE_ERROR:
1011 case CURLE_READ_ERROR:
1012 case CURLE_OPERATION_TIMEDOUT:
1013 case CURLE_SSL_CONNECT_ERROR:
1014 case CURLE_GOT_NOTHING:
1015 case CURLE_SSL_ENGINE_SETFAILED:
1016 case CURLE_SEND_ERROR:
1017 case CURLE_RECV_ERROR:
1018 case CURLE_SSL_ENGINE_INITFAILED:
1019 case CURLE_LOGIN_DENIED:
1020 if (request->_retry.delay >= HTTP_DIFFSEC) {
1021 http_sleep(request->_retry.delay);
1022 }
1023 goto retry;
1024 default:
1025 break;
1026 }
1027 }
1028 }
1029 }
1030 /* }}} */
1031
1032 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
1033 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ctx)
1034 {
1035 http_request *request = (http_request *) ctx;
1036 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
1037
1038 if (request->body) {
1039 switch (request->body->type) {
1040 case HTTP_REQUEST_BODY_CSTRING:
1041 {
1042 size_t out = MIN(len * n, request->body->size - request->body->priv);
1043
1044 if (out) {
1045 memcpy(data, ((char *) request->body->data) + request->body->priv, out);
1046 request->body->priv += out;
1047 return out;
1048 }
1049 break;
1050 }
1051
1052 case HTTP_REQUEST_BODY_UPLOADFILE:
1053 return php_stream_read((php_stream *) request->body->data, data, len * n);
1054 }
1055 }
1056 return 0;
1057 }
1058 /* }}} */
1059
1060 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
1061 static int http_curl_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow)
1062 {
1063 zval *param, retval;
1064 http_request *request = (http_request *) ctx;
1065 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
1066
1067 INIT_PZVAL(&retval);
1068 ZVAL_NULL(&retval);
1069
1070 MAKE_STD_ZVAL(param);
1071 array_init(param);
1072 add_assoc_double(param, "dltotal", dltotal);
1073 add_assoc_double(param, "dlnow", dlnow);
1074 add_assoc_double(param, "ultotal", ultotal);
1075 add_assoc_double(param, "ulnow", ulnow);
1076
1077 with_error_handling(EH_NORMAL, NULL) {
1078 request->_in_progress_cb = 1;
1079 call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, &param TSRMLS_CC);
1080 request->_in_progress_cb = 0;
1081 } end_error_handling();
1082
1083 zval_ptr_dtor(&param);
1084 zval_dtor(&retval);
1085
1086 return 0;
1087 }
1088 /* }}} */
1089
1090 /* {{{ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
1091 static curlioerr http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
1092 {
1093 http_request *request = (http_request *) ctx;
1094 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
1095
1096 if (cmd != CURLIOCMD_RESTARTREAD) {
1097 return CURLIOE_UNKNOWNCMD;
1098 }
1099
1100 if (request->body) {
1101 switch (request->body->type) {
1102 case HTTP_REQUEST_BODY_CSTRING:
1103 request->body->priv = 0;
1104 return CURLIOE_OK;
1105 break;
1106
1107 case HTTP_REQUEST_BODY_UPLOADFILE:
1108 if (SUCCESS == php_stream_rewind((php_stream *) request->body->data)) {
1109 return CURLIOE_OK;
1110 }
1111 break;
1112 }
1113 }
1114
1115 return CURLIOE_FAILRESTART;
1116 }
1117 /* }}} */
1118
1119 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
1120 static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
1121 {
1122 http_request *request = (http_request *) ctx;
1123
1124 #define EMPTY_HEADER(d, l) (!l || (l == 1 && d[0] == '\n') || (l == 2 && d[0] == '\r' && d[1] == '\n'))
1125 switch (type) {
1126 case CURLINFO_DATA_IN:
1127 if (request->conv.last_type == CURLINFO_HEADER_IN) {
1128 phpstr_appends(&request->conv.response, HTTP_CRLF);
1129 }
1130 phpstr_append(&request->conv.response, data, length);
1131 break;
1132 case CURLINFO_HEADER_IN:
1133 if (!EMPTY_HEADER(data, length)) {
1134 phpstr_append(&request->conv.response, data, length);
1135 }
1136 break;
1137 case CURLINFO_DATA_OUT:
1138 case CURLINFO_HEADER_OUT:
1139 phpstr_append(&request->conv.request, data, length);
1140 break;
1141 default:
1142 break;
1143 }
1144
1145 #if 0
1146 {
1147 const char _sym[] = "><><><";
1148 if (type) {
1149 for (fprintf(stderr, "%c ", _sym[type-1]); length--; data++) {
1150 fprintf(stderr, HTTP_IS_CTYPE(print, *data)?"%c":"\\x%02X", (int) *data);
1151 if (*data == '\n' && length) {
1152 fprintf(stderr, "\n%c ", _sym[type-1]);
1153 }
1154 }
1155 fprintf(stderr, "\n");
1156 } else {
1157 fprintf(stderr, "# %s", data);
1158 }
1159 }
1160 #endif
1161
1162 if (type) {
1163 request->conv.last_type = type;
1164 }
1165 return 0;
1166 }
1167 /* }}} */
1168
1169 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
1170 static inline zval *_http_request_option_ex(http_request *r, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
1171 {
1172 if (options) {
1173 zval **zoption;
1174 ulong h = zend_hash_func(key, keylen);
1175
1176 if (SUCCESS == zend_hash_quick_find(options, key, keylen, h, (void *) &zoption)) {
1177 zval *copy;
1178
1179 MAKE_STD_ZVAL(copy);
1180 ZVAL_ZVAL(copy, *zoption, 1, 0);
1181
1182 convert_to_type(type, copy);
1183 http_request_option_cache_ex(r, key, keylen, h, copy);
1184 zval_ptr_dtor(&copy);
1185 return copy;
1186 }
1187 }
1188
1189 return NULL;
1190 }
1191 /* }}} */
1192
1193 /* {{{ static inline zval *http_request_option_cache(http_request *, char *key, zval *) */
1194 static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC)
1195 {
1196 ZVAL_ADDREF(opt);
1197
1198 if (h) {
1199 zend_hash_quick_update(&r->_cache.options, key, keylen, h, &opt, sizeof(zval *), NULL);
1200 } else {
1201 zend_hash_update(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL);
1202 }
1203
1204 return opt;
1205 }
1206 /* }}} */
1207
1208 #endif /* HTTP_HAVE_CURL */
1209
1210 /*
1211 * Local variables:
1212 * tab-width: 4
1213 * c-basic-offset: 4
1214 * End:
1215 * vim600: noet sw=4 ts=4 fdm=marker
1216 * vim<600: noet sw=4 ts=4
1217 */
1218