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