2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-10-26 19:32:43 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-10-26 19:32:43 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-10 10:14:29 +01:00
|
|
|
#include "CurlInputPlugin.hxx"
|
2016-12-19 14:42:04 +01:00
|
|
|
#include "lib/curl/Easy.hxx"
|
2017-01-03 07:33:00 +01:00
|
|
|
#include "lib/curl/Global.hxx"
|
|
|
|
#include "lib/curl/Request.hxx"
|
2017-01-03 07:30:53 +01:00
|
|
|
#include "lib/curl/Handler.hxx"
|
2017-01-06 19:23:47 +01:00
|
|
|
#include "lib/curl/Slist.hxx"
|
2014-05-02 22:31:02 +02:00
|
|
|
#include "../AsyncInputStream.hxx"
|
2014-05-11 15:32:47 +02:00
|
|
|
#include "../IcyInputStream.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "../InputPlugin.hxx"
|
2014-01-24 00:20:01 +01:00
|
|
|
#include "config/ConfigGlobal.hxx"
|
2015-01-21 22:13:44 +01:00
|
|
|
#include "config/Block.hxx"
|
2013-12-03 13:14:11 +01:00
|
|
|
#include "tag/TagBuilder.hxx"
|
2013-08-07 22:42:45 +02:00
|
|
|
#include "event/Call.hxx"
|
2017-01-25 23:15:52 +01:00
|
|
|
#include "event/Loop.hxx"
|
2013-01-10 10:33:20 +01:00
|
|
|
#include "IOThread.hxx"
|
2017-01-25 23:07:50 +01:00
|
|
|
#include "thread/Cond.hxx"
|
2013-10-20 23:09:51 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2014-08-07 15:15:56 +02:00
|
|
|
#include "util/StringUtil.hxx"
|
2013-10-21 09:48:31 +02:00
|
|
|
#include "util/NumberParser.hxx"
|
2016-09-09 15:37:06 +02:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Domain.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2016-09-09 15:11:52 +02:00
|
|
|
#include "PluginUnavailable.hxx"
|
2008-10-26 19:32:43 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2013-01-10 20:52:25 +01:00
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2013-01-10 20:46:47 +01:00
|
|
|
#if LIBCURL_VERSION_NUM < 0x071200
|
|
|
|
#error libcurl is too old
|
|
|
|
#endif
|
|
|
|
|
2011-08-23 20:46:51 +02:00
|
|
|
/**
|
|
|
|
* Do not buffer more than this number of bytes. It should be a
|
|
|
|
* reasonable limit that doesn't make low-end machines suffer too
|
|
|
|
* much, but doesn't cause stuttering on high-latency lines.
|
|
|
|
*/
|
|
|
|
static const size_t CURL_MAX_BUFFERED = 512 * 1024;
|
|
|
|
|
2011-09-16 08:54:47 +02:00
|
|
|
/**
|
|
|
|
* Resume the stream at this number of bytes after it has been paused.
|
|
|
|
*/
|
|
|
|
static const size_t CURL_RESUME_AT = 384 * 1024;
|
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
struct CurlInputStream final : public AsyncInputStream, CurlResponseHandler {
|
2008-10-26 19:32:43 +01:00
|
|
|
/* some buffers which were passed to libcurl, which we have
|
|
|
|
too free */
|
2013-10-19 17:15:17 +02:00
|
|
|
char range[32];
|
2017-01-06 19:23:47 +01:00
|
|
|
CurlSlist request_headers;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
CurlRequest *request = nullptr;
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2009-01-03 23:55:03 +01:00
|
|
|
/** parser for icy-metadata */
|
2014-05-11 15:32:47 +02:00
|
|
|
IcyInputStream *icy;
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2017-01-25 23:12:29 +01:00
|
|
|
CurlInputStream(EventLoop &event_loop, const char *_url,
|
|
|
|
Mutex &_mutex, Cond &_cond)
|
|
|
|
:AsyncInputStream(event_loop, _url, _mutex, _cond,
|
2016-06-17 17:44:45 +02:00
|
|
|
CURL_MAX_BUFFERED,
|
2014-05-02 22:31:02 +02:00
|
|
|
CURL_RESUME_AT),
|
2016-09-09 15:37:06 +02:00
|
|
|
icy(new IcyInputStream(this)) {
|
|
|
|
}
|
2013-01-10 22:18:23 +01:00
|
|
|
|
2014-03-15 20:38:08 +01:00
|
|
|
~CurlInputStream();
|
2013-01-10 22:18:23 +01:00
|
|
|
|
2014-03-15 20:38:08 +01:00
|
|
|
CurlInputStream(const CurlInputStream &) = delete;
|
|
|
|
CurlInputStream &operator=(const CurlInputStream &) = delete;
|
2014-03-15 20:40:43 +01:00
|
|
|
|
2016-09-09 15:37:06 +02:00
|
|
|
static InputStream *Open(const char *url, Mutex &mutex, Cond &cond);
|
2014-03-15 22:56:05 +01:00
|
|
|
|
2016-09-09 15:37:06 +02:00
|
|
|
void InitEasy();
|
2014-03-15 22:38:46 +01:00
|
|
|
|
2014-03-15 20:40:43 +01:00
|
|
|
/**
|
|
|
|
* Frees the current "libcurl easy" handle, and everything
|
|
|
|
* associated with it.
|
|
|
|
*
|
|
|
|
* Runs in the I/O thread.
|
|
|
|
*/
|
|
|
|
void FreeEasy();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frees the current "libcurl easy" handle, and everything associated
|
|
|
|
* with it.
|
|
|
|
*
|
|
|
|
* The mutex must not be locked.
|
|
|
|
*/
|
|
|
|
void FreeEasyIndirect();
|
|
|
|
|
2017-01-03 08:18:46 +01:00
|
|
|
/**
|
|
|
|
* The DoSeek() implementation invoked in the IOThread.
|
|
|
|
*/
|
|
|
|
void SeekInternal(offset_type new_offset);
|
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
/* virtual methods from CurlResponseHandler */
|
|
|
|
void OnHeaders(unsigned status,
|
|
|
|
std::multimap<std::string, std::string> &&headers) override;
|
|
|
|
void OnData(ConstBuffer<void> data) override;
|
|
|
|
void OnEnd() override;
|
|
|
|
void OnError(std::exception_ptr e) override;
|
2017-01-03 07:33:00 +01:00
|
|
|
|
2014-05-02 22:31:02 +02:00
|
|
|
/* virtual methods from AsyncInputStream */
|
|
|
|
virtual void DoResume() override;
|
|
|
|
virtual void DoSeek(offset_type new_offset) override;
|
2008-10-26 19:32:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** libcurl should accept "ICY 200 OK" */
|
|
|
|
static struct curl_slist *http_200_aliases;
|
|
|
|
|
2009-04-25 13:35:04 +02:00
|
|
|
/** HTTP proxy settings */
|
|
|
|
static const char *proxy, *proxy_user, *proxy_password;
|
|
|
|
static unsigned proxy_port;
|
|
|
|
|
2014-07-11 16:39:42 +02:00
|
|
|
static bool verify_peer, verify_host;
|
|
|
|
|
2016-12-19 16:37:01 +01:00
|
|
|
static CurlGlobal *curl_global;
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain curl_domain("curl");
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2014-05-02 22:31:02 +02:00
|
|
|
void
|
|
|
|
CurlInputStream::DoResume()
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2017-01-25 23:15:52 +01:00
|
|
|
assert(GetEventLoop().IsInside());
|
2011-09-15 07:43:35 +02:00
|
|
|
|
2014-05-02 22:31:02 +02:00
|
|
|
mutex.unlock();
|
2017-01-03 07:30:53 +01:00
|
|
|
request->Resume();
|
2014-05-02 22:31:02 +02:00
|
|
|
mutex.lock();
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 20:40:43 +01:00
|
|
|
void
|
|
|
|
CurlInputStream::FreeEasy()
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2017-01-25 23:15:52 +01:00
|
|
|
assert(GetEventLoop().IsInside());
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
if (request == nullptr)
|
2011-08-24 02:54:05 +02:00
|
|
|
return;
|
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
delete request;
|
|
|
|
request = nullptr;
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2017-01-06 19:23:47 +01:00
|
|
|
request_headers.Clear();
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 20:40:43 +01:00
|
|
|
void
|
|
|
|
CurlInputStream::FreeEasyIndirect()
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2017-01-25 23:15:52 +01:00
|
|
|
BlockingCall(GetEventLoop(), [this](){
|
2014-03-15 20:40:43 +01:00
|
|
|
FreeEasy();
|
2016-12-19 16:37:01 +01:00
|
|
|
curl_global->InvalidateSockets();
|
2013-08-07 22:42:45 +02:00
|
|
|
});
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
void
|
|
|
|
CurlInputStream::OnHeaders(unsigned status,
|
|
|
|
std::multimap<std::string, std::string> &&headers)
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2017-01-25 23:15:52 +01:00
|
|
|
assert(GetEventLoop().IsInside());
|
2016-09-16 17:15:17 +02:00
|
|
|
assert(!postponed_exception);
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
if (status < 200 || status >= 300)
|
|
|
|
throw FormatRuntimeError("got HTTP status %ld", status);
|
2014-03-15 20:40:43 +01:00
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2011-09-15 07:43:35 +02:00
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
if (IsSeekPending()) {
|
|
|
|
/* don't update metadata while seeking */
|
|
|
|
SeekDone();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!icy->IsEnabled() &&
|
|
|
|
headers.find("accept-ranges") != headers.end())
|
|
|
|
/* a stream with icy-metadata is not seekable */
|
|
|
|
seekable = true;
|
|
|
|
|
|
|
|
auto i = headers.find("content-length");
|
|
|
|
if (i != headers.end())
|
|
|
|
size = offset + ParseUint64(i->second.c_str());
|
|
|
|
|
|
|
|
i = headers.find("content-type");
|
|
|
|
if (i != headers.end())
|
|
|
|
SetMimeType(std::move(i->second));
|
|
|
|
|
|
|
|
i = headers.find("icy-name");
|
|
|
|
if (i == headers.end()) {
|
|
|
|
i = headers.find("ice-name");
|
|
|
|
if (i == headers.end())
|
|
|
|
i = headers.find("x-audiocast-name");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != headers.end()) {
|
|
|
|
TagBuilder tag_builder;
|
|
|
|
tag_builder.AddItem(TAG_NAME, i->second.c_str());
|
|
|
|
|
|
|
|
SetTag(tag_builder.CommitNew());
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
2011-09-15 08:20:41 +02:00
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
if (!icy->IsEnabled()) {
|
|
|
|
i = headers.find("icy-metaint");
|
|
|
|
|
|
|
|
if (i != headers.end()) {
|
|
|
|
size_t icy_metaint = ParseUint64(i->second.c_str());
|
|
|
|
#ifndef WIN32
|
|
|
|
/* Windows doesn't know "%z" */
|
|
|
|
FormatDebug(curl_domain, "icy-metaint=%zu", icy_metaint);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (icy_metaint > 0) {
|
|
|
|
icy->Enable(icy_metaint);
|
|
|
|
|
|
|
|
/* a stream with icy-metadata is not
|
|
|
|
seekable */
|
|
|
|
seekable = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CurlInputStream::OnData(ConstBuffer<void> data)
|
|
|
|
{
|
|
|
|
assert(data.size > 0);
|
|
|
|
|
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
|
|
|
|
2014-05-02 22:31:02 +02:00
|
|
|
if (IsSeekPending())
|
|
|
|
SeekDone();
|
2017-01-03 07:30:53 +01:00
|
|
|
|
|
|
|
if (data.size > GetBufferSpace()) {
|
|
|
|
AsyncInputStream::Pause();
|
|
|
|
throw CurlRequest::Pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
AppendToBuffer(data.data, data.size);
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
void
|
2017-01-03 07:30:53 +01:00
|
|
|
CurlInputStream::OnEnd()
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2017-01-03 07:30:53 +01:00
|
|
|
cond.broadcast();
|
|
|
|
|
|
|
|
AsyncInputStream::SetClosed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CurlInputStream::OnError(std::exception_ptr e)
|
|
|
|
{
|
|
|
|
postponed_exception = std::move(e);
|
|
|
|
|
|
|
|
if (IsSeekPending())
|
|
|
|
SeekDone();
|
|
|
|
else if (!IsReady())
|
|
|
|
SetReady();
|
|
|
|
else
|
|
|
|
cond.broadcast();
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
AsyncInputStream::SetClosed();
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-10-17 10:20:57 +02:00
|
|
|
* InputPlugin methods
|
2011-08-24 02:54:05 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-09-05 12:05:54 +02:00
|
|
|
static void
|
|
|
|
input_curl_init(const ConfigBlock &block)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
|
|
|
CURLcode code = curl_global_init(CURL_GLOBAL_ALL);
|
2016-09-09 15:11:52 +02:00
|
|
|
if (code != CURLE_OK)
|
|
|
|
throw PluginUnavailable(curl_easy_strerror(code));
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2013-11-23 12:02:17 +01:00
|
|
|
const auto version_info = curl_version_info(CURLVERSION_FIRST);
|
|
|
|
if (version_info != nullptr) {
|
|
|
|
FormatDebug(curl_domain, "version %s", version_info->version);
|
|
|
|
if (version_info->features & CURL_VERSION_SSL)
|
|
|
|
FormatDebug(curl_domain, "with %s",
|
|
|
|
version_info->ssl_version);
|
|
|
|
}
|
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
http_200_aliases = curl_slist_append(http_200_aliases, "ICY 200 OK");
|
2009-03-02 20:45:50 +01:00
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
proxy = block.GetBlockValue("proxy");
|
|
|
|
proxy_port = block.GetBlockValue("proxy_port", 0u);
|
|
|
|
proxy_user = block.GetBlockValue("proxy_user");
|
|
|
|
proxy_password = block.GetBlockValue("proxy_password");
|
2009-04-25 13:35:04 +02:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (proxy == nullptr) {
|
2009-04-25 13:35:04 +02:00
|
|
|
/* deprecated proxy configuration */
|
2015-06-26 08:05:20 +02:00
|
|
|
proxy = config_get_string(ConfigOption::HTTP_PROXY_HOST);
|
2015-01-21 22:36:13 +01:00
|
|
|
proxy_port = config_get_positive(ConfigOption::HTTP_PROXY_PORT, 0);
|
2015-06-26 08:05:20 +02:00
|
|
|
proxy_user = config_get_string(ConfigOption::HTTP_PROXY_USER);
|
2015-01-21 22:36:13 +01:00
|
|
|
proxy_password = config_get_string(ConfigOption::HTTP_PROXY_PASSWORD,
|
2009-04-25 13:35:04 +02:00
|
|
|
"");
|
|
|
|
}
|
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
verify_peer = block.GetBlockValue("verify_peer", true);
|
|
|
|
verify_host = block.GetBlockValue("verify_host", true);
|
2014-07-11 16:39:42 +02:00
|
|
|
|
2016-12-19 16:41:12 +01:00
|
|
|
try {
|
|
|
|
curl_global = new CurlGlobal(io_thread_get());
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
LogError(e);
|
2014-03-02 00:17:32 +01:00
|
|
|
curl_slist_free_all(http_200_aliases);
|
|
|
|
curl_global_cleanup();
|
2016-09-09 15:11:52 +02:00
|
|
|
throw PluginUnavailable("curl_multi_init() failed");
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
2009-03-02 20:45:50 +01:00
|
|
|
static void
|
|
|
|
input_curl_finish(void)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
2017-01-25 23:14:52 +01:00
|
|
|
BlockingCall(curl_global->GetEventLoop(), [](){
|
2016-12-19 16:37:01 +01:00
|
|
|
delete curl_global;
|
2013-08-07 22:42:45 +02:00
|
|
|
});
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
curl_slist_free_all(http_200_aliases);
|
2014-09-12 17:03:03 +02:00
|
|
|
http_200_aliases = nullptr;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
|
|
|
curl_global_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-03-15 20:38:08 +01:00
|
|
|
CurlInputStream::~CurlInputStream()
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
2014-03-15 20:40:43 +01:00
|
|
|
FreeEasyIndirect();
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 15:37:06 +02:00
|
|
|
void
|
|
|
|
CurlInputStream::InitEasy()
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
2017-01-03 07:30:53 +01:00
|
|
|
request = new CurlRequest(*curl_global, GetURI(), *this);
|
|
|
|
|
|
|
|
request->SetOption(CURLOPT_HTTP200ALIASES, http_200_aliases);
|
|
|
|
request->SetOption(CURLOPT_FOLLOWLOCATION, 1l);
|
|
|
|
request->SetOption(CURLOPT_MAXREDIRS, 5l);
|
|
|
|
request->SetOption(CURLOPT_FAILONERROR, 1l);
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (proxy != nullptr)
|
2017-01-03 07:30:53 +01:00
|
|
|
request->SetOption(CURLOPT_PROXY, proxy);
|
2009-01-13 22:57:05 +01:00
|
|
|
|
2009-04-25 13:35:04 +02:00
|
|
|
if (proxy_port > 0)
|
2017-01-03 07:30:53 +01:00
|
|
|
request->SetOption(CURLOPT_PROXYPORT, (long)proxy_port);
|
2009-01-25 16:00:51 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (proxy_user != nullptr && proxy_password != nullptr) {
|
2013-10-19 17:15:17 +02:00
|
|
|
char proxy_auth_str[1024];
|
|
|
|
snprintf(proxy_auth_str, sizeof(proxy_auth_str),
|
|
|
|
"%s:%s",
|
|
|
|
proxy_user, proxy_password);
|
2017-01-03 07:30:53 +01:00
|
|
|
request->SetOption(CURLOPT_PROXYUSERPWD, proxy_auth_str);
|
2009-01-13 22:57:05 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
request->SetOption(CURLOPT_SSL_VERIFYPEER, verify_peer ? 1l : 0l);
|
|
|
|
request->SetOption(CURLOPT_SSL_VERIFYHOST, verify_host ? 2l : 0l);
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2017-01-06 19:23:47 +01:00
|
|
|
request_headers.Clear();
|
|
|
|
request_headers.Append("Icy-Metadata: 1");
|
|
|
|
request->SetOption(CURLOPT_HTTPHEADER, request_headers.Get());
|
2017-01-07 16:01:58 +01:00
|
|
|
|
|
|
|
request->Start();
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 22:31:02 +02:00
|
|
|
void
|
2017-01-03 08:18:46 +01:00
|
|
|
CurlInputStream::SeekInternal(offset_type new_offset)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
|
|
|
/* close the old connection and open a new one */
|
|
|
|
|
2017-01-03 08:18:46 +01:00
|
|
|
FreeEasy();
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
offset = new_offset;
|
2017-01-09 18:08:33 +01:00
|
|
|
if (offset == size) {
|
2008-11-20 12:45:17 +01:00
|
|
|
/* seek to EOF: simulate empty result; avoid
|
|
|
|
triggering a "416 Requested Range Not Satisfiable"
|
|
|
|
response */
|
2017-01-09 18:08:33 +01:00
|
|
|
SeekDone();
|
2014-05-02 22:31:02 +02:00
|
|
|
return;
|
2017-01-09 18:08:33 +01:00
|
|
|
}
|
2008-11-20 12:45:17 +01:00
|
|
|
|
2017-01-03 08:18:46 +01:00
|
|
|
InitEasy();
|
2008-10-26 19:32:43 +01:00
|
|
|
|
|
|
|
/* send the "Range" header */
|
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
if (offset > 0) {
|
|
|
|
sprintf(range, "%lld-", (long long)offset);
|
2017-01-03 07:30:53 +01:00
|
|
|
request->SetOption(CURLOPT_RANGE, range);
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
2017-01-03 08:18:46 +01:00
|
|
|
}
|
2011-09-15 07:43:35 +02:00
|
|
|
|
2017-01-03 08:18:46 +01:00
|
|
|
void
|
|
|
|
CurlInputStream::DoSeek(offset_type new_offset)
|
|
|
|
{
|
|
|
|
assert(IsReady());
|
|
|
|
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
|
2017-01-25 23:15:52 +01:00
|
|
|
BlockingCall(GetEventLoop(), [this, new_offset](){
|
2017-01-03 08:18:46 +01:00
|
|
|
SeekInternal(new_offset);
|
|
|
|
});
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
2014-03-15 22:56:05 +01:00
|
|
|
inline InputStream *
|
2016-09-09 15:37:06 +02:00
|
|
|
CurlInputStream::Open(const char *url, Mutex &mutex, Cond &cond)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
2017-01-25 23:12:29 +01:00
|
|
|
CurlInputStream *c = new CurlInputStream(curl_global->GetEventLoop(),
|
|
|
|
url, mutex, cond);
|
2016-09-09 15:37:06 +02:00
|
|
|
|
|
|
|
try {
|
2017-01-25 23:15:52 +01:00
|
|
|
BlockingCall(c->GetEventLoop(), [c](){
|
2017-01-03 07:30:53 +01:00
|
|
|
c->InitEasy();
|
2017-01-03 10:53:20 +01:00
|
|
|
});
|
2016-09-09 15:37:06 +02:00
|
|
|
} catch (...) {
|
2013-01-10 22:18:23 +01:00
|
|
|
delete c;
|
2016-09-09 15:37:06 +02:00
|
|
|
throw;
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
2014-05-11 15:32:47 +02:00
|
|
|
return c->icy;
|
2014-03-15 22:56:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static InputStream *
|
2016-09-09 15:37:06 +02:00
|
|
|
input_curl_open(const char *url, Mutex &mutex, Cond &cond)
|
2014-03-15 22:56:05 +01:00
|
|
|
{
|
2017-01-03 13:16:29 +01:00
|
|
|
if (strncmp(url, "http://", 7) != 0 &&
|
|
|
|
strncmp(url, "https://", 8) != 0)
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-01-15 16:12:11 +01:00
|
|
|
|
2016-09-09 15:37:06 +02:00
|
|
|
return CurlInputStream::Open(url, mutex, cond);
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
2008-10-26 20:38:44 +01:00
|
|
|
|
2013-10-17 10:20:57 +02:00
|
|
|
const struct InputPlugin input_plugin_curl = {
|
2013-01-10 10:14:29 +01:00
|
|
|
"curl",
|
|
|
|
input_curl_init,
|
|
|
|
input_curl_finish,
|
|
|
|
input_curl_open,
|
2008-10-26 20:38:44 +01:00
|
|
|
};
|