* Fixed Bug #7192: Build against libcurl >= 7.15.2 fails
[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-2006, 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_request_api.h"
23 #include "php_http_url_api.h"
24
25 #ifdef ZEND_ENGINE_2
26 # include "php_http_request_object.h"
27 #endif
28
29 /* {{{ cruft for thread safe SSL crypto locks */
30 #if defined(ZTS) && defined(HTTP_HAVE_SSL)
31 # ifdef PHP_WIN32
32 # define HTTP_NEED_OPENSSL_TSL
33 # include <openssl/crypto.h>
34 # else /* !PHP_WIN32 */
35 # if defined(HTTP_HAVE_OPENSSL)
36 # if defined(HAVE_OPENSSL_CRYPTO_H)
37 # define HTTP_NEED_OPENSSL_TSL
38 # include <openssl/crypto.h>
39 # else
40 # warning \
41 "libcurl was compiled with OpenSSL support, but configure could not find " \
42 "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
43 "cause random crashes on SSL requests"
44 # endif
45 # elif defined(HTTP_HAVE_GNUTLS)
46 # if defined(HAVE_GCRYPT_H)
47 # define HTTP_NEED_GNUTLS_TSL
48 # include <gcrypt.h>
49 # else
50 # warning \
51 "libcurl was compiled with GnuTLS support, but configure could not find " \
52 "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
53 "cause random crashes on SSL requests"
54 # endif
55 # else
56 # warning \
57 "libcurl was compiled with SSL support, but configure could not determine which" \
58 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
59 "cause random crashes on SSL requests"
60 # endif /* HTTP_HAVE_OPENSSL || HTTP_HAVE_GNUTLS */
61 # endif /* PHP_WIN32 */
62 #endif /* ZTS && HTTP_HAVE_SSL */
63
64 #ifdef HTTP_NEED_OPENSSL_TSL
65 static MUTEX_T *http_openssl_tsl = NULL;
66
67 static void http_openssl_thread_lock(int mode, int n, const char * file, int line)
68 {
69 if (mode & CRYPTO_LOCK) {
70 tsrm_mutex_lock(http_openssl_tsl[n]);
71 } else {
72 tsrm_mutex_unlock(http_openssl_tsl[n]);
73 }
74 }
75
76 static ulong http_openssl_thread_id(void)
77 {
78 return (ulong) tsrm_thread_id();
79 }
80 #endif
81 #ifdef HTTP_NEED_GNUTLS_TSL
82 static int http_gnutls_mutex_create(void **m)
83 {
84 if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
85 return SUCCESS;
86 } else {
87 return FAILURE;
88 }
89 }
90
91 static int http_gnutls_mutex_destroy(void **m)
92 {
93 tsrm_mutex_free(*((MUTEX_T *) m));
94 return SUCCESS;
95 }
96
97 static int http_gnutls_mutex_lock(void **m)
98 {
99 return tsrm_mutex_lock(*((MUTEX_T *) m));
100 }
101
102 static int http_gnutls_mutex_unlock(void **m)
103 {
104 return tsrm_mutex_unlock(*((MUTEX_T *) m));
105 }
106
107 static struct gcry_thread_cbs http_gnutls_tsl = {
108 GCRY_THREAD_OPTION_USER,
109 NULL,
110 http_gnutls_mutex_create,
111 http_gnutls_mutex_destroy,
112 http_gnutls_mutex_lock,
113 http_gnutls_mutex_unlock
114 };
115 #endif
116 /* }}} */
117
118 /* {{{ MINIT */
119 PHP_MINIT_FUNCTION(http_request)
120 {
121 #ifdef HTTP_NEED_OPENSSL_TSL
122 int i, c = CRYPTO_num_locks();
123
124 http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
125
126 for (i = 0; i < c; ++i) {
127 http_openssl_tsl[i] = tsrm_mutex_alloc();
128 }
129
130 CRYPTO_set_id_callback(http_openssl_thread_id);
131 CRYPTO_set_locking_callback(http_openssl_thread_lock);
132 #endif
133 #ifdef HTTP_NED_GNUTLS_TSL
134 gcry_control(GCRYCTL_SET_THREAD_CBS, &http_gnutls_tsl);
135 #endif
136
137 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
138 return FAILURE;
139 }
140
141 HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC);
142 HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST);
143 HTTP_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM);
144 HTTP_LONG_CONSTANT("HTTP_AUTH_ANY", CURLAUTH_ANY);
145
146 HTTP_LONG_CONSTANT("HTTP_VERSION_NONE", CURL_HTTP_VERSION_NONE);
147 HTTP_LONG_CONSTANT("HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0);
148 HTTP_LONG_CONSTANT("HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1);
149
150 #if HTTP_CURL_VERSION(7,15,2)
151 HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS4", CURLPROXY_SOCKS4);
152 #endif
153 HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS5", CURLPROXY_SOCKS5);
154 HTTP_LONG_CONSTANT("HTTP_PROXY_HTTP", CURLPROXY_HTTP);
155 return SUCCESS;
156 }
157 /* }}} */
158
159 /* {{{ MSHUTDOWN */
160 PHP_MSHUTDOWN_FUNCTION(http_request)
161 {
162 #ifdef HTTP_NEED_OPENSSL_TSL
163 CRYPTO_set_id_callback(http_openssl_thread_id);
164 CRYPTO_set_locking_callback(http_openssl_thread_lock);
165 #endif
166 curl_global_cleanup();
167 #ifdef HTTP_NEED_OPENSSL_TSL
168 if (http_openssl_tsl) {
169 int i, c = CRYPTO_num_locks();
170
171 CRYPTO_set_id_callback(NULL);
172 CRYPTO_set_locking_callback(NULL);
173
174 for (i = 0; i < c; ++i) {
175 tsrm_mutex_free(http_openssl_tsl[i]);
176 }
177
178 free(http_openssl_tsl);
179 http_openssl_tsl = NULL;
180 }
181 #endif
182 return SUCCESS;
183 }
184 /* }}} */
185
186 /* {{{ MACROS */
187 #ifndef HAVE_CURL_EASY_STRERROR
188 # define curl_easy_strerror(dummy) "unknown error"
189 #endif
190
191 #define HTTP_CURL_INFO(I) \
192 { \
193 char *N = #I; \
194 HTTP_CURL_INFO_EX(I, N+lenof("CURLINFO_")); \
195 }
196 #define HTTP_CURL_INFO_EX(I, X) \
197 switch (I & ~CURLINFO_MASK) \
198 { \
199 case CURLINFO_STRING: \
200 { \
201 char *c; \
202 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &c)) { \
203 char *key = estrndup(X, strlen(X)); \
204 add_assoc_string(&array, pretty_key(key, strlen(X), 0, 0), c ? c : "", 1); \
205 efree(key); \
206 } \
207 } \
208 break; \
209 \
210 case CURLINFO_DOUBLE: \
211 { \
212 double d; \
213 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &d)) { \
214 char *key = estrndup(X, strlen(X)); \
215 add_assoc_double(&array, pretty_key(key, strlen(X), 0, 0), d); \
216 efree(key); \
217 } \
218 } \
219 break; \
220 \
221 case CURLINFO_LONG: \
222 { \
223 long l; \
224 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &l)) { \
225 char *key = estrndup(X, strlen(X)); \
226 add_assoc_long(&array, pretty_key(key, strlen(X), 0, 0), l); \
227 efree(key); \
228 } \
229 } \
230 break; \
231 \
232 case CURLINFO_SLIST: \
233 { \
234 struct curl_slist *l, *p; \
235 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &l)) { \
236 zval *subarray; \
237 char *key = estrndup(X, strlen(X)); \
238 MAKE_STD_ZVAL(subarray); \
239 array_init(subarray); \
240 for (p = l; p; p = p->next) { \
241 add_next_index_string(subarray, p->data, 1); \
242 } \
243 add_assoc_zval(&array, pretty_key(key, strlen(X), 0, 0), subarray); \
244 curl_slist_free_all(l); \
245 efree(key); \
246 } \
247 } \
248 }
249
250 #define HTTP_CURL_OPT(OPTION, p) HTTP_CURL_OPT_EX(request->ch, OPTION, (p))
251 #define HTTP_CURL_OPT_EX(ch, OPTION, p) curl_easy_setopt((ch), OPTION, (p))
252
253 #define HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \
254 { \
255 char *K = #OPTION; \
256 HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \
257 }
258 #define HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
259 if (!strcasecmp(key, keyname)) { \
260 zval *copy = http_request_option_cache(request, keyname, zval_copy(IS_STRING, *param)); \
261 if (obdc) { \
262 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(copy), return FAILURE); \
263 } \
264 HTTP_CURL_OPT(optname, Z_STRVAL_P(copy)); \
265 key = NULL; \
266 continue; \
267 }
268 #define HTTP_CURL_OPT_LONG(OPTION, ldiff) \
269 { \
270 char *K = #OPTION; \
271 HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \
272 }
273 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
274 if (!strcasecmp(key, keyname)) { \
275 zval *copy = http_request_option_cache(request, keyname, zval_copy(IS_LONG, *param)); \
276 HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \
277 key = NULL; \
278 continue; \
279 }
280 /* }}} */
281
282 /* {{{ forward declarations */
283 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
284 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
285 static inline zval *_http_request_option_ex(http_request *request, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
286 #define http_request_option_cache(r, k, z) _http_request_option_cache_ex((r), (k), sizeof(k), 0, (z) TSRMLS_CC)
287 #define http_request_option_cache_ex(r, k, kl, h, z) _http_request_option_cache_ex((r), (k), (kl), (h), (z) TSRMLS_CC)
288 static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC);
289
290 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
291 static int http_curl_progress_callback(void *, double, double, double, double);
292 static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *);
293 static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { return n*l; }
294 static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *);
295 /* }}} */
296
297 /* {{{ CURL *http_curl_init(http_request *) */
298 PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, http_request *request)
299 {
300 if (ch || (ch = curl_easy_init())) {
301 #if defined(ZTS)
302 HTTP_CURL_OPT_EX(ch, CURLOPT_NOSIGNAL, 1);
303 #endif
304 HTTP_CURL_OPT_EX(ch, CURLOPT_HEADER, 0);
305 HTTP_CURL_OPT_EX(ch, CURLOPT_FILETIME, 1);
306 HTTP_CURL_OPT_EX(ch, CURLOPT_AUTOREFERER, 1);
307 HTTP_CURL_OPT_EX(ch, CURLOPT_VERBOSE, 1);
308 HTTP_CURL_OPT_EX(ch, CURLOPT_HEADERFUNCTION, NULL);
309 HTTP_CURL_OPT_EX(ch, CURLOPT_DEBUGFUNCTION, http_curl_raw_callback);
310 HTTP_CURL_OPT_EX(ch, CURLOPT_READFUNCTION, http_curl_read_callback);
311 HTTP_CURL_OPT_EX(ch, CURLOPT_IOCTLFUNCTION, http_curl_ioctl_callback);
312 HTTP_CURL_OPT_EX(ch, CURLOPT_WRITEFUNCTION, http_curl_dummy_callback);
313
314 /* set context */
315 if (request) {
316 HTTP_CURL_OPT_EX(ch, CURLOPT_PRIVATE, request);
317 HTTP_CURL_OPT_EX(ch, CURLOPT_DEBUGDATA, request);
318 HTTP_CURL_OPT_EX(ch, CURLOPT_ERRORBUFFER, request->_error);
319
320 /* attach curl handle */
321 request->ch = ch;
322 /* set defaults (also in http_request_reset()) */
323 http_request_defaults(request);
324 }
325 }
326
327 return ch;
328 }
329 /* }}} */
330
331 /* {{{ void http_curl_free(CURL **) */
332 PHP_HTTP_API void _http_curl_free(CURL **ch)
333 {
334 if (*ch) {
335 /* avoid nasty segfaults with already cleaned up callbacks */
336 HTTP_CURL_OPT_EX(*ch, CURLOPT_NOPROGRESS, 1);
337 HTTP_CURL_OPT_EX(*ch, CURLOPT_PROGRESSFUNCTION, NULL);
338 HTTP_CURL_OPT_EX(*ch, CURLOPT_VERBOSE, 0);
339 HTTP_CURL_OPT_EX(*ch, CURLOPT_DEBUGFUNCTION, NULL);
340 curl_easy_cleanup(*ch);
341 *ch = NULL;
342 }
343 }
344 /* }}} */
345
346 /* {{{ http_request *http_request_init(http_request *) */
347 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)
348 {
349 http_request *r;
350
351 if (request) {
352 r = request;
353 } else {
354 r = emalloc_rel(sizeof(http_request));
355 }
356 memset(r, 0, sizeof(http_request));
357
358 r->ch = ch;
359 r->url = (url) ? http_absolute_url(url) : NULL;
360 r->meth = (meth > 0) ? meth : HTTP_GET;
361
362 phpstr_init(&r->conv.request);
363 phpstr_init_ex(&r->conv.response, HTTP_CURLBUF_SIZE, 0);
364 phpstr_init(&r->_cache.cookies);
365 zend_hash_init(&r->_cache.options, 0, NULL, ZVAL_PTR_DTOR, 0);
366
367 TSRMLS_SET_CTX(r->tsrm_ls);
368
369 return r;
370 }
371 /* }}} */
372
373 /* {{{ void http_request_dtor(http_request *) */
374 PHP_HTTP_API void _http_request_dtor(http_request *request)
375 {
376 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
377
378 http_curl_free(&request->ch);
379 http_request_reset(request);
380
381 phpstr_dtor(&request->_cache.cookies);
382 zend_hash_destroy(&request->_cache.options);
383 if (request->_cache.headers) {
384 curl_slist_free_all(request->_cache.headers);
385 request->_cache.headers = NULL;
386 }
387 if (request->_progress_callback) {
388 zval_ptr_dtor(&request->_progress_callback);
389 request->_progress_callback = NULL;
390 }
391 }
392 /* }}} */
393
394 /* {{{ void http_request_free(http_request **) */
395 PHP_HTTP_API void _http_request_free(http_request **request)
396 {
397 if (*request) {
398 TSRMLS_FETCH_FROM_CTX((*request)->tsrm_ls);
399 http_request_body_free(&(*request)->body);
400 http_request_dtor(*request);
401 efree(*request);
402 *request = NULL;
403 }
404 }
405 /* }}} */
406
407 /* {{{ void http_request_reset(http_request *) */
408 PHP_HTTP_API void _http_request_reset(http_request *request)
409 {
410 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
411 STR_SET(request->url, NULL);
412 request->conv.last_type = 0;
413 phpstr_dtor(&request->conv.request);
414 phpstr_dtor(&request->conv.response);
415 http_request_body_dtor(request->body);
416
417 if (request->ch) {
418 http_request_defaults(request);
419 }
420 request->_error[0] = '\0';
421 }
422 /* }}} */
423
424 /* {{{ void http_request_defaults(http_request *) */
425 PHP_HTTP_API void _http_request_defaults(http_request *request)
426 {
427 if (request->ch) {
428 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, NULL);
429 HTTP_CURL_OPT(CURLOPT_URL, NULL);
430 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 1);
431 HTTP_CURL_OPT(CURLOPT_PROXY, NULL);
432 HTTP_CURL_OPT(CURLOPT_PROXYPORT, 0);
433 HTTP_CURL_OPT(CURLOPT_PROXYTYPE, 0);
434 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD, NULL);
435 HTTP_CURL_OPT(CURLOPT_PROXYAUTH, 0);
436 HTTP_CURL_OPT(CURLOPT_INTERFACE, NULL);
437 HTTP_CURL_OPT(CURLOPT_PORT, 0);
438 #if HTTP_CURL_VERSION(7,15,2)
439 HTTP_CURL_OPT(CURLOPT_LOCALPORT, 0);
440 HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, 0);
441 #endif
442 HTTP_CURL_OPT(CURLOPT_USERPWD, NULL);
443 HTTP_CURL_OPT(CURLOPT_HTTPAUTH, 0);
444 HTTP_CURL_OPT(CURLOPT_ENCODING, NULL);
445 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION, 0);
446 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, 0);
447 HTTP_CURL_OPT(CURLOPT_REFERER, NULL);
448 HTTP_CURL_OPT(CURLOPT_USERAGENT, "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
449 HTTP_CURL_OPT(CURLOPT_HTTPHEADER, NULL);
450 HTTP_CURL_OPT(CURLOPT_COOKIE, NULL);
451 #if HTTP_CURL_VERSION(7,14,1)
452 HTTP_CURL_OPT(CURLOPT_COOKIELIST, NULL);
453 #endif
454 HTTP_CURL_OPT(CURLOPT_COOKIEFILE, NULL);
455 HTTP_CURL_OPT(CURLOPT_COOKIEJAR, NULL);
456 HTTP_CURL_OPT(CURLOPT_RANGE, NULL);
457 HTTP_CURL_OPT(CURLOPT_RESUME_FROM, 0);
458 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, 0);
459 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, 0);
460 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, 0);
461 HTTP_CURL_OPT(CURLOPT_TIMEOUT, 0);
462 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT, 3);
463 HTTP_CURL_OPT(CURLOPT_SSLCERT, NULL);
464 HTTP_CURL_OPT(CURLOPT_SSLCERTTYPE, NULL);
465 HTTP_CURL_OPT(CURLOPT_SSLCERTPASSWD, NULL);
466 HTTP_CURL_OPT(CURLOPT_SSLKEY, NULL);
467 HTTP_CURL_OPT(CURLOPT_SSLKEYTYPE, NULL);
468 HTTP_CURL_OPT(CURLOPT_SSLKEYPASSWD, NULL);
469 HTTP_CURL_OPT(CURLOPT_SSLENGINE, NULL);
470 HTTP_CURL_OPT(CURLOPT_SSLVERSION, 0);
471 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYPEER, 0);
472 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYHOST, 0);
473 HTTP_CURL_OPT(CURLOPT_SSL_CIPHER_LIST, NULL);
474 HTTP_CURL_OPT(CURLOPT_CAINFO, NULL);
475 HTTP_CURL_OPT(CURLOPT_CAPATH, NULL);
476 HTTP_CURL_OPT(CURLOPT_RANDOM_FILE, NULL);
477 HTTP_CURL_OPT(CURLOPT_EGDSOCKET, NULL);
478 HTTP_CURL_OPT(CURLOPT_POSTFIELDS, NULL);
479 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, 0);
480 HTTP_CURL_OPT(CURLOPT_HTTPPOST, NULL);
481 HTTP_CURL_OPT(CURLOPT_IOCTLDATA, NULL);
482 HTTP_CURL_OPT(CURLOPT_READDATA, NULL);
483 HTTP_CURL_OPT(CURLOPT_INFILESIZE, 0);
484 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
485 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST, NULL);
486 HTTP_CURL_OPT(CURLOPT_NOBODY, 0);
487 HTTP_CURL_OPT(CURLOPT_POST, 0);
488 HTTP_CURL_OPT(CURLOPT_UPLOAD, 0);
489 HTTP_CURL_OPT(CURLOPT_HTTPGET, 1);
490 }
491 }
492 /* }}} */
493
494 PHP_HTTP_API void _http_request_set_progress_callback(http_request *request, zval *cb)
495 {
496 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
497
498 if (request->_progress_callback) {
499 zval_ptr_dtor(&request->_progress_callback);
500 }
501 if ((request->_progress_callback = cb)) {
502 ZVAL_ADDREF(cb);
503 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 0);
504 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA, request);
505 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, http_curl_progress_callback);
506 } else {
507 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 1);
508 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA, NULL);
509 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, NULL);
510 }
511 }
512
513 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
514 PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *options)
515 {
516 zval *zoption;
517 zend_bool range_req = 0;
518
519 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
520
521 HTTP_CHECK_CURL_INIT(request->ch, http_curl_init(request), return FAILURE);
522
523 /* set options */
524 HTTP_CURL_OPT(CURLOPT_URL, request->url);
525
526 /* progress callback */
527 if ((zoption = http_request_option(request, options, "onprogress", -1))) {
528 http_request_set_progress_callback(request, zoption);
529 }
530
531 /* proxy */
532 if ((zoption = http_request_option(request, options, "proxyhost", IS_STRING))) {
533 if (Z_STRLEN_P(zoption)) {
534 HTTP_CURL_OPT(CURLOPT_PROXY, Z_STRVAL_P(zoption));
535 }
536 /* type */
537 if ((zoption = http_request_option(request, options, "proxytype", IS_LONG))) {
538 HTTP_CURL_OPT(CURLOPT_PROXYTYPE, Z_LVAL_P(zoption));
539 }
540 /* port */
541 if ((zoption = http_request_option(request, options, "proxyport", IS_LONG))) {
542 HTTP_CURL_OPT(CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
543 }
544 /* user:pass */
545 if ((zoption = http_request_option(request, options, "proxyauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
546 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
547 }
548 /* auth method */
549 if ((zoption = http_request_option(request, options, "proxyauthtype", IS_LONG))) {
550 HTTP_CURL_OPT(CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
551 }
552 }
553
554 /* outgoing interface */
555 if ((zoption = http_request_option(request, options, "interface", IS_STRING))) {
556 HTTP_CURL_OPT(CURLOPT_INTERFACE, Z_STRVAL_P(zoption));
557
558 #if HTTP_CURL_VERSION(7,15,2)
559 if ((zoption = http_request_option(request, options, "portrange", IS_ARRAY))) {
560 zval **prs, **pre;
561
562 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
563 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &prs)) {
564 zend_hash_move_forward(Z_ARRVAL_P(zoption));
565 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &pre)) {
566 zval *prs_cpy = zval_copy(IS_LONG, *prs), *pre_cpy = zval_copy(IS_LONG, *pre);
567
568 if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) {
569 fprintf(stderr, "Using portrange: %ld-%ld\n", Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy));
570 HTTP_CURL_OPT(CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy)));
571 HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L);
572 }
573 zval_free(&prs_cpy);
574 zval_free(&pre_cpy);
575 }
576 }
577 }
578 #endif
579 }
580
581 /* another port */
582 if ((zoption = http_request_option(request, options, "port", IS_LONG))) {
583 HTTP_CURL_OPT(CURLOPT_PORT, Z_LVAL_P(zoption));
584 }
585
586 /* auth */
587 if ((zoption = http_request_option(request, options, "httpauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
588 HTTP_CURL_OPT(CURLOPT_USERPWD, Z_STRVAL_P(zoption));
589 }
590 if ((zoption = http_request_option(request, options, "httpauthtype", IS_LONG))) {
591 HTTP_CURL_OPT(CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
592 }
593
594 /* redirects, defaults to 0 */
595 if ((zoption = http_request_option(request, options, "redirect", IS_LONG))) {
596 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
597 HTTP_CURL_OPT(CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
598 if ((zoption = http_request_option(request, options, "unrestrictedauth", IS_BOOL))) {
599 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
600 }
601 }
602
603 /* referer */
604 if ((zoption = http_request_option(request, options, "referer", IS_STRING)) && Z_STRLEN_P(zoption)) {
605 HTTP_CURL_OPT(CURLOPT_REFERER, Z_STRVAL_P(zoption));
606 }
607
608 /* useragent, default "PECL::HTTP/version (PHP/version)" */
609 if ((zoption = http_request_option(request, options, "useragent", IS_STRING))) {
610 /* allow to send no user agent, not even default one */
611 if (Z_STRLEN_P(zoption)) {
612 HTTP_CURL_OPT(CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
613 } else {
614 HTTP_CURL_OPT(CURLOPT_USERAGENT, NULL);
615 }
616 }
617
618 /* resume */
619 if ((zoption = http_request_option(request, options, "resume", IS_LONG)) && (Z_LVAL_P(zoption) != 0)) {
620 range_req = 1;
621 HTTP_CURL_OPT(CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
622 }
623 /* or range of kind array(array(0,499), array(100,1499)) */
624 else if ((zoption = http_request_option(request, options, "range", IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
625 HashPosition pos1, pos2;
626 zval **rr, **rb, **re;
627 phpstr rs;
628
629 phpstr_init(&rs);
630 FOREACH_VAL(pos1, zoption, rr) {
631 if (Z_TYPE_PP(rr) == IS_ARRAY) {
632 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2);
633 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void **) &rb, &pos2)) {
634 zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2);
635 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void **) &re, &pos2)) {
636 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))) &&
637 ((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)))) {
638 zval *rbl = zval_copy(IS_LONG, *rb), *rel = zval_copy(IS_LONG, *re);
639
640 if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) {
641 phpstr_appendf(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel));
642 }
643 zval_free(&rbl);
644 zval_free(&rel);
645 }
646 }
647 }
648 }
649 }
650
651 if (PHPSTR_LEN(&rs)) {
652 zval *cached_range;
653
654 /* ditch last comma */
655 PHPSTR_VAL(&rs)[PHPSTR_LEN(&rs)-- -1] = '\0';
656 /* cache string */
657 MAKE_STD_ZVAL(cached_range);
658 ZVAL_STRINGL(cached_range, PHPSTR_VAL(&rs), PHPSTR_LEN(&rs), 0);
659 HTTP_CURL_OPT(CURLOPT_RANGE, Z_STRVAL_P(http_request_option_cache(request, "range", cached_range)));
660 zval_ptr_dtor(&cached_range);
661 }
662 }
663
664 /* additional headers, array('name' => 'value') */
665 if (request->_cache.headers) {
666 curl_slist_free_all(request->_cache.headers);
667 request->_cache.headers = NULL;
668 }
669 if ((zoption = http_request_option(request, options, "headers", IS_ARRAY))) {
670 char *header_key;
671 ulong header_idx;
672 HashPosition pos;
673
674 FOREACH_KEY(pos, zoption, header_key, header_idx) {
675 if (header_key) {
676 zval **header_val;
677 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &header_val, &pos)) {
678 char header[1024] = {0};
679
680 ZVAL_ADDREF(*header_val);
681 convert_to_string_ex(header_val);
682 if (!strcasecmp(header_key, "range")) {
683 range_req = 1;
684 }
685 snprintf(header, lenof(header), "%s: %s", header_key, Z_STRVAL_PP(header_val));
686 request->_cache.headers = curl_slist_append(request->_cache.headers, header);
687 zval_ptr_dtor(header_val);
688 }
689
690 /* reset */
691 header_key = NULL;
692 }
693 }
694 }
695 /* etag */
696 if ((zoption = http_request_option(request, options, "etag", IS_STRING)) && Z_STRLEN_P(zoption)) {
697 char match_header[1024] = {0}, *quoted_etag = NULL;
698
699 if ((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"')) {
700 spprintf(&quoted_etag, 0, "\"%s\"", Z_STRVAL_P(zoption));
701 }
702 snprintf(match_header, lenof(match_header), "%s: %s", range_req?"If-Match":"If-None-Match", quoted_etag?quoted_etag:Z_STRVAL_P(zoption));
703 request->_cache.headers = curl_slist_append(request->_cache.headers, match_header);
704 STR_FREE(quoted_etag);
705 }
706 /* compression */
707 if ((zoption = http_request_option(request, options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
708 request->_cache.headers = curl_slist_append(request->_cache.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
709 }
710 HTTP_CURL_OPT(CURLOPT_HTTPHEADER, request->_cache.headers);
711
712 /* lastmodified */
713 if ((zoption = http_request_option(request, options, "lastmodified", IS_LONG))) {
714 if (Z_LVAL_P(zoption)) {
715 if (Z_LVAL_P(zoption) > 0) {
716 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
717 } else {
718 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, HTTP_GET_REQUEST_TIME() + Z_LVAL_P(zoption));
719 }
720 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
721 } else {
722 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
723 }
724 }
725
726 /* cookies, array('name' => 'value') */
727 if ((zoption = http_request_option(request, options, "cookies", IS_ARRAY))) {
728 phpstr_dtor(&request->_cache.cookies);
729 if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
730 zval *urlenc_cookies = NULL;
731 /* check whether cookies should not be urlencoded; default is to urlencode them */
732 if ((!(urlenc_cookies = http_request_option(request, options, "encodecookies", IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) {
733 if (SUCCESS == http_urlencode_hash_recursive(HASH_OF(zoption), &request->_cache.cookies, "; ", lenof("; "), NULL, 0)) {
734 phpstr_fix(&request->_cache.cookies);
735 HTTP_CURL_OPT(CURLOPT_COOKIE, request->_cache.cookies.data);
736 }
737 } else {
738 HashPosition pos;
739 char *cookie_key = NULL;
740 ulong cookie_idx;
741
742 FOREACH_KEY(pos, zoption, cookie_key, cookie_idx) {
743 if (cookie_key) {
744 zval **cookie_val;
745 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &cookie_val, &pos)) {
746 zval *val = zval_copy(IS_STRING, *cookie_val);
747 phpstr_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key, Z_STRVAL_P(val));
748 zval_free(&val);
749 }
750
751 /* reset */
752 cookie_key = NULL;
753 }
754 }
755
756 phpstr_fix(&request->_cache.cookies);
757 if (PHPSTR_LEN(&request->_cache.cookies)) {
758 HTTP_CURL_OPT(CURLOPT_COOKIE, PHPSTR_VAL(&request->_cache.cookies));
759 }
760 }
761 }
762 }
763
764 #if HTTP_CURL_VERSION(7,14,1)
765 /* reset cookies */
766 if ((zoption = http_request_option(request, options, "resetcookies", IS_BOOL)) && Z_LVAL_P(zoption)) {
767 HTTP_CURL_OPT(CURLOPT_COOKIELIST, "ALL");
768 }
769 #endif
770
771 /* session cookies */
772 if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL))) {
773 if (Z_LVAL_P(zoption)) {
774 /* accept cookies for this session */
775 HTTP_CURL_OPT(CURLOPT_COOKIEFILE, "");
776 } else {
777 /* reset session cookies */
778 HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 1);
779 }
780 }
781
782 /* cookiestore, read initial cookies from that file and store cookies back into that file */
783 if ((zoption = http_request_option(request, options, "cookiestore", IS_STRING)) && Z_STRLEN_P(zoption)) {
784 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE);
785 HTTP_CURL_OPT(CURLOPT_COOKIEFILE, Z_STRVAL_P(zoption));
786 HTTP_CURL_OPT(CURLOPT_COOKIEJAR, Z_STRVAL_P(zoption));
787 }
788
789 /* maxfilesize */
790 if ((zoption = http_request_option(request, options, "maxfilesize", IS_LONG))) {
791 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
792 }
793
794 /* http protocol */
795 if ((zoption = http_request_option(request, options, "protocol", IS_LONG))) {
796 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption));
797 }
798
799 /* timeout, defaults to 0 */
800 if ((zoption = http_request_option(request, options, "timeout", IS_LONG))) {
801 HTTP_CURL_OPT(CURLOPT_TIMEOUT, Z_LVAL_P(zoption));
802 }
803
804 /* connecttimeout, defaults to 0 */
805 if ((zoption = http_request_option(request, options, "connecttimeout", IS_LONG))) {
806 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT, Z_LVAL_P(zoption));
807 }
808
809 /* ssl */
810 if ((zoption = http_request_option(request, options, "ssl", IS_ARRAY))) {
811 ulong idx;
812 char *key = NULL;
813 zval **param;
814 HashPosition pos;
815
816 FOREACH_KEYVAL(pos, zoption, key, idx, param) {
817 if (key) {
818 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1);
819 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0);
820 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0);
821
822 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0);
823 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0);
824 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0);
825
826 HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0);
827 HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0);
828
829 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1);
830 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1);
831 HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0);
832
833 HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1);
834 HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1);
835 HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1);
836 HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1);
837
838 /* reset key */
839 key = NULL;
840 }
841 }
842 }
843
844 /* request method */
845 switch (request->meth)
846 {
847 case HTTP_GET:
848 HTTP_CURL_OPT(CURLOPT_HTTPGET, 1);
849 break;
850
851 case HTTP_HEAD:
852 HTTP_CURL_OPT(CURLOPT_NOBODY, 1);
853 break;
854
855 case HTTP_POST:
856 HTTP_CURL_OPT(CURLOPT_POST, 1);
857 break;
858
859 case HTTP_PUT:
860 HTTP_CURL_OPT(CURLOPT_UPLOAD, 1);
861 break;
862
863 default:
864 if (http_request_method_exists(0, request->meth, NULL)) {
865 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST, http_request_method_name(request->meth));
866 } else {
867 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", request->meth, request->url);
868 return FAILURE;
869 }
870 break;
871 }
872
873 /* attach request body */
874 if (request->body && (request->meth != HTTP_GET) && (request->meth != HTTP_HEAD) && (request->meth != HTTP_OPTIONS)) {
875 switch (request->body->type)
876 {
877 case HTTP_REQUEST_BODY_EMPTY:
878 /* nothing */
879 break;
880
881 case HTTP_REQUEST_BODY_CURLPOST:
882 HTTP_CURL_OPT(CURLOPT_HTTPPOST, (struct curl_httppost *) request->body->data);
883 break;
884
885 case HTTP_REQUEST_BODY_CSTRING:
886 if (request->meth != HTTP_PUT) {
887 HTTP_CURL_OPT(CURLOPT_POSTFIELDS, request->body->data);
888 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, request->body->size);
889 break;
890 }
891 /* fallthrough, PUT/UPLOAD _needs_ READDATA */
892 case HTTP_REQUEST_BODY_UPLOADFILE:
893 HTTP_CURL_OPT(CURLOPT_IOCTLDATA, request);
894 HTTP_CURL_OPT(CURLOPT_READDATA, request);
895 HTTP_CURL_OPT(CURLOPT_INFILESIZE, request->body->size);
896 break;
897
898 default:
899 /* shouldn't ever happen */
900 http_error_ex(HE_ERROR, 0, "Unknown request body type: %d (%s)", request->body->type, request->url);
901 return FAILURE;
902 break;
903 }
904 }
905
906 return SUCCESS;
907 }
908 /* }}} */
909
910 /* {{{ void http_request_exec(http_request *) */
911 PHP_HTTP_API void _http_request_exec(http_request *request)
912 {
913 CURLcode result;
914 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
915
916 if (CURLE_OK != (result = curl_easy_perform(request->ch))) {
917 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), request->_error, request->url);
918 }
919 }
920 /* }}} */
921
922 /* {{{ void http_request_info(http_request *, HashTable *) */
923 PHP_HTTP_API void _http_request_info(http_request *request, HashTable *info)
924 {
925 zval array;
926 INIT_ZARR(array, info);
927
928 HTTP_CURL_INFO(CURLINFO_EFFECTIVE_URL);
929 HTTP_CURL_INFO(CURLINFO_RESPONSE_CODE);
930 HTTP_CURL_INFO_EX(CURLINFO_HTTP_CONNECTCODE, "connect_code");
931 HTTP_CURL_INFO(CURLINFO_FILETIME);
932 HTTP_CURL_INFO(CURLINFO_TOTAL_TIME);
933 HTTP_CURL_INFO(CURLINFO_NAMELOOKUP_TIME);
934 HTTP_CURL_INFO(CURLINFO_CONNECT_TIME);
935 HTTP_CURL_INFO(CURLINFO_PRETRANSFER_TIME);
936 HTTP_CURL_INFO(CURLINFO_STARTTRANSFER_TIME);
937 HTTP_CURL_INFO(CURLINFO_REDIRECT_TIME);
938 HTTP_CURL_INFO(CURLINFO_REDIRECT_COUNT);
939 HTTP_CURL_INFO(CURLINFO_SIZE_UPLOAD);
940 HTTP_CURL_INFO(CURLINFO_SIZE_DOWNLOAD);
941 HTTP_CURL_INFO(CURLINFO_SPEED_DOWNLOAD);
942 HTTP_CURL_INFO(CURLINFO_SPEED_UPLOAD);
943 HTTP_CURL_INFO(CURLINFO_HEADER_SIZE);
944 HTTP_CURL_INFO(CURLINFO_REQUEST_SIZE);
945 HTTP_CURL_INFO(CURLINFO_SSL_VERIFYRESULT);
946 HTTP_CURL_INFO(CURLINFO_SSL_ENGINES);
947 HTTP_CURL_INFO(CURLINFO_CONTENT_LENGTH_DOWNLOAD);
948 HTTP_CURL_INFO(CURLINFO_CONTENT_LENGTH_UPLOAD);
949 HTTP_CURL_INFO(CURLINFO_CONTENT_TYPE);
950 HTTP_CURL_INFO(CURLINFO_HTTPAUTH_AVAIL);
951 HTTP_CURL_INFO(CURLINFO_PROXYAUTH_AVAIL);
952 HTTP_CURL_INFO(CURLINFO_NUM_CONNECTS);
953 #if HTTP_CURL_VERSION(7,14,1)
954 HTTP_CURL_INFO_EX(CURLINFO_COOKIELIST, "cookies");
955 #endif
956 HTTP_CURL_INFO(CURLINFO_OS_ERRNO);
957 add_assoc_string(&array, "error", request->_error, 1);
958 }
959 /* }}} */
960
961 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
962 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ctx)
963 {
964 http_request *request = (http_request *) ctx;
965 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
966
967 if (request->body) {
968 switch (request->body->type)
969 {
970 case HTTP_REQUEST_BODY_CSTRING:
971 {
972 size_t out = MIN(len * n, request->body->size - request->body->priv);
973
974 if (out) {
975 memcpy(data, ((char *) request->body->data) + request->body->priv, out);
976 request->body->priv += out;
977 return out;
978 }
979 }
980 break;
981
982 case HTTP_REQUEST_BODY_UPLOADFILE:
983 return php_stream_read((php_stream *) request->body->data, data, len * n);
984 break;
985 }
986 }
987 return 0;
988 }
989 /* }}} */
990
991 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
992 static int http_curl_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow)
993 {
994 zval *param, retval;
995 http_request *request = (http_request *) ctx;
996 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
997
998 INIT_PZVAL(&retval);
999 ZVAL_NULL(&retval);
1000
1001 MAKE_STD_ZVAL(param);
1002 array_init(param);
1003 add_assoc_double(param, "dltotal", dltotal);
1004 add_assoc_double(param, "dlnow", dlnow);
1005 add_assoc_double(param, "ultotal", ultotal);
1006 add_assoc_double(param, "ulnow", ulnow);
1007
1008 call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, &param TSRMLS_CC);
1009
1010 zval_ptr_dtor(&param);
1011 zval_dtor(&retval);
1012
1013 return 0;
1014 }
1015 /* }}} */
1016
1017 /* {{{ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
1018 static curlioerr http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
1019 {
1020 http_request *request = (http_request *) ctx;
1021 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
1022
1023 if (cmd != CURLIOCMD_RESTARTREAD) {
1024 return CURLIOE_UNKNOWNCMD;
1025 }
1026
1027 if (request->body) {
1028 switch (request->body->type)
1029 {
1030 case HTTP_REQUEST_BODY_CSTRING:
1031 request->body->priv = 0;
1032 return CURLIOE_OK;
1033 break;
1034
1035 case HTTP_REQUEST_BODY_UPLOADFILE:
1036 if (SUCCESS == php_stream_rewind((php_stream *) request->body->data)) {
1037 return CURLIOE_OK;
1038 }
1039 break;
1040 }
1041 }
1042
1043 return CURLIOE_FAILRESTART;
1044 }
1045 /* }}} */
1046
1047 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
1048 static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
1049 {
1050 http_request *request = (http_request *) ctx;
1051
1052 switch (type)
1053 {
1054 case CURLINFO_DATA_IN:
1055 if (request->conv.last_type == CURLINFO_HEADER_IN) {
1056 phpstr_appends(&request->conv.response, HTTP_CRLF);
1057 }
1058 case CURLINFO_HEADER_IN:
1059 phpstr_append(&request->conv.response, data, length);
1060 break;
1061 case CURLINFO_DATA_OUT:
1062 if (request->conv.last_type == CURLINFO_HEADER_OUT) {
1063 phpstr_appends(&request->conv.request, HTTP_CRLF);
1064 }
1065 case CURLINFO_HEADER_OUT:
1066 phpstr_append(&request->conv.request, data, length);
1067 break;
1068 default:
1069 #if 0
1070 fprintf(stderr, "## ", type);
1071 if (!type) {
1072 fprintf(stderr, "%s", data);
1073 } else {
1074 ulong i;
1075 for (i = 1; i <= length; ++i) {
1076 fprintf(stderr, "%02X ", data[i-1] & 0xFF);
1077 if (!(i % 20)) {
1078 fprintf(stderr, "\n## ");
1079 }
1080 }
1081 fprintf(stderr, "\n");
1082 }
1083 if (data[length-1] != 0xa) {
1084 fprintf(stderr, "\n");
1085 }
1086 #endif
1087 break;
1088 }
1089
1090 if (type) {
1091 request->conv.last_type = type;
1092 }
1093 return 0;
1094 }
1095 /* }}} */
1096
1097 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
1098 static inline zval *_http_request_option_ex(http_request *r, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
1099 {
1100 zval **zoption;
1101 #ifdef ZEND_ENGINE_2
1102 ulong h = zend_get_hash_value(key, keylen);
1103 #else
1104 ulong h = 0;
1105 #endif
1106
1107 if (!options ||
1108 #ifdef ZEND_ENGINE_2
1109 (SUCCESS != zend_hash_quick_find(options, key, keylen, h, (void **) &zoption))
1110 #else
1111 (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))
1112 #endif
1113 ) {
1114 return NULL;
1115 }
1116
1117 return http_request_option_cache_ex(r, key, keylen, h, zval_copy(type, *zoption));
1118 }
1119 /* }}} */
1120
1121 /* {{{ static inline zval *http_request_option_cache(http_request *, char *key, zval *) */
1122 static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC)
1123 {
1124 ZVAL_ADDREF(opt);
1125
1126 #ifdef ZEND_ENGINE_2
1127 if (h) {
1128 _zend_hash_quick_add_or_update(&r->_cache.options, key, keylen, h, &opt, sizeof(zval *), NULL,
1129 zend_hash_quick_exists(&r->_cache.options, key, keylen, h)?HASH_UPDATE:HASH_ADD ZEND_FILE_LINE_CC);
1130 }
1131 else
1132 #endif
1133 {
1134 if (zend_hash_exists(&r->_cache.options, key, keylen)) {
1135 zend_hash_update(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL);
1136 } else {
1137 zend_hash_add(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL);
1138 }
1139 }
1140
1141 return opt;
1142 }
1143 /* }}} */
1144
1145 #endif /* HTTP_HAVE_CURL */
1146
1147 /*
1148 * Local variables:
1149 * tab-width: 4
1150 * c-basic-offset: 4
1151 * End:
1152 * vim600: noet sw=4 ts=4 fdm=marker
1153 * vim<600: noet sw=4 ts=4
1154 */
1155