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