+/* Macro for easy handling of CURL errors. */
+#define CURL_CHECK_ERROR(fn, args) \
+ ({ \
+ CURLcode __r = fn args; \
+ if (__r != CURLE_OK) { \
+ fprintf (stderr, "%s: %s\n", #fn, curl_easy_strerror (__r)); \
+ } \
+ __r; \
+ })
+
+static size_t
+write_fn (void *ptr, size_t size, size_t nmemb, void *stream)
+{
+ int bytes = size * nmemb;
+ return bytes;
+}
+
+static size_t
+header_fn (void *ptr, size_t size, size_t nmemb, void *stream)
+{
+ int bytes = size * nmemb;
+ return bytes;
+}
+
+/* Called from the message loop to initialize the CURL handle. */
+static void
+do_curl_init (void)
+{
+ DEBUG ("initializing libcurl");
+
+ curl = curl_easy_init ();
+ if (!curl) { /* This is probably quite bad, so abort. */
+ DEBUG ("curl_easy_init failed");
+ abort ();
+ }
+
+ CURL_CHECK_ERROR (curl_easy_setopt,
+ (curl, CURLOPT_CAINFO, cainfo));
+ CURL_CHECK_ERROR (curl_easy_setopt,
+ (curl, CURLOPT_SSL_VERIFYHOST, check_cert ? 2 : 0));
+ CURL_CHECK_ERROR (curl_easy_setopt,
+ (curl, CURLOPT_SSL_VERIFYPEER, check_cert ? 1 : 0));
+
+ CURL_CHECK_ERROR (curl_easy_setopt,
+ (curl, CURLOPT_WRITEFUNCTION, write_fn));
+ CURL_CHECK_ERROR (curl_easy_setopt,
+ (curl, CURLOPT_HEADERFUNCTION, header_fn));
+
+ /* This enables error messages in curl_easy_perform. */
+ CURL_CHECK_ERROR (curl_easy_setopt,
+ (curl, CURLOPT_ERRORBUFFER, curl_error_buffer));
+
+ /* This enables cookie handling, using an internal cookiejar. */
+ CURL_CHECK_ERROR (curl_easy_setopt,
+ (curl, CURLOPT_COOKIEFILE, ""));
+}
+
+/* Called from the message loop. Try to connect to the current URI.
+ * Returns true on success.
+ */