X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_request_api.c;h=5214a77c401f025e4bad8bc3a893f2915b082dfb;hp=85bbb17fde292ab6bb5daabf55f1d7728474b92b;hb=8ae4de63a85ad21442948aa35f45e28368c91929;hpb=20e6f54b9dba78fec83ad8141034116edc4c84d3 diff --git a/http_request_api.c b/http_request_api.c index 85bbb17..5214a77 100644 --- a/http_request_api.c +++ b/http_request_api.c @@ -1,16 +1,13 @@ /* - +----------------------------------------------------------------------+ - | PECL :: http | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.0 of the PHP license, that | - | is bundled with this package in the file LICENSE, and is available | - | through the world-wide-web at http://www.php.net/license/3_0.txt. | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Copyright (c) 2004-2005 Michael Wallner | - +----------------------------------------------------------------------+ + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2005, Michael Wallner | + +--------------------------------------------------------------------+ */ /* $Id$ */ @@ -22,15 +19,6 @@ #ifdef HTTP_HAVE_CURL -#if defined(ZTS) && defined(HTTP_HAVE_SSL) -# if !defined(HAVE_OPENSSL_CRYPTO_H) -# error "libcurl was compiled with OpenSSL support, but we have no openssl/crypto.h" -# else -# define HTTP_NEED_SSL -# include -# endif -#endif - #include "php_http.h" #include "php_http_std_defs.h" #include "php_http_api.h" @@ -49,23 +37,62 @@ #include +/* {{{ cruft for thread safe SSL crypto locks */ +#if defined(ZTS) && defined(HTTP_HAVE_SSL) +# ifdef PHP_WIN32 +# define HTTP_NEED_SSL_TSL +# define HTTP_NEED_OPENSSL_TSL +# include +# else /* !PHP_WIN32 */ +# if defined(HTTP_HAVE_OPENSSL) +# if defined(HAVE_OPENSSL_CRYPTO_H) +# define HTTP_NEED_SSL_TSL +# define HTTP_NEED_OPENSSL_TSL +# include +# else +# warning \ + "libcurl was compiled with OpenSSL support, but configure could not find " \ + "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \ + "cause random crashes on SSL requests" +# endif +# elif defined(HTTP_HAVE_GNUTLS) +# if defined(HAVE_GCRYPT_H) +# define HTTP_NEED_SSL_TSL +# define HTTP_NEED_GNUTLS_TSL +# include +# else +# warning \ + "libcurl was compiled with GnuTLS support, but configure could not find " \ + "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \ + "cause random crashes on SSL requests" +# endif +# else +# warning \ + "libcurl was compiled with SSL support, but configure could not determine which" \ + "library was used; thus no SSL crypto locking callbacks will be set, which may " \ + "cause random crashes on SSL requests" +# endif /* HTTP_HAVE_OPENSSL || HTTP_HAVE_GNUTLS */ +# endif /* PHP_WIN32 */ +#endif /* ZTS && HTTP_HAVE_SSL */ +/* }}} */ + ZEND_EXTERN_MODULE_GLOBALS(http); -#ifdef HTTP_NEED_SSL -static inline zend_bool http_ssl_init(void); +#ifdef HTTP_NEED_SSL_TSL +static inline void http_ssl_init(void); static inline void http_ssl_cleanup(void); #endif PHP_MINIT_FUNCTION(http_request) { +#ifdef HTTP_NEED_SSL_TSL + http_ssl_init(); +#endif + if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) { return FAILURE; } -#ifdef HTTP_NEED_SSL - http_ssl_init(); -#endif - #if LIBCURL_VERSION_NUM >= 0x070a05 HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC); HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST); @@ -79,7 +106,7 @@ PHP_MINIT_FUNCTION(http_request) PHP_MSHUTDOWN_FUNCTION(http_request) { curl_global_cleanup(); -#ifdef HTTP_NEED_SSL +#ifdef HTTP_NEED_SSL_TSL http_ssl_cleanup(); #endif return SUCCESS; @@ -807,10 +834,6 @@ static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size { HTTP_REQUEST_CALLBACK_DATA(ctx, http_request_conv *, conv); -#if 0 - fprintf(stderr, "DEBUG: %s\n", data); -#endif - switch (type) { case CURLINFO_DATA_IN: @@ -831,6 +854,26 @@ static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size phpstr_append(conv->request, data, length); } break; +#if 0 + default: + fprintf(stderr, "## ", type); + if (!type) { + fprintf(stderr, "%s", data); + } else { + ulong i; + for (i = 1; i <= length; ++i) { + fprintf(stderr, "%02X ", data[i-1] & 0xFF); + if (!(i % 20)) { + fprintf(stderr, "\n## "); + } + } + fprintf(stderr, "\n"); + } + if (data[length-1] != 0xa) { + fprintf(stderr, "\n"); + } + break; +#endif } if (type) { @@ -867,64 +910,106 @@ static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t k } /* }}} */ -#ifdef HTTP_NEED_SSL - -static MUTEX_T *http_ssl_mutex = NULL; +#ifdef HTTP_NEED_OPENSSL_TSL +/* {{{ */ +static MUTEX_T *http_openssl_tsl = NULL; static void http_ssl_lock(int mode, int n, const char * file, int line) { if (mode & CRYPTO_LOCK) { - tsrm_mutex_lock(http_ssl_mutex[n]); + tsrm_mutex_lock(http_openssl_tsl[n]); } else { - tsrm_mutex_unlock(http_ssl_mutex[n]); + tsrm_mutex_unlock(http_openssl_tsl[n]); } } -static unsigned long http_ssl_id(void) +static ulong http_ssl_id(void) { - return (unsigned long) tsrm_thread_id(); + return (ulong) tsrm_thread_id(); } -static inline zend_bool http_ssl_init(void) +static inline void http_ssl_init(void) { - curl_version_info_data *cvid = curl_version_info(CURLVERSION_NOW); + int i, c = CRYPTO_num_locks(); - if (cvid && (cvid->features & CURL_VERSION_SSL)) { - int i, c = CRYPTO_num_locks(); - - http_ssl_mutex = malloc(c * sizeof(MUTEX_T)); - - for (i = 0; i < c; ++i) { - http_ssl_mutex[i] = tsrm_mutex_alloc(); - } - - CRYPTO_set_id_callback(http_ssl_id); - CRYPTO_set_locking_callback(http_ssl_lock); - - return 1; + http_openssl_tsl = malloc(c * sizeof(MUTEX_T)); + + for (i = 0; i < c; ++i) { + http_openssl_tsl[i] = tsrm_mutex_alloc(); } - return 0; + CRYPTO_set_id_callback(http_ssl_id); + CRYPTO_set_locking_callback(http_ssl_lock); } static inline void http_ssl_cleanup(void) { - if (http_ssl_mutex) { + if (http_openssl_tsl) { int i, c = CRYPTO_num_locks(); CRYPTO_set_id_callback(NULL); CRYPTO_set_locking_callback(NULL); for (i = 0; i < c; ++i) { - tsrm_mutex_free(http_ssl_mutex[i]); + tsrm_mutex_free(http_openssl_tsl[i]); } - free(http_ssl_mutex); - http_ssl_mutex = NULL; + free(http_openssl_tsl); + http_openssl_tsl = NULL; + } +} +#endif /* HTTP_NEED_OPENSSL_TSL */ +/* }}} */ + +#ifdef HTTP_NEED_GNUTLS_TSL +/* {{{ */ +static int http_ssl_mutex_create(void **m) +{ + if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) { + return SUCCESS; + } else { + return FAILURE; } } -#endif /* HTTP_NEED_SSL */ +static int http_ssl_mutex_destroy(void **m) +{ + tsrm_mutex_free(*((MUTEX_T *) m)); + return SUCCESS; +} + +static int http_ssl_mutex_lock(void **m) +{ + return tsrm_mutex_lock(*((MUTEX_T *) m)); +} + +static int http_ssl_mutex_unlock(void **m) +{ + return tsrm_mutex_unlock(*((MUTEX_T *) m)); +} + +static struct gcry_thread_cbs http_gnutls_tsl = { + GCRY_THREAD_OPTIONS_USER, + NULL, + http_ssl_mutex_create, + http_ssl_mutex_destroy, + http_ssl_mutex_lock, + http_ssl_mutex_unlock +}; + +static inline void http_ssl_init(void) +{ + gcry_control(GCRYCTL_SET_THREAD_CBS, &http_gnutls_tsl); +} + +static inline void http_ssl_cleanup(void) +{ + return; +} +#endif /* HTTP_NEED_GNUTLS_TSL */ +/* }}} */ + +/* {{{ http_curl_defaults(CURL *) */ static inline void _http_curl_defaults(CURL *ch) { HTTP_CURL_OPT(URL, NULL); @@ -981,6 +1066,7 @@ static inline void _http_curl_defaults(CURL *ch) HTTP_CURL_OPT(READDATA, NULL); HTTP_CURL_OPT(INFILESIZE, 0); } +/* }}} */ #endif /* HTTP_HAVE_CURL */