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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #define HTTP_WANT_CURL
24 #include "php_http_api.h"
25 #include "php_http_request_api.h"
26 #include "php_http_url_api.h"
29 # include "php_http_request_object.h"
32 /* {{{ cruft for thread safe SSL crypto locks */
33 #if defined(ZTS) && defined(HTTP_HAVE_SSL)
35 # define HTTP_NEED_SSL_TSL
36 # define HTTP_NEED_OPENSSL_TSL
37 # include <openssl/crypto.h>
38 # else /* !PHP_WIN32 */
39 # if defined(HTTP_HAVE_OPENSSL)
40 # if defined(HAVE_OPENSSL_CRYPTO_H)
41 # define HTTP_NEED_SSL_TSL
42 # define HTTP_NEED_OPENSSL_TSL
43 # include <openssl/crypto.h>
46 "libcurl was compiled with OpenSSL support, but configure could not find " \
47 "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
48 "cause random crashes on SSL requests"
50 # elif defined(HTTP_HAVE_GNUTLS)
51 # if defined(HAVE_GCRYPT_H)
52 # define HTTP_NEED_SSL_TSL
53 # define HTTP_NEED_GNUTLS_TSL
57 "libcurl was compiled with GnuTLS support, but configure could not find " \
58 "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
59 "cause random crashes on SSL requests"
63 "libcurl was compiled with SSL support, but configure could not determine which" \
64 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
65 "cause random crashes on SSL requests"
66 # endif /* HTTP_HAVE_OPENSSL || HTTP_HAVE_GNUTLS */
67 # endif /* PHP_WIN32 */
68 #endif /* ZTS && HTTP_HAVE_SSL */
70 #ifdef HTTP_NEED_SSL_TSL
71 static inline void http_ssl_init(void);
72 static inline void http_ssl_cleanup(void);
77 PHP_MINIT_FUNCTION(http_request
)
79 #ifdef HTTP_NEED_SSL_TSL
83 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
87 HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
);
88 HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
);
89 HTTP_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
);
90 HTTP_LONG_CONSTANT("HTTP_AUTH_ANY", CURLAUTH_ANY
);
97 PHP_MSHUTDOWN_FUNCTION(http_request
)
99 curl_global_cleanup();
100 #ifdef HTTP_NEED_SSL_TSL
108 #ifndef HAVE_CURL_EASY_STRERROR
109 # define curl_easy_strerror(dummy) "unkown error"
112 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
113 #define HTTP_CURL_INFO_EX(I, X) \
114 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
116 case CURLINFO_STRING: \
119 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &c)) { \
120 char *key = estrndup(#X, lenof(#X)); \
121 add_assoc_string(&array, pretty_key(key, lenof(#X), 0, 0), c ? c : "", 1); \
127 case CURLINFO_DOUBLE: \
130 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &d)) { \
131 char *key = estrndup(#X, lenof(#X)); \
132 add_assoc_double(&array, pretty_key(key, lenof(#X), 0, 0), d); \
138 case CURLINFO_LONG: \
141 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
142 char *key = estrndup(#X, lenof(#X)); \
143 add_assoc_long(&array, pretty_key(key, lenof(#X), 0, 0), l); \
149 case CURLINFO_SLIST: \
151 struct curl_slist *l, *p; \
152 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
154 char *key = estrndup(#X, lenof(#X)); \
155 MAKE_STD_ZVAL(subarray); \
156 array_init(subarray); \
157 for (p = l; p; p = p->next) { \
158 add_next_index_string(subarray, p->data, 1); \
160 add_assoc_zval(&array, pretty_key(key, lenof(#X), 0, 0), subarray); \
161 curl_slist_free_all(l); \
167 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(request->ch, CURLOPT_##OPTION, (p))
168 #define HTTP_CURL_OPT_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, keyname, obdc)
169 #define HTTP_CURL_OPT_SSL_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname, obdc)
170 #define HTTP_CURL_OPT_SSL_STRING_(keyname,obdc ) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname, obdc)
171 #define HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
172 if (!strcasecmp(key, #keyname)) { \
173 zval *copy = http_request_option_cache(request, #keyname, zval_copy(IS_STRING, *param)); \
175 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(copy), return FAILURE); \
177 HTTP_CURL_OPT(optname, Z_STRVAL_P(copy)); \
181 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
182 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
183 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
184 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
185 if (!strcasecmp(key, #keyname)) { \
186 zval *copy = http_request_option_cache(request, #keyname, zval_copy(IS_LONG, *param)); \
187 HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \
193 /* {{{ forward declarations */
194 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
195 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
196 static inline zval
*_http_request_option_ex(http_request
*request
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
197 #define http_request_option_cache(r, k, z) _http_request_option_cache_ex((r), (k), sizeof(k), 0, (z) TSRMLS_CC)
198 #define http_request_option_cache_ex(r, k, kl, h, z) _http_request_option_cache_ex((r), (k), (kl), (h), (z) TSRMLS_CC)
199 static inline zval
*_http_request_option_cache_ex(http_request
*r
, char *key
, size_t keylen
, ulong h
, zval
*opt TSRMLS_DC
);
201 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
202 static int http_curl_progress_callback(void *, double, double, double, double);
203 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
204 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
205 static curlioerr
http_curl_ioctl_callback(CURL
*, curliocmd
, void *);
208 /* {{{ http_request *http_request_init(http_request *) */
209 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
)
216 r
= emalloc_rel(sizeof(http_request
));
218 memset(r
, 0, sizeof(http_request
));
221 r
->url
= (url
) ? http_absolute_url(url
) : NULL
;
222 r
->meth
= (meth
> 0) ? meth
: HTTP_GET
;
224 phpstr_init(&r
->conv
.request
);
225 phpstr_init_ex(&r
->conv
.response
, HTTP_CURLBUF_SIZE
, 0);
226 phpstr_init(&r
->_cache
.cookies
);
227 zend_hash_init(&r
->_cache
.options
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
229 TSRMLS_SET_CTX(r
->tsrm_ls
);
235 /* {{{ void http_request_dtor(http_request *) */
236 PHP_HTTP_API
void _http_request_dtor(http_request
*request
)
238 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
241 /* avoid nasty segfaults with already cleaned up callbacks */
242 curl_easy_setopt(request
->ch
, CURLOPT_NOPROGRESS
, 1);
243 curl_easy_setopt(request
->ch
, CURLOPT_PROGRESSFUNCTION
, NULL
);
244 curl_easy_setopt(request
->ch
, CURLOPT_VERBOSE
, 0);
245 curl_easy_setopt(request
->ch
, CURLOPT_DEBUGFUNCTION
, NULL
);
246 curl_easy_cleanup(request
->ch
);
250 http_request_reset(request
);
252 phpstr_dtor(&request
->_cache
.cookies
);
253 zend_hash_destroy(&request
->_cache
.options
);
254 if (request
->_cache
.headers
) {
255 curl_slist_free_all(request
->_cache
.headers
);
256 request
->_cache
.headers
= NULL
;
258 if (request
->_progress_callback
) {
259 zval_ptr_dtor(&request
->_progress_callback
);
260 request
->_progress_callback
= NULL
;
265 /* {{{ void http_request_free(http_request **) */
266 PHP_HTTP_API
void _http_request_free(http_request
**request
)
269 TSRMLS_FETCH_FROM_CTX((*request
)->tsrm_ls
);
270 http_request_body_free(&(*request
)->body
);
271 http_request_dtor(*request
);
278 /* {{{ void http_request_reset(http_request *) */
279 PHP_HTTP_API
void _http_request_reset(http_request
*request
)
281 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
282 STR_SET(request
->url
, NULL
);
283 request
->conv
.last_type
= 0;
284 phpstr_dtor(&request
->conv
.request
);
285 phpstr_dtor(&request
->conv
.response
);
286 http_request_body_dtor(request
->body
);
290 /* {{{ void http_request_defaults(http_request *) */
291 PHP_HTTP_API
void _http_request_defaults(http_request
*request
)
294 #ifdef HAVE_CURL_EASY_RESET
295 curl_easy_reset(request
->ch
);
298 HTTP_CURL_OPT(NOSIGNAL
, 1);
300 HTTP_CURL_OPT(PRIVATE
, request
);
301 HTTP_CURL_OPT(ERRORBUFFER
, request
->_error
);
302 HTTP_CURL_OPT(HEADER
, 0);
303 HTTP_CURL_OPT(FILETIME
, 1);
304 HTTP_CURL_OPT(AUTOREFERER
, 1);
305 HTTP_CURL_OPT(VERBOSE
, 1);
306 HTTP_CURL_OPT(HEADERFUNCTION
, NULL
);
307 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_raw_callback
);
308 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
309 HTTP_CURL_OPT(IOCTLFUNCTION
, http_curl_ioctl_callback
);
310 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_dummy_callback
);
311 HTTP_CURL_OPT(PROGRESSFUNCTION
, NULL
);
312 HTTP_CURL_OPT(URL
, NULL
);
313 HTTP_CURL_OPT(NOPROGRESS
, 1);
314 HTTP_CURL_OPT(PROXY
, NULL
);
315 HTTP_CURL_OPT(PROXYPORT
, 0);
316 HTTP_CURL_OPT(PROXYUSERPWD
, NULL
);
317 HTTP_CURL_OPT(PROXYAUTH
, 0);
318 HTTP_CURL_OPT(INTERFACE
, NULL
);
319 HTTP_CURL_OPT(PORT
, 0);
320 HTTP_CURL_OPT(USERPWD
, NULL
);
321 HTTP_CURL_OPT(HTTPAUTH
, 0);
322 HTTP_CURL_OPT(ENCODING
, NULL
);
323 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
324 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, 0);
325 HTTP_CURL_OPT(REFERER
, NULL
);
326 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
327 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
328 HTTP_CURL_OPT(COOKIE
, NULL
);
329 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
330 HTTP_CURL_OPT(COOKIEJAR
, NULL
);
331 HTTP_CURL_OPT(RESUME_FROM
, 0);
332 HTTP_CURL_OPT(MAXFILESIZE
, 0);
333 HTTP_CURL_OPT(TIMECONDITION
, 0);
334 HTTP_CURL_OPT(TIMEVALUE
, 0);
335 HTTP_CURL_OPT(TIMEOUT
, 0);
336 HTTP_CURL_OPT(CONNECTTIMEOUT
, 3);
337 HTTP_CURL_OPT(SSLCERT
, NULL
);
338 HTTP_CURL_OPT(SSLCERTTYPE
, NULL
);
339 HTTP_CURL_OPT(SSLCERTPASSWD
, NULL
);
340 HTTP_CURL_OPT(SSLKEY
, NULL
);
341 HTTP_CURL_OPT(SSLKEYTYPE
, NULL
);
342 HTTP_CURL_OPT(SSLKEYPASSWD
, NULL
);
343 HTTP_CURL_OPT(SSLENGINE
, NULL
);
344 HTTP_CURL_OPT(SSLVERSION
, 0);
345 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
346 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
347 HTTP_CURL_OPT(SSL_CIPHER_LIST
, NULL
);
348 HTTP_CURL_OPT(CAINFO
, NULL
);
349 HTTP_CURL_OPT(CAPATH
, NULL
);
350 HTTP_CURL_OPT(RANDOM_FILE
, NULL
);
351 HTTP_CURL_OPT(EGDSOCKET
, NULL
);
352 HTTP_CURL_OPT(POSTFIELDS
, NULL
);
353 HTTP_CURL_OPT(POSTFIELDSIZE
, 0);
354 HTTP_CURL_OPT(HTTPPOST
, NULL
);
355 HTTP_CURL_OPT(IOCTLDATA
, NULL
);
356 HTTP_CURL_OPT(READDATA
, NULL
);
357 HTTP_CURL_OPT(INFILESIZE
, 0);
362 PHP_HTTP_API
void _http_request_set_progress_callback(http_request
*request
, zval
*cb
)
364 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
366 if (request
->_progress_callback
) {
367 zval_ptr_dtor(&request
->_progress_callback
);
372 request
->_progress_callback
= cb
;
375 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
376 PHP_HTTP_API STATUS
_http_request_prepare(http_request
*request
, HashTable
*options
)
379 zend_bool range_req
= 0;
381 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
383 HTTP_CHECK_CURL_INIT(request
->ch
, curl_easy_init(), return FAILURE
);
385 http_request_defaults(request
);
388 HTTP_CURL_OPT(DEBUGDATA
, request
);
389 HTTP_CURL_OPT(URL
, request
->url
);
391 /* progress callback */
392 if ((zoption
= http_request_option(request
, options
, "onprogress", 0))) {
393 HTTP_CURL_OPT(NOPROGRESS
, 0);
394 HTTP_CURL_OPT(PROGRESSDATA
, request
);
395 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
396 http_request_set_progress_callback(request
, zoption
);
400 if ((zoption
= http_request_option(request
, options
, "proxyhost", IS_STRING
))) {
401 HTTP_CURL_OPT(PROXY
, Z_STRVAL_P(zoption
));
403 if ((zoption
= http_request_option(request
, options
, "proxyport", IS_LONG
))) {
404 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
407 if ((zoption
= http_request_option(request
, options
, "proxyauth", IS_STRING
))) {
408 HTTP_CURL_OPT(PROXYUSERPWD
, Z_STRVAL_P(zoption
));
411 if ((zoption
= http_request_option(request
, options
, "proxyauthtype", IS_LONG
))) {
412 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
416 /* outgoing interface */
417 if ((zoption
= http_request_option(request
, options
, "interface", IS_STRING
))) {
418 HTTP_CURL_OPT(INTERFACE
, Z_STRVAL_P(zoption
));
422 if ((zoption
= http_request_option(request
, options
, "port", IS_LONG
))) {
423 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
427 if ((zoption
= http_request_option(request
, options
, "httpauth", IS_STRING
))) {
428 HTTP_CURL_OPT(USERPWD
, Z_STRVAL_P(zoption
));
430 if ((zoption
= http_request_option(request
, options
, "httpauthtype", IS_LONG
))) {
431 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
434 /* redirects, defaults to 0 */
435 if ((zoption
= http_request_option(request
, options
, "redirect", IS_LONG
))) {
436 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
437 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
438 if ((zoption
= http_request_option(request
, options
, "unrestrictedauth", IS_BOOL
))) {
439 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
444 if ((zoption
= http_request_option(request
, options
, "referer", IS_STRING
))) {
445 HTTP_CURL_OPT(REFERER
, Z_STRVAL_P(zoption
));
448 /* useragent, default "PECL::HTTP/version (PHP/version)" */
449 if ((zoption
= http_request_option(request
, options
, "useragent", IS_STRING
))) {
450 HTTP_CURL_OPT(USERAGENT
, Z_STRVAL_P(zoption
));
453 /* additional headers, array('name' => 'value') */
454 if (request
->_cache
.headers
) {
455 curl_slist_free_all(request
->_cache
.headers
);
456 request
->_cache
.headers
= NULL
;
458 if ((zoption
= http_request_option(request
, options
, "headers", IS_ARRAY
))) {
463 FOREACH_KEY(pos
, zoption
, header_key
, header_idx
) {
466 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption
), (void **) &header_val
, &pos
)) {
467 char header
[1024] = {0};
469 ZVAL_ADDREF(*header_val
);
470 convert_to_string_ex(header_val
);
471 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
472 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, header
);
473 zval_ptr_dtor(header_val
);
481 if ((zoption
= http_request_option(request
, options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
482 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5,*;q=0.1");
484 HTTP_CURL_OPT(HTTPHEADER
, request
->_cache
.headers
);
486 /* cookies, array('name' => 'value') */
487 if ((zoption
= http_request_option(request
, options
, "cookies", IS_ARRAY
))) {
488 phpstr_dtor(&request
->_cache
.cookies
);
489 if (SUCCESS
== http_urlencode_hash_recursive(HASH_OF(zoption
), &request
->_cache
.cookies
, "; ", sizeof("; ")-1, NULL
, 0)) {
490 phpstr_fix(&request
->_cache
.cookies
);
491 HTTP_CURL_OPT(COOKIE
, request
->_cache
.cookies
.data
);
495 #if LIBCURL_VERSION_NUM >= 0x070f01
497 if ((zoption
= http_request_option(request
, options
, "resetcookies", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
498 HTTP_CURL_OPT(COOKIELIST
, "ALL");
502 /* session cookies */
503 if ((zoption
= http_request_option(request
, options
, "cookiesession", IS_BOOL
))) {
504 if (Z_LVAL_P(zoption
)) {
505 /* accept cookies for this session */
506 HTTP_CURL_OPT(COOKIEFILE
, "");
508 /* reset session cookies */
509 HTTP_CURL_OPT(COOKIESESSION
, 1);
513 /* cookiestore, read initial cookies from that file and store cookies back into that file */
514 if ((zoption
= http_request_option(request
, options
, "cookiestore", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
515 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption
), return FAILURE
);
516 HTTP_CURL_OPT(COOKIEFILE
, Z_STRVAL_P(zoption
));
517 HTTP_CURL_OPT(COOKIEJAR
, Z_STRVAL_P(zoption
));
521 if ((zoption
= http_request_option(request
, options
, "resume", IS_LONG
)) && (Z_LVAL_P(zoption
) != 0)) {
523 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
527 if ((zoption
= http_request_option(request
, options
, "maxfilesize", IS_LONG
))) {
528 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
532 if ((zoption
= http_request_option(request
, options
, "lastmodified", IS_LONG
))) {
533 if (Z_LVAL_P(zoption
)) {
534 if (Z_LVAL_P(zoption
) > 0) {
535 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
537 HTTP_CURL_OPT(TIMEVALUE
, time(NULL
) + Z_LVAL_P(zoption
));
539 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
541 HTTP_CURL_OPT(TIMECONDITION
, CURL_TIMECOND_NONE
);
545 /* timeout, defaults to 0 */
546 if ((zoption
= http_request_option(request
, options
, "timeout", IS_LONG
))) {
547 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
550 /* connecttimeout, defaults to 3 */
551 if ((zoption
= http_request_option(request
, options
, "connecttimeout", IS_LONG
))) {
552 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
556 if ((zoption
= http_request_option(request
, options
, "ssl", IS_ARRAY
))) {
562 FOREACH_KEYVAL(pos
, zoption
, key
, idx
, param
) {
564 HTTP_CURL_OPT_SSL_STRING(CERT
, 1);
565 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
, 0);
566 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
, 0);
568 HTTP_CURL_OPT_SSL_STRING(KEY
, 0);
569 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
, 0);
570 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
, 0);
572 HTTP_CURL_OPT_SSL_STRING(ENGINE
, 0);
573 HTTP_CURL_OPT_SSL_LONG(VERSION
);
575 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
576 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
577 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
, 0);
579 HTTP_CURL_OPT_STRING(CAINFO
, 1);
580 HTTP_CURL_OPT_STRING(CAPATH
, 1);
581 HTTP_CURL_OPT_STRING(RANDOM_FILE
, 1);
582 HTTP_CURL_OPT_STRING(EGDSOCKET
, 1);
591 switch (request
->meth
)
594 curl_easy_setopt(request
->ch
, CURLOPT_HTTPGET
, 1);
598 curl_easy_setopt(request
->ch
, CURLOPT_NOBODY
, 1);
602 curl_easy_setopt(request
->ch
, CURLOPT_POST
, 1);
606 curl_easy_setopt(request
->ch
, CURLOPT_UPLOAD
, 1);
610 if (http_request_method_exists(0, request
->meth
, NULL
)) {
611 curl_easy_setopt(request
->ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(request
->meth
));
613 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d (%s)", request
->meth
, request
->url
);
619 /* attach request body */
620 if (request
->body
&& (request
->meth
!= HTTP_GET
) && (request
->meth
!= HTTP_HEAD
)) {
621 switch (request
->body
->type
)
623 case HTTP_REQUEST_BODY_CSTRING
:
624 curl_easy_setopt(request
->ch
, CURLOPT_POSTFIELDS
, request
->body
->data
);
625 curl_easy_setopt(request
->ch
, CURLOPT_POSTFIELDSIZE
, request
->body
->size
);
628 case HTTP_REQUEST_BODY_CURLPOST
:
629 curl_easy_setopt(request
->ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) request
->body
->data
);
632 case HTTP_REQUEST_BODY_UPLOADFILE
:
633 curl_easy_setopt(request
->ch
, CURLOPT_IOCTLDATA
, request
);
634 curl_easy_setopt(request
->ch
, CURLOPT_READDATA
, request
);
635 curl_easy_setopt(request
->ch
, CURLOPT_INFILESIZE
, request
->body
->size
);
639 /* shouldn't ever happen */
640 http_error_ex(HE_ERROR
, 0, "Unknown request body type: %d (%s)", request
->body
->type
, request
->url
);
650 /* {{{ void http_request_exec(http_request *) */
651 PHP_HTTP_API
void _http_request_exec(http_request
*request
)
654 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
656 if (CURLE_OK
!= (result
= curl_easy_perform(request
->ch
))) {
657 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "%s; %s (%s)", curl_easy_strerror(result
), request
->_error
, request
->url
);
662 /* {{{ void http_request_info(http_request *, HashTable *) */
663 PHP_HTTP_API
void _http_request_info(http_request
*request
, HashTable
*info
)
666 INIT_ZARR(array
, info
);
668 HTTP_CURL_INFO(EFFECTIVE_URL
);
669 HTTP_CURL_INFO(RESPONSE_CODE
);
670 HTTP_CURL_INFO_EX(HTTP_CONNECTCODE
, connect_code
);
671 HTTP_CURL_INFO(FILETIME
);
672 HTTP_CURL_INFO(TOTAL_TIME
);
673 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
674 HTTP_CURL_INFO(CONNECT_TIME
);
675 HTTP_CURL_INFO(PRETRANSFER_TIME
);
676 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
677 HTTP_CURL_INFO(REDIRECT_TIME
);
678 HTTP_CURL_INFO(REDIRECT_COUNT
);
679 HTTP_CURL_INFO(SIZE_UPLOAD
);
680 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
681 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
682 HTTP_CURL_INFO(SPEED_UPLOAD
);
683 HTTP_CURL_INFO(HEADER_SIZE
);
684 HTTP_CURL_INFO(REQUEST_SIZE
);
685 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
686 HTTP_CURL_INFO(SSL_ENGINES
);
687 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
688 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
689 HTTP_CURL_INFO(CONTENT_TYPE
);
690 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
691 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
692 /*HTTP_CURL_INFO(OS_ERRNO);*/
693 HTTP_CURL_INFO(NUM_CONNECTS
);
694 #if LIBCURL_VERSION_NUM >= 0x070e01
695 HTTP_CURL_INFO_EX(COOKIELIST
, cookies
);
700 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
701 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
703 http_request
*request
= (http_request
*) ctx
;
704 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
706 if (request
->body
== NULL
|| request
->body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
709 return php_stream_read((php_stream
*) request
->body
->data
, data
, len
* n
);
713 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
714 static int http_curl_progress_callback(void *ctx
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
717 zval
*params_pass
[4], params_local
[4], retval
;
718 http_request
*request
= (http_request
*) ctx
;
719 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
721 params_pass
[0] = ¶ms_local
[0];
722 params_pass
[1] = ¶ms_local
[1];
723 params_pass
[2] = ¶ms_local
[2];
724 params_pass
[3] = ¶ms_local
[3];
726 INIT_PZVAL(params_pass
[0]);
727 INIT_PZVAL(params_pass
[1]);
728 INIT_PZVAL(params_pass
[2]);
729 INIT_PZVAL(params_pass
[3]);
730 ZVAL_DOUBLE(params_pass
[0], dltotal
);
731 ZVAL_DOUBLE(params_pass
[1], dlnow
);
732 ZVAL_DOUBLE(params_pass
[2], ultotal
);
733 ZVAL_DOUBLE(params_pass
[3], ulnow
);
737 rc
= call_user_function(EG(function_table
), NULL
, request
->_progress_callback
, &retval
, 4, params_pass TSRMLS_CC
);
743 /* {{{ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
744 static curlioerr
http_curl_ioctl_callback(CURL
*ch
, curliocmd cmd
, void *ctx
)
746 http_request
*request
= (http_request
*) ctx
;
747 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
749 if (cmd
!= CURLIOCMD_RESTARTREAD
) {
750 return CURLIOE_UNKNOWNCMD
;
752 if ( request
->body
== NULL
||
753 request
->body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
||
754 SUCCESS
!= php_stream_rewind((php_stream
*) request
->body
->data
)) {
755 return CURLIOE_FAILRESTART
;
761 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
762 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
764 http_request
*request
= (http_request
*) ctx
;
768 case CURLINFO_DATA_IN
:
769 if (request
->conv
.last_type
== CURLINFO_HEADER_IN
) {
770 phpstr_appends(&request
->conv
.response
, HTTP_CRLF
);
772 case CURLINFO_HEADER_IN
:
773 phpstr_append(&request
->conv
.response
, data
, length
);
775 case CURLINFO_DATA_OUT
:
776 if (request
->conv
.last_type
== CURLINFO_HEADER_OUT
) {
777 phpstr_appends(&request
->conv
.request
, HTTP_CRLF
);
779 case CURLINFO_HEADER_OUT
:
780 phpstr_append(&request
->conv
.request
, data
, length
);
784 fprintf(stderr
, "## ", type
);
786 fprintf(stderr
, "%s", data
);
789 for (i
= 1; i
<= length
; ++i
) {
790 fprintf(stderr
, "%02X ", data
[i
-1] & 0xFF);
792 fprintf(stderr
, "\n## ");
795 fprintf(stderr
, "\n");
797 if (data
[length
-1] != 0xa) {
798 fprintf(stderr
, "\n");
805 request
->conv
.last_type
= type
;
811 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
812 static inline zval
*_http_request_option_ex(http_request
*r
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
816 ulong h
= zend_get_hash_value(key
, keylen
);
823 (SUCCESS
!= zend_hash_quick_find(options
, key
, keylen
, h
, (void **) &zoption
))
825 (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))
831 return http_request_option_cache_ex(r
, key
, keylen
, h
, zval_copy(type
, *zoption
));
835 /* {{{ static inline zval *http_request_option_cache(http_request *, char *key, zval *) */
836 static inline zval
*_http_request_option_cache_ex(http_request
*r
, char *key
, size_t keylen
, ulong h
, zval
*opt TSRMLS_DC
)
842 _zend_hash_quick_add_or_update(&r
->_cache
.options
, key
, keylen
, h
, &opt
, sizeof(zval
*), NULL
,
843 zend_hash_quick_exists(&r
->_cache
.options
, key
, keylen
, h
)?HASH_UPDATE
:HASH_ADD ZEND_FILE_LINE_CC
);
848 if (zend_hash_exists(&r
->_cache
.options
, key
, keylen
)) {
849 zend_hash_update(&r
->_cache
.options
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
851 zend_hash_add(&r
->_cache
.options
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
859 #ifdef HTTP_NEED_OPENSSL_TSL
861 static MUTEX_T
*http_openssl_tsl
= NULL
;
863 static void http_ssl_lock(int mode
, int n
, const char * file
, int line
)
865 if (mode
& CRYPTO_LOCK
) {
866 tsrm_mutex_lock(http_openssl_tsl
[n
]);
868 tsrm_mutex_unlock(http_openssl_tsl
[n
]);
872 static ulong
http_ssl_id(void)
874 return (ulong
) tsrm_thread_id();
877 static inline void http_ssl_init(void)
879 int i
, c
= CRYPTO_num_locks();
881 http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
883 for (i
= 0; i
< c
; ++i
) {
884 http_openssl_tsl
[i
] = tsrm_mutex_alloc();
887 CRYPTO_set_id_callback(http_ssl_id
);
888 CRYPTO_set_locking_callback(http_ssl_lock
);
891 static inline void http_ssl_cleanup(void)
893 if (http_openssl_tsl
) {
894 int i
, c
= CRYPTO_num_locks();
896 CRYPTO_set_id_callback(NULL
);
897 CRYPTO_set_locking_callback(NULL
);
899 for (i
= 0; i
< c
; ++i
) {
900 tsrm_mutex_free(http_openssl_tsl
[i
]);
903 free(http_openssl_tsl
);
904 http_openssl_tsl
= NULL
;
907 #endif /* HTTP_NEED_OPENSSL_TSL */
910 #ifdef HTTP_NEED_GNUTLS_TSL
912 static int http_ssl_mutex_create(void **m
)
914 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
921 static int http_ssl_mutex_destroy(void **m
)
923 tsrm_mutex_free(*((MUTEX_T
*) m
));
927 static int http_ssl_mutex_lock(void **m
)
929 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
932 static int http_ssl_mutex_unlock(void **m
)
934 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
937 static struct gcry_thread_cbs http_gnutls_tsl
= {
938 GCRY_THREAD_OPTIONS_USER
,
940 http_ssl_mutex_create
,
941 http_ssl_mutex_destroy
,
943 http_ssl_mutex_unlock
946 static inline void http_ssl_init(void)
948 gcry_control(GCRYCTL_SET_THREAD_CBS
, &http_gnutls_tsl
);
951 static inline void http_ssl_cleanup(void)
955 #endif /* HTTP_NEED_GNUTLS_TSL */
958 #endif /* HTTP_HAVE_CURL */
965 * vim600: noet sw=4 ts=4 fdm=marker
966 * vim<600: noet sw=4 ts=4