2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
21 #include "php_http_api.h"
22 #include "php_http_request_api.h"
23 #include "php_http_url_api.h"
26 # include "php_http_request_object.h"
29 /* {{{ cruft for thread safe SSL crypto locks */
30 #if defined(ZTS) && defined(HTTP_HAVE_SSL)
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>
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"
45 # elif defined(HTTP_HAVE_GNUTLS)
46 # if defined(HAVE_GCRYPT_H)
47 # define HTTP_NEED_GNUTLS_TSL
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"
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 */
64 #ifdef HTTP_NEED_OPENSSL_TSL
65 static MUTEX_T
*http_openssl_tsl
= NULL
;
67 static void http_openssl_thread_lock(int mode
, int n
, const char * file
, int line
)
69 if (mode
& CRYPTO_LOCK
) {
70 tsrm_mutex_lock(http_openssl_tsl
[n
]);
72 tsrm_mutex_unlock(http_openssl_tsl
[n
]);
76 static ulong
http_openssl_thread_id(void)
78 return (ulong
) tsrm_thread_id();
81 #ifdef HTTP_NEED_GNUTLS_TSL
82 static int http_gnutls_mutex_create(void **m
)
84 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
91 static int http_gnutls_mutex_destroy(void **m
)
93 tsrm_mutex_free(*((MUTEX_T
*) m
));
97 static int http_gnutls_mutex_lock(void **m
)
99 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
102 static int http_gnutls_mutex_unlock(void **m
)
104 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
107 static struct gcry_thread_cbs http_gnutls_tsl
= {
108 GCRY_THREAD_OPTION_USER
,
110 http_gnutls_mutex_create
,
111 http_gnutls_mutex_destroy
,
112 http_gnutls_mutex_lock
,
113 http_gnutls_mutex_unlock
119 PHP_MINIT_FUNCTION(http_request
)
121 #ifdef HTTP_NEED_OPENSSL_TSL
122 int i
, c
= CRYPTO_num_locks();
124 http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
126 for (i
= 0; i
< c
; ++i
) {
127 http_openssl_tsl
[i
] = tsrm_mutex_alloc();
130 CRYPTO_set_id_callback(http_openssl_thread_id
);
131 CRYPTO_set_locking_callback(http_openssl_thread_lock
);
133 #ifdef HTTP_NED_GNUTLS_TSL
134 gcry_control(GCRYCTL_SET_THREAD_CBS
, &http_gnutls_tsl
);
137 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
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
);
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
);
155 PHP_MSHUTDOWN_FUNCTION(http_request
)
157 #ifdef HTTP_NEED_OPENSSL_TSL
158 CRYPTO_set_id_callback(http_openssl_thread_id
);
159 CRYPTO_set_locking_callback(http_openssl_thread_lock
);
161 curl_global_cleanup();
162 #ifdef HTTP_NEED_OPENSSL_TSL
163 if (http_openssl_tsl
) {
164 int i
, c
= CRYPTO_num_locks();
166 CRYPTO_set_id_callback(NULL
);
167 CRYPTO_set_locking_callback(NULL
);
169 for (i
= 0; i
< c
; ++i
) {
170 tsrm_mutex_free(http_openssl_tsl
[i
]);
173 free(http_openssl_tsl
);
174 http_openssl_tsl
= NULL
;
182 #ifndef HAVE_CURL_EASY_STRERROR
183 # define curl_easy_strerror(dummy) "unkown error"
186 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
187 #define HTTP_CURL_INFO_EX(I, X) \
188 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
190 case CURLINFO_STRING: \
193 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &c)) { \
194 char *key = estrndup(#X, lenof(#X)); \
195 add_assoc_string(&array, pretty_key(key, lenof(#X), 0, 0), c ? c : "", 1); \
201 case CURLINFO_DOUBLE: \
204 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &d)) { \
205 char *key = estrndup(#X, lenof(#X)); \
206 add_assoc_double(&array, pretty_key(key, lenof(#X), 0, 0), d); \
212 case CURLINFO_LONG: \
215 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
216 char *key = estrndup(#X, lenof(#X)); \
217 add_assoc_long(&array, pretty_key(key, lenof(#X), 0, 0), l); \
223 case CURLINFO_SLIST: \
225 struct curl_slist *l, *p; \
226 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
228 char *key = estrndup(#X, lenof(#X)); \
229 MAKE_STD_ZVAL(subarray); \
230 array_init(subarray); \
231 for (p = l; p; p = p->next) { \
232 add_next_index_string(subarray, p->data, 1); \
234 add_assoc_zval(&array, pretty_key(key, lenof(#X), 0, 0), subarray); \
235 curl_slist_free_all(l); \
241 #define HTTP_CURL_OPT(OPTION, p) HTTP_CURL_OPT_EX(request->ch, OPTION, (p))
242 #define HTTP_CURL_OPT_EX(ch, OPTION, p) curl_easy_setopt((ch), CURLOPT_##OPTION, (p))
243 #define HTTP_CURL_OPT_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, keyname, obdc)
244 #define HTTP_CURL_OPT_SSL_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname, obdc)
245 #define HTTP_CURL_OPT_SSL_STRING_(keyname,obdc ) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname, obdc)
246 #define HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
247 if (!strcasecmp(key, #keyname)) { \
248 zval *copy = http_request_option_cache(request, #keyname, zval_copy(IS_STRING, *param)); \
250 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(copy), return FAILURE); \
252 HTTP_CURL_OPT(optname, Z_STRVAL_P(copy)); \
256 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
257 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
258 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
259 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
260 if (!strcasecmp(key, #keyname)) { \
261 zval *copy = http_request_option_cache(request, #keyname, zval_copy(IS_LONG, *param)); \
262 HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \
268 /* {{{ forward declarations */
269 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
270 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
271 static inline zval
*_http_request_option_ex(http_request
*request
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
272 #define http_request_option_cache(r, k, z) _http_request_option_cache_ex((r), (k), sizeof(k), 0, (z) TSRMLS_CC)
273 #define http_request_option_cache_ex(r, k, kl, h, z) _http_request_option_cache_ex((r), (k), (kl), (h), (z) TSRMLS_CC)
274 static inline zval
*_http_request_option_cache_ex(http_request
*r
, char *key
, size_t keylen
, ulong h
, zval
*opt TSRMLS_DC
);
276 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
277 static int http_curl_progress_callback(void *, double, double, double, double);
278 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
279 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
280 static curlioerr
http_curl_ioctl_callback(CURL
*, curliocmd
, void *);
283 /* {{{ CURL *http_curl_init(http_request *) */
284 PHP_HTTP_API CURL
* _http_curl_init_ex(CURL
*ch
, http_request
*request
)
286 if (ch
|| (ch
= curl_easy_init())) {
288 HTTP_CURL_OPT_EX(ch
, NOSIGNAL
, 1);
290 HTTP_CURL_OPT_EX(ch
, HEADER
, 0);
291 HTTP_CURL_OPT_EX(ch
, FILETIME
, 1);
292 HTTP_CURL_OPT_EX(ch
, AUTOREFERER
, 1);
293 HTTP_CURL_OPT_EX(ch
, VERBOSE
, 1);
294 HTTP_CURL_OPT_EX(ch
, HEADERFUNCTION
, NULL
);
295 HTTP_CURL_OPT_EX(ch
, DEBUGFUNCTION
, http_curl_raw_callback
);
296 HTTP_CURL_OPT_EX(ch
, READFUNCTION
, http_curl_read_callback
);
297 HTTP_CURL_OPT_EX(ch
, IOCTLFUNCTION
, http_curl_ioctl_callback
);
298 HTTP_CURL_OPT_EX(ch
, WRITEFUNCTION
, http_curl_dummy_callback
);
302 HTTP_CURL_OPT_EX(ch
, PRIVATE
, request
);
303 HTTP_CURL_OPT_EX(ch
, DEBUGDATA
, request
);
304 HTTP_CURL_OPT_EX(ch
, ERRORBUFFER
, request
->_error
);
306 /* attach curl handle */
308 /* set defaults (also in http_request_reset()) */
309 http_request_defaults(request
);
317 /* {{{ void http_curl_free(CURL **) */
318 PHP_HTTP_API
void _http_curl_free(CURL
**ch
)
321 /* avoid nasty segfaults with already cleaned up callbacks */
322 HTTP_CURL_OPT_EX(*ch
, NOPROGRESS
, 1);
323 HTTP_CURL_OPT_EX(*ch
, PROGRESSFUNCTION
, NULL
);
324 HTTP_CURL_OPT_EX(*ch
, VERBOSE
, 0);
325 HTTP_CURL_OPT_EX(*ch
, DEBUGFUNCTION
, NULL
);
326 curl_easy_cleanup(*ch
);
332 /* {{{ http_request *http_request_init(http_request *) */
333 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
)
340 r
= emalloc_rel(sizeof(http_request
));
342 memset(r
, 0, sizeof(http_request
));
345 r
->url
= (url
) ? http_absolute_url(url
) : NULL
;
346 r
->meth
= (meth
> 0) ? meth
: HTTP_GET
;
348 phpstr_init(&r
->conv
.request
);
349 phpstr_init_ex(&r
->conv
.response
, HTTP_CURLBUF_SIZE
, 0);
350 phpstr_init(&r
->_cache
.cookies
);
351 zend_hash_init(&r
->_cache
.options
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
353 TSRMLS_SET_CTX(r
->tsrm_ls
);
359 /* {{{ void http_request_dtor(http_request *) */
360 PHP_HTTP_API
void _http_request_dtor(http_request
*request
)
362 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
364 http_curl_free(&request
->ch
);
365 http_request_reset(request
);
367 phpstr_dtor(&request
->_cache
.cookies
);
368 zend_hash_destroy(&request
->_cache
.options
);
369 if (request
->_cache
.headers
) {
370 curl_slist_free_all(request
->_cache
.headers
);
371 request
->_cache
.headers
= NULL
;
373 if (request
->_progress_callback
) {
374 zval_ptr_dtor(&request
->_progress_callback
);
375 request
->_progress_callback
= NULL
;
380 /* {{{ void http_request_free(http_request **) */
381 PHP_HTTP_API
void _http_request_free(http_request
**request
)
384 TSRMLS_FETCH_FROM_CTX((*request
)->tsrm_ls
);
385 http_request_body_free(&(*request
)->body
);
386 http_request_dtor(*request
);
393 /* {{{ void http_request_reset(http_request *) */
394 PHP_HTTP_API
void _http_request_reset(http_request
*request
)
396 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
397 STR_SET(request
->url
, NULL
);
398 request
->conv
.last_type
= 0;
399 phpstr_dtor(&request
->conv
.request
);
400 phpstr_dtor(&request
->conv
.response
);
401 http_request_body_dtor(request
->body
);
404 http_request_defaults(request
);
409 /* {{{ void http_request_defaults(http_request *) */
410 PHP_HTTP_API
void _http_request_defaults(http_request
*request
)
413 HTTP_CURL_OPT(PROGRESSFUNCTION
, NULL
);
414 HTTP_CURL_OPT(URL
, NULL
);
415 HTTP_CURL_OPT(NOPROGRESS
, 1);
416 HTTP_CURL_OPT(PROXY
, NULL
);
417 HTTP_CURL_OPT(PROXYPORT
, 0);
418 HTTP_CURL_OPT(PROXYUSERPWD
, NULL
);
419 HTTP_CURL_OPT(PROXYAUTH
, 0);
420 HTTP_CURL_OPT(INTERFACE
, NULL
);
421 HTTP_CURL_OPT(PORT
, 0);
422 HTTP_CURL_OPT(USERPWD
, NULL
);
423 HTTP_CURL_OPT(HTTPAUTH
, 0);
424 HTTP_CURL_OPT(ENCODING
, NULL
);
425 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
426 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, 0);
427 HTTP_CURL_OPT(REFERER
, NULL
);
428 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
429 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
430 HTTP_CURL_OPT(COOKIE
, NULL
);
431 #if LIBCURL_VERSION_NUM >= 0x070e01
432 HTTP_CURL_OPT(COOKIELIST
, NULL
);
434 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
435 HTTP_CURL_OPT(COOKIEJAR
, NULL
);
436 HTTP_CURL_OPT(RESUME_FROM
, 0);
437 HTTP_CURL_OPT(MAXFILESIZE
, 0);
438 HTTP_CURL_OPT(TIMECONDITION
, 0);
439 HTTP_CURL_OPT(TIMEVALUE
, 0);
440 HTTP_CURL_OPT(TIMEOUT
, 0);
441 HTTP_CURL_OPT(CONNECTTIMEOUT
, 3);
442 HTTP_CURL_OPT(SSLCERT
, NULL
);
443 HTTP_CURL_OPT(SSLCERTTYPE
, NULL
);
444 HTTP_CURL_OPT(SSLCERTPASSWD
, NULL
);
445 HTTP_CURL_OPT(SSLKEY
, NULL
);
446 HTTP_CURL_OPT(SSLKEYTYPE
, NULL
);
447 HTTP_CURL_OPT(SSLKEYPASSWD
, NULL
);
448 HTTP_CURL_OPT(SSLENGINE
, NULL
);
449 HTTP_CURL_OPT(SSLVERSION
, 0);
450 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
451 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
452 HTTP_CURL_OPT(SSL_CIPHER_LIST
, NULL
);
453 HTTP_CURL_OPT(CAINFO
, NULL
);
454 HTTP_CURL_OPT(CAPATH
, NULL
);
455 HTTP_CURL_OPT(RANDOM_FILE
, NULL
);
456 HTTP_CURL_OPT(EGDSOCKET
, NULL
);
457 HTTP_CURL_OPT(POSTFIELDS
, NULL
);
458 HTTP_CURL_OPT(POSTFIELDSIZE
, 0);
459 HTTP_CURL_OPT(HTTPPOST
, NULL
);
460 HTTP_CURL_OPT(IOCTLDATA
, NULL
);
461 HTTP_CURL_OPT(READDATA
, NULL
);
462 HTTP_CURL_OPT(INFILESIZE
, 0);
463 HTTP_CURL_OPT(HTTP_VERSION
, CURL_HTTP_VERSION_NONE
);
464 HTTP_CURL_OPT(CUSTOMREQUEST
, NULL
);
465 HTTP_CURL_OPT(NOBODY
, 0);
466 HTTP_CURL_OPT(POST
, 0);
467 HTTP_CURL_OPT(UPLOAD
, 0);
468 HTTP_CURL_OPT(HTTPGET
, 1);
473 PHP_HTTP_API
void _http_request_set_progress_callback(http_request
*request
, zval
*cb
)
475 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
477 if (request
->_progress_callback
) {
478 zval_ptr_dtor(&request
->_progress_callback
);
480 if ((request
->_progress_callback
= cb
)) {
482 HTTP_CURL_OPT(NOPROGRESS
, 0);
483 HTTP_CURL_OPT(PROGRESSDATA
, request
);
484 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
486 HTTP_CURL_OPT(NOPROGRESS
, 1);
487 HTTP_CURL_OPT(PROGRESSDATA
, NULL
);
488 HTTP_CURL_OPT(PROGRESSFUNCTION
, NULL
);
492 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
493 PHP_HTTP_API STATUS
_http_request_prepare(http_request
*request
, HashTable
*options
)
496 zend_bool range_req
= 0;
498 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
500 HTTP_CHECK_CURL_INIT(request
->ch
, http_curl_init(request
), return FAILURE
);
503 HTTP_CURL_OPT(URL
, request
->url
);
505 /* progress callback */
506 if ((zoption
= http_request_option(request
, options
, "onprogress", -1))) {
507 http_request_set_progress_callback(request
, zoption
);
511 if ((zoption
= http_request_option(request
, options
, "proxyhost", IS_STRING
))) {
512 if (Z_STRLEN_P(zoption
)) {
513 HTTP_CURL_OPT(PROXY
, Z_STRVAL_P(zoption
));
517 if ((zoption
= http_request_option(request
, options
, "proxyport", IS_LONG
))) {
518 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
521 if ((zoption
= http_request_option(request
, options
, "proxyauth", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
522 HTTP_CURL_OPT(PROXYUSERPWD
, Z_STRVAL_P(zoption
));
525 if ((zoption
= http_request_option(request
, options
, "proxyauthtype", IS_LONG
))) {
526 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
530 /* outgoing interface */
531 if ((zoption
= http_request_option(request
, options
, "interface", IS_STRING
))) {
532 HTTP_CURL_OPT(INTERFACE
, Z_STRVAL_P(zoption
));
536 if ((zoption
= http_request_option(request
, options
, "port", IS_LONG
))) {
537 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
541 if ((zoption
= http_request_option(request
, options
, "httpauth", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
542 HTTP_CURL_OPT(USERPWD
, Z_STRVAL_P(zoption
));
544 if ((zoption
= http_request_option(request
, options
, "httpauthtype", IS_LONG
))) {
545 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
548 /* redirects, defaults to 0 */
549 if ((zoption
= http_request_option(request
, options
, "redirect", IS_LONG
))) {
550 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
551 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
552 if ((zoption
= http_request_option(request
, options
, "unrestrictedauth", IS_BOOL
))) {
553 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
558 if ((zoption
= http_request_option(request
, options
, "referer", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
559 HTTP_CURL_OPT(REFERER
, Z_STRVAL_P(zoption
));
562 /* useragent, default "PECL::HTTP/version (PHP/version)" */
563 if ((zoption
= http_request_option(request
, options
, "useragent", IS_STRING
))) {
564 /* allow to send no user agent, not even default one */
565 if (Z_STRLEN_P(zoption
)) {
566 HTTP_CURL_OPT(USERAGENT
, Z_STRVAL_P(zoption
));
568 HTTP_CURL_OPT(USERAGENT
, NULL
);
572 /* additional headers, array('name' => 'value') */
573 if (request
->_cache
.headers
) {
574 curl_slist_free_all(request
->_cache
.headers
);
575 request
->_cache
.headers
= NULL
;
577 if ((zoption
= http_request_option(request
, options
, "headers", IS_ARRAY
))) {
582 FOREACH_KEY(pos
, zoption
, header_key
, header_idx
) {
585 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption
), (void **) &header_val
, &pos
)) {
586 char header
[1024] = {0};
588 ZVAL_ADDREF(*header_val
);
589 convert_to_string_ex(header_val
);
590 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
591 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, header
);
592 zval_ptr_dtor(header_val
);
600 if ((zoption
= http_request_option(request
, options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
601 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
603 HTTP_CURL_OPT(HTTPHEADER
, request
->_cache
.headers
);
605 /* cookies, array('name' => 'value') */
606 if ((zoption
= http_request_option(request
, options
, "cookies", IS_ARRAY
))) {
607 phpstr_dtor(&request
->_cache
.cookies
);
608 if (zend_hash_num_elements(Z_ARRVAL_P(zoption
))) {
609 if (SUCCESS
== http_urlencode_hash_recursive(HASH_OF(zoption
), &request
->_cache
.cookies
, "; ", sizeof("; ")-1, NULL
, 0)) {
610 phpstr_fix(&request
->_cache
.cookies
);
611 HTTP_CURL_OPT(COOKIE
, request
->_cache
.cookies
.data
);
616 #if LIBCURL_VERSION_NUM >= 0x070e01
618 if ((zoption
= http_request_option(request
, options
, "resetcookies", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
619 HTTP_CURL_OPT(COOKIELIST
, "ALL");
623 /* session cookies */
624 if ((zoption
= http_request_option(request
, options
, "cookiesession", IS_BOOL
))) {
625 if (Z_LVAL_P(zoption
)) {
626 /* accept cookies for this session */
627 HTTP_CURL_OPT(COOKIEFILE
, "");
629 /* reset session cookies */
630 HTTP_CURL_OPT(COOKIESESSION
, 1);
634 /* cookiestore, read initial cookies from that file and store cookies back into that file */
635 if ((zoption
= http_request_option(request
, options
, "cookiestore", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
636 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption
), return FAILURE
);
637 HTTP_CURL_OPT(COOKIEFILE
, Z_STRVAL_P(zoption
));
638 HTTP_CURL_OPT(COOKIEJAR
, Z_STRVAL_P(zoption
));
642 if ((zoption
= http_request_option(request
, options
, "resume", IS_LONG
)) && (Z_LVAL_P(zoption
) != 0)) {
644 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
648 if ((zoption
= http_request_option(request
, options
, "maxfilesize", IS_LONG
))) {
649 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
653 if ((zoption
= http_request_option(request
, options
, "protocol", IS_LONG
))) {
654 HTTP_CURL_OPT(HTTP_VERSION
, Z_LVAL_P(zoption
));
658 if ((zoption
= http_request_option(request
, options
, "lastmodified", IS_LONG
))) {
659 if (Z_LVAL_P(zoption
)) {
660 if (Z_LVAL_P(zoption
) > 0) {
661 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
663 HTTP_CURL_OPT(TIMEVALUE
, HTTP_GET_REQUEST_TIME() + Z_LVAL_P(zoption
));
665 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
667 HTTP_CURL_OPT(TIMECONDITION
, CURL_TIMECOND_NONE
);
671 /* timeout, defaults to 0 */
672 if ((zoption
= http_request_option(request
, options
, "timeout", IS_LONG
))) {
673 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
676 /* connecttimeout, defaults to 3 */
677 if ((zoption
= http_request_option(request
, options
, "connecttimeout", IS_LONG
))) {
678 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
682 if ((zoption
= http_request_option(request
, options
, "ssl", IS_ARRAY
))) {
688 FOREACH_KEYVAL(pos
, zoption
, key
, idx
, param
) {
690 HTTP_CURL_OPT_SSL_STRING(CERT
, 1);
691 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
, 0);
692 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
, 0);
694 HTTP_CURL_OPT_SSL_STRING(KEY
, 0);
695 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
, 0);
696 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
, 0);
698 HTTP_CURL_OPT_SSL_STRING(ENGINE
, 0);
699 HTTP_CURL_OPT_SSL_LONG(VERSION
);
701 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
702 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
703 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
, 0);
705 HTTP_CURL_OPT_STRING(CAINFO
, 1);
706 HTTP_CURL_OPT_STRING(CAPATH
, 1);
707 HTTP_CURL_OPT_STRING(RANDOM_FILE
, 1);
708 HTTP_CURL_OPT_STRING(EGDSOCKET
, 1);
717 switch (request
->meth
)
720 HTTP_CURL_OPT(HTTPGET
, 1);
724 HTTP_CURL_OPT(NOBODY
, 1);
728 HTTP_CURL_OPT(POST
, 1);
732 HTTP_CURL_OPT(UPLOAD
, 1);
736 if (http_request_method_exists(0, request
->meth
, NULL
)) {
737 HTTP_CURL_OPT(CUSTOMREQUEST
, http_request_method_name(request
->meth
));
739 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d (%s)", request
->meth
, request
->url
);
745 /* attach request body */
746 if (request
->body
&& (request
->meth
!= HTTP_GET
) && (request
->meth
!= HTTP_HEAD
) && (request
->meth
!= HTTP_OPTIONS
)) {
747 switch (request
->body
->type
)
749 case HTTP_REQUEST_BODY_EMPTY
:
753 case HTTP_REQUEST_BODY_CSTRING
:
754 HTTP_CURL_OPT(POSTFIELDS
, request
->body
->data
);
755 HTTP_CURL_OPT(POSTFIELDSIZE
, request
->body
->size
);
758 case HTTP_REQUEST_BODY_CURLPOST
:
759 HTTP_CURL_OPT(HTTPPOST
, (struct curl_httppost
*) request
->body
->data
);
762 case HTTP_REQUEST_BODY_UPLOADFILE
:
763 HTTP_CURL_OPT(IOCTLDATA
, request
);
764 HTTP_CURL_OPT(READDATA
, request
);
765 HTTP_CURL_OPT(INFILESIZE
, request
->body
->size
);
769 /* shouldn't ever happen */
770 http_error_ex(HE_ERROR
, 0, "Unknown request body type: %d (%s)", request
->body
->type
, request
->url
);
780 /* {{{ void http_request_exec(http_request *) */
781 PHP_HTTP_API
void _http_request_exec(http_request
*request
)
784 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
786 if (CURLE_OK
!= (result
= curl_easy_perform(request
->ch
))) {
787 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "%s; %s (%s)", curl_easy_strerror(result
), request
->_error
, request
->url
);
792 /* {{{ void http_request_info(http_request *, HashTable *) */
793 PHP_HTTP_API
void _http_request_info(http_request
*request
, HashTable
*info
)
796 INIT_ZARR(array
, info
);
798 HTTP_CURL_INFO(EFFECTIVE_URL
);
799 HTTP_CURL_INFO(RESPONSE_CODE
);
800 HTTP_CURL_INFO_EX(HTTP_CONNECTCODE
, connect_code
);
801 HTTP_CURL_INFO(FILETIME
);
802 HTTP_CURL_INFO(TOTAL_TIME
);
803 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
804 HTTP_CURL_INFO(CONNECT_TIME
);
805 HTTP_CURL_INFO(PRETRANSFER_TIME
);
806 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
807 HTTP_CURL_INFO(REDIRECT_TIME
);
808 HTTP_CURL_INFO(REDIRECT_COUNT
);
809 HTTP_CURL_INFO(SIZE_UPLOAD
);
810 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
811 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
812 HTTP_CURL_INFO(SPEED_UPLOAD
);
813 HTTP_CURL_INFO(HEADER_SIZE
);
814 HTTP_CURL_INFO(REQUEST_SIZE
);
815 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
816 HTTP_CURL_INFO(SSL_ENGINES
);
817 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
818 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
819 HTTP_CURL_INFO(CONTENT_TYPE
);
820 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
821 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
822 /*HTTP_CURL_INFO(OS_ERRNO);*/
823 HTTP_CURL_INFO(NUM_CONNECTS
);
824 #if LIBCURL_VERSION_NUM >= 0x070e01
825 HTTP_CURL_INFO_EX(COOKIELIST
, cookies
);
830 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
831 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
833 http_request
*request
= (http_request
*) ctx
;
834 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
836 if (request
->body
== NULL
|| request
->body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
839 return php_stream_read((php_stream
*) request
->body
->data
, data
, len
* n
);
843 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
844 static int http_curl_progress_callback(void *ctx
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
847 http_request
*request
= (http_request
*) ctx
;
848 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
853 MAKE_STD_ZVAL(param
);
855 add_assoc_double(param
, "dltotal", dltotal
);
856 add_assoc_double(param
, "dlnow", dlnow
);
857 add_assoc_double(param
, "ultotal", ultotal
);
858 add_assoc_double(param
, "ulnow", ulnow
);
860 call_user_function(EG(function_table
), NULL
, request
->_progress_callback
, &retval
, 1, ¶m TSRMLS_CC
);
862 zval_ptr_dtor(¶m
);
869 /* {{{ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
870 static curlioerr
http_curl_ioctl_callback(CURL
*ch
, curliocmd cmd
, void *ctx
)
872 http_request
*request
= (http_request
*) ctx
;
873 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
875 if (cmd
!= CURLIOCMD_RESTARTREAD
) {
876 return CURLIOE_UNKNOWNCMD
;
878 if ( request
->body
== NULL
||
879 request
->body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
||
880 SUCCESS
!= php_stream_rewind((php_stream
*) request
->body
->data
)) {
881 return CURLIOE_FAILRESTART
;
887 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
888 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
890 http_request
*request
= (http_request
*) ctx
;
894 case CURLINFO_DATA_IN
:
895 if (request
->conv
.last_type
== CURLINFO_HEADER_IN
) {
896 phpstr_appends(&request
->conv
.response
, HTTP_CRLF
);
898 case CURLINFO_HEADER_IN
:
899 phpstr_append(&request
->conv
.response
, data
, length
);
901 case CURLINFO_DATA_OUT
:
902 if (request
->conv
.last_type
== CURLINFO_HEADER_OUT
) {
903 phpstr_appends(&request
->conv
.request
, HTTP_CRLF
);
905 case CURLINFO_HEADER_OUT
:
906 phpstr_append(&request
->conv
.request
, data
, length
);
910 fprintf(stderr
, "## ", type
);
912 fprintf(stderr
, "%s", data
);
915 for (i
= 1; i
<= length
; ++i
) {
916 fprintf(stderr
, "%02X ", data
[i
-1] & 0xFF);
918 fprintf(stderr
, "\n## ");
921 fprintf(stderr
, "\n");
923 if (data
[length
-1] != 0xa) {
924 fprintf(stderr
, "\n");
931 request
->conv
.last_type
= type
;
937 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
938 static inline zval
*_http_request_option_ex(http_request
*r
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
942 ulong h
= zend_get_hash_value(key
, keylen
);
949 (SUCCESS
!= zend_hash_quick_find(options
, key
, keylen
, h
, (void **) &zoption
))
951 (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))
957 return http_request_option_cache_ex(r
, key
, keylen
, h
, zval_copy(type
, *zoption
));
961 /* {{{ static inline zval *http_request_option_cache(http_request *, char *key, zval *) */
962 static inline zval
*_http_request_option_cache_ex(http_request
*r
, char *key
, size_t keylen
, ulong h
, zval
*opt TSRMLS_DC
)
968 _zend_hash_quick_add_or_update(&r
->_cache
.options
, key
, keylen
, h
, &opt
, sizeof(zval
*), NULL
,
969 zend_hash_quick_exists(&r
->_cache
.options
, key
, keylen
, h
)?HASH_UPDATE
:HASH_ADD ZEND_FILE_LINE_CC
);
974 if (zend_hash_exists(&r
->_cache
.options
, key
, keylen
)) {
975 zend_hash_update(&r
->_cache
.options
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
977 zend_hash_add(&r
->_cache
.options
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
985 #endif /* HTTP_HAVE_CURL */
992 * vim600: noet sw=4 ts=4 fdm=marker
993 * vim<600: noet sw=4 ts=4