* attempt to be smarter than windows ;)
[m6w6/ext-http] / http_curl_api.c
index 7a53beb10c471cfefd67d869a767e6b9bacfda4e..6e96940daf7ad77da0c906f18286d4a0ab9a4623 100644 (file)
 #endif
 
 #ifdef PHP_WIN32
-#      define _WINSOCKAPI_
-#      define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
 #      include <winsock2.h>
-#      include <sys/types.h>
 #endif
 
 #include <curl/curl.h>
-#include <curl/easy.h>
 
 #include "php.h"
 #include "php_http.h"
 
 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
+
 #define http_curl_startup(ch, clean_curl, URL, options) \
        if (!ch) { \
                if (!(ch = curl_easy_init())) { \
@@ -50,10 +52,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(); \
@@ -87,9 +100,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 +216,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 +235,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 +247,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 +257,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 +363,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 +422,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 +437,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 +451,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 +460,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 +487,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 +507,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 +530,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 +581,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);