2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-10 10:14:29 +01:00
|
|
|
* Copyright (C) 2003-2013 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"
|
2013-01-24 19:14:40 +01:00
|
|
|
#include "InputStream.hxx"
|
2013-01-25 22:43:01 +01:00
|
|
|
#include "InputPlugin.hxx"
|
2013-09-05 08:47:10 +02:00
|
|
|
#include "ConfigGlobal.hxx"
|
|
|
|
#include "ConfigData.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.hxx"
|
2013-01-10 22:33:16 +01:00
|
|
|
#include "IcyMetaDataParser.hxx"
|
2013-11-06 19:01:50 +01:00
|
|
|
#include "event/SocketMonitor.hxx"
|
|
|
|
#include "event/TimeoutMonitor.hxx"
|
2013-08-07 22:42:45 +02:00
|
|
|
#include "event/Call.hxx"
|
2013-01-10 10:33:20 +01:00
|
|
|
#include "IOThread.hxx"
|
2013-10-20 23:09:51 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2013-10-19 15:25:32 +02:00
|
|
|
#include "util/CharUtil.hxx"
|
2013-10-21 09:48:31 +02:00
|
|
|
#include "util/NumberParser.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2008-10-26 19:32:43 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
2009-03-27 17:24:24 +01:00
|
|
|
|
|
|
|
#if defined(WIN32)
|
|
|
|
#include <winsock2.h>
|
|
|
|
#else
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2013-01-10 23:00:23 +01:00
|
|
|
#include <list>
|
2013-01-10 20:52:25 +01:00
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
#include <curl/curl.h>
|
2013-10-21 09:53:38 +02:00
|
|
|
#include <glib.h>
|
2008-10-26 19:32:43 +01:00
|
|
|
|
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;
|
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
/**
|
|
|
|
* Buffers created by input_curl_writefunction().
|
|
|
|
*/
|
2013-01-10 23:00:23 +01:00
|
|
|
class CurlInputBuffer {
|
2008-10-26 19:32:43 +01:00
|
|
|
/** size of the payload */
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
/** how much has been consumed yet? */
|
|
|
|
size_t consumed;
|
|
|
|
|
|
|
|
/** the payload */
|
2013-01-10 23:00:23 +01:00
|
|
|
uint8_t *data;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CurlInputBuffer(const void *_data, size_t _size)
|
|
|
|
:size(_size), consumed(0), data(new uint8_t[size]) {
|
|
|
|
memcpy(data, _data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
~CurlInputBuffer() {
|
|
|
|
delete[] data;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurlInputBuffer(const CurlInputBuffer &) = delete;
|
|
|
|
CurlInputBuffer &operator=(const CurlInputBuffer &) = delete;
|
|
|
|
|
|
|
|
const void *Begin() const {
|
|
|
|
return data + consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t TotalSize() const {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Available() const {
|
|
|
|
return size - consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark a part of the buffer as consumed.
|
|
|
|
*
|
|
|
|
* @return false if the buffer is now empty
|
|
|
|
*/
|
|
|
|
bool Consume(size_t length) {
|
|
|
|
assert(consumed < size);
|
|
|
|
|
|
|
|
consumed += length;
|
|
|
|
if (consumed < size)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
assert(consumed == size);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Read(void *dest, size_t length) {
|
|
|
|
assert(consumed + length <= size);
|
|
|
|
|
|
|
|
memcpy(dest, data + consumed, length);
|
|
|
|
return Consume(length);
|
|
|
|
}
|
2008-10-26 19:32:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct input_curl {
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream base;
|
2009-12-30 23:27:37 +01:00
|
|
|
|
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];
|
2008-10-26 19:32:43 +01:00
|
|
|
struct curl_slist *request_headers;
|
|
|
|
|
|
|
|
/** the curl handles */
|
|
|
|
CURL *easy;
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
/** list of buffers, where input_curl_writefunction() appends
|
|
|
|
to, and input_curl_read() reads from them */
|
2013-01-10 23:00:23 +01:00
|
|
|
std::list<CurlInputBuffer> buffers;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
/**
|
|
|
|
* Is the connection currently paused? That happens when the
|
|
|
|
* buffer was getting too large. It will be unpaused when the
|
|
|
|
* buffer is below the threshold again.
|
|
|
|
*/
|
|
|
|
bool paused;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2008-11-06 06:48:30 +01:00
|
|
|
/** error message provided by libcurl */
|
|
|
|
char error[CURL_ERROR_SIZE];
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2009-01-03 23:55:03 +01:00
|
|
|
/** parser for icy-metadata */
|
2013-01-10 22:33:16 +01:00
|
|
|
IcyMetaDataParser icy;
|
2009-01-03 23:55:03 +01:00
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
/** the stream name from the icy-name response header */
|
2013-10-19 17:18:56 +02:00
|
|
|
std::string meta_name;
|
2009-01-03 23:29:45 +01:00
|
|
|
|
|
|
|
/** the tag object ready to be requested via
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::ReadTag() */
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *tag;
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error postponed_error;
|
2013-01-10 22:18:23 +01:00
|
|
|
|
2013-01-27 17:20:50 +01:00
|
|
|
input_curl(const char *url, Mutex &mutex, Cond &cond)
|
2013-01-28 20:32:23 +01:00
|
|
|
:base(input_plugin_curl, url, mutex, cond),
|
2013-10-19 17:15:17 +02:00
|
|
|
request_headers(nullptr),
|
2013-01-10 22:18:23 +01:00
|
|
|
paused(false),
|
2013-08-10 18:02:44 +02:00
|
|
|
tag(nullptr) {}
|
2013-01-10 22:18:23 +01:00
|
|
|
|
|
|
|
~input_curl();
|
|
|
|
|
|
|
|
input_curl(const input_curl &) = delete;
|
|
|
|
input_curl &operator=(const input_curl &) = delete;
|
2008-10-26 19:32:43 +01:00
|
|
|
};
|
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
class CurlMulti;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Monitor for one socket created by CURL.
|
|
|
|
*/
|
|
|
|
class CurlSocket final : SocketMonitor {
|
|
|
|
CurlMulti &multi;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CurlSocket(CurlMulti &_multi, EventLoop &_loop, int _fd)
|
|
|
|
:SocketMonitor(_fd, _loop), multi(_multi) {}
|
|
|
|
|
|
|
|
~CurlSocket() {
|
2013-11-07 01:09:53 +01:00
|
|
|
/* TODO: sometimes, CURL uses CURL_POLL_REMOVE after
|
|
|
|
closing the socket, and sometimes, it uses
|
|
|
|
CURL_POLL_REMOVE just to move the (still open)
|
|
|
|
connection to the pool; in the first case,
|
|
|
|
Abandon() would be most appropriate, but it breaks
|
|
|
|
the second case - is that a CURL bug? is there a
|
|
|
|
better solution? */
|
|
|
|
|
|
|
|
Steal();
|
2013-11-06 19:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function for CURLMOPT_SOCKETFUNCTION.
|
|
|
|
*/
|
|
|
|
static int SocketFunction(CURL *easy,
|
|
|
|
curl_socket_t s, int action,
|
|
|
|
void *userp, void *socketp);
|
|
|
|
|
|
|
|
virtual bool OnSocketReady(unsigned flags) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr int FlagsToCurlCSelect(unsigned flags) {
|
|
|
|
return (flags & (READ | HANGUP) ? CURL_CSELECT_IN : 0) |
|
|
|
|
(flags & WRITE ? CURL_CSELECT_OUT : 0) |
|
|
|
|
(flags & ERROR ? CURL_CSELECT_ERR : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_const
|
|
|
|
static unsigned CurlPollToFlags(int action) {
|
|
|
|
switch (action) {
|
|
|
|
case CURL_POLL_NONE:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case CURL_POLL_IN:
|
|
|
|
return READ;
|
|
|
|
|
|
|
|
case CURL_POLL_OUT:
|
|
|
|
return WRITE;
|
|
|
|
|
|
|
|
case CURL_POLL_INOUT:
|
|
|
|
return READ|WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
gcc_unreachable();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-15 07:14:36 +01:00
|
|
|
/**
|
2013-11-06 18:50:00 +01:00
|
|
|
* Manager for the global CURLM object.
|
2013-01-15 07:14:36 +01:00
|
|
|
*/
|
2013-11-06 19:01:50 +01:00
|
|
|
class CurlMulti final : private TimeoutMonitor {
|
2013-11-06 18:50:00 +01:00
|
|
|
CURLM *const multi;
|
|
|
|
|
2013-01-15 07:14:36 +01:00
|
|
|
public:
|
2013-11-06 18:50:00 +01:00
|
|
|
CurlMulti(EventLoop &_loop, CURLM *_multi);
|
|
|
|
|
|
|
|
~CurlMulti() {
|
|
|
|
curl_multi_cleanup(multi);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Add(input_curl *c, Error &error);
|
|
|
|
void Remove(input_curl *c);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for finished HTTP responses.
|
|
|
|
*
|
|
|
|
* Runs in the I/O thread. The caller must not hold locks.
|
|
|
|
*/
|
|
|
|
void ReadInfo();
|
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
void Assign(curl_socket_t fd, CurlSocket &cs) {
|
|
|
|
curl_multi_assign(multi, fd, &cs);
|
|
|
|
}
|
2013-01-15 07:14:36 +01:00
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
void SocketAction(curl_socket_t fd, int ev_bitmask);
|
2013-01-15 07:14:36 +01:00
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
void InvalidateSockets() {
|
|
|
|
SocketAction(CURL_SOCKET_TIMEOUT, 0);
|
|
|
|
}
|
2013-01-15 07:14:36 +01:00
|
|
|
|
2013-11-06 18:50:00 +01:00
|
|
|
private:
|
2013-11-06 19:01:50 +01:00
|
|
|
static int TimerFunction(CURLM *multi, long timeout_ms, void *userp);
|
|
|
|
|
|
|
|
virtual void OnTimeout() override;
|
2013-01-15 07:14:36 +01:00
|
|
|
};
|
|
|
|
|
2013-11-23 12:08:46 +01:00
|
|
|
/**
|
|
|
|
* libcurl version number encoded in a 24 bit integer.
|
|
|
|
*/
|
|
|
|
static unsigned curl_version_num;
|
|
|
|
|
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;
|
|
|
|
|
2013-11-06 18:50:00 +01:00
|
|
|
static CurlMulti *curl_multi;
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain http_domain("http");
|
|
|
|
static constexpr Domain curl_domain("curl");
|
|
|
|
static constexpr Domain curlm_domain("curlm");
|
2009-11-14 23:53:04 +01:00
|
|
|
|
2013-11-06 18:50:00 +01:00
|
|
|
CurlMulti::CurlMulti(EventLoop &_loop, CURLM *_multi)
|
2013-11-06 19:01:50 +01:00
|
|
|
:TimeoutMonitor(_loop), multi(_multi)
|
2013-11-06 18:50:00 +01:00
|
|
|
{
|
2013-11-06 19:01:50 +01:00
|
|
|
curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION,
|
|
|
|
CurlSocket::SocketFunction);
|
|
|
|
curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, this);
|
|
|
|
|
|
|
|
curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, TimerFunction);
|
|
|
|
curl_multi_setopt(multi, CURLMOPT_TIMERDATA, this);
|
2013-11-06 18:50:00 +01:00
|
|
|
}
|
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
/**
|
|
|
|
* Find a request by its CURL "easy" handle.
|
|
|
|
*
|
2011-09-15 07:43:35 +02:00
|
|
|
* Runs in the I/O thread. No lock needed.
|
2011-08-24 02:54:05 +02:00
|
|
|
*/
|
2013-11-06 18:21:43 +01:00
|
|
|
gcc_pure
|
2011-08-24 02:54:05 +02:00
|
|
|
static struct input_curl *
|
|
|
|
input_curl_find_request(CURL *easy)
|
|
|
|
{
|
2011-09-15 07:43:35 +02:00
|
|
|
assert(io_thread_inside());
|
|
|
|
|
2013-11-06 18:21:43 +01:00
|
|
|
void *p;
|
|
|
|
CURLcode code = curl_easy_getinfo(easy, CURLINFO_PRIVATE, &p);
|
|
|
|
if (code != CURLE_OK)
|
|
|
|
return nullptr;
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-11-06 18:21:43 +01:00
|
|
|
return (input_curl *)p;
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 22:42:45 +02:00
|
|
|
static void
|
|
|
|
input_curl_resume(struct input_curl *c)
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2011-09-15 07:43:35 +02:00
|
|
|
assert(io_thread_inside());
|
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
if (c->paused) {
|
|
|
|
c->paused = false;
|
2011-09-16 08:50:39 +02:00
|
|
|
curl_easy_pause(c->easy, CURLPAUSE_CONT);
|
2013-11-06 18:50:00 +01:00
|
|
|
curl_multi->InvalidateSockets();
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
int
|
|
|
|
CurlSocket::SocketFunction(gcc_unused CURL *easy,
|
|
|
|
curl_socket_t s, int action,
|
|
|
|
void *userp, void *socketp) {
|
|
|
|
CurlMulti &multi = *(CurlMulti *)userp;
|
|
|
|
CurlSocket *cs = (CurlSocket *)socketp;
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
assert(io_thread_inside());
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
if (action == CURL_POLL_REMOVE) {
|
|
|
|
delete cs;
|
|
|
|
return 0;
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
if (cs == nullptr) {
|
|
|
|
cs = new CurlSocket(multi, io_thread_get(), s);
|
|
|
|
multi.Assign(s, *cs);
|
|
|
|
} else {
|
|
|
|
#ifdef USE_EPOLL
|
|
|
|
/* when using epoll, we need to unregister the socket
|
|
|
|
each time this callback is invoked, because older
|
|
|
|
CURL versions may omit the CURL_POLL_REMOVE call
|
|
|
|
when the socket has been closed and recreated with
|
|
|
|
the same file number (bug found in CURL 7.26, CURL
|
|
|
|
7.33 not affected); in that case, epoll refuses the
|
|
|
|
EPOLL_CTL_MOD because it does not know the new
|
|
|
|
socket yet */
|
|
|
|
cs->Cancel();
|
|
|
|
#endif
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
unsigned flags = CurlPollToFlags(action);
|
|
|
|
if (flags != 0)
|
|
|
|
cs->Schedule(flags);
|
|
|
|
return 0;
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
bool
|
|
|
|
CurlSocket::OnSocketReady(unsigned flags)
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2011-09-15 08:18:50 +02:00
|
|
|
assert(io_thread_inside());
|
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
multi.SocketAction(Get(), FlagsToCurlCSelect(flags));
|
|
|
|
return true;
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
2011-09-15 07:43:35 +02:00
|
|
|
/**
|
|
|
|
* Runs in the I/O thread. No lock needed.
|
|
|
|
*/
|
2013-11-06 18:50:00 +01:00
|
|
|
inline bool
|
|
|
|
CurlMulti::Add(struct input_curl *c, Error &error)
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2011-09-15 07:43:35 +02:00
|
|
|
assert(io_thread_inside());
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(c != nullptr);
|
|
|
|
assert(c->easy != nullptr);
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-11-06 18:50:00 +01:00
|
|
|
CURLMcode mcode = curl_multi_add_handle(multi, c->easy);
|
2011-08-24 02:54:05 +02:00
|
|
|
if (mcode != CURLM_OK) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(curlm_domain, mcode,
|
|
|
|
"curl_multi_add_handle() failed: %s",
|
|
|
|
curl_multi_strerror(mcode));
|
2011-08-24 02:54:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-06 18:50:00 +01:00
|
|
|
InvalidateSockets();
|
2011-08-24 02:54:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-15 07:43:35 +02:00
|
|
|
/**
|
|
|
|
* Call input_curl_easy_add() in the I/O thread. May be called from
|
|
|
|
* any thread. Caller must not hold a mutex.
|
|
|
|
*/
|
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
input_curl_easy_add_indirect(struct input_curl *c, Error &error)
|
2011-09-15 07:43:35 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(c != nullptr);
|
|
|
|
assert(c->easy != nullptr);
|
2011-09-15 07:43:35 +02:00
|
|
|
|
2013-08-07 22:42:45 +02:00
|
|
|
bool result;
|
2013-08-10 18:02:44 +02:00
|
|
|
BlockingCall(io_thread_get(), [c, &error, &result](){
|
2013-11-06 18:50:00 +01:00
|
|
|
result = curl_multi->Add(c, error);
|
2013-08-07 22:42:45 +02:00
|
|
|
});
|
|
|
|
return result;
|
2011-09-15 07:43:35 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 18:50:00 +01:00
|
|
|
inline void
|
|
|
|
CurlMulti::Remove(input_curl *c)
|
|
|
|
{
|
|
|
|
curl_multi_remove_handle(multi, c->easy);
|
|
|
|
}
|
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
/**
|
|
|
|
* Frees the current "libcurl easy" handle, and everything associated
|
|
|
|
* with it.
|
|
|
|
*
|
2011-09-15 07:43:35 +02:00
|
|
|
* Runs in the I/O thread.
|
2011-08-24 02:54:05 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
input_curl_easy_free(struct input_curl *c)
|
|
|
|
{
|
2011-09-15 07:43:35 +02:00
|
|
|
assert(io_thread_inside());
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(c != nullptr);
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (c->easy == nullptr)
|
2011-08-24 02:54:05 +02:00
|
|
|
return;
|
|
|
|
|
2013-11-06 18:50:00 +01:00
|
|
|
curl_multi->Remove(c);
|
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
curl_easy_cleanup(c->easy);
|
2013-10-19 18:19:03 +02:00
|
|
|
c->easy = nullptr;
|
2011-08-24 02:54:05 +02:00
|
|
|
|
|
|
|
curl_slist_free_all(c->request_headers);
|
2013-10-19 18:19:03 +02:00
|
|
|
c->request_headers = nullptr;
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frees the current "libcurl easy" handle, and everything associated
|
|
|
|
* with it.
|
|
|
|
*
|
|
|
|
* The mutex must not be locked.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
input_curl_easy_free_indirect(struct input_curl *c)
|
|
|
|
{
|
2013-08-07 22:42:45 +02:00
|
|
|
BlockingCall(io_thread_get(), [c](){
|
|
|
|
input_curl_easy_free(c);
|
2013-11-06 18:50:00 +01:00
|
|
|
curl_multi->InvalidateSockets();
|
2013-08-07 22:42:45 +02:00
|
|
|
});
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(c->easy == nullptr);
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A HTTP request is finished.
|
|
|
|
*
|
2011-09-15 07:43:35 +02:00
|
|
|
* Runs in the I/O thread. The caller must not hold locks.
|
2011-08-24 02:54:05 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
input_curl_request_done(struct input_curl *c, CURLcode result, long status)
|
|
|
|
{
|
2011-09-15 08:18:50 +02:00
|
|
|
assert(io_thread_inside());
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(c != nullptr);
|
|
|
|
assert(c->easy == nullptr);
|
2013-08-10 18:02:44 +02:00
|
|
|
assert(!c->postponed_error.IsDefined());
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
const ScopeLock protect(c->base.mutex);
|
2011-09-15 07:43:35 +02:00
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
if (result != CURLE_OK) {
|
2013-08-10 18:02:44 +02:00
|
|
|
c->postponed_error.Format(curl_domain, result,
|
|
|
|
"curl failed: %s", c->error);
|
2011-08-24 02:54:05 +02:00
|
|
|
} else if (status < 200 || status >= 300) {
|
2013-08-10 18:02:44 +02:00
|
|
|
c->postponed_error.Format(http_domain, status,
|
|
|
|
"got HTTP status %ld",
|
|
|
|
status);
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
2011-09-15 08:20:41 +02:00
|
|
|
|
2011-09-15 08:17:36 +02:00
|
|
|
c->base.ready = true;
|
2013-01-27 17:20:50 +01:00
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
c->base.cond.broadcast();
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_curl_handle_done(CURL *easy_handle, CURLcode result)
|
|
|
|
{
|
|
|
|
struct input_curl *c = input_curl_find_request(easy_handle);
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(c != nullptr);
|
2011-08-24 02:54:05 +02:00
|
|
|
|
|
|
|
long status = 0;
|
|
|
|
curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &status);
|
|
|
|
|
|
|
|
input_curl_easy_free(c);
|
|
|
|
input_curl_request_done(c, result, status);
|
|
|
|
}
|
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
void
|
|
|
|
CurlMulti::SocketAction(curl_socket_t fd, int ev_bitmask)
|
|
|
|
{
|
|
|
|
int running_handles;
|
|
|
|
CURLMcode mcode = curl_multi_socket_action(multi, fd, ev_bitmask,
|
|
|
|
&running_handles);
|
|
|
|
if (mcode != CURLM_OK)
|
|
|
|
FormatError(curlm_domain,
|
|
|
|
"curl_multi_socket_action() failed: %s",
|
|
|
|
curl_multi_strerror(mcode));
|
|
|
|
|
|
|
|
ReadInfo();
|
|
|
|
}
|
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
/**
|
|
|
|
* Check for finished HTTP responses.
|
|
|
|
*
|
2011-09-15 07:43:35 +02:00
|
|
|
* Runs in the I/O thread. The caller must not hold locks.
|
2011-08-24 02:54:05 +02:00
|
|
|
*/
|
2013-11-06 18:50:00 +01:00
|
|
|
inline void
|
|
|
|
CurlMulti::ReadInfo()
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2011-09-15 08:18:50 +02:00
|
|
|
assert(io_thread_inside());
|
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
CURLMsg *msg;
|
|
|
|
int msgs_in_queue;
|
|
|
|
|
2013-11-06 18:50:00 +01:00
|
|
|
while ((msg = curl_multi_info_read(multi,
|
2013-10-19 18:19:03 +02:00
|
|
|
&msgs_in_queue)) != nullptr) {
|
2011-08-24 02:54:05 +02:00
|
|
|
if (msg->msg == CURLMSG_DONE)
|
|
|
|
input_curl_handle_done(msg->easy_handle, msg->data.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
int
|
|
|
|
CurlMulti::TimerFunction(gcc_unused CURLM *_multi, long timeout_ms, void *userp)
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2013-11-06 19:01:50 +01:00
|
|
|
CurlMulti &multi = *(CurlMulti *)userp;
|
|
|
|
assert(_multi == multi.multi);
|
2011-09-15 08:18:50 +02:00
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
if (timeout_ms < 0) {
|
|
|
|
multi.Cancel();
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
if (timeout_ms >= 0 && timeout_ms < 10)
|
|
|
|
/* CURL 7.21.1 likes to report "timeout=0", which
|
|
|
|
means we're running in a busy loop. Quite a bad
|
|
|
|
idea to waste so much CPU. Let's use a lower limit
|
|
|
|
of 10ms. */
|
|
|
|
timeout_ms = 10;
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-11-06 19:01:50 +01:00
|
|
|
multi.Schedule(timeout_ms);
|
|
|
|
return 0;
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
2013-01-15 07:14:36 +01:00
|
|
|
void
|
2013-11-06 19:01:50 +01:00
|
|
|
CurlMulti::OnTimeout()
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2013-11-06 19:01:50 +01:00
|
|
|
SocketAction(CURL_SOCKET_TIMEOUT, 0);
|
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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-03-02 20:45:50 +01:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
input_curl_init(const config_param ¶m, Error &error)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
|
|
|
CURLcode code = curl_global_init(CURL_GLOBAL_ALL);
|
2009-03-02 20:45:50 +01:00
|
|
|
if (code != CURLE_OK) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(curl_domain, code,
|
|
|
|
"curl_global_init() failed: %s",
|
|
|
|
curl_easy_strerror(code));
|
2009-03-02 20:45:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
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);
|
2013-11-23 12:08:46 +01:00
|
|
|
|
|
|
|
curl_version_num = version_info->version_num;
|
2013-11-23 12:02:17 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-08-04 13:47:48 +02:00
|
|
|
proxy = param.GetBlockValue("proxy");
|
|
|
|
proxy_port = param.GetBlockValue("proxy_port", 0u);
|
|
|
|
proxy_user = param.GetBlockValue("proxy_user");
|
|
|
|
proxy_password = param.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 */
|
2013-10-19 18:19:03 +02:00
|
|
|
proxy = config_get_string(CONF_HTTP_PROXY_HOST, nullptr);
|
2009-04-25 13:35:04 +02:00
|
|
|
proxy_port = config_get_positive(CONF_HTTP_PROXY_PORT, 0);
|
2013-10-19 18:19:03 +02:00
|
|
|
proxy_user = config_get_string(CONF_HTTP_PROXY_USER, nullptr);
|
2009-04-25 13:35:04 +02:00
|
|
|
proxy_password = config_get_string(CONF_HTTP_PROXY_PASSWORD,
|
|
|
|
"");
|
|
|
|
}
|
|
|
|
|
2013-11-06 18:50:00 +01:00
|
|
|
CURLM *multi = curl_multi_init();
|
|
|
|
if (multi == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(curl_domain, 0, "curl_multi_init() failed");
|
2011-08-24 02:54:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-06 18:50:00 +01:00
|
|
|
curl_multi = new CurlMulti(io_thread_get(), multi);
|
2009-03-02 20:45:50 +01:00
|
|
|
return true;
|
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
|
|
|
{
|
2013-08-07 22:42:45 +02:00
|
|
|
BlockingCall(io_thread_get(), [](){
|
2013-11-06 18:50:00 +01:00
|
|
|
delete curl_multi;
|
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);
|
|
|
|
|
|
|
|
curl_global_cleanup();
|
|
|
|
}
|
|
|
|
|
2011-08-23 20:46:51 +02:00
|
|
|
/**
|
|
|
|
* Determine the total sizes of all buffers, including portions that
|
|
|
|
* have already been consumed.
|
2011-08-24 02:54:05 +02:00
|
|
|
*
|
|
|
|
* The caller must lock the mutex.
|
2011-08-23 20:46:51 +02:00
|
|
|
*/
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_pure
|
2011-08-23 20:46:51 +02:00
|
|
|
static size_t
|
|
|
|
curl_total_buffer_size(const struct input_curl *c)
|
|
|
|
{
|
|
|
|
size_t total = 0;
|
|
|
|
|
2013-01-10 23:00:23 +01:00
|
|
|
for (const auto &i : c->buffers)
|
|
|
|
total += i.TotalSize();
|
2011-08-23 20:46:51 +02:00
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2013-01-10 22:18:23 +01:00
|
|
|
input_curl::~input_curl()
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
2013-07-30 20:11:57 +02:00
|
|
|
delete tag;
|
|
|
|
|
2013-01-10 22:18:23 +01:00
|
|
|
input_curl_easy_free_indirect(this);
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 21:06:12 +02:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
input_curl_check(InputStream *is, Error &error)
|
2011-09-16 21:06:12 +02:00
|
|
|
{
|
|
|
|
struct input_curl *c = (struct input_curl *)is;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
bool success = !c->postponed_error.IsDefined();
|
2011-09-16 21:06:12 +02:00
|
|
|
if (!success) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error = std::move(c->postponed_error);
|
|
|
|
c->postponed_error.Clear();
|
2011-09-16 21:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
static Tag *
|
2013-10-23 22:08:59 +02:00
|
|
|
input_curl_tag(InputStream *is)
|
2009-01-03 23:29:45 +01:00
|
|
|
{
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_curl *c = (struct input_curl *)is;
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *tag = c->tag;
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
c->tag = nullptr;
|
2009-01-03 23:29:45 +01:00
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2009-10-11 23:09:38 +02:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
fill_buffer(struct input_curl *c, Error &error)
|
2009-10-11 23:09:38 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
while (c->easy != nullptr && c->buffers.empty())
|
2013-01-28 23:35:01 +01:00
|
|
|
c->base.cond.wait(c->base.mutex);
|
2009-10-11 23:09:38 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (c->postponed_error.IsDefined()) {
|
|
|
|
error = std::move(c->postponed_error);
|
|
|
|
c->postponed_error.Clear();
|
2011-08-24 02:54:05 +02:00
|
|
|
return false;
|
2009-10-11 23:09:38 +02:00
|
|
|
}
|
|
|
|
|
2013-01-10 23:00:23 +01:00
|
|
|
return !c->buffers.empty();
|
2008-11-21 16:57:55 +01:00
|
|
|
}
|
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
static size_t
|
2013-01-10 23:00:23 +01:00
|
|
|
read_from_buffer(IcyMetaDataParser &icy, std::list<CurlInputBuffer> &buffers,
|
2009-12-30 21:41:45 +01:00
|
|
|
void *dest0, size_t length)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
2013-01-10 23:00:23 +01:00
|
|
|
auto &buffer = buffers.front();
|
2013-01-10 10:14:29 +01:00
|
|
|
uint8_t *dest = (uint8_t *)dest0;
|
2009-01-03 23:55:03 +01:00
|
|
|
size_t nbytes = 0;
|
|
|
|
|
2013-01-10 23:00:23 +01:00
|
|
|
if (length > buffer.Available())
|
|
|
|
length = buffer.Available();
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2009-01-03 23:55:03 +01:00
|
|
|
while (true) {
|
|
|
|
size_t chunk;
|
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
chunk = icy.Data(length);
|
2009-01-03 23:55:03 +01:00
|
|
|
if (chunk > 0) {
|
2013-01-10 23:00:23 +01:00
|
|
|
const bool empty = !buffer.Read(dest, chunk);
|
2009-01-03 23:55:03 +01:00
|
|
|
|
|
|
|
nbytes += chunk;
|
|
|
|
dest += chunk;
|
|
|
|
length -= chunk;
|
|
|
|
|
2013-01-10 23:00:23 +01:00
|
|
|
if (empty) {
|
|
|
|
buffers.pop_front();
|
2009-01-03 23:55:03 +01:00
|
|
|
break;
|
2013-01-10 23:00:23 +01:00
|
|
|
}
|
2013-01-15 23:39:32 +01:00
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
break;
|
2009-01-03 23:55:03 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 23:00:23 +01:00
|
|
|
chunk = icy.Meta(buffer.Begin(), length);
|
2009-01-03 23:55:03 +01:00
|
|
|
if (chunk > 0) {
|
2013-01-10 23:00:23 +01:00
|
|
|
const bool empty = !buffer.Consume(chunk);
|
2009-01-03 23:55:03 +01:00
|
|
|
|
|
|
|
length -= chunk;
|
|
|
|
|
2013-01-10 23:00:23 +01:00
|
|
|
if (empty) {
|
|
|
|
buffers.pop_front();
|
2009-01-03 23:55:03 +01:00
|
|
|
break;
|
2013-01-10 23:00:23 +01:00
|
|
|
}
|
2013-01-15 23:39:32 +01:00
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
break;
|
2009-01-03 23:55:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
copy_icy_tag(struct input_curl *c)
|
|
|
|
{
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *tag = c->icy.ReadTag();
|
2009-01-03 23:55:03 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (tag == nullptr)
|
2009-01-03 23:55:03 +01:00
|
|
|
return;
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
delete c->tag;
|
2009-01-03 23:55:03 +01:00
|
|
|
|
2013-10-19 17:18:56 +02:00
|
|
|
if (!c->meta_name.empty() && !tag->HasType(TAG_NAME))
|
|
|
|
tag->AddItem(TAG_NAME, c->meta_name.c_str());
|
2009-01-03 23:55:03 +01:00
|
|
|
|
|
|
|
c->tag = tag;
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
input_curl_available(InputStream *is)
|
2011-09-14 21:46:41 +02:00
|
|
|
{
|
|
|
|
struct input_curl *c = (struct input_curl *)is;
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
return c->postponed_error.IsDefined() || c->easy == nullptr ||
|
2013-01-10 23:00:23 +01:00
|
|
|
!c->buffers.empty();
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
static size_t
|
2013-10-23 22:08:59 +02:00
|
|
|
input_curl_read(InputStream *is, void *ptr, size_t size,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_curl *c = (struct input_curl *)is;
|
2009-10-11 23:09:38 +02:00
|
|
|
bool success;
|
2008-10-26 19:32:43 +01:00
|
|
|
size_t nbytes = 0;
|
2013-01-10 10:14:29 +01:00
|
|
|
char *dest = (char *)ptr;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2009-10-11 23:13:49 +02:00
|
|
|
do {
|
|
|
|
/* fill the buffer */
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
success = fill_buffer(c, error);
|
2011-09-14 21:46:41 +02:00
|
|
|
if (!success)
|
2009-10-11 23:13:49 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* send buffer contents */
|
|
|
|
|
2013-01-10 23:00:23 +01:00
|
|
|
while (size > 0 && !c->buffers.empty()) {
|
2013-01-10 22:33:16 +01:00
|
|
|
size_t copy = read_from_buffer(c->icy, c->buffers,
|
2009-12-30 21:41:45 +01:00
|
|
|
dest + nbytes, size);
|
2009-10-11 23:13:49 +02:00
|
|
|
|
|
|
|
nbytes += copy;
|
|
|
|
size -= copy;
|
|
|
|
}
|
|
|
|
} while (nbytes == 0);
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
if (c->icy.IsDefined())
|
2009-01-03 23:55:03 +01:00
|
|
|
copy_icy_tag(c);
|
|
|
|
|
2013-10-17 09:43:55 +02:00
|
|
|
is->offset += (InputPlugin::offset_type)nbytes;
|
2008-11-02 17:06:15 +01:00
|
|
|
|
2011-09-16 08:54:47 +02:00
|
|
|
if (c->paused && curl_total_buffer_size(c) < CURL_RESUME_AT) {
|
2013-01-28 23:35:01 +01:00
|
|
|
c->base.mutex.unlock();
|
2013-08-07 22:42:45 +02:00
|
|
|
|
|
|
|
BlockingCall(io_thread_get(), [c](){
|
|
|
|
input_curl_resume(c);
|
|
|
|
});
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
c->base.mutex.lock();
|
2011-09-16 08:45:39 +02:00
|
|
|
}
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2008-10-26 20:54:52 +01:00
|
|
|
static void
|
2013-10-23 22:08:59 +02:00
|
|
|
input_curl_close(InputStream *is)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_curl *c = (struct input_curl *)is;
|
|
|
|
|
2013-01-10 22:18:23 +01:00
|
|
|
delete c;
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
2008-10-26 20:56:46 +01:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
input_curl_eof(gcc_unused InputStream *is)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_curl *c = (struct input_curl *)is;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
return c->easy == nullptr && c->buffers.empty();
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** called by curl when new data is available */
|
|
|
|
static size_t
|
|
|
|
input_curl_headerfunction(void *ptr, size_t size, size_t nmemb, void *stream)
|
|
|
|
{
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_curl *c = (struct input_curl *)stream;
|
2008-10-26 19:32:43 +01:00
|
|
|
char name[64];
|
|
|
|
|
|
|
|
size *= nmemb;
|
|
|
|
|
2013-01-10 10:14:29 +01:00
|
|
|
const char *header = (const char *)ptr;
|
|
|
|
const char *end = header + size;
|
|
|
|
|
|
|
|
const char *value = (const char *)memchr(header, ':', size);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (value == nullptr || (size_t)(value - header) >= sizeof(name))
|
2008-10-26 19:32:43 +01:00
|
|
|
return size;
|
|
|
|
|
2008-10-26 21:12:56 +01:00
|
|
|
memcpy(name, header, value - header);
|
|
|
|
name[value - header] = 0;
|
|
|
|
|
|
|
|
/* skip the colon */
|
|
|
|
|
|
|
|
++value;
|
|
|
|
|
|
|
|
/* strip the value */
|
|
|
|
|
2013-10-19 15:25:32 +02:00
|
|
|
while (value < end && IsWhitespaceOrNull(*value))
|
2008-10-26 21:12:56 +01:00
|
|
|
++value;
|
|
|
|
|
2013-10-19 15:25:32 +02:00
|
|
|
while (end > value && IsWhitespaceOrNull(end[-1]))
|
2008-10-26 21:12:56 +01:00
|
|
|
--end;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2013-10-20 23:09:51 +02:00
|
|
|
if (StringEqualsCaseASCII(name, "accept-ranges")) {
|
2009-01-03 23:55:03 +01:00
|
|
|
/* a stream with icy-metadata is not seekable */
|
2013-01-10 22:33:16 +01:00
|
|
|
if (!c->icy.IsDefined())
|
2009-12-30 23:27:37 +01:00
|
|
|
c->base.seekable = true;
|
2013-10-20 23:09:51 +02:00
|
|
|
} else if (StringEqualsCaseASCII(name, "content-length")) {
|
2008-10-26 21:12:56 +01:00
|
|
|
char buffer[64];
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2008-10-26 21:12:56 +01:00
|
|
|
if ((size_t)(end - header) >= sizeof(buffer))
|
2008-10-26 19:32:43 +01:00
|
|
|
return size;
|
|
|
|
|
2008-10-26 21:12:56 +01:00
|
|
|
memcpy(buffer, value, end - value);
|
|
|
|
buffer[end - value] = 0;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2013-10-21 09:48:31 +02:00
|
|
|
c->base.size = c->base.offset + ParseUint64(buffer);
|
2013-10-20 23:09:51 +02:00
|
|
|
} else if (StringEqualsCaseASCII(name, "content-type")) {
|
2013-01-28 23:41:45 +01:00
|
|
|
c->base.mime.assign(value, end);
|
2013-10-20 23:09:51 +02:00
|
|
|
} else if (StringEqualsCaseASCII(name, "icy-name") ||
|
|
|
|
StringEqualsCaseASCII(name, "ice-name") ||
|
|
|
|
StringEqualsCaseASCII(name, "x-audiocast-name")) {
|
2013-10-19 17:18:56 +02:00
|
|
|
c->meta_name.assign(value, end);
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
delete c->tag;
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
c->tag = new Tag();
|
2013-10-19 17:18:56 +02:00
|
|
|
c->tag->AddItem(TAG_NAME, c->meta_name.c_str());
|
2013-10-20 23:09:51 +02:00
|
|
|
} else if (StringEqualsCaseASCII(name, "icy-metaint")) {
|
2009-01-03 23:55:03 +01:00
|
|
|
char buffer[64];
|
|
|
|
size_t icy_metaint;
|
|
|
|
|
|
|
|
if ((size_t)(end - header) >= sizeof(buffer) ||
|
2013-01-10 22:33:16 +01:00
|
|
|
c->icy.IsDefined())
|
2009-01-03 23:55:03 +01:00
|
|
|
return size;
|
|
|
|
|
|
|
|
memcpy(buffer, value, end - value);
|
|
|
|
buffer[end - value] = 0;
|
|
|
|
|
2013-10-21 09:48:31 +02:00
|
|
|
icy_metaint = ParseUint64(buffer);
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(curl_domain, "icy-metaint=%zu", icy_metaint);
|
2009-01-03 23:55:03 +01:00
|
|
|
|
|
|
|
if (icy_metaint > 0) {
|
2013-01-10 22:33:16 +01:00
|
|
|
c->icy.Start(icy_metaint);
|
2009-01-03 23:55:03 +01:00
|
|
|
|
|
|
|
/* a stream with icy-metadata is not
|
|
|
|
seekable */
|
2009-12-30 23:27:37 +01:00
|
|
|
c->base.seekable = false;
|
2009-01-03 23:55:03 +01:00
|
|
|
}
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** called by curl when new data is available */
|
|
|
|
static size_t
|
|
|
|
input_curl_writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
|
|
|
|
{
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_curl *c = (struct input_curl *)stream;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
|
|
|
size *= nmemb;
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
const ScopeLock protect(c->base.mutex);
|
2011-09-15 07:43:35 +02:00
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
if (curl_total_buffer_size(c) + size >= CURL_MAX_BUFFERED) {
|
|
|
|
c->paused = true;
|
|
|
|
return CURL_WRITEFUNC_PAUSE;
|
|
|
|
}
|
|
|
|
|
2013-01-10 23:00:23 +01:00
|
|
|
c->buffers.emplace_back(ptr, size);
|
2009-12-30 23:27:37 +01:00
|
|
|
c->base.ready = true;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
c->base.cond.broadcast();
|
2008-10-26 19:32:43 +01:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
input_curl_easy_init(struct input_curl *c, Error &error)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
|
|
|
CURLcode code;
|
|
|
|
|
|
|
|
c->easy = curl_easy_init();
|
2013-10-19 18:19:03 +02:00
|
|
|
if (c->easy == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(curl_domain, "curl_easy_init() failed");
|
2008-10-26 19:32:43 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-06 18:21:43 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_PRIVATE, (void *)c);
|
2008-11-30 13:06:18 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_USERAGENT,
|
|
|
|
"Music Player Daemon " VERSION);
|
2008-10-26 19:32:43 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_HEADERFUNCTION,
|
|
|
|
input_curl_headerfunction);
|
2009-12-30 23:27:37 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_WRITEHEADER, c);
|
2008-10-26 19:32:43 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_WRITEFUNCTION,
|
|
|
|
input_curl_writefunction);
|
2009-12-30 23:27:37 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_WRITEDATA, c);
|
2008-10-26 19:32:43 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_HTTP200ALIASES, http_200_aliases);
|
2008-11-30 13:06:21 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_FOLLOWLOCATION, 1);
|
2011-01-29 08:43:30 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_NETRC, 1);
|
2008-11-30 13:06:21 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_MAXREDIRS, 5);
|
2008-11-06 06:36:25 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_FAILONERROR, true);
|
2008-11-06 06:48:30 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_ERRORBUFFER, c->error);
|
2011-08-26 19:28:09 +02:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_NOPROGRESS, 1l);
|
|
|
|
curl_easy_setopt(c->easy, CURLOPT_NOSIGNAL, 1l);
|
|
|
|
curl_easy_setopt(c->easy, CURLOPT_CONNECTTIMEOUT, 10l);
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (proxy != nullptr)
|
2009-04-25 13:35:04 +02:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_PROXY, proxy);
|
2009-01-13 22:57:05 +01:00
|
|
|
|
2009-04-25 13:35:04 +02:00
|
|
|
if (proxy_port > 0)
|
|
|
|
curl_easy_setopt(c->easy, 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);
|
2009-01-13 22:57:05 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_PROXYUSERPWD, proxy_auth_str);
|
|
|
|
}
|
|
|
|
|
2013-01-28 23:41:45 +01:00
|
|
|
code = curl_easy_setopt(c->easy, CURLOPT_URL, c->base.uri.c_str());
|
2009-11-14 23:53:04 +01:00
|
|
|
if (code != CURLE_OK) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(curl_domain, code,
|
|
|
|
"curl_easy_setopt() failed: %s",
|
|
|
|
curl_easy_strerror(code));
|
2008-10-26 19:32:43 +01:00
|
|
|
return false;
|
2009-11-14 23:53:04 +01:00
|
|
|
}
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
c->request_headers = nullptr;
|
2008-10-26 19:32:43 +01:00
|
|
|
c->request_headers = curl_slist_append(c->request_headers,
|
|
|
|
"Icy-Metadata: 1");
|
|
|
|
curl_easy_setopt(c->easy, CURLOPT_HTTPHEADER, c->request_headers);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-26 20:56:46 +01:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
input_curl_seek(InputStream *is, InputPlugin::offset_type offset,
|
2013-10-17 09:43:55 +02:00
|
|
|
int whence,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_curl *c = (struct input_curl *)is;
|
2008-10-26 19:32:43 +01:00
|
|
|
bool ret;
|
|
|
|
|
2009-01-15 16:12:22 +01:00
|
|
|
assert(is->ready);
|
|
|
|
|
2009-12-30 21:41:45 +01:00
|
|
|
if (whence == SEEK_SET && offset == is->offset)
|
|
|
|
/* no-op */
|
|
|
|
return true;
|
2008-11-02 17:06:15 +01:00
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
if (!is->seekable)
|
2008-10-26 20:56:46 +01:00
|
|
|
return false;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
|
|
|
/* calculate the absolute offset */
|
|
|
|
|
|
|
|
switch (whence) {
|
|
|
|
case SEEK_SET:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
2008-11-21 16:56:10 +01:00
|
|
|
offset += is->offset;
|
2008-10-26 19:32:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
2008-11-16 20:42:08 +01:00
|
|
|
if (is->size < 0)
|
|
|
|
/* stream size is not known */
|
|
|
|
return false;
|
|
|
|
|
2008-11-21 16:56:10 +01:00
|
|
|
offset += is->size;
|
2008-10-26 19:32:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2008-10-26 20:56:46 +01:00
|
|
|
return false;
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
2008-11-21 16:56:10 +01:00
|
|
|
if (offset < 0)
|
2008-10-26 20:56:46 +01:00
|
|
|
return false;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2008-11-21 17:10:20 +01:00
|
|
|
/* check if we can fast-forward the buffer */
|
|
|
|
|
2013-01-10 23:00:23 +01:00
|
|
|
while (offset > is->offset && !c->buffers.empty()) {
|
|
|
|
auto &buffer = c->buffers.front();
|
|
|
|
size_t length = buffer.Available();
|
2013-10-17 09:43:55 +02:00
|
|
|
if (offset - is->offset < (InputPlugin::offset_type)length)
|
2008-11-21 17:10:20 +01:00
|
|
|
length = offset - is->offset;
|
|
|
|
|
2013-01-10 23:00:23 +01:00
|
|
|
const bool empty = !buffer.Consume(length);
|
|
|
|
if (empty)
|
|
|
|
c->buffers.pop_front();
|
2009-01-07 16:30:43 +01:00
|
|
|
|
2008-11-21 17:10:20 +01:00
|
|
|
is->offset += length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset == is->offset)
|
|
|
|
return true;
|
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
/* close the old connection and open a new one */
|
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
c->base.mutex.unlock();
|
2011-09-14 21:46:41 +02:00
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
input_curl_easy_free_indirect(c);
|
2013-01-10 23:00:23 +01:00
|
|
|
c->buffers.clear();
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2008-11-21 16:56:10 +01:00
|
|
|
is->offset = offset;
|
2008-11-20 12:45:17 +01:00
|
|
|
if (is->offset == is->size) {
|
|
|
|
/* seek to EOF: simulate empty result; avoid
|
|
|
|
triggering a "416 Requested Range Not Satisfiable"
|
|
|
|
response */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
ret = input_curl_easy_init(c, error);
|
2008-10-26 19:32:43 +01:00
|
|
|
if (!ret)
|
2008-10-26 20:56:46 +01:00
|
|
|
return false;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
|
|
|
/* send the "Range" header */
|
|
|
|
|
|
|
|
if (is->offset > 0) {
|
2013-10-19 17:15:17 +02:00
|
|
|
sprintf(c->range, "%lld-", (long long)is->offset);
|
2008-10-26 19:32:43 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_RANGE, c->range);
|
|
|
|
}
|
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
c->base.ready = false;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!input_curl_easy_add_indirect(c, error))
|
2008-10-26 20:56:46 +01:00
|
|
|
return false;
|
2011-09-15 07:43:35 +02:00
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
c->base.mutex.lock();
|
2011-08-24 02:54:05 +02:00
|
|
|
|
|
|
|
while (!c->base.ready)
|
2013-01-28 23:35:01 +01:00
|
|
|
c->base.cond.wait(c->base.mutex);
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (c->postponed_error.IsDefined()) {
|
|
|
|
error = std::move(c->postponed_error);
|
|
|
|
c->postponed_error.Clear();
|
2011-08-24 02:54:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
static InputStream *
|
2013-01-27 17:20:50 +01:00
|
|
|
input_curl_open(const char *url, Mutex &mutex, Cond &cond,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2008-10-26 19:32:43 +01:00
|
|
|
{
|
2013-10-14 22:00:21 +02:00
|
|
|
if (memcmp(url, "http://", 7) != 0 &&
|
|
|
|
memcmp(url, "https://", 8) != 0)
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2008-10-27 10:10:24 +01:00
|
|
|
|
2013-01-10 22:18:23 +01:00
|
|
|
struct input_curl *c = new input_curl(url, mutex, cond);
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!input_curl_easy_init(c, error)) {
|
2013-01-10 22:18:23 +01:00
|
|
|
delete c;
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2008-10-26 19:32:43 +01:00
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!input_curl_easy_add_indirect(c, error)) {
|
2013-01-10 22:18:23 +01:00
|
|
|
delete c;
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-01-15 16:12:11 +01:00
|
|
|
}
|
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
return &c->base;
|
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,
|
|
|
|
input_curl_close,
|
|
|
|
input_curl_check,
|
|
|
|
nullptr,
|
|
|
|
input_curl_tag,
|
|
|
|
input_curl_available,
|
|
|
|
input_curl_read,
|
|
|
|
input_curl_eof,
|
|
|
|
input_curl_seek,
|
2008-10-26 20:38:44 +01:00
|
|
|
};
|