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) "unknown error"
186 #define HTTP_CURL_INFO(I) \
189 HTTP_CURL_INFO_EX(I, N+lenof("CURLINFO_")); \
191 #define HTTP_CURL_INFO_EX(I, X) \
192 switch (I & ~CURLINFO_MASK) \
194 case CURLINFO_STRING: \
197 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &c)) { \
198 char *key = estrndup(X, strlen(X)); \
199 add_assoc_string(&array, pretty_key(key, strlen(X), 0, 0), c ? c : "", 1); \
205 case CURLINFO_DOUBLE: \
208 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &d)) { \
209 char *key = estrndup(X, strlen(X)); \
210 add_assoc_double(&array, pretty_key(key, strlen(X), 0, 0), d); \
216 case CURLINFO_LONG: \
219 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &l)) { \
220 char *key = estrndup(X, strlen(X)); \
221 add_assoc_long(&array, pretty_key(key, strlen(X), 0, 0), l); \
227 case CURLINFO_SLIST: \
229 struct curl_slist *l, *p; \
230 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &l)) { \
232 char *key = estrndup(X, strlen(X)); \
233 MAKE_STD_ZVAL(subarray); \
234 array_init(subarray); \
235 for (p = l; p; p = p->next) { \
236 add_next_index_string(subarray, p->data, 1); \
238 add_assoc_zval(&array, pretty_key(key, strlen(X), 0, 0), subarray); \
239 curl_slist_free_all(l); \
245 #define HTTP_CURL_OPT(OPTION, p) HTTP_CURL_OPT_EX(request->ch, OPTION, (p))
246 #define HTTP_CURL_OPT_EX(ch, OPTION, p) curl_easy_setopt((ch), OPTION, (p))
248 #define HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \
251 HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \
253 #define HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
254 if (!strcasecmp(key, keyname)) { \
255 zval *copy = http_request_option_cache(request, keyname, zval_copy(IS_STRING, *param)); \
257 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(copy), return FAILURE); \
259 HTTP_CURL_OPT(optname, Z_STRVAL_P(copy)); \
263 #define HTTP_CURL_OPT_LONG(OPTION, ldiff) \
266 HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \
268 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
269 if (!strcasecmp(key, keyname)) { \
270 zval *copy = http_request_option_cache(request, keyname, zval_copy(IS_LONG, *param)); \
271 HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \
277 /* {{{ forward declarations */
278 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
279 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
280 static inline zval
*_http_request_option_ex(http_request
*request
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
281 #define http_request_option_cache(r, k, z) _http_request_option_cache_ex((r), (k), sizeof(k), 0, (z) TSRMLS_CC)
282 #define http_request_option_cache_ex(r, k, kl, h, z) _http_request_option_cache_ex((r), (k), (kl), (h), (z) TSRMLS_CC)
283 static inline zval
*_http_request_option_cache_ex(http_request
*r
, char *key
, size_t keylen
, ulong h
, zval
*opt TSRMLS_DC
);
285 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
286 static int http_curl_progress_callback(void *, double, double, double, double);
287 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
288 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
289 static curlioerr
http_curl_ioctl_callback(CURL
*, curliocmd
, void *);
292 /* {{{ CURL *http_curl_init(http_request *) */
293 PHP_HTTP_API CURL
* _http_curl_init_ex(CURL
*ch
, http_request
*request
)
295 if (ch
|| (ch
= curl_easy_init())) {
297 HTTP_CURL_OPT_EX(ch
, CURLOPT_NOSIGNAL
, 1);
299 HTTP_CURL_OPT_EX(ch
, CURLOPT_HEADER
, 0);
300 HTTP_CURL_OPT_EX(ch
, CURLOPT_FILETIME
, 1);
301 HTTP_CURL_OPT_EX(ch
, CURLOPT_AUTOREFERER
, 1);
302 HTTP_CURL_OPT_EX(ch
, CURLOPT_VERBOSE
, 1);
303 HTTP_CURL_OPT_EX(ch
, CURLOPT_HEADERFUNCTION
, NULL
);
304 HTTP_CURL_OPT_EX(ch
, CURLOPT_DEBUGFUNCTION
, http_curl_raw_callback
);
305 HTTP_CURL_OPT_EX(ch
, CURLOPT_READFUNCTION
, http_curl_read_callback
);
306 HTTP_CURL_OPT_EX(ch
, CURLOPT_IOCTLFUNCTION
, http_curl_ioctl_callback
);
307 HTTP_CURL_OPT_EX(ch
, CURLOPT_WRITEFUNCTION
, http_curl_dummy_callback
);
311 HTTP_CURL_OPT_EX(ch
, CURLOPT_PRIVATE
, request
);
312 HTTP_CURL_OPT_EX(ch
, CURLOPT_DEBUGDATA
, request
);
313 HTTP_CURL_OPT_EX(ch
, CURLOPT_ERRORBUFFER
, request
->_error
);
315 /* attach curl handle */
317 /* set defaults (also in http_request_reset()) */
318 http_request_defaults(request
);
326 /* {{{ void http_curl_free(CURL **) */
327 PHP_HTTP_API
void _http_curl_free(CURL
**ch
)
330 /* avoid nasty segfaults with already cleaned up callbacks */
331 HTTP_CURL_OPT_EX(*ch
, CURLOPT_NOPROGRESS
, 1);
332 HTTP_CURL_OPT_EX(*ch
, CURLOPT_PROGRESSFUNCTION
, NULL
);
333 HTTP_CURL_OPT_EX(*ch
, CURLOPT_VERBOSE
, 0);
334 HTTP_CURL_OPT_EX(*ch
, CURLOPT_DEBUGFUNCTION
, NULL
);
335 curl_easy_cleanup(*ch
);
341 /* {{{ http_request *http_request_init(http_request *) */
342 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 r
= emalloc_rel(sizeof(http_request
));
351 memset(r
, 0, sizeof(http_request
));
354 r
->url
= (url
) ? http_absolute_url(url
) : NULL
;
355 r
->meth
= (meth
> 0) ? meth
: HTTP_GET
;
357 phpstr_init(&r
->conv
.request
);
358 phpstr_init_ex(&r
->conv
.response
, HTTP_CURLBUF_SIZE
, 0);
359 phpstr_init(&r
->_cache
.cookies
);
360 zend_hash_init(&r
->_cache
.options
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
362 TSRMLS_SET_CTX(r
->tsrm_ls
);
368 /* {{{ void http_request_dtor(http_request *) */
369 PHP_HTTP_API
void _http_request_dtor(http_request
*request
)
371 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
373 http_curl_free(&request
->ch
);
374 http_request_reset(request
);
376 phpstr_dtor(&request
->_cache
.cookies
);
377 zend_hash_destroy(&request
->_cache
.options
);
378 if (request
->_cache
.headers
) {
379 curl_slist_free_all(request
->_cache
.headers
);
380 request
->_cache
.headers
= NULL
;
382 if (request
->_progress_callback
) {
383 zval_ptr_dtor(&request
->_progress_callback
);
384 request
->_progress_callback
= NULL
;
389 /* {{{ void http_request_free(http_request **) */
390 PHP_HTTP_API
void _http_request_free(http_request
**request
)
393 TSRMLS_FETCH_FROM_CTX((*request
)->tsrm_ls
);
394 http_request_body_free(&(*request
)->body
);
395 http_request_dtor(*request
);
402 /* {{{ void http_request_reset(http_request *) */
403 PHP_HTTP_API
void _http_request_reset(http_request
*request
)
405 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
406 STR_SET(request
->url
, NULL
);
407 request
->conv
.last_type
= 0;
408 phpstr_dtor(&request
->conv
.request
);
409 phpstr_dtor(&request
->conv
.response
);
410 http_request_body_dtor(request
->body
);
413 http_request_defaults(request
);
415 request
->_error
[0] = '\0';
419 /* {{{ void http_request_defaults(http_request *) */
420 PHP_HTTP_API
void _http_request_defaults(http_request
*request
)
423 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION
, NULL
);
424 HTTP_CURL_OPT(CURLOPT_URL
, NULL
);
425 HTTP_CURL_OPT(CURLOPT_NOPROGRESS
, 1);
426 HTTP_CURL_OPT(CURLOPT_PROXY
, NULL
);
427 HTTP_CURL_OPT(CURLOPT_PROXYPORT
, 0);
428 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD
, NULL
);
429 HTTP_CURL_OPT(CURLOPT_PROXYAUTH
, 0);
430 HTTP_CURL_OPT(CURLOPT_INTERFACE
, NULL
);
431 HTTP_CURL_OPT(CURLOPT_PORT
, 0);
432 #if HTTP_CURL_VERSION(7,15,2)
433 HTTP_CURL_OPT(CURLOPT_LOCALPORT
, 0);
434 HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE
, 0);
436 HTTP_CURL_OPT(CURLOPT_USERPWD
, NULL
);
437 HTTP_CURL_OPT(CURLOPT_HTTPAUTH
, 0);
438 HTTP_CURL_OPT(CURLOPT_ENCODING
, NULL
);
439 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION
, 0);
440 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH
, 0);
441 HTTP_CURL_OPT(CURLOPT_REFERER
, NULL
);
442 HTTP_CURL_OPT(CURLOPT_USERAGENT
, "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
443 HTTP_CURL_OPT(CURLOPT_HTTPHEADER
, NULL
);
444 HTTP_CURL_OPT(CURLOPT_COOKIE
, NULL
);
445 #if HTTP_CURL_VERSION(7,14,1)
446 HTTP_CURL_OPT(CURLOPT_COOKIELIST
, NULL
);
448 HTTP_CURL_OPT(CURLOPT_COOKIEFILE
, NULL
);
449 HTTP_CURL_OPT(CURLOPT_COOKIEJAR
, NULL
);
450 HTTP_CURL_OPT(CURLOPT_RESUME_FROM
, 0);
451 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE
, 0);
452 HTTP_CURL_OPT(CURLOPT_TIMECONDITION
, 0);
453 HTTP_CURL_OPT(CURLOPT_TIMEVALUE
, 0);
454 HTTP_CURL_OPT(CURLOPT_TIMEOUT
, 0);
455 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT
, 3);
456 HTTP_CURL_OPT(CURLOPT_SSLCERT
, NULL
);
457 HTTP_CURL_OPT(CURLOPT_SSLCERTTYPE
, NULL
);
458 HTTP_CURL_OPT(CURLOPT_SSLCERTPASSWD
, NULL
);
459 HTTP_CURL_OPT(CURLOPT_SSLKEY
, NULL
);
460 HTTP_CURL_OPT(CURLOPT_SSLKEYTYPE
, NULL
);
461 HTTP_CURL_OPT(CURLOPT_SSLKEYPASSWD
, NULL
);
462 HTTP_CURL_OPT(CURLOPT_SSLENGINE
, NULL
);
463 HTTP_CURL_OPT(CURLOPT_SSLVERSION
, 0);
464 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYPEER
, 0);
465 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYHOST
, 0);
466 HTTP_CURL_OPT(CURLOPT_SSL_CIPHER_LIST
, NULL
);
467 HTTP_CURL_OPT(CURLOPT_CAINFO
, NULL
);
468 HTTP_CURL_OPT(CURLOPT_CAPATH
, NULL
);
469 HTTP_CURL_OPT(CURLOPT_RANDOM_FILE
, NULL
);
470 HTTP_CURL_OPT(CURLOPT_EGDSOCKET
, NULL
);
471 HTTP_CURL_OPT(CURLOPT_POSTFIELDS
, NULL
);
472 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE
, 0);
473 HTTP_CURL_OPT(CURLOPT_HTTPPOST
, NULL
);
474 HTTP_CURL_OPT(CURLOPT_IOCTLDATA
, NULL
);
475 HTTP_CURL_OPT(CURLOPT_READDATA
, NULL
);
476 HTTP_CURL_OPT(CURLOPT_INFILESIZE
, 0);
477 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION
, CURL_HTTP_VERSION_NONE
);
478 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST
, NULL
);
479 HTTP_CURL_OPT(CURLOPT_NOBODY
, 0);
480 HTTP_CURL_OPT(CURLOPT_POST
, 0);
481 HTTP_CURL_OPT(CURLOPT_UPLOAD
, 0);
482 HTTP_CURL_OPT(CURLOPT_HTTPGET
, 1);
487 PHP_HTTP_API
void _http_request_set_progress_callback(http_request
*request
, zval
*cb
)
489 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
491 if (request
->_progress_callback
) {
492 zval_ptr_dtor(&request
->_progress_callback
);
494 if ((request
->_progress_callback
= cb
)) {
496 HTTP_CURL_OPT(CURLOPT_NOPROGRESS
, 0);
497 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA
, request
);
498 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION
, http_curl_progress_callback
);
500 HTTP_CURL_OPT(CURLOPT_NOPROGRESS
, 1);
501 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA
, NULL
);
502 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION
, NULL
);
506 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
507 PHP_HTTP_API STATUS
_http_request_prepare(http_request
*request
, HashTable
*options
)
510 zend_bool range_req
= 0;
512 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
514 HTTP_CHECK_CURL_INIT(request
->ch
, http_curl_init(request
), return FAILURE
);
517 HTTP_CURL_OPT(CURLOPT_URL
, request
->url
);
519 /* progress callback */
520 if ((zoption
= http_request_option(request
, options
, "onprogress", -1))) {
521 http_request_set_progress_callback(request
, zoption
);
525 if ((zoption
= http_request_option(request
, options
, "proxyhost", IS_STRING
))) {
526 if (Z_STRLEN_P(zoption
)) {
527 HTTP_CURL_OPT(CURLOPT_PROXY
, Z_STRVAL_P(zoption
));
531 if ((zoption
= http_request_option(request
, options
, "proxyport", IS_LONG
))) {
532 HTTP_CURL_OPT(CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
535 if ((zoption
= http_request_option(request
, options
, "proxyauth", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
536 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD
, Z_STRVAL_P(zoption
));
539 if ((zoption
= http_request_option(request
, options
, "proxyauthtype", IS_LONG
))) {
540 HTTP_CURL_OPT(CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
544 /* outgoing interface */
545 if ((zoption
= http_request_option(request
, options
, "interface", IS_STRING
))) {
546 HTTP_CURL_OPT(CURLOPT_INTERFACE
, Z_STRVAL_P(zoption
));
548 #if HTTP_CURL_VERSION(7,15,2)
549 if ((zoption
= http_request_option(request
, options
, "portrange", IS_ARRAY
))) {
552 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
553 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &prs
)) {
554 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
555 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &pre
)) {
556 zval
*prs_cpy
= zval_copy(IS_LONG
, *prs
), *pre_cpy
= zval_copy(IS_LONG
, *pre
);
558 if (Z_LVAL_P(prs_cpy
) && Z_LVAL_P(pre_cpy
)) {
559 HTTP_CURL_OPT(CURLOPT_LOCALPORT
, MIN(Z_LVAL_P(prs_cpy
), Z_LVAL_P(pre_cpy
)));
560 HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE
, ABS(Z_LVAL_P(prs_cpy
)-Z_LVAL_P(pre_cpy
))+1L);
571 if ((zoption
= http_request_option(request
, options
, "port", IS_LONG
))) {
572 HTTP_CURL_OPT(CURLOPT_PORT
, Z_LVAL_P(zoption
));
576 if ((zoption
= http_request_option(request
, options
, "httpauth", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
577 HTTP_CURL_OPT(CURLOPT_USERPWD
, Z_STRVAL_P(zoption
));
579 if ((zoption
= http_request_option(request
, options
, "httpauthtype", IS_LONG
))) {
580 HTTP_CURL_OPT(CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
583 /* redirects, defaults to 0 */
584 if ((zoption
= http_request_option(request
, options
, "redirect", IS_LONG
))) {
585 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
586 HTTP_CURL_OPT(CURLOPT_MAXREDIRS
, Z_LVAL_P(zoption
));
587 if ((zoption
= http_request_option(request
, options
, "unrestrictedauth", IS_BOOL
))) {
588 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
593 if ((zoption
= http_request_option(request
, options
, "referer", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
594 HTTP_CURL_OPT(CURLOPT_REFERER
, Z_STRVAL_P(zoption
));
597 /* useragent, default "PECL::HTTP/version (PHP/version)" */
598 if ((zoption
= http_request_option(request
, options
, "useragent", IS_STRING
))) {
599 /* allow to send no user agent, not even default one */
600 if (Z_STRLEN_P(zoption
)) {
601 HTTP_CURL_OPT(CURLOPT_USERAGENT
, Z_STRVAL_P(zoption
));
603 HTTP_CURL_OPT(CURLOPT_USERAGENT
, NULL
);
608 if ((zoption
= http_request_option(request
, options
, "resume", IS_LONG
)) && (Z_LVAL_P(zoption
) != 0)) {
610 HTTP_CURL_OPT(CURLOPT_RESUME_FROM
, Z_LVAL_P(zoption
));
613 /* additional headers, array('name' => 'value') */
614 if (request
->_cache
.headers
) {
615 curl_slist_free_all(request
->_cache
.headers
);
616 request
->_cache
.headers
= NULL
;
618 if ((zoption
= http_request_option(request
, options
, "headers", IS_ARRAY
))) {
623 FOREACH_KEY(pos
, zoption
, header_key
, header_idx
) {
626 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption
), (void **) &header_val
, &pos
)) {
627 char header
[1024] = {0};
629 ZVAL_ADDREF(*header_val
);
630 convert_to_string_ex(header_val
);
631 if (!strcasecmp(header_key
, "range")) {
634 snprintf(header
, lenof(header
), "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
635 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, header
);
636 zval_ptr_dtor(header_val
);
645 if ((zoption
= http_request_option(request
, options
, "etag", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
646 char match_header
[1024] = {0}, *quoted_etag
= NULL
;
648 if ((Z_STRVAL_P(zoption
)[0] != '"') || (Z_STRVAL_P(zoption
)[Z_STRLEN_P(zoption
)-1] != '"')) {
649 spprintf("ed_etag
, 0, "\"%s\"", Z_STRVAL_P(zoption
));
651 snprintf(match_header
, lenof(match_header
), "%s: %s", range_req
?"If-Match":"If-None-Match", quoted_etag
?quoted_etag
:Z_STRVAL_P(zoption
));
652 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, match_header
);
653 STR_FREE(quoted_etag
);
656 if ((zoption
= http_request_option(request
, options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
657 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
659 HTTP_CURL_OPT(CURLOPT_HTTPHEADER
, request
->_cache
.headers
);
662 if ((zoption
= http_request_option(request
, options
, "lastmodified", IS_LONG
))) {
663 if (Z_LVAL_P(zoption
)) {
664 if (Z_LVAL_P(zoption
) > 0) {
665 HTTP_CURL_OPT(CURLOPT_TIMEVALUE
, Z_LVAL_P(zoption
));
667 HTTP_CURL_OPT(CURLOPT_TIMEVALUE
, HTTP_GET_REQUEST_TIME() + Z_LVAL_P(zoption
));
669 HTTP_CURL_OPT(CURLOPT_TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
671 HTTP_CURL_OPT(CURLOPT_TIMECONDITION
, CURL_TIMECOND_NONE
);
675 /* cookies, array('name' => 'value') */
676 if ((zoption
= http_request_option(request
, options
, "cookies", IS_ARRAY
))) {
677 phpstr_dtor(&request
->_cache
.cookies
);
678 if (zend_hash_num_elements(Z_ARRVAL_P(zoption
))) {
679 zval
*urlenc_cookies
= NULL
;
680 /* check whether cookies should not be urlencoded; default is to urlencode them */
681 if ((!(urlenc_cookies
= http_request_option(request
, options
, "encodecookies", IS_BOOL
))) || Z_BVAL_P(urlenc_cookies
)) {
682 if (SUCCESS
== http_urlencode_hash_recursive(HASH_OF(zoption
), &request
->_cache
.cookies
, "; ", lenof("; "), NULL
, 0)) {
683 phpstr_fix(&request
->_cache
.cookies
);
684 HTTP_CURL_OPT(CURLOPT_COOKIE
, request
->_cache
.cookies
.data
);
688 char *cookie_key
= NULL
;
691 FOREACH_KEY(pos
, zoption
, cookie_key
, cookie_idx
) {
694 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption
), (void **) &cookie_val
, &pos
)) {
695 zval
*val
= zval_copy(IS_STRING
, *cookie_val
);
696 phpstr_appendf(&request
->_cache
.cookies
, "%s=%s; ", cookie_key
, Z_STRVAL_P(val
));
705 phpstr_fix(&request
->_cache
.cookies
);
706 if (PHPSTR_LEN(&request
->_cache
.cookies
)) {
707 HTTP_CURL_OPT(CURLOPT_COOKIE
, PHPSTR_VAL(&request
->_cache
.cookies
));
713 #if HTTP_CURL_VERSION(7,14,1)
715 if ((zoption
= http_request_option(request
, options
, "resetcookies", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
716 HTTP_CURL_OPT(CURLOPT_COOKIELIST
, "ALL");
720 /* session cookies */
721 if ((zoption
= http_request_option(request
, options
, "cookiesession", IS_BOOL
))) {
722 if (Z_LVAL_P(zoption
)) {
723 /* accept cookies for this session */
724 HTTP_CURL_OPT(CURLOPT_COOKIEFILE
, "");
726 /* reset session cookies */
727 HTTP_CURL_OPT(CURLOPT_COOKIESESSION
, 1);
731 /* cookiestore, read initial cookies from that file and store cookies back into that file */
732 if ((zoption
= http_request_option(request
, options
, "cookiestore", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
733 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption
), return FAILURE
);
734 HTTP_CURL_OPT(CURLOPT_COOKIEFILE
, Z_STRVAL_P(zoption
));
735 HTTP_CURL_OPT(CURLOPT_COOKIEJAR
, Z_STRVAL_P(zoption
));
739 if ((zoption
= http_request_option(request
, options
, "maxfilesize", IS_LONG
))) {
740 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE
, Z_LVAL_P(zoption
));
744 if ((zoption
= http_request_option(request
, options
, "protocol", IS_LONG
))) {
745 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION
, Z_LVAL_P(zoption
));
748 /* timeout, defaults to 0 */
749 if ((zoption
= http_request_option(request
, options
, "timeout", IS_LONG
))) {
750 HTTP_CURL_OPT(CURLOPT_TIMEOUT
, Z_LVAL_P(zoption
));
753 /* connecttimeout, defaults to 0 */
754 if ((zoption
= http_request_option(request
, options
, "connecttimeout", IS_LONG
))) {
755 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
759 if ((zoption
= http_request_option(request
, options
, "ssl", IS_ARRAY
))) {
765 FOREACH_KEYVAL(pos
, zoption
, key
, idx
, param
) {
767 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT
, 0, 1);
768 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE
, 0, 0);
769 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD
, 0, 0);
771 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY
, 0, 0);
772 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE
, 0, 0);
773 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD
, 0, 0);
775 HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE
, 0, 0);
776 HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION
, 0);
778 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER
, 1);
779 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST
, 1);
780 HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST
, 1, 0);
782 HTTP_CURL_OPT_STRING(CURLOPT_CAINFO
, -3, 1);
783 HTTP_CURL_OPT_STRING(CURLOPT_CAPATH
, -3, 1);
784 HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE
, -3, 1);
785 HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET
, -3, 1);
794 switch (request
->meth
)
797 HTTP_CURL_OPT(CURLOPT_HTTPGET
, 1);
801 HTTP_CURL_OPT(CURLOPT_NOBODY
, 1);
805 HTTP_CURL_OPT(CURLOPT_POST
, 1);
809 HTTP_CURL_OPT(CURLOPT_UPLOAD
, 1);
813 if (http_request_method_exists(0, request
->meth
, NULL
)) {
814 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST
, http_request_method_name(request
->meth
));
816 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d (%s)", request
->meth
, request
->url
);
822 /* attach request body */
823 if (request
->body
&& (request
->meth
!= HTTP_GET
) && (request
->meth
!= HTTP_HEAD
) && (request
->meth
!= HTTP_OPTIONS
)) {
824 switch (request
->body
->type
)
826 case HTTP_REQUEST_BODY_EMPTY
:
830 case HTTP_REQUEST_BODY_CSTRING
:
831 HTTP_CURL_OPT(CURLOPT_POSTFIELDS
, request
->body
->data
);
832 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE
, request
->body
->size
);
835 case HTTP_REQUEST_BODY_CURLPOST
:
836 HTTP_CURL_OPT(CURLOPT_HTTPPOST
, (struct curl_httppost
*) request
->body
->data
);
839 case HTTP_REQUEST_BODY_UPLOADFILE
:
840 HTTP_CURL_OPT(CURLOPT_IOCTLDATA
, request
);
841 HTTP_CURL_OPT(CURLOPT_READDATA
, request
);
842 HTTP_CURL_OPT(CURLOPT_INFILESIZE
, request
->body
->size
);
846 /* shouldn't ever happen */
847 http_error_ex(HE_ERROR
, 0, "Unknown request body type: %d (%s)", request
->body
->type
, request
->url
);
857 /* {{{ void http_request_exec(http_request *) */
858 PHP_HTTP_API
void _http_request_exec(http_request
*request
)
861 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
863 if (CURLE_OK
!= (result
= curl_easy_perform(request
->ch
))) {
864 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "%s; %s (%s)", curl_easy_strerror(result
), request
->_error
, request
->url
);
869 /* {{{ void http_request_info(http_request *, HashTable *) */
870 PHP_HTTP_API
void _http_request_info(http_request
*request
, HashTable
*info
)
873 INIT_ZARR(array
, info
);
875 HTTP_CURL_INFO(CURLINFO_EFFECTIVE_URL
);
876 HTTP_CURL_INFO(CURLINFO_RESPONSE_CODE
);
877 HTTP_CURL_INFO_EX(CURLINFO_HTTP_CONNECTCODE
, "connect_code");
878 HTTP_CURL_INFO(CURLINFO_FILETIME
);
879 HTTP_CURL_INFO(CURLINFO_TOTAL_TIME
);
880 HTTP_CURL_INFO(CURLINFO_NAMELOOKUP_TIME
);
881 HTTP_CURL_INFO(CURLINFO_CONNECT_TIME
);
882 HTTP_CURL_INFO(CURLINFO_PRETRANSFER_TIME
);
883 HTTP_CURL_INFO(CURLINFO_STARTTRANSFER_TIME
);
884 HTTP_CURL_INFO(CURLINFO_REDIRECT_TIME
);
885 HTTP_CURL_INFO(CURLINFO_REDIRECT_COUNT
);
886 HTTP_CURL_INFO(CURLINFO_SIZE_UPLOAD
);
887 HTTP_CURL_INFO(CURLINFO_SIZE_DOWNLOAD
);
888 HTTP_CURL_INFO(CURLINFO_SPEED_DOWNLOAD
);
889 HTTP_CURL_INFO(CURLINFO_SPEED_UPLOAD
);
890 HTTP_CURL_INFO(CURLINFO_HEADER_SIZE
);
891 HTTP_CURL_INFO(CURLINFO_REQUEST_SIZE
);
892 HTTP_CURL_INFO(CURLINFO_SSL_VERIFYRESULT
);
893 HTTP_CURL_INFO(CURLINFO_SSL_ENGINES
);
894 HTTP_CURL_INFO(CURLINFO_CONTENT_LENGTH_DOWNLOAD
);
895 HTTP_CURL_INFO(CURLINFO_CONTENT_LENGTH_UPLOAD
);
896 HTTP_CURL_INFO(CURLINFO_CONTENT_TYPE
);
897 HTTP_CURL_INFO(CURLINFO_HTTPAUTH_AVAIL
);
898 HTTP_CURL_INFO(CURLINFO_PROXYAUTH_AVAIL
);
899 HTTP_CURL_INFO(CURLINFO_NUM_CONNECTS
);
900 #if HTTP_CURL_VERSION(7,14,1)
901 HTTP_CURL_INFO_EX(CURLINFO_COOKIELIST
, "cookies");
903 HTTP_CURL_INFO(CURLINFO_OS_ERRNO
);
904 add_assoc_string(&array
, "error", request
->_error
, 1);
908 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
909 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
911 http_request
*request
= (http_request
*) ctx
;
912 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
914 if (request
->body
== NULL
|| request
->body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
917 return php_stream_read((php_stream
*) request
->body
->data
, data
, len
* n
);
921 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
922 static int http_curl_progress_callback(void *ctx
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
925 http_request
*request
= (http_request
*) ctx
;
926 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
931 MAKE_STD_ZVAL(param
);
933 add_assoc_double(param
, "dltotal", dltotal
);
934 add_assoc_double(param
, "dlnow", dlnow
);
935 add_assoc_double(param
, "ultotal", ultotal
);
936 add_assoc_double(param
, "ulnow", ulnow
);
938 call_user_function(EG(function_table
), NULL
, request
->_progress_callback
, &retval
, 1, ¶m TSRMLS_CC
);
940 zval_ptr_dtor(¶m
);
947 /* {{{ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
948 static curlioerr
http_curl_ioctl_callback(CURL
*ch
, curliocmd cmd
, void *ctx
)
950 http_request
*request
= (http_request
*) ctx
;
951 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
953 if (cmd
!= CURLIOCMD_RESTARTREAD
) {
954 return CURLIOE_UNKNOWNCMD
;
956 if ( request
->body
== NULL
||
957 request
->body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
||
958 SUCCESS
!= php_stream_rewind((php_stream
*) request
->body
->data
)) {
959 return CURLIOE_FAILRESTART
;
965 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
966 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
968 http_request
*request
= (http_request
*) ctx
;
972 case CURLINFO_DATA_IN
:
973 if (request
->conv
.last_type
== CURLINFO_HEADER_IN
) {
974 phpstr_appends(&request
->conv
.response
, HTTP_CRLF
);
976 case CURLINFO_HEADER_IN
:
977 phpstr_append(&request
->conv
.response
, data
, length
);
979 case CURLINFO_DATA_OUT
:
980 if (request
->conv
.last_type
== CURLINFO_HEADER_OUT
) {
981 phpstr_appends(&request
->conv
.request
, HTTP_CRLF
);
983 case CURLINFO_HEADER_OUT
:
984 phpstr_append(&request
->conv
.request
, data
, length
);
988 fprintf(stderr
, "## ", type
);
990 fprintf(stderr
, "%s", data
);
993 for (i
= 1; i
<= length
; ++i
) {
994 fprintf(stderr
, "%02X ", data
[i
-1] & 0xFF);
996 fprintf(stderr
, "\n## ");
999 fprintf(stderr
, "\n");
1001 if (data
[length
-1] != 0xa) {
1002 fprintf(stderr
, "\n");
1009 request
->conv
.last_type
= type
;
1015 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
1016 static inline zval
*_http_request_option_ex(http_request
*r
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
1019 #ifdef ZEND_ENGINE_2
1020 ulong h
= zend_get_hash_value(key
, keylen
);
1026 #ifdef ZEND_ENGINE_2
1027 (SUCCESS
!= zend_hash_quick_find(options
, key
, keylen
, h
, (void **) &zoption
))
1029 (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))
1035 return http_request_option_cache_ex(r
, key
, keylen
, h
, zval_copy(type
, *zoption
));
1039 /* {{{ static inline zval *http_request_option_cache(http_request *, char *key, zval *) */
1040 static inline zval
*_http_request_option_cache_ex(http_request
*r
, char *key
, size_t keylen
, ulong h
, zval
*opt TSRMLS_DC
)
1044 #ifdef ZEND_ENGINE_2
1046 _zend_hash_quick_add_or_update(&r
->_cache
.options
, key
, keylen
, h
, &opt
, sizeof(zval
*), NULL
,
1047 zend_hash_quick_exists(&r
->_cache
.options
, key
, keylen
, h
)?HASH_UPDATE
:HASH_ADD ZEND_FILE_LINE_CC
);
1052 if (zend_hash_exists(&r
->_cache
.options
, key
, keylen
)) {
1053 zend_hash_update(&r
->_cache
.options
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
1055 zend_hash_add(&r
->_cache
.options
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
1063 #endif /* HTTP_HAVE_CURL */
1070 * vim600: noet sw=4 ts=4 fdm=marker
1071 * vim<600: noet sw=4 ts=4