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