+#if LIBCURL_VERSION_NUM >= 0x070c01
+# define http_curl_reset(ch) curl_easy_reset(ch)
+#else
+# define http_curl_reset(ch)
+#endif
+
+#if LIBCURL_VERSION_NUM < 0x070c00
+# define curl_easy_strerror(code) "unkown error"
+#endif
+
+#define http_curl_startup(ch, clean_curl, URL, options) \
+ if (!ch) { \
+ if (!(ch = curl_easy_init())) { \
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl"); \
+ 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(); \
+ http_curl_freebuf(); \
+ if (clean_curl) { \
+ curl_easy_cleanup(ch); \
+ ch = NULL; \
+ }
+
+#define http_curl_freestr() \
+ zend_llist_clean(&HTTP_G(to_free))
+
+#define http_curl_initbuf() \
+ http_curl_initbuf_ex(0)
+
+#define http_curl_initbuf_ex(chunk_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; \
+ HTTP_G(curlbuf).size = size; \
+ }
+
+#define http_curl_freebuf() \
+ if (HTTP_G(curlbuf).data) { \
+ efree(HTTP_G(curlbuf).data); \
+ HTTP_G(curlbuf).data = NULL; \
+ } \
+ HTTP_G(curlbuf).used = 0; \
+ HTTP_G(curlbuf).free = 0; \
+ 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, HTTP_G(curlbuf).used);
+
+#define http_curl_sizebuf(for_size) \
+ { \
+ size_t size = (for_size); \
+ if (size > HTTP_G(curlbuf).free) { \
+ size_t bsize = HTTP_G(curlbuf).size; \
+ while (size > bsize) { \
+ bsize *= 2; \
+ } \
+ HTTP_G(curlbuf).data = erealloc(HTTP_G(curlbuf).data, HTTP_G(curlbuf).used + bsize); \
+ HTTP_G(curlbuf).free += bsize; \
+ } \
+ }
+
+