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-2007, 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_persistent_handle_api.h"
23 #include "php_http_request_api.h"
24 #include "php_http_url_api.h"
27 # include "php_http_request_object.h"
30 #include "php_http_request_int.h"
32 /* {{{ cruft for thread safe SSL crypto locks */
33 #ifdef HTTP_NEED_OPENSSL_TSL
34 static MUTEX_T
*http_openssl_tsl
= NULL
;
36 static void http_openssl_thread_lock(int mode
, int n
, const char * file
, int line
)
38 if (mode
& CRYPTO_LOCK
) {
39 tsrm_mutex_lock(http_openssl_tsl
[n
]);
41 tsrm_mutex_unlock(http_openssl_tsl
[n
]);
45 static ulong
http_openssl_thread_id(void)
47 return (ulong
) tsrm_thread_id();
50 #ifdef HTTP_NEED_GNUTLS_TSL
51 static int http_gnutls_mutex_create(void **m
)
53 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
60 static int http_gnutls_mutex_destroy(void **m
)
62 tsrm_mutex_free(*((MUTEX_T
*) m
));
66 static int http_gnutls_mutex_lock(void **m
)
68 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
71 static int http_gnutls_mutex_unlock(void **m
)
73 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
76 static struct gcry_thread_cbs http_gnutls_tsl
= {
77 GCRY_THREAD_OPTION_USER
,
79 http_gnutls_mutex_create
,
80 http_gnutls_mutex_destroy
,
81 http_gnutls_mutex_lock
,
82 http_gnutls_mutex_unlock
87 /* safe curl wrappers */
88 #define init_curl_storage(ch) \
90 http_request_storage *st = pecalloc(1, sizeof(http_request_storage), 1); \
91 curl_easy_setopt(ch, CURLOPT_PRIVATE, st); \
92 curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer); \
95 static void *safe_curl_init(void)
99 if ((ch
= curl_easy_init())) {
100 init_curl_storage(ch
);
105 static void *safe_curl_copy(void *p
)
109 if ((ch
= curl_easy_duphandle(p
))) {
110 init_curl_storage(ch
);
115 static void safe_curl_dtor(void *p
) {
116 http_request_storage
*st
= http_request_storage_get(p
);
118 curl_easy_cleanup(p
);
124 if (st
->cookiestore
) {
125 pefree(st
->cookiestore
, 1);
133 PHP_MINIT_FUNCTION(http_request
)
135 #ifdef HTTP_NEED_OPENSSL_TSL
136 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
137 if (!CRYPTO_get_id_callback()) {
138 int i
, c
= CRYPTO_num_locks();
140 http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
142 for (i
= 0; i
< c
; ++i
) {
143 http_openssl_tsl
[i
] = tsrm_mutex_alloc();
146 CRYPTO_set_id_callback(http_openssl_thread_id
);
147 CRYPTO_set_locking_callback(http_openssl_thread_lock
);
150 #ifdef HTTP_NEED_GNUTLS_TSL
151 gcry_control(GCRYCTL_SET_THREAD_CBS
, &http_gnutls_tsl
);
154 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
158 if (SUCCESS
!= http_persistent_handle_provide("http_request", safe_curl_init
, safe_curl_dtor
, safe_curl_copy
)) {
162 HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
);
163 HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
);
164 #if HTTP_CURL_VERSION(7,19,3)
165 HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST_IE", CURLAUTH_DIGEST_IE
);
167 HTTP_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
);
168 HTTP_LONG_CONSTANT("HTTP_AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE
);
169 HTTP_LONG_CONSTANT("HTTP_AUTH_ANY", CURLAUTH_ANY
);
171 HTTP_LONG_CONSTANT("HTTP_VERSION_NONE", CURL_HTTP_VERSION_NONE
); /* to be removed */
172 HTTP_LONG_CONSTANT("HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0
);
173 HTTP_LONG_CONSTANT("HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1
);
174 HTTP_LONG_CONSTANT("HTTP_VERSION_ANY", CURL_HTTP_VERSION_NONE
);
176 HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1
);
177 HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2
);
178 HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3
);
179 HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT
);
181 HTTP_LONG_CONSTANT("HTTP_IPRESOLVE_V4", CURL_IPRESOLVE_V4
);
182 HTTP_LONG_CONSTANT("HTTP_IPRESOLVE_V6", CURL_IPRESOLVE_V6
);
183 HTTP_LONG_CONSTANT("HTTP_IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER
);
185 #if HTTP_CURL_VERSION(7,15,2)
186 HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS4", CURLPROXY_SOCKS4
);
188 #if HTTP_CURL_VERSION(7,18,0)
189 HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS4A", CURLPROXY_SOCKS4A
);
190 HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS5_HOSTNAME", CURLPROXY_SOCKS5_HOSTNAME
);
192 HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS5", CURLPROXY_SOCKS5
);
193 HTTP_LONG_CONSTANT("HTTP_PROXY_HTTP", CURLPROXY_HTTP
);
194 #if HTTP_CURL_VERSION(7,19,4)
195 HTTP_LONG_CONSTANT("HTTP_PROXY_HTTP_1_0", CURLPROXY_HTTP_1_0
);
198 #if HTTP_CURL_VERSION(7,19,1)
199 HTTP_LONG_CONSTANT("HTTP_POSTREDIR_301", CURL_REDIR_POST_301
);
200 HTTP_LONG_CONSTANT("HTTP_POSTREDIR_302", CURL_REDIR_POST_302
);
201 HTTP_LONG_CONSTANT("HTTP_POSTREDIR_ALL", CURL_REDIR_POST_ALL
);
208 PHP_MSHUTDOWN_FUNCTION(http_request
)
210 curl_global_cleanup();
211 #ifdef HTTP_NEED_OPENSSL_TSL
212 if (http_openssl_tsl
) {
213 int i
, c
= CRYPTO_num_locks();
215 CRYPTO_set_id_callback(NULL
);
216 CRYPTO_set_locking_callback(NULL
);
218 for (i
= 0; i
< c
; ++i
) {
219 tsrm_mutex_free(http_openssl_tsl
[i
]);
222 free(http_openssl_tsl
);
223 http_openssl_tsl
= NULL
;
230 /* {{{ forward declarations */
231 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
232 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
233 static inline zval
*_http_request_option_ex(http_request
*request
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
234 #define http_request_option_cache(r, k, z) _http_request_option_cache_ex((r), (k), sizeof(k), 0, (z) TSRMLS_CC)
235 #define http_request_option_cache_ex(r, k, kl, h, z) _http_request_option_cache_ex((r), (k), (kl), (h), (z) TSRMLS_CC)
236 static inline zval
*_http_request_option_cache_ex(http_request
*r
, char *key
, size_t keylen
, ulong h
, zval
*opt TSRMLS_DC
);
238 #define http_request_cookies_enabled(r) _http_request_cookies_enabled((r))
239 static inline int _http_request_cookies_enabled(http_request
*r
);
241 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
242 static int http_curl_progress_callback(void *, double, double, double, double);
243 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
244 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
245 static curlioerr
http_curl_ioctl_callback(CURL
*, curliocmd
, void *);
248 /* {{{ CURL *http_curl_init(http_request *) */
249 PHP_HTTP_API CURL
* _http_curl_init_ex(CURL
*ch
, http_request
*request TSRMLS_DC
)
251 if (ch
|| (SUCCESS
== http_persistent_handle_acquire("http_request", &ch
))) {
253 curl_easy_setopt(ch
, CURLOPT_NOSIGNAL
, 1L);
255 curl_easy_setopt(ch
, CURLOPT_HEADER
, 0L);
256 curl_easy_setopt(ch
, CURLOPT_FILETIME
, 1L);
257 curl_easy_setopt(ch
, CURLOPT_AUTOREFERER
, 1L);
258 curl_easy_setopt(ch
, CURLOPT_VERBOSE
, 1L);
259 curl_easy_setopt(ch
, CURLOPT_HEADERFUNCTION
, NULL
);
260 curl_easy_setopt(ch
, CURLOPT_DEBUGFUNCTION
, http_curl_raw_callback
);
261 curl_easy_setopt(ch
, CURLOPT_READFUNCTION
, http_curl_read_callback
);
262 curl_easy_setopt(ch
, CURLOPT_IOCTLFUNCTION
, http_curl_ioctl_callback
);
263 curl_easy_setopt(ch
, CURLOPT_WRITEFUNCTION
, http_curl_dummy_callback
);
267 curl_easy_setopt(ch
, CURLOPT_DEBUGDATA
, request
);
269 /* attach curl handle */
271 /* set defaults (also in http_request_reset()) */
272 http_request_defaults(request
);
280 /* {{{ CURL *http_curl_copy(CURL *) */
281 PHP_HTTP_API CURL
*_http_curl_copy(CURL
*ch TSRMLS_DC
)
285 if (SUCCESS
== http_persistent_handle_accrete("http_request", ch
, ©
)) {
292 /* {{{ void http_curl_free(CURL **) */
293 PHP_HTTP_API
void _http_curl_free(CURL
**ch TSRMLS_DC
)
296 curl_easy_setopt(*ch
, CURLOPT_NOPROGRESS
, 1L);
297 curl_easy_setopt(*ch
, CURLOPT_PROGRESSFUNCTION
, NULL
);
298 curl_easy_setopt(*ch
, CURLOPT_VERBOSE
, 0L);
299 curl_easy_setopt(*ch
, CURLOPT_DEBUGFUNCTION
, NULL
);
301 http_persistent_handle_release("http_request", ch
);
306 /* {{{ http_request *http_request_init(http_request *) */
307 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
)
314 r
= emalloc_rel(sizeof(http_request
));
316 memset(r
, 0, sizeof(http_request
));
319 r
->url
= (url
) ? http_absolute_url(url
) : NULL
;
320 r
->meth
= (meth
> 0) ? meth
: HTTP_GET
;
322 phpstr_init(&r
->conv
.request
);
323 phpstr_init_ex(&r
->conv
.response
, HTTP_CURLBUF_SIZE
, 0);
324 phpstr_init(&r
->_cache
.cookies
);
325 zend_hash_init(&r
->_cache
.options
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
327 TSRMLS_SET_CTX(r
->tsrm_ls
);
333 /* {{{ void http_request_dtor(http_request *) */
334 PHP_HTTP_API
void _http_request_dtor(http_request
*request
)
336 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
338 http_request_reset(request
);
339 http_curl_free(&request
->ch
);
341 phpstr_dtor(&request
->_cache
.cookies
);
342 zend_hash_destroy(&request
->_cache
.options
);
343 if (request
->_cache
.headers
) {
344 curl_slist_free_all(request
->_cache
.headers
);
345 request
->_cache
.headers
= NULL
;
347 if (request
->_progress_callback
) {
348 zval_ptr_dtor(&request
->_progress_callback
);
349 request
->_progress_callback
= NULL
;
354 /* {{{ void http_request_free(http_request **) */
355 PHP_HTTP_API
void _http_request_free(http_request
**request
)
358 TSRMLS_FETCH_FROM_CTX((*request
)->tsrm_ls
);
359 http_request_body_free(&(*request
)->body
);
360 http_request_dtor(*request
);
367 /* {{{ void http_request_reset(http_request *) */
368 PHP_HTTP_API
void _http_request_reset(http_request
*request
)
370 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
371 STR_SET(request
->url
, NULL
);
372 request
->conv
.last_type
= 0;
373 phpstr_dtor(&request
->conv
.request
);
374 phpstr_dtor(&request
->conv
.response
);
375 http_request_body_dtor(request
->body
);
376 http_request_defaults(request
);
379 http_request_storage
*st
= http_request_storage_get(request
->ch
);
386 if (st
->cookiestore
) {
387 pefree(st
->cookiestore
, 1);
388 st
->cookiestore
= NULL
;
390 st
->errorbuffer
[0] = '\0';
396 /* {{{ STATUS http_request_enable_cookies(http_request *) */
397 PHP_HTTP_API STATUS
_http_request_enable_cookies(http_request
*request
)
400 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
402 HTTP_CHECK_CURL_INIT(request
->ch
, http_curl_init_ex(request
->ch
, request
), initialized
= 0);
403 if (initialized
&& (http_request_cookies_enabled(request
) || (CURLE_OK
== curl_easy_setopt(request
->ch
, CURLOPT_COOKIEFILE
, "")))) {
406 http_error(HE_WARNING
, HTTP_E_REQUEST
, "Could not enable cookies for this session");
411 /* {{{ STATUS http_request_reset_cookies(http_request *, int) */
412 PHP_HTTP_API STATUS
_http_request_reset_cookies(http_request
*request
, int session_only
)
415 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
417 HTTP_CHECK_CURL_INIT(request
->ch
, http_curl_init_ex(request
->ch
, request
), initialized
= 0);
419 if (!http_request_cookies_enabled(request
)) {
420 if (SUCCESS
!= http_request_enable_cookies(request
)) {
425 #if HTTP_CURL_VERSION(7,15,4)
426 if (CURLE_OK
== curl_easy_setopt(request
->ch
, CURLOPT_COOKIELIST
, "SESS")) {
430 http_error(HE_WARNING
, HTTP_E_REQUEST
, "Could not reset session cookies (need libcurl >= v7.15.4)");
433 #if HTTP_CURL_VERSION(7,14,1)
434 if (CURLE_OK
== curl_easy_setopt(request
->ch
, CURLOPT_COOKIELIST
, "ALL")) {
438 http_error(HE_WARNING
, HTTP_E_REQUEST
, "Could not reset cookies (need libcurl >= v7.14.1)");
446 PHP_HTTP_API STATUS
_http_request_flush_cookies(http_request
*request
)
449 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
451 HTTP_CHECK_CURL_INIT(request
->ch
, http_curl_init_ex(request
->ch
, request
), initialized
= 0);
453 if (!http_request_cookies_enabled(request
)) {
456 #if HTTP_CURL_VERSION(7,17,1)
457 if (CURLE_OK
== curl_easy_setopt(request
->ch
, CURLOPT_COOKIELIST
, "FLUSH")) {
461 http_error(HE_WARNING
, HTTP_E_REQUEST
, "Could not flush cookies (need libcurl >= v7.17.1)");
467 /* {{{ void http_request_defaults(http_request *) */
468 PHP_HTTP_API
void _http_request_defaults(http_request
*request
)
471 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION
, NULL
);
472 HTTP_CURL_OPT(CURLOPT_URL
, NULL
);
473 HTTP_CURL_OPT(CURLOPT_NOPROGRESS
, 1L);
474 #if HTTP_CURL_VERSION(7,19,4)
475 HTTP_CURL_OPT(CURLOPT_NOPROXY
, NULL
);
477 HTTP_CURL_OPT(CURLOPT_PROXY
, NULL
);
478 HTTP_CURL_OPT(CURLOPT_PROXYPORT
, 0L);
479 HTTP_CURL_OPT(CURLOPT_PROXYTYPE
, 0L);
480 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD
, NULL
);
481 HTTP_CURL_OPT(CURLOPT_PROXYAUTH
, 0L);
482 HTTP_CURL_OPT(CURLOPT_HTTPPROXYTUNNEL
, 0L);
483 HTTP_CURL_OPT(CURLOPT_DNS_CACHE_TIMEOUT
, 60L);
484 HTTP_CURL_OPT(CURLOPT_IPRESOLVE
, 0);
485 HTTP_CURL_OPT(CURLOPT_LOW_SPEED_LIMIT
, 0L);
486 HTTP_CURL_OPT(CURLOPT_LOW_SPEED_TIME
, 0L);
487 #if HTTP_CURL_VERSION(7,15,5)
489 HTTP_CURL_OPT(CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0);
490 HTTP_CURL_OPT(CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0);
494 HTTP_CURL_OPT(CURLOPT_MAXCONNECTS, 5L); */
495 HTTP_CURL_OPT(CURLOPT_FRESH_CONNECT
, 0L);
496 HTTP_CURL_OPT(CURLOPT_FORBID_REUSE
, 0L);
497 HTTP_CURL_OPT(CURLOPT_INTERFACE
, NULL
);
498 HTTP_CURL_OPT(CURLOPT_PORT
, 0L);
499 #if HTTP_CURL_VERSION(7,15,2)
500 HTTP_CURL_OPT(CURLOPT_LOCALPORT
, 0L);
501 HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE
, 0L);
503 HTTP_CURL_OPT(CURLOPT_USERPWD
, NULL
);
504 HTTP_CURL_OPT(CURLOPT_HTTPAUTH
, 0L);
505 HTTP_CURL_OPT(CURLOPT_ENCODING
, NULL
);
506 #if HTTP_CURL_VERSION(7,16,2)
507 /* we do this ourself anyway */
508 HTTP_CURL_OPT(CURLOPT_HTTP_CONTENT_DECODING
, 0L);
509 HTTP_CURL_OPT(CURLOPT_HTTP_TRANSFER_DECODING
, 0L);
511 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION
, 0L);
512 #if HTTP_CURL_VERSION(7,19,1)
513 HTTP_CURL_OPT(CURLOPT_POSTREDIR
, 0L);
514 #elif HTTP_CURL_VERSION(7,17,1)
515 HTTP_CURL_OPT(CURLOPT_POST301
, 0L);
517 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH
, 0L);
518 HTTP_CURL_OPT(CURLOPT_REFERER
, NULL
);
519 HTTP_CURL_OPT(CURLOPT_USERAGENT
, "PECL::HTTP/" PHP_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
520 HTTP_CURL_OPT(CURLOPT_HTTPHEADER
, NULL
);
521 HTTP_CURL_OPT(CURLOPT_COOKIE
, NULL
);
522 #if HTTP_CURL_VERSION(7,14,1)
523 HTTP_CURL_OPT(CURLOPT_COOKIELIST
, NULL
);
525 HTTP_CURL_OPT(CURLOPT_RANGE
, NULL
);
526 HTTP_CURL_OPT(CURLOPT_RESUME_FROM
, 0L);
527 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE
, 0L);
528 HTTP_CURL_OPT(CURLOPT_TIMECONDITION
, 0L);
529 HTTP_CURL_OPT(CURLOPT_TIMEVALUE
, 0L);
530 HTTP_CURL_OPT(CURLOPT_TIMEOUT
, 0L);
531 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT
, 3);
532 HTTP_CURL_OPT(CURLOPT_SSLCERT
, NULL
);
533 HTTP_CURL_OPT(CURLOPT_SSLCERTTYPE
, NULL
);
534 HTTP_CURL_OPT(CURLOPT_SSLCERTPASSWD
, NULL
);
535 HTTP_CURL_OPT(CURLOPT_SSLKEY
, NULL
);
536 HTTP_CURL_OPT(CURLOPT_SSLKEYTYPE
, NULL
);
537 HTTP_CURL_OPT(CURLOPT_SSLKEYPASSWD
, NULL
);
538 HTTP_CURL_OPT(CURLOPT_SSLENGINE
, NULL
);
539 HTTP_CURL_OPT(CURLOPT_SSLVERSION
, 0L);
540 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYPEER
, 0L);
541 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYHOST
, 0L);
542 HTTP_CURL_OPT(CURLOPT_SSL_CIPHER_LIST
, NULL
);
543 #if HTTP_CURL_VERSION(7,19,0)
544 HTTP_CURL_OPT(CURLOPT_ISSUERCERT
, NULL
);
545 #if defined(HTTP_HAVE_OPENSSL)
546 HTTP_CURL_OPT(CURLOPT_CRLFILE
, NULL
);
549 #if HTTP_CURL_VERSION(7,19,1) && defined(HTTP_HAVE_OPENSSL)
550 HTTP_CURL_OPT(CURLOPT_CERTINFO
, NULL
);
552 #ifdef HTTP_CURL_CAINFO
553 HTTP_CURL_OPT(CURLOPT_CAINFO
, HTTP_CURL_CAINFO
);
555 HTTP_CURL_OPT(CURLOPT_CAINFO
, NULL
);
557 HTTP_CURL_OPT(CURLOPT_CAPATH
, NULL
);
558 HTTP_CURL_OPT(CURLOPT_RANDOM_FILE
, NULL
);
559 HTTP_CURL_OPT(CURLOPT_EGDSOCKET
, NULL
);
560 HTTP_CURL_OPT(CURLOPT_POSTFIELDS
, NULL
);
561 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE
, 0L);
562 HTTP_CURL_OPT(CURLOPT_HTTPPOST
, NULL
);
563 HTTP_CURL_OPT(CURLOPT_IOCTLDATA
, NULL
);
564 HTTP_CURL_OPT(CURLOPT_READDATA
, NULL
);
565 HTTP_CURL_OPT(CURLOPT_INFILESIZE
, 0L);
566 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION
, CURL_HTTP_VERSION_NONE
);
567 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST
, NULL
);
568 HTTP_CURL_OPT(CURLOPT_NOBODY
, 0L);
569 HTTP_CURL_OPT(CURLOPT_POST
, 0L);
570 HTTP_CURL_OPT(CURLOPT_UPLOAD
, 0L);
571 HTTP_CURL_OPT(CURLOPT_HTTPGET
, 1L);
576 PHP_HTTP_API
void _http_request_set_progress_callback(http_request
*request
, zval
*cb
)
578 if (request
->_progress_callback
) {
579 zval_ptr_dtor(&request
->_progress_callback
);
581 if ((request
->_progress_callback
= cb
)) {
583 HTTP_CURL_OPT(CURLOPT_NOPROGRESS
, 0);
584 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA
, request
);
585 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION
, http_curl_progress_callback
);
587 HTTP_CURL_OPT(CURLOPT_NOPROGRESS
, 1);
588 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA
, NULL
);
589 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION
, NULL
);
593 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
594 PHP_HTTP_API STATUS
_http_request_prepare(http_request
*request
, HashTable
*options
)
597 zend_bool range_req
= 0;
598 http_request_storage
*storage
;
600 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
602 HTTP_CHECK_CURL_INIT(request
->ch
, http_curl_init(request
), return FAILURE
);
604 if (!(storage
= http_request_storage_get(request
->ch
))) {
607 storage
->errorbuffer
[0] = '\0';
610 pefree(storage
->url
, 1);
612 storage
->url
= pestrdup(request
->url
, 1);
613 HTTP_CURL_OPT(CURLOPT_URL
, storage
->url
);
615 /* progress callback */
616 if ((zoption
= http_request_option(request
, options
, "onprogress", -1))) {
617 http_request_set_progress_callback(request
, zoption
);
621 if ((zoption
= http_request_option(request
, options
, "proxyhost", IS_STRING
))) {
622 HTTP_CURL_OPT(CURLOPT_PROXY
, Z_STRVAL_P(zoption
));
624 if ((zoption
= http_request_option(request
, options
, "proxytype", IS_LONG
))) {
625 HTTP_CURL_OPT(CURLOPT_PROXYTYPE
, Z_LVAL_P(zoption
));
628 if ((zoption
= http_request_option(request
, options
, "proxyport", IS_LONG
))) {
629 HTTP_CURL_OPT(CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
632 if ((zoption
= http_request_option(request
, options
, "proxyauth", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
633 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD
, Z_STRVAL_P(zoption
));
636 if ((zoption
= http_request_option(request
, options
, "proxyauthtype", IS_LONG
))) {
637 HTTP_CURL_OPT(CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
640 if ((zoption
= http_request_option(request
, options
, "proxytunnel", IS_BOOL
)) && Z_BVAL_P(zoption
)) {
641 HTTP_CURL_OPT(CURLOPT_HTTPPROXYTUNNEL
, 1L);
644 #if HTTP_CURL_VERSION(7,19,4)
645 if ((zoption
= http_request_option
, request
, options
, "noproxy", IS_STRING
))) {
646 HTTP_CURL_OPT(CURLOPT_NOPROXY
, Z_STRVAL_P(zoption
));
651 if ((zoption
= http_request_option(request
, options
, "dns_cache_timeout", IS_LONG
))) {
652 HTTP_CURL_OPT(CURLOPT_DNS_CACHE_TIMEOUT
, Z_LVAL_P(zoption
));
654 if ((zoption
= http_request_option(request
, options
, "ipresolve", IS_LONG
)) && Z_LVAL_P(zoption
)) {
655 HTTP_CURL_OPT(CURLOPT_IPRESOLVE
, Z_LVAL_P(zoption
));
659 if ((zoption
= http_request_option(request
, options
, "low_speed_limit", IS_LONG
))) {
660 HTTP_CURL_OPT(CURLOPT_LOW_SPEED_LIMIT
, Z_LVAL_P(zoption
));
662 if ((zoption
= http_request_option(request
, options
, "low_speed_time", IS_LONG
))) {
663 HTTP_CURL_OPT(CURLOPT_LOW_SPEED_TIME
, Z_LVAL_P(zoption
));
665 #if HTTP_CURL_VERSION(7,15,5)
667 if ((zoption = http_request_option(request, options, "max_send_speed", IS_LONG))) {
668 HTTP_CURL_OPT(CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
670 if ((zoption = http_request_option(request, options, "max_recv_speed", IS_LONG))) {
671 HTTP_CURL_OPT(CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
676 if ((zoption = http_request_option(request, options, "maxconnects", IS_LONG))) {
677 HTTP_CURL_OPT(CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption));
679 if ((zoption
= http_request_option(request
, options
, "fresh_connect", IS_BOOL
)) && Z_BVAL_P(zoption
)) {
680 HTTP_CURL_OPT(CURLOPT_FRESH_CONNECT
, 1L);
682 if ((zoption
= http_request_option(request
, options
, "forbid_reuse", IS_BOOL
)) && Z_BVAL_P(zoption
)) {
683 HTTP_CURL_OPT(CURLOPT_FORBID_REUSE
, 1L);
686 /* outgoing interface */
687 if ((zoption
= http_request_option(request
, options
, "interface", IS_STRING
))) {
688 HTTP_CURL_OPT(CURLOPT_INTERFACE
, Z_STRVAL_P(zoption
));
690 #if HTTP_CURL_VERSION(7,15,2)
691 if ((zoption
= http_request_option(request
, options
, "portrange", IS_ARRAY
))) {
694 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
695 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void *) &prs
)) {
696 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
697 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void *) &pre
)) {
698 zval
*prs_cpy
= http_zsep(IS_LONG
, *prs
);
699 zval
*pre_cpy
= http_zsep(IS_LONG
, *pre
);
701 if (Z_LVAL_P(prs_cpy
) && Z_LVAL_P(pre_cpy
)) {
702 HTTP_CURL_OPT(CURLOPT_LOCALPORT
, MIN(Z_LVAL_P(prs_cpy
), Z_LVAL_P(pre_cpy
)));
703 HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE
, labs(Z_LVAL_P(prs_cpy
)-Z_LVAL_P(pre_cpy
))+1L);
705 zval_ptr_dtor(&prs_cpy
);
706 zval_ptr_dtor(&pre_cpy
);
714 if ((zoption
= http_request_option(request
, options
, "port", IS_LONG
))) {
715 HTTP_CURL_OPT(CURLOPT_PORT
, Z_LVAL_P(zoption
));
718 /* RFC4007 zone_id */
719 #if HTTP_CURL_VERSION(7,19,0)
720 if ((zoption
= http_request_option(request
, options
, "address_scope", IS_LONG
))) {
721 HTTP_CURL_OPT(CURLOPT_ADDRESS_SCOPE
, Z_LVAL_P(zoption
));
726 if ((zoption
= http_request_option(request
, options
, "httpauth", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
727 HTTP_CURL_OPT(CURLOPT_USERPWD
, Z_STRVAL_P(zoption
));
729 if ((zoption
= http_request_option(request
, options
, "httpauthtype", IS_LONG
))) {
730 HTTP_CURL_OPT(CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
733 /* redirects, defaults to 0 */
734 if ((zoption
= http_request_option(request
, options
, "redirect", IS_LONG
))) {
735 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1L : 0L);
736 HTTP_CURL_OPT(CURLOPT_MAXREDIRS
, Z_LVAL_P(zoption
));
737 if ((zoption
= http_request_option(request
, options
, "unrestrictedauth", IS_BOOL
))) {
738 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
740 #if HTTP_CURL_VERSION(7,17,1)
741 if ((zoption
= http_request_option(request
, options
, "postredir", IS_BOOL
))) {
742 # if HTTP_CURL_VERSION(7,19,1)
743 HTTP_CURL_OPT(CURLOPT_POSTREDIR
, Z_BVAL_P(zoption
) ? 1L : 0L);
745 HTTP_CURL_OPT(CURLOPT_POST301
, Z_BVAL_P(zoption
) ? 1L : 0L);
751 /* retries, defaults to 0 */
752 if ((zoption
= http_request_option(request
, options
, "retrycount", IS_LONG
))) {
753 request
->_retry
.count
= Z_LVAL_P(zoption
);
754 if ((zoption
= http_request_option(request
, options
, "retrydelay", IS_DOUBLE
))) {
755 request
->_retry
.delay
= Z_DVAL_P(zoption
);
757 request
->_retry
.delay
= 0;
760 request
->_retry
.count
= 0;
764 if ((zoption
= http_request_option(request
, options
, "referer", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
765 HTTP_CURL_OPT(CURLOPT_REFERER
, Z_STRVAL_P(zoption
));
768 /* useragent, default "PECL::HTTP/version (PHP/version)" */
769 if ((zoption
= http_request_option(request
, options
, "useragent", IS_STRING
))) {
770 /* allow to send no user agent, not even default one */
771 if (Z_STRLEN_P(zoption
)) {
772 HTTP_CURL_OPT(CURLOPT_USERAGENT
, Z_STRVAL_P(zoption
));
774 HTTP_CURL_OPT(CURLOPT_USERAGENT
, NULL
);
779 if ((zoption
= http_request_option(request
, options
, "resume", IS_LONG
)) && (Z_LVAL_P(zoption
) > 0)) {
781 HTTP_CURL_OPT(CURLOPT_RESUME_FROM
, Z_LVAL_P(zoption
));
783 /* or range of kind array(array(0,499), array(100,1499)) */
784 else if ((zoption
= http_request_option(request
, options
, "range", IS_ARRAY
)) && zend_hash_num_elements(Z_ARRVAL_P(zoption
))) {
785 HashPosition pos1
, pos2
;
786 zval
**rr
, **rb
, **re
;
790 FOREACH_VAL(pos1
, zoption
, rr
) {
791 if (Z_TYPE_PP(rr
) == IS_ARRAY
) {
792 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr
), &pos2
);
793 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr
), (void *) &rb
, &pos2
)) {
794 zend_hash_move_forward_ex(Z_ARRVAL_PP(rr
), &pos2
);
795 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr
), (void *) &re
, &pos2
)) {
796 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))) &&
797 ((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)))) {
798 zval
*rbl
= http_zsep(IS_LONG
, *rb
);
799 zval
*rel
= http_zsep(IS_LONG
, *re
);
801 if ((Z_LVAL_P(rbl
) >= 0) && (Z_LVAL_P(rel
) >= 0)) {
802 phpstr_appendf(&rs
, "%ld-%ld,", Z_LVAL_P(rbl
), Z_LVAL_P(rel
));
812 if (PHPSTR_LEN(&rs
)) {
815 /* ditch last comma */
816 PHPSTR_VAL(&rs
)[PHPSTR_LEN(&rs
)-- -1] = '\0';
818 MAKE_STD_ZVAL(cached_range
);
819 ZVAL_STRINGL(cached_range
, PHPSTR_VAL(&rs
), PHPSTR_LEN(&rs
), 0);
820 HTTP_CURL_OPT(CURLOPT_RANGE
, Z_STRVAL_P(http_request_option_cache(request
, "range", cached_range
)));
821 zval_ptr_dtor(&cached_range
);
825 /* additional headers, array('name' => 'value') */
826 if (request
->_cache
.headers
) {
827 curl_slist_free_all(request
->_cache
.headers
);
828 request
->_cache
.headers
= NULL
;
830 if ((zoption
= http_request_option(request
, options
, "headers", IS_ARRAY
))) {
831 HashKey header_key
= initHashKey(0);
835 FOREACH_KEYVAL(pos
, zoption
, header_key
, header_val
) {
836 if (header_key
.type
== HASH_KEY_IS_STRING
) {
838 zval
*header_cpy
= http_zsep(IS_STRING
, *header_val
);
840 if (!strcasecmp(header_key
.str
, "range")) {
843 snprintf(header
, sizeof(header
), "%s: %s", header_key
.str
, Z_STRVAL_P(header_cpy
));
844 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, header
);
845 zval_ptr_dtor(&header_cpy
);
850 if ((zoption
= http_request_option(request
, options
, "etag", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
851 char match_header
[1024], *quoted_etag
= NULL
;
853 if ((Z_STRVAL_P(zoption
)[0] != '"') || (Z_STRVAL_P(zoption
)[Z_STRLEN_P(zoption
)-1] != '"')) {
854 spprintf("ed_etag
, 0, "\"%s\"", Z_STRVAL_P(zoption
));
856 snprintf(match_header
, sizeof(match_header
), "%s: %s", range_req
?"If-Match":"If-None-Match", quoted_etag
?quoted_etag
:Z_STRVAL_P(zoption
));
857 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, match_header
);
858 STR_FREE(quoted_etag
);
861 if ((zoption
= http_request_option(request
, options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
862 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
864 HTTP_CURL_OPT(CURLOPT_HTTPHEADER
, request
->_cache
.headers
);
867 if ((zoption
= http_request_option(request
, options
, "lastmodified", IS_LONG
))) {
868 if (Z_LVAL_P(zoption
)) {
869 if (Z_LVAL_P(zoption
) > 0) {
870 HTTP_CURL_OPT(CURLOPT_TIMEVALUE
, Z_LVAL_P(zoption
));
872 HTTP_CURL_OPT(CURLOPT_TIMEVALUE
, (long) HTTP_G
->request
.time
+ Z_LVAL_P(zoption
));
874 HTTP_CURL_OPT(CURLOPT_TIMECONDITION
, (long) (range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
));
876 HTTP_CURL_OPT(CURLOPT_TIMECONDITION
, CURL_TIMECOND_NONE
);
880 /* cookies, array('name' => 'value') */
881 if ((zoption
= http_request_option(request
, options
, "cookies", IS_ARRAY
))) {
882 phpstr_dtor(&request
->_cache
.cookies
);
883 if (zend_hash_num_elements(Z_ARRVAL_P(zoption
))) {
884 zval
*urlenc_cookies
= NULL
;
885 /* check whether cookies should not be urlencoded; default is to urlencode them */
886 if ((!(urlenc_cookies
= http_request_option(request
, options
, "encodecookies", IS_BOOL
))) || Z_BVAL_P(urlenc_cookies
)) {
887 if (SUCCESS
== http_urlencode_hash_recursive(HASH_OF(zoption
), &request
->_cache
.cookies
, "; ", lenof("; "), NULL
, 0)) {
888 phpstr_fix(&request
->_cache
.cookies
);
889 HTTP_CURL_OPT(CURLOPT_COOKIE
, request
->_cache
.cookies
.data
);
893 HashKey cookie_key
= initHashKey(0);
896 FOREACH_KEYVAL(pos
, zoption
, cookie_key
, cookie_val
) {
897 if (cookie_key
.type
== HASH_KEY_IS_STRING
) {
898 zval
*val
= http_zsep(IS_STRING
, *cookie_val
);
899 phpstr_appendf(&request
->_cache
.cookies
, "%s=%s; ", cookie_key
.str
, Z_STRVAL_P(val
));
904 phpstr_fix(&request
->_cache
.cookies
);
905 if (PHPSTR_LEN(&request
->_cache
.cookies
)) {
906 HTTP_CURL_OPT(CURLOPT_COOKIE
, PHPSTR_VAL(&request
->_cache
.cookies
));
912 /* don't load session cookies from cookiestore */
913 if ((zoption
= http_request_option(request
, options
, "cookiesession", IS_BOOL
)) && Z_BVAL_P(zoption
)) {
914 HTTP_CURL_OPT(CURLOPT_COOKIESESSION
, 1L);
917 /* cookiestore, read initial cookies from that file and store cookies back into that file */
918 if ((zoption
= http_request_option(request
, options
, "cookiestore", IS_STRING
))) {
919 if (Z_STRLEN_P(zoption
)) {
920 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption
), return FAILURE
);
922 if (storage
->cookiestore
) {
923 pefree(storage
->cookiestore
, 1);
925 storage
->cookiestore
= pestrndup(Z_STRVAL_P(zoption
), Z_STRLEN_P(zoption
), 1);
926 HTTP_CURL_OPT(CURLOPT_COOKIEFILE
, storage
->cookiestore
);
927 HTTP_CURL_OPT(CURLOPT_COOKIEJAR
, storage
->cookiestore
);
931 if ((zoption
= http_request_option(request
, options
, "maxfilesize", IS_LONG
))) {
932 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE
, Z_LVAL_P(zoption
));
936 if ((zoption
= http_request_option(request
, options
, "protocol", IS_LONG
))) {
937 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION
, Z_LVAL_P(zoption
));
940 #if HTTP_CURL_VERSION(7,16,2)
941 /* timeout, defaults to 0 */
942 if ((zoption
= http_request_option(request
, options
, "timeout", IS_DOUBLE
))) {
943 HTTP_CURL_OPT(CURLOPT_TIMEOUT_MS
, (long)(Z_DVAL_P(zoption
)*1000));
945 /* connecttimeout, defaults to 0 */
946 if ((zoption
= http_request_option(request
, options
, "connecttimeout", IS_DOUBLE
))) {
947 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT_MS
, (long)(Z_DVAL_P(zoption
)*1000));
950 /* timeout, defaults to 0 */
951 if ((zoption
= http_request_option(request
, options
, "timeout", IS_LONG
))) {
952 HTTP_CURL_OPT(CURLOPT_TIMEOUT
, Z_LVAL_P(zoption
));
954 /* connecttimeout, defaults to 0 */
955 if ((zoption
= http_request_option(request
, options
, "connecttimeout", IS_LONG
))) {
956 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
961 if ((zoption
= http_request_option(request
, options
, "ssl", IS_ARRAY
))) {
962 HashKey key
= initHashKey(0);
966 FOREACH_KEYVAL(pos
, zoption
, key
, param
) {
967 if (key
.type
== HASH_KEY_IS_STRING
) {
968 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT
, 0, 1);
969 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE
, 0, 0);
970 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD
, 0, 0);
972 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY
, 0, 0);
973 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE
, 0, 0);
974 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD
, 0, 0);
976 HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE
, 0, 0);
977 HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION
, 0);
979 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER
, 1);
980 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST
, 1);
981 HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST
, 1, 0);
983 HTTP_CURL_OPT_STRING(CURLOPT_CAINFO
, -3, 1);
984 HTTP_CURL_OPT_STRING(CURLOPT_CAPATH
, -3, 1);
985 HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE
, -3, 1);
986 HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET
, -3, 1);
987 #if HTTP_CURL_VERSION(7,19,0)
988 HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT
, -3, 1);
989 #if defined(HTTP_HAVE_OPENSSL)
990 HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE
, -3, 1);
993 #if HTTP_CURL_VERSION(7,19,1) && defined(HTTP_HAVE_OPENSSL)
994 HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO
, -3);
1000 /* request method */
1001 switch (request
->meth
) {
1003 HTTP_CURL_OPT(CURLOPT_HTTPGET
, 1L);
1007 HTTP_CURL_OPT(CURLOPT_NOBODY
, 1L);
1011 HTTP_CURL_OPT(CURLOPT_POST
, 1L);
1015 HTTP_CURL_OPT(CURLOPT_UPLOAD
, 1L);
1019 if (http_request_method_exists(0, request
->meth
, NULL
)) {
1020 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST
, http_request_method_name(request
->meth
));
1022 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d (%s)", request
->meth
, request
->url
);
1028 /* attach request body */
1029 if (request
->body
&& (request
->meth
!= HTTP_GET
) && (request
->meth
!= HTTP_HEAD
) && (request
->meth
!= HTTP_OPTIONS
)) {
1030 switch (request
->body
->type
) {
1031 case HTTP_REQUEST_BODY_EMPTY
:
1035 case HTTP_REQUEST_BODY_CURLPOST
:
1036 HTTP_CURL_OPT(CURLOPT_HTTPPOST
, (struct curl_httppost
*) request
->body
->data
);
1039 case HTTP_REQUEST_BODY_CSTRING
:
1040 if (request
->meth
!= HTTP_PUT
) {
1041 HTTP_CURL_OPT(CURLOPT_POSTFIELDS
, request
->body
->data
);
1042 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE
, request
->body
->size
);
1045 /* fallthrough, PUT/UPLOAD _needs_ READDATA */
1046 case HTTP_REQUEST_BODY_UPLOADFILE
:
1047 HTTP_CURL_OPT(CURLOPT_IOCTLDATA
, request
);
1048 HTTP_CURL_OPT(CURLOPT_READDATA
, request
);
1049 HTTP_CURL_OPT(CURLOPT_INFILESIZE
, request
->body
->size
);
1053 /* shouldn't ever happen */
1054 http_error_ex(HE_ERROR
, 0, "Unknown request body type: %d (%s)", request
->body
->type
, request
->url
);
1063 /* {{{ void http_request_exec(http_request *) */
1064 PHP_HTTP_API
void _http_request_exec(http_request
*request
)
1068 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
1071 if (CURLE_OK
!= (result
= curl_easy_perform(request
->ch
))) {
1072 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "%s; %s (%s)", curl_easy_strerror(result
), http_request_storage_get(request
->ch
)->errorbuffer
, request
->url
);
1074 if (request
->_retry
.count
> tries
++) {
1076 case CURLE_COULDNT_RESOLVE_PROXY
:
1077 case CURLE_COULDNT_RESOLVE_HOST
:
1078 case CURLE_COULDNT_CONNECT
:
1079 case CURLE_WRITE_ERROR
:
1080 case CURLE_READ_ERROR
:
1081 case CURLE_OPERATION_TIMEDOUT
:
1082 case CURLE_SSL_CONNECT_ERROR
:
1083 case CURLE_GOT_NOTHING
:
1084 case CURLE_SSL_ENGINE_SETFAILED
:
1085 case CURLE_SEND_ERROR
:
1086 case CURLE_RECV_ERROR
:
1087 case CURLE_SSL_ENGINE_INITFAILED
:
1088 case CURLE_LOGIN_DENIED
:
1089 if (request
->_retry
.delay
>= HTTP_DIFFSEC
) {
1090 http_sleep(request
->_retry
.delay
);
1101 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
1102 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
1104 http_request
*request
= (http_request
*) ctx
;
1105 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
1107 if (request
->body
) {
1108 switch (request
->body
->type
) {
1109 case HTTP_REQUEST_BODY_CSTRING
:
1111 size_t out
= MIN(len
* n
, request
->body
->size
- request
->body
->priv
);
1114 memcpy(data
, ((char *) request
->body
->data
) + request
->body
->priv
, out
);
1115 request
->body
->priv
+= out
;
1121 case HTTP_REQUEST_BODY_UPLOADFILE
:
1122 return php_stream_read((php_stream
*) request
->body
->data
, data
, len
* n
);
1129 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
1130 static int http_curl_progress_callback(void *ctx
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
1132 zval
*param
, retval
;
1133 http_request
*request
= (http_request
*) ctx
;
1134 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
1136 INIT_PZVAL(&retval
);
1139 MAKE_STD_ZVAL(param
);
1141 add_assoc_double(param
, "dltotal", dltotal
);
1142 add_assoc_double(param
, "dlnow", dlnow
);
1143 add_assoc_double(param
, "ultotal", ultotal
);
1144 add_assoc_double(param
, "ulnow", ulnow
);
1146 with_error_handling(EH_NORMAL
, NULL
) {
1147 request
->_in_progress_cb
= 1;
1148 call_user_function(EG(function_table
), NULL
, request
->_progress_callback
, &retval
, 1, ¶m TSRMLS_CC
);
1149 request
->_in_progress_cb
= 0;
1150 } end_error_handling();
1152 zval_ptr_dtor(¶m
);
1159 /* {{{ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
1160 static curlioerr
http_curl_ioctl_callback(CURL
*ch
, curliocmd cmd
, void *ctx
)
1162 http_request
*request
= (http_request
*) ctx
;
1163 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
1165 if (cmd
!= CURLIOCMD_RESTARTREAD
) {
1166 return CURLIOE_UNKNOWNCMD
;
1169 if (request
->body
) {
1170 switch (request
->body
->type
) {
1171 case HTTP_REQUEST_BODY_CSTRING
:
1172 request
->body
->priv
= 0;
1176 case HTTP_REQUEST_BODY_UPLOADFILE
:
1177 if (SUCCESS
== php_stream_rewind((php_stream
*) request
->body
->data
)) {
1184 return CURLIOE_FAILRESTART
;
1188 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
1189 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
1191 http_request
*request
= (http_request
*) ctx
;
1193 #define EMPTY_HEADER(d, l) (!l || (l == 1 && d[0] == '\n') || (l == 2 && d[0] == '\r' && d[1] == '\n'))
1195 case CURLINFO_DATA_IN
:
1196 if (request
->conv
.last_type
== CURLINFO_HEADER_IN
) {
1197 phpstr_appends(&request
->conv
.response
, HTTP_CRLF
);
1199 phpstr_append(&request
->conv
.response
, data
, length
);
1201 case CURLINFO_HEADER_IN
:
1202 if (!EMPTY_HEADER(data
, length
)) {
1203 phpstr_append(&request
->conv
.response
, data
, length
);
1206 case CURLINFO_DATA_OUT
:
1207 case CURLINFO_HEADER_OUT
:
1208 phpstr_append(&request
->conv
.request
, data
, length
);
1216 const char _sym
[] = "><><><";
1218 for (fprintf(stderr
, "%c ", _sym
[type
-1]); length
--; data
++) {
1219 fprintf(stderr
, HTTP_IS_CTYPE(print
, *data
)?"%c":"\\x%02X", (int) *data
);
1220 if (*data
== '\n' && length
) {
1221 fprintf(stderr
, "\n%c ", _sym
[type
-1]);
1224 fprintf(stderr
, "\n");
1226 fprintf(stderr
, "# %s", data
);
1232 request
->conv
.last_type
= type
;
1238 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
1239 static inline zval
*_http_request_option_ex(http_request
*r
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
1243 ulong h
= zend_hash_func(key
, keylen
);
1245 if (SUCCESS
== zend_hash_quick_find(options
, key
, keylen
, h
, (void *) &zoption
)) {
1246 zval
*option
, *cached
;
1248 option
= http_zsep(type
, *zoption
);
1249 cached
= http_request_option_cache_ex(r
, key
, keylen
, h
, option
);
1251 zval_ptr_dtor(&option
);
1260 /* {{{ static inline zval *http_request_option_cache(http_request *, char *key, zval *) */
1261 static inline zval
*_http_request_option_cache_ex(http_request
*r
, char *key
, size_t keylen
, ulong h
, zval
*opt TSRMLS_DC
)
1266 zend_hash_quick_update(&r
->_cache
.options
, key
, keylen
, h
, &opt
, sizeof(zval
*), NULL
);
1268 zend_hash_update(&r
->_cache
.options
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
1275 /* {{{ static inline int http_request_cookies_enabled(http_request *) */
1276 static inline int _http_request_cookies_enabled(http_request
*request
) {
1277 http_request_storage
*st
;
1279 if (request
->ch
&& (st
= http_request_storage_get(request
->ch
)) && st
->cookiestore
) {
1280 /* cookies are enabled */
1287 #endif /* HTTP_HAVE_CURL */
1294 * vim600: noet sw=4 ts=4 fdm=marker
1295 * vim<600: noet sw=4 ts=4