X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=sidebyside;f=http_curl_api.c;h=3dc462cdc95a3f8d539121b0efdfc2d23fcd4802;hb=3e75fa70f5996af755acceab7302e295f99ff670;hp=7a53beb10c471cfefd67d869a767e6b9bacfda4e;hpb=c00621939ec351c74846b367765ddac4d3955eb9;p=m6w6%2Fext-http diff --git a/http_curl_api.c b/http_curl_api.c index 7a53beb..3dc462c 100644 --- a/http_curl_api.c +++ b/http_curl_api.c @@ -20,14 +20,10 @@ #endif #ifdef PHP_WIN32 -# define _WINSOCKAPI_ -# define ZEND_INCLUDE_FULL_WINDOWS_HEADERS # include -# include #endif #include -#include #include "php.h" #include "php_http.h" @@ -43,6 +39,17 @@ ZEND_DECLARE_MODULE_GLOBALS(http) +#if LIBCURL_VERSION_NUM >= 0x070c01 +# define http_curl_reset(ch) curl_easy_reset(ch) +#else +# define http_curl_reset(ch) +#endif + +/* FIXXME: correct version where strerror is supported! */ +#if LIBCURL_VERSION_NUM < 0x070b01 +# define curl_easy_strerror(code) "unkown error" +#endif + #define http_curl_startup(ch, clean_curl, URL, options) \ if (!ch) { \ if (!(ch = curl_easy_init())) { \ @@ -50,10 +57,21 @@ ZEND_DECLARE_MODULE_GLOBALS(http) return FAILURE; \ } \ clean_curl = 1; \ + } else { \ + http_curl_reset(ch); \ } \ http_curl_initbuf(); \ http_curl_setopts(ch, URL, options); +#define http_curl_perform(ch, clean_curl) \ + { \ + CURLcode result; \ + if (CURLE_OK != (result = curl_easy_perform(ch))) { \ + http_curl_cleanup(ch, clean_curl); \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request: %s", curl_easy_strerror(result)); \ + return FAILURE; \ + } \ + } #define http_curl_cleanup(ch, clean_curl) \ http_curl_freestr(); \ @@ -66,11 +84,15 @@ ZEND_DECLARE_MODULE_GLOBALS(http) #define http_curl_freestr() \ zend_llist_clean(&HTTP_G(to_free)) -#define http_curl_initbuf() http_curl_initbuf_ex(0) +#define http_curl_initbuf() \ + http_curl_initbuf_ex(0) #define http_curl_initbuf_ex(chunk_size) \ { \ - size_t size = (chunk_size > 0) ? chunk_size : HTTP_CURLBUF_SIZE; \ + size_t size = chunk_size; \ + if (size < 1) { \ + size = HTTP_CURLBUF_SIZE; \ + } \ http_curl_freebuf(); \ HTTP_G(curlbuf).data = emalloc(size); \ HTTP_G(curlbuf).free = size; \ @@ -87,9 +109,9 @@ ZEND_DECLARE_MODULE_GLOBALS(http) HTTP_G(curlbuf).size = 0; #define http_curl_copybuf(data, size) \ - * size = HTTP_G(curlbuf).used; \ - * data = ecalloc(1, HTTP_G(curlbuf).used + 1); \ - memcpy(* data, HTTP_G(curlbuf).data, * size); + *size = HTTP_G(curlbuf).used; \ + *data = ecalloc(1, HTTP_G(curlbuf).used + 1); \ + memcpy(*data, HTTP_G(curlbuf).data, HTTP_G(curlbuf).used); #define http_curl_sizebuf(for_size) \ { \ @@ -203,7 +225,7 @@ static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *opti curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1); curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, http_curl_body_callback); curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, http_curl_hdrs_callback); -#if defined(ZTS) && (LIBCURL_VERSION_NUM >= 71000) +#if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00) curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1); #endif @@ -222,7 +244,7 @@ static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *opti if (zoption = http_curl_getopt1(options, "proxyauth", IS_STRING)) { curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, http_curl_copystr(Z_STRVAL_P(zoption))); } -#if LIBCURL_VERSION_NUM >= 71007 +#if LIBCURL_VERSION_NUM >= 0x070a07 /* auth method */ if (zoption = http_curl_getopt1(options, "proxyauthtype", IS_LONG)) { curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption)); @@ -234,7 +256,7 @@ static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *opti if (zoption = http_curl_getopt1(options, "interface", IS_STRING)) { curl_easy_setopt(ch, CURLOPT_INTERFACE, http_curl_copystr(Z_STRVAL_P(zoption))); } - + /* another port */ if (zoption = http_curl_getopt1(options, "port", IS_LONG)) { curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption)); @@ -244,7 +266,7 @@ static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *opti if (zoption = http_curl_getopt1(options, "httpauth", IS_STRING)) { curl_easy_setopt(ch, CURLOPT_USERPWD, http_curl_copystr(Z_STRVAL_P(zoption))); } -#if LIBCURL_VERSION_NUM >= 71006 +#if LIBCURL_VERSION_NUM >= 0x070a06 if (zoption = http_curl_getopt1(options, "httpauthtype", IS_LONG)) { curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption)); } @@ -350,7 +372,7 @@ static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *opti if (zoption = http_curl_getopt1(options, "maxfilesize", IS_LONG)) { curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption)); } - + /* lastmodified */ if (zoption = http_curl_getopt1(options, "lastmodified", IS_LONG)) { curl_easy_setopt(ch, CURLOPT_TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE); @@ -409,14 +431,14 @@ static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC) HTTP_CURL_INFO(EFFECTIVE_URL); -#if LIBCURL_VERSION_NUM >= 71007 +#if LIBCURL_VERSION_NUM >= 0x070a07 HTTP_CURL_INFO(RESPONSE_CODE); #else HTTP_CURL_INFO_EX(HTTP_CODE, RESPONSE_CODE); #endif HTTP_CURL_INFO(HTTP_CONNECTCODE); -#if LIBCURL_VERSION_NUM >= 70500 +#if LIBCURL_VERSION_NUM >= 0x070500 HTTP_CURL_INFO(FILETIME); #endif HTTP_CURL_INFO(TOTAL_TIME); @@ -424,7 +446,7 @@ static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC) HTTP_CURL_INFO(CONNECT_TIME); HTTP_CURL_INFO(PRETRANSFER_TIME); HTTP_CURL_INFO(STARTTRANSFER_TIME); -#if LIBCURL_VERSION_NUM >= 70907 +#if LIBCURL_VERSION_NUM >= 0x070907 HTTP_CURL_INFO(REDIRECT_TIME); HTTP_CURL_INFO(REDIRECT_COUNT); #endif @@ -438,8 +460,8 @@ static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC) HTTP_CURL_INFO(REQUEST_SIZE); HTTP_CURL_INFO(SSL_VERIFYRESULT); -#if LIBCURL_VERSION_NUM >= 71203 - /*HTTP_CURL_INFO(SSL_ENGINES); +#if LIBCURL_VERSION_NUM >= 0x070c03 + /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */ #endif @@ -447,20 +469,20 @@ static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC) HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD); HTTP_CURL_INFO(CONTENT_TYPE); -#if LIBCURL_VERSION_NUM >= 71003 +#if LIBCURL_VERSION_NUM >= 0x070a03 /*HTTP_CURL_INFO(PRIVATE);*/ #endif -#if LIBCURL_VERSION_NUM >= 71008 +#if LIBCURL_VERSION_NUM >= 0x070a08 HTTP_CURL_INFO(HTTPAUTH_AVAIL); HTTP_CURL_INFO(PROXYAUTH_AVAIL); #endif -#if LIBCURL_VERSION_NUM >= 71202 +#if LIBCURL_VERSION_NUM >= 0x070c02 /*HTTP_CURL_INFO(OS_ERRNO);*/ #endif -#if LIBCURL_VERSION_NUM >= 71203 +#if LIBCURL_VERSION_NUM >= 0x070c03 HTTP_CURL_INFO(NUM_CONNECTS); #endif } @@ -474,12 +496,7 @@ PHP_HTTP_API STATUS _http_get_ex(CURL *ch, const char *URL, HashTable *options, http_curl_startup(ch, clean_curl, URL, options); curl_easy_setopt(ch, CURLOPT_HTTPGET, 1); - - if (CURLE_OK != curl_easy_perform(ch)) { - http_curl_cleanup(ch, clean_curl); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request"); - return FAILURE; - } + http_curl_perform(ch, clean_curl); if (info) { http_curl_getinfo(ch, info); @@ -499,12 +516,7 @@ PHP_HTTP_API STATUS _http_head_ex(CURL *ch, const char *URL, HashTable *options, http_curl_startup(ch, clean_curl, URL, options); curl_easy_setopt(ch, CURLOPT_NOBODY, 1); - - if (CURLE_OK != curl_easy_perform(ch)) { - http_curl_cleanup(ch, clean_curl); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request"); - return FAILURE; - } + http_curl_perform(ch, clean_curl); if (info) { http_curl_getinfo(ch, info); @@ -527,12 +539,7 @@ PHP_HTTP_API STATUS _http_post_data_ex(CURL *ch, const char *URL, char *postdata curl_easy_setopt(ch, CURLOPT_POST, 1); curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata); curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len); - - if (CURLE_OK != curl_easy_perform(ch)) { - http_curl_cleanup(ch, clean_curl); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request"); - return FAILURE; - } + http_curl_perform(ch, clean_curl); if (info) { http_curl_getinfo(ch, info); @@ -583,12 +590,7 @@ PHP_HTTP_API STATUS _http_post_curldata_ex(CURL *ch, const char *URL, http_curl_startup(ch, clean_curl, URL, options); curl_easy_setopt(ch, CURLOPT_POST, 1); curl_easy_setopt(ch, CURLOPT_HTTPPOST, curldata); - - if (CURLE_OK != curl_easy_perform(ch)) { - http_curl_cleanup(ch, clean_curl); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request"); - return FAILURE; - } + http_curl_perform(ch, clean_curl); if (info) { http_curl_getinfo(ch, info);