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