summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.am1
-rw-r--r--src/input/plugins/CurlInputPlugin.cxx77
-rw-r--r--src/lib/curl/Easy.hxx91
3 files changed, 127 insertions, 42 deletions
diff --git a/Makefile.am b/Makefile.am
index 2b08c1f8e..44c70fb82 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1276,6 +1276,7 @@ if ENABLE_CURL
libinput_a_SOURCES += \
src/input/IcyInputStream.cxx src/input/IcyInputStream.hxx \
src/input/plugins/CurlInputPlugin.cxx src/input/plugins/CurlInputPlugin.hxx \
+ src/lib/curl/Easy.hxx \
src/IcyMetaDataParser.cxx src/IcyMetaDataParser.hxx
endif
diff --git a/src/input/plugins/CurlInputPlugin.cxx b/src/input/plugins/CurlInputPlugin.cxx
index a563ecc8e..7eb4c034c 100644
--- a/src/input/plugins/CurlInputPlugin.cxx
+++ b/src/input/plugins/CurlInputPlugin.cxx
@@ -19,6 +19,7 @@
#include "config.h"
#include "CurlInputPlugin.hxx"
+#include "lib/curl/Easy.hxx"
#include "../AsyncInputStream.hxx"
#include "../IcyInputStream.hxx"
#include "../InputPlugin.hxx"
@@ -65,7 +66,7 @@ struct CurlInputStream final : public AsyncInputStream {
struct curl_slist *request_headers;
/** the curl handles */
- CURL *easy = nullptr;
+ CurlEasy easy;
/** error message provided by libcurl */
char error_buffer[CURL_ERROR_SIZE];
@@ -78,6 +79,7 @@ struct CurlInputStream final : public AsyncInputStream {
CURL_MAX_BUFFERED,
CURL_RESUME_AT),
request_headers(nullptr),
+ easy(nullptr),
icy(new IcyInputStream(this)) {
}
@@ -293,7 +295,7 @@ CurlInputStream::DoResume()
mutex.unlock();
- curl_easy_pause(easy, CURLPAUSE_CONT);
+ curl_easy_pause(easy.Get(), CURLPAUSE_CONT);
if (curl_version_num < 0x072000)
/* libcurl older than 7.32.0 does not update
@@ -362,9 +364,9 @@ CurlMulti::Add(CurlInputStream *c)
{
assert(io_thread_inside());
assert(c != nullptr);
- assert(c->easy != nullptr);
+ assert(c->easy);
- CURLMcode mcode = curl_multi_add_handle(multi, c->easy);
+ CURLMcode mcode = curl_multi_add_handle(multi, c->easy.Get());
if (mcode != CURLM_OK)
throw FormatRuntimeError("curl_multi_add_handle() failed: %s",
curl_multi_strerror(mcode));
@@ -382,7 +384,7 @@ static void
input_curl_easy_add_indirect(CurlInputStream *c)
{
assert(c != nullptr);
- assert(c->easy != nullptr);
+ assert(c->easy);
BlockingCall(io_thread_get(), [c](){
curl_multi->Add(c);
@@ -392,7 +394,7 @@ input_curl_easy_add_indirect(CurlInputStream *c)
inline void
CurlMulti::Remove(CurlInputStream *c)
{
- curl_multi_remove_handle(multi, c->easy);
+ curl_multi_remove_handle(multi, c->easy.Get());
}
void
@@ -400,12 +402,11 @@ CurlInputStream::FreeEasy()
{
assert(io_thread_inside());
- if (easy == nullptr)
+ if (!easy)
return;
curl_multi->Remove(this);
- curl_easy_cleanup(easy);
easy = nullptr;
curl_slist_free_all(request_headers);
@@ -420,7 +421,7 @@ CurlInputStream::FreeEasyIndirect()
curl_multi->InvalidateSockets();
});
- assert(easy == nullptr);
+ assert(!easy);
}
inline void
@@ -726,55 +727,47 @@ input_curl_writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
void
CurlInputStream::InitEasy()
{
- easy = curl_easy_init();
- if (easy == nullptr)
- throw std::runtime_error("curl_easy_init() failed");
-
- curl_easy_setopt(easy, CURLOPT_PRIVATE, (void *)this);
- curl_easy_setopt(easy, CURLOPT_USERAGENT,
- "Music Player Daemon " VERSION);
- curl_easy_setopt(easy, CURLOPT_HEADERFUNCTION,
- input_curl_headerfunction);
- curl_easy_setopt(easy, CURLOPT_WRITEHEADER, this);
- curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION,
- input_curl_writefunction);
- curl_easy_setopt(easy, CURLOPT_WRITEDATA, this);
- curl_easy_setopt(easy, CURLOPT_HTTP200ALIASES, http_200_aliases);
- curl_easy_setopt(easy, CURLOPT_FOLLOWLOCATION, 1l);
- curl_easy_setopt(easy, CURLOPT_NETRC, 1l);
- curl_easy_setopt(easy, CURLOPT_MAXREDIRS, 5l);
- curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1l);
- curl_easy_setopt(easy, CURLOPT_ERRORBUFFER, error_buffer);
- curl_easy_setopt(easy, CURLOPT_NOPROGRESS, 1l);
- curl_easy_setopt(easy, CURLOPT_NOSIGNAL, 1l);
- curl_easy_setopt(easy, CURLOPT_CONNECTTIMEOUT, 10l);
+ easy = CurlEasy();
+
+ easy.SetOption(CURLOPT_PRIVATE, (void *)this);
+ easy.SetOption(CURLOPT_USERAGENT, "Music Player Daemon " VERSION);
+ easy.SetOption(CURLOPT_HEADERFUNCTION, input_curl_headerfunction);
+ easy.SetOption(CURLOPT_WRITEHEADER, this);
+ easy.SetOption(CURLOPT_WRITEFUNCTION, input_curl_writefunction);
+ easy.SetOption(CURLOPT_WRITEDATA, this);
+ easy.SetOption(CURLOPT_HTTP200ALIASES, http_200_aliases);
+ easy.SetOption(CURLOPT_FOLLOWLOCATION, 1l);
+ easy.SetOption(CURLOPT_NETRC, 1l);
+ easy.SetOption(CURLOPT_MAXREDIRS, 5l);
+ easy.SetOption(CURLOPT_FAILONERROR, 1l);
+ easy.SetOption(CURLOPT_ERRORBUFFER, error_buffer);
+ easy.SetOption(CURLOPT_NOPROGRESS, 1l);
+ easy.SetOption(CURLOPT_NOSIGNAL, 1l);
+ easy.SetOption(CURLOPT_CONNECTTIMEOUT, 10l);
if (proxy != nullptr)
- curl_easy_setopt(easy, CURLOPT_PROXY, proxy);
+ easy.SetOption(CURLOPT_PROXY, proxy);
if (proxy_port > 0)
- curl_easy_setopt(easy, CURLOPT_PROXYPORT, (long)proxy_port);
+ easy.SetOption(CURLOPT_PROXYPORT, (long)proxy_port);
if (proxy_user != nullptr && proxy_password != nullptr) {
char proxy_auth_str[1024];
snprintf(proxy_auth_str, sizeof(proxy_auth_str),
"%s:%s",
proxy_user, proxy_password);
- curl_easy_setopt(easy, CURLOPT_PROXYUSERPWD, proxy_auth_str);
+ easy.SetOption(CURLOPT_PROXYUSERPWD, proxy_auth_str);
}
- curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, verify_peer ? 1l : 0l);
- curl_easy_setopt(easy, CURLOPT_SSL_VERIFYHOST, verify_host ? 2l : 0l);
+ easy.SetOption(CURLOPT_SSL_VERIFYPEER, verify_peer ? 1l : 0l);
+ easy.SetOption(CURLOPT_SSL_VERIFYHOST, verify_host ? 2l : 0l);
- CURLcode code = curl_easy_setopt(easy, CURLOPT_URL, GetURI());
- if (code != CURLE_OK)
- throw FormatRuntimeError("curl_easy_setopt() failed: %s",
- curl_easy_strerror(code));
+ easy.SetOption(CURLOPT_URL, GetURI());
request_headers = nullptr;
request_headers = curl_slist_append(request_headers,
"Icy-Metadata: 1");
- curl_easy_setopt(easy, CURLOPT_HTTPHEADER, request_headers);
+ easy.SetOption(CURLOPT_HTTPHEADER, request_headers);
}
void
@@ -809,7 +802,7 @@ CurlInputStream::DoSeek(offset_type new_offset)
if (offset > 0) {
sprintf(range, "%lld-", (long long)offset);
- curl_easy_setopt(easy, CURLOPT_RANGE, range);
+ easy.SetOption(CURLOPT_RANGE, range);
}
try {
diff --git a/src/lib/curl/Easy.hxx b/src/lib/curl/Easy.hxx
new file mode 100644
index 000000000..decb5771f
--- /dev/null
+++ b/src/lib/curl/Easy.hxx
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2016 Max Kellermann <max@duempel.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef CURL_EASY_HXX
+#define CURL_EASY_HXX
+
+#include <curl/curl.h>
+
+#include <utility>
+#include <stdexcept>
+#include <cstddef>
+
+/**
+ * An OO wrapper for a "CURL*" (a libCURL "easy" handle).
+ */
+class CurlEasy {
+ CURL *handle = nullptr;
+
+public:
+ /**
+ * Allocate a new CURL*.
+ *
+ * Throws std::runtime_error on error.
+ */
+ CurlEasy()
+ :handle(curl_easy_init())
+ {
+ if (handle == nullptr)
+ throw std::runtime_error("curl_easy_init() failed");
+ }
+
+ /**
+ * Create an empty instance.
+ */
+ CurlEasy(std::nullptr_t):handle(nullptr) {}
+
+ CurlEasy(CurlEasy &&src):handle(std::exchange(src.handle, nullptr)) {}
+
+ ~CurlEasy() {
+ if (handle != nullptr)
+ curl_easy_cleanup(handle);
+ }
+
+ operator bool() const {
+ return handle != nullptr;
+ }
+
+ CurlEasy &operator=(CurlEasy &&src) {
+ std::swap(handle, src.handle);
+ return *this;
+ }
+
+ CURL *Get() {
+ return handle;
+ }
+
+ template<typename T>
+ void SetOption(CURLoption option, T value) {
+ CURLcode code = curl_easy_setopt(handle, option, value);
+ if (code != CURLE_OK)
+ throw std::runtime_error(curl_easy_strerror(code));
+ }
+};
+
+#endif