- adjust ini entry names to those of the globals struct
[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_NEED_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_ex(request, keyname, strlen(keyname)+1, 0, 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 = zval_copy(IS_LONG, *param); \
276 HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \
277 key = NULL; \
278 zval_free(&copy); \
279 continue; \
280 }
281 /* }}} */
282
283 /* {{{ forward declarations */
284 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
285 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
286 static inline zval *_http_request_option_ex(http_request *request, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
287 #define http_request_option_cache(r, k, z) _http_request_option_cache_ex((r), (k), sizeof(k), 0, (z) TSRMLS_CC)
288 #define http_request_option_cache_ex(r, k, kl, h, z) _http_request_option_cache_ex((r), (k), (kl), (h), (z) TSRMLS_CC)
289 static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC);
290
291 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
292 static int http_curl_progress_callback(void *, double, double, double, double);
293 static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *);
294 static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { return n*l; }
295 static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *);
296 /* }}} */
297
298 /* {{{ CURL *http_curl_init(http_request *) */
299 PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, http_request *request)
300 {
301 if (ch || (ch = curl_easy_init())) {
302 #if defined(ZTS)
303 HTTP_CURL_OPT_EX(ch, CURLOPT_NOSIGNAL, 1L);
304 #endif
305 HTTP_CURL_OPT_EX(ch, CURLOPT_HEADER, 0L);
306 HTTP_CURL_OPT_EX(ch, CURLOPT_FILETIME, 1L);
307 HTTP_CURL_OPT_EX(ch, CURLOPT_AUTOREFERER, 1L);
308 HTTP_CURL_OPT_EX(ch, CURLOPT_VERBOSE, 1L);
309 HTTP_CURL_OPT_EX(ch, CURLOPT_HEADERFUNCTION, NULL);
310 HTTP_CURL_OPT_EX(ch, CURLOPT_DEBUGFUNCTION, http_curl_raw_callback);
311 HTTP_CURL_OPT_EX(ch, CURLOPT_READFUNCTION, http_curl_read_callback);
312 HTTP_CURL_OPT_EX(ch, CURLOPT_IOCTLFUNCTION, http_curl_ioctl_callback);
313 HTTP_CURL_OPT_EX(ch, CURLOPT_WRITEFUNCTION, http_curl_dummy_callback);
314
315 /* set context */
316 if (request) {
317 HTTP_CURL_OPT_EX(ch, CURLOPT_PRIVATE, request);
318 HTTP_CURL_OPT_EX(ch, CURLOPT_DEBUGDATA, request);
319 HTTP_CURL_OPT_EX(ch, CURLOPT_ERRORBUFFER, request->_error);
320
321 /* attach curl handle */
322 request->ch = ch;
323 /* set defaults (also in http_request_reset()) */
324 http_request_defaults(request);
325 }
326 }
327
328 return ch;
329 }
330 /* }}} */
331
332 /* {{{ void http_curl_free(CURL **) */
333 PHP_HTTP_API void _http_curl_free(CURL **ch)
334 {
335 if (*ch) {
336 /* avoid nasty segfaults with already cleaned up callbacks */
337 HTTP_CURL_OPT_EX(*ch, CURLOPT_NOPROGRESS, 1L);
338 HTTP_CURL_OPT_EX(*ch, CURLOPT_PROGRESSFUNCTION, NULL);
339 HTTP_CURL_OPT_EX(*ch, CURLOPT_VERBOSE, 0L);
340 HTTP_CURL_OPT_EX(*ch, CURLOPT_DEBUGFUNCTION, NULL);
341 curl_easy_cleanup(*ch);
342 *ch = NULL;
343 }
344 }
345 /* }}} */
346
347 /* {{{ http_request *http_request_init(http_request *) */
348 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)
349 {
350 http_request *r;
351
352 if (request) {
353 r = request;
354 } else {
355 r = emalloc_rel(sizeof(http_request));
356 }
357 memset(r, 0, sizeof(http_request));
358
359 r->ch = ch;
360 r->url = (url) ? http_absolute_url(url) : NULL;
361 r->meth = (meth > 0) ? meth : HTTP_GET;
362
363 phpstr_init(&r->conv.request);
364 phpstr_init_ex(&r->conv.response, HTTP_CURLBUF_SIZE, 0);
365 phpstr_init(&r->_cache.cookies);
366 zend_hash_init(&r->_cache.options, 0, NULL, ZVAL_PTR_DTOR, 0);
367
368 TSRMLS_SET_CTX(r->tsrm_ls);
369
370 return r;
371 }
372 /* }}} */
373
374 /* {{{ void http_request_dtor(http_request *) */
375 PHP_HTTP_API void _http_request_dtor(http_request *request)
376 {
377 http_curl_free(&request->ch);
378 http_request_reset(request);
379
380 phpstr_dtor(&request->_cache.cookies);
381 zend_hash_destroy(&request->_cache.options);
382 if (request->_cache.headers) {
383 curl_slist_free_all(request->_cache.headers);
384 request->_cache.headers = NULL;
385 }
386 if (request->_progress_callback) {
387 zval_ptr_dtor(&request->_progress_callback);
388 request->_progress_callback = NULL;
389 }
390 }
391 /* }}} */
392
393 /* {{{ void http_request_free(http_request **) */
394 PHP_HTTP_API void _http_request_free(http_request **request)
395 {
396 if (*request) {
397 TSRMLS_FETCH_FROM_CTX((*request)->tsrm_ls);
398 http_request_body_free(&(*request)->body);
399 http_request_dtor(*request);
400 efree(*request);
401 *request = NULL;
402 }
403 }
404 /* }}} */
405
406 /* {{{ void http_request_reset(http_request *) */
407 PHP_HTTP_API void _http_request_reset(http_request *request)
408 {
409 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
410 STR_SET(request->url, NULL);
411 request->conv.last_type = 0;
412 phpstr_dtor(&request->conv.request);
413 phpstr_dtor(&request->conv.response);
414 http_request_body_dtor(request->body);
415
416 if (request->ch) {
417 http_request_defaults(request);
418 }
419 request->_error[0] = '\0';
420 }
421 /* }}} */
422
423 /* {{{ STATUS http_request_enable_cookies(http_request *) */
424 PHP_HTTP_API STATUS _http_request_enable_cookies(http_request *request)
425 {
426 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
427 if (CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIEFILE, "")) {
428 return SUCCESS;
429 }
430 http_error(HE_WARNING, HTTP_E_REQUEST, "Could not enable cookies for this session");
431 return FAILURE;
432 }
433 /* }}} */
434
435 /* {{{ STATUS http_request_reset_cookies(http_request *, int) */
436 PHP_HTTP_API STATUS _http_request_reset_cookies(http_request *request, int session_only)
437 {
438 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
439
440 if (session_only) {
441 #if HTTP_CURL_VERSION(7,15,4)
442 curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "SESS");
443 return SUCCESS;
444 #else
445 http_error(HE_WARNING, HTTP_E_REQUEST, "Could not reset session cookies (need libcurl >= v7.15.4)");
446 #endif
447 } else {
448 #if HTTP_CURL_VERSION(7,14,1)
449 curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "ALL");
450 return SUCCESS;
451 #else
452 http_error(HE_WARNING, HTTP_E_REQUEST, "Could not reset cookies (need libcurl >= v7.14.1)");
453 #endif
454 }
455 return FAILURE;
456 }
457 /* }}} */
458
459 /* {{{ void http_request_defaults(http_request *) */
460 PHP_HTTP_API void _http_request_defaults(http_request *request)
461 {
462 if (request->ch) {
463 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, NULL);
464 HTTP_CURL_OPT(CURLOPT_URL, NULL);
465 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 1L);
466 HTTP_CURL_OPT(CURLOPT_PROXY, NULL);
467 HTTP_CURL_OPT(CURLOPT_PROXYPORT, 0L);
468 HTTP_CURL_OPT(CURLOPT_PROXYTYPE, 0L);
469 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD, NULL);
470 HTTP_CURL_OPT(CURLOPT_PROXYAUTH, 0L);
471 HTTP_CURL_OPT(CURLOPT_INTERFACE, NULL);
472 HTTP_CURL_OPT(CURLOPT_PORT, 0L);
473 #if HTTP_CURL_VERSION(7,15,2)
474 HTTP_CURL_OPT(CURLOPT_LOCALPORT, 0L);
475 HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, 0L);
476 #endif
477 HTTP_CURL_OPT(CURLOPT_USERPWD, NULL);
478 HTTP_CURL_OPT(CURLOPT_HTTPAUTH, 0L);
479 HTTP_CURL_OPT(CURLOPT_ENCODING, NULL);
480 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION, 0L);
481 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, 0L);
482 HTTP_CURL_OPT(CURLOPT_REFERER, NULL);
483 HTTP_CURL_OPT(CURLOPT_USERAGENT, "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
484 HTTP_CURL_OPT(CURLOPT_HTTPHEADER, NULL);
485 HTTP_CURL_OPT(CURLOPT_COOKIE, NULL);
486 #if HTTP_CURL_VERSION(7,14,1)
487 HTTP_CURL_OPT(CURLOPT_COOKIELIST, NULL);
488 #endif
489 HTTP_CURL_OPT(CURLOPT_RANGE, NULL);
490 HTTP_CURL_OPT(CURLOPT_RESUME_FROM, 0L);
491 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, 0L);
492 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, 0L);
493 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, 0L);
494 HTTP_CURL_OPT(CURLOPT_TIMEOUT, 0L);
495 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT, 3);
496 HTTP_CURL_OPT(CURLOPT_SSLCERT, NULL);
497 HTTP_CURL_OPT(CURLOPT_SSLCERTTYPE, NULL);
498 HTTP_CURL_OPT(CURLOPT_SSLCERTPASSWD, NULL);
499 HTTP_CURL_OPT(CURLOPT_SSLKEY, NULL);
500 HTTP_CURL_OPT(CURLOPT_SSLKEYTYPE, NULL);
501 HTTP_CURL_OPT(CURLOPT_SSLKEYPASSWD, NULL);
502 HTTP_CURL_OPT(CURLOPT_SSLENGINE, NULL);
503 HTTP_CURL_OPT(CURLOPT_SSLVERSION, 0L);
504 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYPEER, 0L);
505 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYHOST, 0L);
506 HTTP_CURL_OPT(CURLOPT_SSL_CIPHER_LIST, NULL);
507 HTTP_CURL_OPT(CURLOPT_CAINFO, NULL);
508 HTTP_CURL_OPT(CURLOPT_CAPATH, NULL);
509 HTTP_CURL_OPT(CURLOPT_RANDOM_FILE, NULL);
510 HTTP_CURL_OPT(CURLOPT_EGDSOCKET, NULL);
511 HTTP_CURL_OPT(CURLOPT_POSTFIELDS, NULL);
512 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, 0L);
513 HTTP_CURL_OPT(CURLOPT_HTTPPOST, NULL);
514 HTTP_CURL_OPT(CURLOPT_IOCTLDATA, NULL);
515 HTTP_CURL_OPT(CURLOPT_READDATA, NULL);
516 HTTP_CURL_OPT(CURLOPT_INFILESIZE, 0L);
517 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
518 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST, NULL);
519 HTTP_CURL_OPT(CURLOPT_NOBODY, 0L);
520 HTTP_CURL_OPT(CURLOPT_POST, 0L);
521 HTTP_CURL_OPT(CURLOPT_UPLOAD, 0L);
522 HTTP_CURL_OPT(CURLOPT_HTTPGET, 1L);
523 }
524 }
525 /* }}} */
526
527 PHP_HTTP_API void _http_request_set_progress_callback(http_request *request, zval *cb)
528 {
529 if (request->_progress_callback) {
530 zval_ptr_dtor(&request->_progress_callback);
531 }
532 if ((request->_progress_callback = cb)) {
533 ZVAL_ADDREF(cb);
534 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 0);
535 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA, request);
536 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, http_curl_progress_callback);
537 } else {
538 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 1);
539 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA, NULL);
540 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, NULL);
541 }
542 }
543
544 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
545 PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *options)
546 {
547 zval *zoption;
548 zend_bool range_req = 0;
549
550 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
551
552 HTTP_CHECK_CURL_INIT(request->ch, http_curl_init(request), return FAILURE);
553
554 /* set options */
555 HTTP_CURL_OPT(CURLOPT_URL, request->url);
556
557 /* progress callback */
558 if ((zoption = http_request_option(request, options, "onprogress", -1))) {
559 http_request_set_progress_callback(request, zoption);
560 }
561
562 /* proxy */
563 if ((zoption = http_request_option(request, options, "proxyhost", IS_STRING))) {
564 if (Z_STRLEN_P(zoption)) {
565 HTTP_CURL_OPT(CURLOPT_PROXY, Z_STRVAL_P(zoption));
566 }
567 /* type */
568 if ((zoption = http_request_option(request, options, "proxytype", IS_LONG))) {
569 HTTP_CURL_OPT(CURLOPT_PROXYTYPE, Z_LVAL_P(zoption));
570 }
571 /* port */
572 if ((zoption = http_request_option(request, options, "proxyport", IS_LONG))) {
573 HTTP_CURL_OPT(CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
574 }
575 /* user:pass */
576 if ((zoption = http_request_option(request, options, "proxyauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
577 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
578 }
579 /* auth method */
580 if ((zoption = http_request_option(request, options, "proxyauthtype", IS_LONG))) {
581 HTTP_CURL_OPT(CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
582 }
583 }
584
585 /* outgoing interface */
586 if ((zoption = http_request_option(request, options, "interface", IS_STRING))) {
587 HTTP_CURL_OPT(CURLOPT_INTERFACE, Z_STRVAL_P(zoption));
588
589 #if HTTP_CURL_VERSION(7,15,2)
590 if ((zoption = http_request_option(request, options, "portrange", IS_ARRAY))) {
591 zval **prs, **pre;
592
593 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
594 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &prs)) {
595 zend_hash_move_forward(Z_ARRVAL_P(zoption));
596 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) {
597 zval *prs_cpy = zval_copy(IS_LONG, *prs), *pre_cpy = zval_copy(IS_LONG, *pre);
598
599 if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) {
600 HTTP_CURL_OPT(CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy)));
601 HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L);
602 }
603 zval_free(&prs_cpy);
604 zval_free(&pre_cpy);
605 }
606 }
607 }
608 #endif
609 }
610
611 /* another port */
612 if ((zoption = http_request_option(request, options, "port", IS_LONG))) {
613 HTTP_CURL_OPT(CURLOPT_PORT, Z_LVAL_P(zoption));
614 }
615
616 /* auth */
617 if ((zoption = http_request_option(request, options, "httpauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
618 HTTP_CURL_OPT(CURLOPT_USERPWD, Z_STRVAL_P(zoption));
619 }
620 if ((zoption = http_request_option(request, options, "httpauthtype", IS_LONG))) {
621 HTTP_CURL_OPT(CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
622 }
623
624 /* redirects, defaults to 0 */
625 if ((zoption = http_request_option(request, options, "redirect", IS_LONG))) {
626 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1L : 0L);
627 HTTP_CURL_OPT(CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
628 if ((zoption = http_request_option(request, options, "unrestrictedauth", IS_BOOL))) {
629 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
630 }
631 }
632
633 /* referer */
634 if ((zoption = http_request_option(request, options, "referer", IS_STRING)) && Z_STRLEN_P(zoption)) {
635 HTTP_CURL_OPT(CURLOPT_REFERER, Z_STRVAL_P(zoption));
636 }
637
638 /* useragent, default "PECL::HTTP/version (PHP/version)" */
639 if ((zoption = http_request_option(request, options, "useragent", IS_STRING))) {
640 /* allow to send no user agent, not even default one */
641 if (Z_STRLEN_P(zoption)) {
642 HTTP_CURL_OPT(CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
643 } else {
644 HTTP_CURL_OPT(CURLOPT_USERAGENT, NULL);
645 }
646 }
647
648 /* resume */
649 if ((zoption = http_request_option(request, options, "resume", IS_LONG)) && (Z_LVAL_P(zoption) > 0)) {
650 range_req = 1;
651 HTTP_CURL_OPT(CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
652 }
653 /* or range of kind array(array(0,499), array(100,1499)) */
654 else if ((zoption = http_request_option(request, options, "range", IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
655 HashPosition pos1, pos2;
656 zval **rr, **rb, **re;
657 phpstr rs;
658
659 phpstr_init(&rs);
660 FOREACH_VAL(pos1, zoption, rr) {
661 if (Z_TYPE_PP(rr) == IS_ARRAY) {
662 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2);
663 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &rb, &pos2)) {
664 zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2);
665 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &re, &pos2)) {
666 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))) &&
667 ((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)))) {
668 zval *rbl = zval_copy(IS_LONG, *rb), *rel = zval_copy(IS_LONG, *re);
669
670 if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) {
671 phpstr_appendf(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel));
672 }
673 zval_free(&rbl);
674 zval_free(&rel);
675 }
676 }
677 }
678 }
679 }
680
681 if (PHPSTR_LEN(&rs)) {
682 zval *cached_range;
683
684 /* ditch last comma */
685 PHPSTR_VAL(&rs)[PHPSTR_LEN(&rs)-- -1] = '\0';
686 /* cache string */
687 MAKE_STD_ZVAL(cached_range);
688 ZVAL_STRINGL(cached_range, PHPSTR_VAL(&rs), PHPSTR_LEN(&rs), 0);
689 HTTP_CURL_OPT(CURLOPT_RANGE, Z_STRVAL_P(http_request_option_cache(request, "range", cached_range)));
690 zval_ptr_dtor(&cached_range);
691 }
692 }
693
694 /* additional headers, array('name' => 'value') */
695 if (request->_cache.headers) {
696 curl_slist_free_all(request->_cache.headers);
697 request->_cache.headers = NULL;
698 }
699 if ((zoption = http_request_option(request, options, "headers", IS_ARRAY))) {
700 char *header_key;
701 ulong header_idx;
702 HashPosition pos;
703
704 FOREACH_KEY(pos, zoption, header_key, header_idx) {
705 if (header_key) {
706 zval **header_val;
707 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void *) &header_val, &pos)) {
708 char header[1024] = {0};
709
710 ZVAL_ADDREF(*header_val);
711 convert_to_string_ex(header_val);
712 if (!strcasecmp(header_key, "range")) {
713 range_req = 1;
714 }
715 snprintf(header, lenof(header), "%s: %s", header_key, Z_STRVAL_PP(header_val));
716 request->_cache.headers = curl_slist_append(request->_cache.headers, header);
717 zval_ptr_dtor(header_val);
718 }
719
720 /* reset */
721 header_key = NULL;
722 }
723 }
724 }
725 /* etag */
726 if ((zoption = http_request_option(request, options, "etag", IS_STRING)) && Z_STRLEN_P(zoption)) {
727 char match_header[1024] = {0}, *quoted_etag = NULL;
728
729 if ((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"')) {
730 spprintf(&quoted_etag, 0, "\"%s\"", Z_STRVAL_P(zoption));
731 }
732 snprintf(match_header, lenof(match_header), "%s: %s", range_req?"If-Match":"If-None-Match", quoted_etag?quoted_etag:Z_STRVAL_P(zoption));
733 request->_cache.headers = curl_slist_append(request->_cache.headers, match_header);
734 STR_FREE(quoted_etag);
735 }
736 /* compression */
737 if ((zoption = http_request_option(request, options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
738 request->_cache.headers = curl_slist_append(request->_cache.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
739 }
740 HTTP_CURL_OPT(CURLOPT_HTTPHEADER, request->_cache.headers);
741
742 /* lastmodified */
743 if ((zoption = http_request_option(request, options, "lastmodified", IS_LONG))) {
744 if (Z_LVAL_P(zoption)) {
745 if (Z_LVAL_P(zoption) > 0) {
746 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
747 } else {
748 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, (long) HTTP_GET_REQUEST_TIME() + Z_LVAL_P(zoption));
749 }
750 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, (long) (range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE));
751 } else {
752 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
753 }
754 }
755
756 /* cookies, array('name' => 'value') */
757 if ((zoption = http_request_option(request, options, "cookies", IS_ARRAY))) {
758 phpstr_dtor(&request->_cache.cookies);
759 if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
760 zval *urlenc_cookies = NULL;
761 /* check whether cookies should not be urlencoded; default is to urlencode them */
762 if ((!(urlenc_cookies = http_request_option(request, options, "encodecookies", IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) {
763 if (SUCCESS == http_urlencode_hash_recursive(HASH_OF(zoption), &request->_cache.cookies, "; ", lenof("; "), NULL, 0)) {
764 phpstr_fix(&request->_cache.cookies);
765 HTTP_CURL_OPT(CURLOPT_COOKIE, request->_cache.cookies.data);
766 }
767 } else {
768 HashPosition pos;
769 char *cookie_key = NULL;
770 ulong cookie_idx;
771
772 FOREACH_KEY(pos, zoption, cookie_key, cookie_idx) {
773 if (cookie_key) {
774 zval **cookie_val;
775 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void *) &cookie_val, &pos)) {
776 zval *val = zval_copy(IS_STRING, *cookie_val);
777 phpstr_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key, Z_STRVAL_P(val));
778 zval_free(&val);
779 }
780
781 /* reset */
782 cookie_key = NULL;
783 }
784 }
785
786 phpstr_fix(&request->_cache.cookies);
787 if (PHPSTR_LEN(&request->_cache.cookies)) {
788 HTTP_CURL_OPT(CURLOPT_COOKIE, PHPSTR_VAL(&request->_cache.cookies));
789 }
790 }
791 }
792 }
793
794 /* don't load session cookies from cookiestore */
795 if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL)) && Z_BVAL_P(zoption)) {
796 HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 1L);
797 }
798
799 /* cookiestore, read initial cookies from that file and store cookies back into that file */
800 if ((zoption = http_request_option(request, options, "cookiestore", IS_STRING))) {
801 if (Z_STRLEN_P(zoption)) {
802 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE);
803 }
804 HTTP_CURL_OPT(CURLOPT_COOKIEFILE, Z_STRVAL_P(zoption));
805 HTTP_CURL_OPT(CURLOPT_COOKIEJAR, Z_STRVAL_P(zoption));
806 }
807
808 /* maxfilesize */
809 if ((zoption = http_request_option(request, options, "maxfilesize", IS_LONG))) {
810 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
811 }
812
813 /* http protocol */
814 if ((zoption = http_request_option(request, options, "protocol", IS_LONG))) {
815 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption));
816 }
817
818 /* timeout, defaults to 0 */
819 if ((zoption = http_request_option(request, options, "timeout", IS_LONG))) {
820 HTTP_CURL_OPT(CURLOPT_TIMEOUT, Z_LVAL_P(zoption));
821 }
822
823 /* connecttimeout, defaults to 0 */
824 if ((zoption = http_request_option(request, options, "connecttimeout", IS_LONG))) {
825 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT, Z_LVAL_P(zoption));
826 }
827
828 /* ssl */
829 if ((zoption = http_request_option(request, options, "ssl", IS_ARRAY))) {
830 ulong idx;
831 char *key = NULL;
832 zval **param;
833 HashPosition pos;
834
835 FOREACH_KEYVAL(pos, zoption, key, idx, param) {
836 if (key) {
837 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1);
838 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0);
839 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0);
840
841 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0);
842 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0);
843 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0);
844
845 HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0);
846 HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0);
847
848 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1);
849 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1);
850 HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0);
851
852 HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1);
853 HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1);
854 HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1);
855 HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1);
856
857 /* reset key */
858 key = NULL;
859 }
860 }
861 }
862
863 /* request method */
864 switch (request->meth) {
865 case HTTP_GET:
866 HTTP_CURL_OPT(CURLOPT_HTTPGET, 1L);
867 break;
868
869 case HTTP_HEAD:
870 HTTP_CURL_OPT(CURLOPT_NOBODY, 1L);
871 break;
872
873 case HTTP_POST:
874 HTTP_CURL_OPT(CURLOPT_POST, 1L);
875 break;
876
877 case HTTP_PUT:
878 HTTP_CURL_OPT(CURLOPT_UPLOAD, 1L);
879 break;
880
881 default:
882 if (http_request_method_exists(0, request->meth, NULL)) {
883 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST, http_request_method_name(request->meth));
884 } else {
885 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", request->meth, request->url);
886 return FAILURE;
887 }
888 break;
889 }
890
891 /* attach request body */
892 if (request->body && (request->meth != HTTP_GET) && (request->meth != HTTP_HEAD) && (request->meth != HTTP_OPTIONS)) {
893 switch (request->body->type) {
894 case HTTP_REQUEST_BODY_EMPTY:
895 /* nothing */
896 break;
897
898 case HTTP_REQUEST_BODY_CURLPOST:
899 HTTP_CURL_OPT(CURLOPT_HTTPPOST, (struct curl_httppost *) request->body->data);
900 break;
901
902 case HTTP_REQUEST_BODY_CSTRING:
903 if (request->meth != HTTP_PUT) {
904 HTTP_CURL_OPT(CURLOPT_POSTFIELDS, request->body->data);
905 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, request->body->size);
906 break;
907 }
908 /* fallthrough, PUT/UPLOAD _needs_ READDATA */
909 case HTTP_REQUEST_BODY_UPLOADFILE:
910 HTTP_CURL_OPT(CURLOPT_IOCTLDATA, request);
911 HTTP_CURL_OPT(CURLOPT_READDATA, request);
912 HTTP_CURL_OPT(CURLOPT_INFILESIZE, request->body->size);
913 break;
914
915 default:
916 /* shouldn't ever happen */
917 http_error_ex(HE_ERROR, 0, "Unknown request body type: %d (%s)", request->body->type, request->url);
918 return FAILURE;
919 }
920 }
921
922 return SUCCESS;
923 }
924 /* }}} */
925
926 /* {{{ void http_request_exec(http_request *) */
927 PHP_HTTP_API void _http_request_exec(http_request *request)
928 {
929 CURLcode result;
930 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
931
932 if (CURLE_OK != (result = curl_easy_perform(request->ch))) {
933 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), request->_error, request->url);
934 }
935 }
936 /* }}} */
937
938 /* {{{ void http_request_info(http_request *, HashTable *) */
939 PHP_HTTP_API void _http_request_info(http_request *request, HashTable *info)
940 {
941 zval array;
942 INIT_ZARR(array, info);
943
944 HTTP_CURL_INFO(CURLINFO_EFFECTIVE_URL);
945 HTTP_CURL_INFO(CURLINFO_RESPONSE_CODE);
946 HTTP_CURL_INFO_EX(CURLINFO_HTTP_CONNECTCODE, "connect_code");
947 HTTP_CURL_INFO(CURLINFO_FILETIME);
948 HTTP_CURL_INFO(CURLINFO_TOTAL_TIME);
949 HTTP_CURL_INFO(CURLINFO_NAMELOOKUP_TIME);
950 HTTP_CURL_INFO(CURLINFO_CONNECT_TIME);
951 HTTP_CURL_INFO(CURLINFO_PRETRANSFER_TIME);
952 HTTP_CURL_INFO(CURLINFO_STARTTRANSFER_TIME);
953 HTTP_CURL_INFO(CURLINFO_REDIRECT_TIME);
954 HTTP_CURL_INFO(CURLINFO_REDIRECT_COUNT);
955 HTTP_CURL_INFO(CURLINFO_SIZE_UPLOAD);
956 HTTP_CURL_INFO(CURLINFO_SIZE_DOWNLOAD);
957 HTTP_CURL_INFO(CURLINFO_SPEED_DOWNLOAD);
958 HTTP_CURL_INFO(CURLINFO_SPEED_UPLOAD);
959 HTTP_CURL_INFO(CURLINFO_HEADER_SIZE);
960 HTTP_CURL_INFO(CURLINFO_REQUEST_SIZE);
961 HTTP_CURL_INFO(CURLINFO_SSL_VERIFYRESULT);
962 HTTP_CURL_INFO(CURLINFO_SSL_ENGINES);
963 HTTP_CURL_INFO(CURLINFO_CONTENT_LENGTH_DOWNLOAD);
964 HTTP_CURL_INFO(CURLINFO_CONTENT_LENGTH_UPLOAD);
965 HTTP_CURL_INFO(CURLINFO_CONTENT_TYPE);
966 HTTP_CURL_INFO(CURLINFO_HTTPAUTH_AVAIL);
967 HTTP_CURL_INFO(CURLINFO_PROXYAUTH_AVAIL);
968 HTTP_CURL_INFO(CURLINFO_NUM_CONNECTS);
969 #if HTTP_CURL_VERSION(7,14,1)
970 HTTP_CURL_INFO_EX(CURLINFO_COOKIELIST, "cookies");
971 #endif
972 HTTP_CURL_INFO(CURLINFO_OS_ERRNO);
973 add_assoc_string(&array, "error", request->_error, 1);
974 }
975 /* }}} */
976
977 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
978 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ctx)
979 {
980 http_request *request = (http_request *) ctx;
981 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
982
983 if (request->body) {
984 switch (request->body->type) {
985 case HTTP_REQUEST_BODY_CSTRING:
986 {
987 size_t out = MIN(len * n, request->body->size - request->body->priv);
988
989 if (out) {
990 memcpy(data, ((char *) request->body->data) + request->body->priv, out);
991 request->body->priv += out;
992 return out;
993 }
994 break;
995 }
996
997 case HTTP_REQUEST_BODY_UPLOADFILE:
998 return php_stream_read((php_stream *) request->body->data, data, len * n);
999 }
1000 }
1001 return 0;
1002 }
1003 /* }}} */
1004
1005 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
1006 static int http_curl_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow)
1007 {
1008 zval *param, retval;
1009 http_request *request = (http_request *) ctx;
1010 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
1011
1012 INIT_PZVAL(&retval);
1013 ZVAL_NULL(&retval);
1014
1015 MAKE_STD_ZVAL(param);
1016 array_init(param);
1017 add_assoc_double(param, "dltotal", dltotal);
1018 add_assoc_double(param, "dlnow", dlnow);
1019 add_assoc_double(param, "ultotal", ultotal);
1020 add_assoc_double(param, "ulnow", ulnow);
1021
1022 with_error_handling(EH_NORMAL, NULL) {
1023 call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, &param TSRMLS_CC);
1024 } end_error_handling();
1025
1026 zval_ptr_dtor(&param);
1027 zval_dtor(&retval);
1028
1029 return 0;
1030 }
1031 /* }}} */
1032
1033 /* {{{ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
1034 static curlioerr http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
1035 {
1036 http_request *request = (http_request *) ctx;
1037 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
1038
1039 if (cmd != CURLIOCMD_RESTARTREAD) {
1040 return CURLIOE_UNKNOWNCMD;
1041 }
1042
1043 if (request->body) {
1044 switch (request->body->type) {
1045 case HTTP_REQUEST_BODY_CSTRING:
1046 request->body->priv = 0;
1047 return CURLIOE_OK;
1048 break;
1049
1050 case HTTP_REQUEST_BODY_UPLOADFILE:
1051 if (SUCCESS == php_stream_rewind((php_stream *) request->body->data)) {
1052 return CURLIOE_OK;
1053 }
1054 break;
1055 }
1056 }
1057
1058 return CURLIOE_FAILRESTART;
1059 }
1060 /* }}} */
1061
1062 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
1063 static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
1064 {
1065 http_request *request = (http_request *) ctx;
1066
1067 switch (type) {
1068 case CURLINFO_DATA_IN:
1069 if (request->conv.last_type == CURLINFO_HEADER_IN) {
1070 phpstr_appends(&request->conv.response, HTTP_CRLF);
1071 }
1072 case CURLINFO_HEADER_IN:
1073 phpstr_append(&request->conv.response, data, length);
1074 break;
1075 case CURLINFO_DATA_OUT:
1076 if (request->conv.last_type == CURLINFO_HEADER_OUT) {
1077 phpstr_appends(&request->conv.request, HTTP_CRLF);
1078 }
1079 case CURLINFO_HEADER_OUT:
1080 phpstr_append(&request->conv.request, data, length);
1081 break;
1082 default:
1083 #if 0
1084 fprintf(stderr, "## ", type);
1085 if (!type) {
1086 fprintf(stderr, "%s", data);
1087 } else {
1088 ulong i;
1089 for (i = 1; i <= length; ++i) {
1090 fprintf(stderr, "%02X ", data[i-1] & 0xFF);
1091 if (!(i % 20)) {
1092 fprintf(stderr, "\n## ");
1093 }
1094 }
1095 fprintf(stderr, "\n");
1096 }
1097 if (data[length-1] != 0xa) {
1098 fprintf(stderr, "\n");
1099 }
1100 #endif
1101 break;
1102 }
1103
1104 if (type) {
1105 request->conv.last_type = type;
1106 }
1107 return 0;
1108 }
1109 /* }}} */
1110
1111 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
1112 static inline zval *_http_request_option_ex(http_request *r, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
1113 {
1114 zval **zoption;
1115 #ifdef ZEND_ENGINE_2
1116 ulong h = zend_get_hash_value(key, keylen);
1117 #else
1118 ulong h = 0;
1119 #endif
1120
1121 if (!options ||
1122 #ifdef ZEND_ENGINE_2
1123 (SUCCESS != zend_hash_quick_find(options, key, keylen, h, (void *) &zoption))
1124 #else
1125 (SUCCESS != zend_hash_find(options, key, keylen, (void *) &zoption))
1126 #endif
1127 ) {
1128 return NULL;
1129 }
1130
1131 return http_request_option_cache_ex(r, key, keylen, h, zval_copy(type, *zoption));
1132 }
1133 /* }}} */
1134
1135 /* {{{ static inline zval *http_request_option_cache(http_request *, char *key, zval *) */
1136 static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC)
1137 {
1138 ZVAL_ADDREF(opt);
1139
1140 #ifdef ZEND_ENGINE_2
1141 if (h) {
1142 _zend_hash_quick_add_or_update(&r->_cache.options, key, keylen, h, &opt, sizeof(zval *), NULL,
1143 zend_hash_quick_exists(&r->_cache.options, key, keylen, h)?HASH_UPDATE:HASH_ADD ZEND_FILE_LINE_CC);
1144 }
1145 else
1146 #endif
1147 {
1148 if (zend_hash_exists(&r->_cache.options, key, keylen)) {
1149 zend_hash_update(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL);
1150 } else {
1151 zend_hash_add(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL);
1152 }
1153 }
1154
1155 return opt;
1156 }
1157 /* }}} */
1158
1159 #endif /* HTTP_HAVE_CURL */
1160
1161 /*
1162 * Local variables:
1163 * tab-width: 4
1164 * c-basic-offset: 4
1165 * End:
1166 * vim600: noet sw=4 ts=4 fdm=marker
1167 * vim<600: noet sw=4 ts=4
1168 */
1169