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