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