X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=http_request_api.c;h=320febcada3d2e5750f0fb87dbcaca7c74630c4b;hb=0ecd173659b63e77e47271e5bd521599cd9e500f;hp=440eacd99957d08fa3efa22e14d2ebde753da719;hpb=7ed0decd1cb0ea45cf10b9741608d2e13c9949ad;p=m6w6%2Fext-http diff --git a/http_request_api.c b/http_request_api.c index 440eacd..320febc 100644 --- a/http_request_api.c +++ b/http_request_api.c @@ -12,10 +12,7 @@ /* $Id$ */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - +#define HTTP_WANT_SAPI #define HTTP_WANT_CURL #include "php_http.h" @@ -32,13 +29,11 @@ /* {{{ 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 @@ -49,7 +44,6 @@ # endif # elif defined(HTTP_HAVE_GNUTLS) # if defined(HAVE_GCRYPT_H) -# define HTTP_NEED_SSL_TSL # define HTTP_NEED_GNUTLS_TSL # include # else @@ -67,17 +61,77 @@ # endif /* PHP_WIN32 */ #endif /* ZTS && HTTP_HAVE_SSL */ -#ifdef HTTP_NEED_SSL_TSL -static inline void http_ssl_init(void); -static inline void http_ssl_cleanup(void); +#ifdef HTTP_NEED_OPENSSL_TSL +static MUTEX_T *http_openssl_tsl = NULL; + +static void http_openssl_thread_lock(int mode, int n, const char * file, int line) +{ + if (mode & CRYPTO_LOCK) { + tsrm_mutex_lock(http_openssl_tsl[n]); + } else { + tsrm_mutex_unlock(http_openssl_tsl[n]); + } +} + +static ulong http_openssl_thread_id(void) +{ + return (ulong) tsrm_thread_id(); +} +#endif +#ifdef HTTP_NEED_GNUTLS_TSL +static int http_gnutls_mutex_create(void **m) +{ + if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) { + return SUCCESS; + } else { + return FAILURE; + } +} + +static int http_gnutls_mutex_destroy(void **m) +{ + tsrm_mutex_free(*((MUTEX_T *) m)); + return SUCCESS; +} + +static int http_gnutls_mutex_lock(void **m) +{ + return tsrm_mutex_lock(*((MUTEX_T *) m)); +} + +static int http_gnutls_mutex_unlock(void **m) +{ + return tsrm_mutex_unlock(*((MUTEX_T *) m)); +} + +static struct gcry_thread_cbs http_gnutls_tsl = { + GCRY_THREAD_OPTION_USER, + NULL, + http_gnutls_mutex_create, + http_gnutls_mutex_destroy, + http_gnutls_mutex_lock, + http_gnutls_mutex_unlock +}; #endif /* }}} */ /* {{{ MINIT */ PHP_MINIT_FUNCTION(http_request) { -#ifdef HTTP_NEED_SSL_TSL - http_ssl_init(); +#ifdef HTTP_NEED_OPENSSL_TSL + int i, c = CRYPTO_num_locks(); + + http_openssl_tsl = malloc(c * sizeof(MUTEX_T)); + + for (i = 0; i < c; ++i) { + http_openssl_tsl[i] = tsrm_mutex_alloc(); + } + + CRYPTO_set_id_callback(http_openssl_thread_id); + CRYPTO_set_locking_callback(http_openssl_thread_lock); +#endif +#ifdef HTTP_NED_GNUTLS_TSL + gcry_control(GCRYCTL_SET_THREAD_CBS, &http_gnutls_tsl); #endif if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) { @@ -100,9 +154,25 @@ PHP_MINIT_FUNCTION(http_request) /* {{{ MSHUTDOWN */ PHP_MSHUTDOWN_FUNCTION(http_request) { +#ifdef HTTP_NEED_OPENSSL_TSL + CRYPTO_set_id_callback(http_openssl_thread_id); + CRYPTO_set_locking_callback(http_openssl_thread_lock); +#endif curl_global_cleanup(); -#ifdef HTTP_NEED_SSL_TSL - http_ssl_cleanup(); +#ifdef HTTP_NEED_OPENSSL_TSL + 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_openssl_tsl[i]); + } + + free(http_openssl_tsl); + http_openssl_tsl = NULL; + } #endif return SUCCESS; } @@ -211,7 +281,7 @@ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *); /* }}} */ /* {{{ CURL *http_curl_init(http_request *) */ -PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, void *context, char *error_buffer) +PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, http_request *request) { if (ch || (ch = curl_easy_init())) { #if defined(ZTS) @@ -226,9 +296,18 @@ PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, void *context, char *error_buff HTTP_CURL_OPT_EX(ch, READFUNCTION, http_curl_read_callback); HTTP_CURL_OPT_EX(ch, IOCTLFUNCTION, http_curl_ioctl_callback); HTTP_CURL_OPT_EX(ch, WRITEFUNCTION, http_curl_dummy_callback); - HTTP_CURL_OPT_EX(ch, DEBUGDATA, context); - HTTP_CURL_OPT_EX(ch, PRIVATE, context); - HTTP_CURL_OPT_EX(ch, ERRORBUFFER, error_buffer); + + /* set context */ + if (request) { + HTTP_CURL_OPT_EX(ch, PRIVATE, request); + HTTP_CURL_OPT_EX(ch, DEBUGDATA, request); + HTTP_CURL_OPT_EX(ch, ERRORBUFFER, request->_error); + + /* attach curl handle */ + request->ch = ch; + /* set defaults (also in http_request_reset()) */ + http_request_defaults(request); + } } return ch; @@ -382,6 +461,11 @@ PHP_HTTP_API void _http_request_defaults(http_request *request) HTTP_CURL_OPT(READDATA, NULL); HTTP_CURL_OPT(INFILESIZE, 0); HTTP_CURL_OPT(HTTP_VERSION, CURL_HTTP_VERSION_NONE); + HTTP_CURL_OPT(CUSTOMREQUEST, NULL); + HTTP_CURL_OPT(NOBODY, 0); + HTTP_CURL_OPT(POST, 0); + HTTP_CURL_OPT(UPLOAD, 0); + HTTP_CURL_OPT(HTTPGET, 1); } } /* }}} */ @@ -414,7 +498,6 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti TSRMLS_FETCH_FROM_CTX(request->tsrm_ls); HTTP_CHECK_CURL_INIT(request->ch, http_curl_init(request), return FAILURE); - http_request_defaults(request); /* set options */ HTTP_CURL_OPT(URL, request->url); @@ -478,6 +561,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti /* useragent, default "PECL::HTTP/version (PHP/version)" */ if ((zoption = http_request_option(request, options, "useragent", IS_STRING))) { + /* allow to send no user agent, not even default one */ if (Z_STRLEN_P(zoption)) { HTTP_CURL_OPT(USERAGENT, Z_STRVAL_P(zoption)); } else { @@ -576,7 +660,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti if (Z_LVAL_P(zoption) > 0) { HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption)); } else { - HTTP_CURL_OPT(TIMEVALUE, time(NULL) + Z_LVAL_P(zoption)); + HTTP_CURL_OPT(TIMEVALUE, HTTP_GET_REQUEST_TIME() + Z_LVAL_P(zoption)); } HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE); } else { @@ -633,38 +717,22 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti switch (request->meth) { case HTTP_GET: - HTTP_CURL_OPT(NOBODY, 0); - HTTP_CURL_OPT(POST, 0); - HTTP_CURL_OPT(UPLOAD, 0); HTTP_CURL_OPT(HTTPGET, 1); break; case HTTP_HEAD: - HTTP_CURL_OPT(POST, 0); - HTTP_CURL_OPT(UPLOAD, 0); - HTTP_CURL_OPT(HTTPGET, 0); HTTP_CURL_OPT(NOBODY, 1); break; case HTTP_POST: - HTTP_CURL_OPT(UPLOAD, 0); - HTTP_CURL_OPT(HTTPGET, 0); - HTTP_CURL_OPT(NOBODY, 0); HTTP_CURL_OPT(POST, 1); break; case HTTP_PUT: - HTTP_CURL_OPT(HTTPGET, 0); - HTTP_CURL_OPT(NOBODY, 0); - HTTP_CURL_OPT(POST, 0); HTTP_CURL_OPT(UPLOAD, 1); break; default: - HTTP_CURL_OPT(HTTPGET, 0); - HTTP_CURL_OPT(NOBODY, 0); - HTTP_CURL_OPT(POST, 0); - HTTP_CURL_OPT(UPLOAD, 0); if (http_request_method_exists(0, request->meth, NULL)) { HTTP_CURL_OPT(CUSTOMREQUEST, http_request_method_name(request->meth)); } else { @@ -914,105 +982,6 @@ static inline zval *_http_request_option_cache_ex(http_request *r, char *key, si } /* }}} */ -#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_openssl_tsl[n]); - } else { - tsrm_mutex_unlock(http_openssl_tsl[n]); - } -} - -static ulong http_ssl_id(void) -{ - return (ulong) tsrm_thread_id(); -} - -static inline void http_ssl_init(void) -{ - int i, c = CRYPTO_num_locks(); - - http_openssl_tsl = malloc(c * sizeof(MUTEX_T)); - - for (i = 0; i < c; ++i) { - http_openssl_tsl[i] = tsrm_mutex_alloc(); - } - - CRYPTO_set_id_callback(http_ssl_id); - CRYPTO_set_locking_callback(http_ssl_lock); -} - -static inline void http_ssl_cleanup(void) -{ - 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_openssl_tsl[i]); - } - - 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; - } -} - -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_OPTION_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 */ -/* }}} */ - #endif /* HTTP_HAVE_CURL */ /*