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 "InputInternal.hxx"
|
|
|
|
#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-01-15 07:14:36 +01:00
|
|
|
#include "event/MultiSocketMonitor.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-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.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
|
|
|
#include <forward_list>
|
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <glib.h>
|
|
|
|
|
2013-01-10 20:46:47 +01:00
|
|
|
#if LIBCURL_VERSION_NUM < 0x071200
|
|
|
|
#error libcurl is too old
|
|
|
|
#endif
|
|
|
|
|
2009-02-17 22:58:27 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "input_curl"
|
|
|
|
|
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 {
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_stream base;
|
|
|
|
|
2008-10-26 19:32:43 +01:00
|
|
|
/* some buffers which were passed to libcurl, which we have
|
|
|
|
too free */
|
2013-01-10 22:21:30 +01:00
|
|
|
char *range;
|
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 */
|
|
|
|
char *meta_name;
|
|
|
|
|
|
|
|
/** the tag object ready to be requested via
|
2013-09-05 00:06:31 +02:00
|
|
|
input_stream::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),
|
|
|
|
range(nullptr), request_headers(nullptr),
|
2013-01-10 22:18:23 +01:00
|
|
|
paused(false),
|
|
|
|
meta_name(nullptr),
|
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-01-15 07:14:36 +01:00
|
|
|
/**
|
|
|
|
* This class monitors all CURL file descriptors.
|
|
|
|
*/
|
|
|
|
class CurlSockets final : private MultiSocketMonitor {
|
|
|
|
public:
|
|
|
|
CurlSockets(EventLoop &_loop)
|
|
|
|
:MultiSocketMonitor(_loop) {}
|
|
|
|
|
|
|
|
using MultiSocketMonitor::InvalidateSockets;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void UpdateSockets();
|
|
|
|
|
2013-08-10 10:57:00 +02:00
|
|
|
virtual int PrepareSockets() override;
|
2013-01-15 07:14:36 +01:00
|
|
|
virtual void DispatchSockets() 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;
|
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
static struct {
|
|
|
|
CURLM *multi;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A linked list of all active HTTP requests. An active
|
|
|
|
* request is one that doesn't have the "eof" flag set.
|
|
|
|
*/
|
2013-01-10 20:52:25 +01:00
|
|
|
std::forward_list<input_curl *> requests;
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-01-15 07:14:36 +01:00
|
|
|
CurlSockets *sockets;
|
2011-09-15 10:24:50 +02:00
|
|
|
} curl;
|
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
|
|
|
|
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
|
|
|
*/
|
|
|
|
static struct input_curl *
|
|
|
|
input_curl_find_request(CURL *easy)
|
|
|
|
{
|
2011-09-15 07:43:35 +02:00
|
|
|
assert(io_thread_inside());
|
|
|
|
|
2013-01-10 20:52:25 +01:00
|
|
|
for (auto c : curl.requests)
|
2011-08-24 02:54:05 +02:00
|
|
|
if (c->easy == easy)
|
|
|
|
return c;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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-09-26 17:25:15 +02:00
|
|
|
curl.sockets->InvalidateSockets();
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the GLib event bit mask for one file descriptor,
|
|
|
|
* obtained from three #fd_set objects filled by curl_multi_fdset().
|
|
|
|
*/
|
2013-01-15 07:14:36 +01:00
|
|
|
static unsigned
|
2011-08-24 02:54:05 +02:00
|
|
|
input_curl_fd_events(int fd, fd_set *rfds, fd_set *wfds, fd_set *efds)
|
|
|
|
{
|
|
|
|
gushort events = 0;
|
|
|
|
|
|
|
|
if (FD_ISSET(fd, rfds)) {
|
|
|
|
events |= G_IO_IN | G_IO_HUP | G_IO_ERR;
|
|
|
|
FD_CLR(fd, rfds);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(fd, wfds)) {
|
|
|
|
events |= G_IO_OUT | G_IO_ERR;
|
|
|
|
FD_CLR(fd, wfds);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(fd, efds)) {
|
|
|
|
events |= G_IO_HUP | G_IO_ERR;
|
|
|
|
FD_CLR(fd, efds);
|
|
|
|
}
|
|
|
|
|
|
|
|
return events;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates all registered GPollFD objects, unregisters old ones,
|
|
|
|
* registers new ones.
|
|
|
|
*
|
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-01-15 07:14:36 +01:00
|
|
|
void
|
|
|
|
CurlSockets::UpdateSockets()
|
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
|
|
|
fd_set rfds, wfds, efds;
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_ZERO(&efds);
|
|
|
|
|
|
|
|
int max_fd;
|
|
|
|
CURLMcode mcode = curl_multi_fdset(curl.multi, &rfds, &wfds,
|
|
|
|
&efds, &max_fd);
|
|
|
|
if (mcode != CURLM_OK) {
|
|
|
|
g_warning("curl_multi_fdset() failed: %s\n",
|
|
|
|
curl_multi_strerror(mcode));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-15 07:14:36 +01:00
|
|
|
UpdateSocketList([&rfds, &wfds, &efds](int fd){
|
|
|
|
return input_curl_fd_events(fd, &rfds,
|
|
|
|
&wfds, &efds);
|
|
|
|
});
|
2011-08-24 02:54:05 +02:00
|
|
|
|
|
|
|
for (int fd = 0; fd <= max_fd; ++fd) {
|
2013-01-15 07:14:36 +01:00
|
|
|
unsigned events = input_curl_fd_events(fd, &rfds, &wfds, &efds);
|
|
|
|
if (events != 0)
|
|
|
|
AddSocket(fd, events);
|
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.
|
|
|
|
*/
|
2011-08-24 02:54:05 +02:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
input_curl_easy_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());
|
2011-08-24 02:54:05 +02:00
|
|
|
assert(c != NULL);
|
|
|
|
assert(c->easy != NULL);
|
|
|
|
assert(input_curl_find_request(c->easy) == NULL);
|
|
|
|
|
2013-01-10 20:52:25 +01:00
|
|
|
curl.requests.push_front(c);
|
2011-08-24 02:54:05 +02:00
|
|
|
|
|
|
|
CURLMcode mcode = curl_multi_add_handle(curl.multi, c->easy);
|
|
|
|
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-01-15 07:14:36 +01:00
|
|
|
curl.sockets->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
|
|
|
{
|
|
|
|
assert(c != NULL);
|
|
|
|
assert(c->easy != NULL);
|
|
|
|
|
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](){
|
|
|
|
result = input_curl_easy_add(c, error);
|
2013-08-07 22:42:45 +02:00
|
|
|
});
|
|
|
|
return result;
|
2011-09-15 07:43:35 +02:00
|
|
|
}
|
|
|
|
|
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());
|
2011-08-24 02:54:05 +02:00
|
|
|
assert(c != NULL);
|
|
|
|
|
|
|
|
if (c->easy == NULL)
|
|
|
|
return;
|
|
|
|
|
2013-01-10 20:52:25 +01:00
|
|
|
curl.requests.remove(c);
|
2011-08-24 02:54:05 +02:00
|
|
|
|
|
|
|
curl_multi_remove_handle(curl.multi, c->easy);
|
|
|
|
curl_easy_cleanup(c->easy);
|
|
|
|
c->easy = NULL;
|
|
|
|
|
|
|
|
curl_slist_free_all(c->request_headers);
|
|
|
|
c->request_headers = NULL;
|
|
|
|
|
|
|
|
g_free(c->range);
|
|
|
|
c->range = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
curl.sockets->InvalidateSockets();
|
|
|
|
});
|
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
assert(c->easy == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abort and free all HTTP requests.
|
|
|
|
*
|
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
|
2013-08-10 18:02:44 +02:00
|
|
|
input_curl_abort_all_requests(const Error &error)
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2011-09-15 08:18:50 +02:00
|
|
|
assert(io_thread_inside());
|
2013-08-10 18:02:44 +02:00
|
|
|
assert(error.IsDefined());
|
2011-09-15 08:20:41 +02:00
|
|
|
|
2013-01-10 20:52:25 +01:00
|
|
|
while (!curl.requests.empty()) {
|
|
|
|
struct input_curl *c = curl.requests.front();
|
2013-08-10 18:02:44 +02:00
|
|
|
assert(!c->postponed_error.IsDefined());
|
2011-09-15 08:20:41 +02:00
|
|
|
|
|
|
|
input_curl_easy_free(c);
|
2011-09-15 10:24:50 +02:00
|
|
|
|
2013-01-28 23:35:01 +01:00
|
|
|
const ScopeLock protect(c->base.mutex);
|
2013-01-27 17:20:50 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
c->postponed_error.Set(error);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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());
|
2011-09-15 08:20:41 +02:00
|
|
|
assert(c != NULL);
|
2011-08-24 02:54:05 +02:00
|
|
|
assert(c->easy == NULL);
|
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);
|
|
|
|
assert(c != NULL);
|
|
|
|
|
|
|
|
long status = 0;
|
|
|
|
curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &status);
|
|
|
|
|
|
|
|
input_curl_easy_free(c);
|
|
|
|
input_curl_request_done(c, result, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
static void
|
|
|
|
input_curl_info_read(void)
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
while ((msg = curl_multi_info_read(curl.multi,
|
|
|
|
&msgs_in_queue)) != NULL) {
|
|
|
|
if (msg->msg == CURLMSG_DONE)
|
|
|
|
input_curl_handle_done(msg->easy_handle, msg->data.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Give control to CURL.
|
|
|
|
*
|
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 bool
|
|
|
|
input_curl_perform(void)
|
|
|
|
{
|
2011-09-15 08:18:50 +02:00
|
|
|
assert(io_thread_inside());
|
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
CURLMcode mcode;
|
|
|
|
|
|
|
|
do {
|
|
|
|
int running_handles;
|
|
|
|
mcode = curl_multi_perform(curl.multi, &running_handles);
|
|
|
|
} while (mcode == CURLM_CALL_MULTI_PERFORM);
|
|
|
|
|
|
|
|
if (mcode != CURLM_OK && mcode != CURLM_CALL_MULTI_PERFORM) {
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
error.Format(curlm_domain, mcode,
|
|
|
|
"curl_multi_perform() failed: %s",
|
|
|
|
curl_multi_strerror(mcode));
|
2011-08-24 02:54:05 +02:00
|
|
|
input_curl_abort_all_requests(error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-10 10:57:00 +02:00
|
|
|
int
|
|
|
|
CurlSockets::PrepareSockets()
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
2013-01-15 07:14:36 +01:00
|
|
|
UpdateSockets();
|
2011-08-24 02:54:05 +02:00
|
|
|
|
|
|
|
long timeout2;
|
|
|
|
CURLMcode mcode = curl_multi_timeout(curl.multi, &timeout2);
|
|
|
|
if (mcode == CURLM_OK) {
|
|
|
|
if (timeout2 >= 0 && timeout2 < 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. */
|
|
|
|
timeout2 = 10;
|
|
|
|
|
2013-08-10 10:57:00 +02:00
|
|
|
return timeout2;
|
|
|
|
} else {
|
2011-08-24 02:54:05 +02:00
|
|
|
g_warning("curl_multi_timeout() failed: %s\n",
|
|
|
|
curl_multi_strerror(mcode));
|
2013-08-10 10:57:00 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2011-08-24 02:54:05 +02:00
|
|
|
}
|
|
|
|
|
2013-01-15 07:14:36 +01:00
|
|
|
void
|
|
|
|
CurlSockets::DispatchSockets()
|
2011-08-24 02:54:05 +02:00
|
|
|
{
|
|
|
|
if (input_curl_perform())
|
|
|
|
input_curl_info_read();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* input_plugin methods
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (proxy == NULL) {
|
|
|
|
/* deprecated proxy configuration */
|
|
|
|
proxy = config_get_string(CONF_HTTP_PROXY_HOST, NULL);
|
|
|
|
proxy_port = config_get_positive(CONF_HTTP_PROXY_PORT, 0);
|
|
|
|
proxy_user = config_get_string(CONF_HTTP_PROXY_USER, NULL);
|
|
|
|
proxy_password = config_get_string(CONF_HTTP_PROXY_PASSWORD,
|
|
|
|
"");
|
|
|
|
}
|
|
|
|
|
2011-08-24 02:54:05 +02:00
|
|
|
curl.multi = curl_multi_init();
|
|
|
|
if (curl.multi == NULL) {
|
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-01-15 07:14:36 +01:00
|
|
|
curl.sockets = new CurlSockets(io_thread_get());
|
2011-08-24 02:54:05 +02:00
|
|
|
|
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-01-10 20:52:25 +01:00
|
|
|
assert(curl.requests.empty());
|
2011-08-24 02:54:05 +02:00
|
|
|
|
2013-08-07 22:42:45 +02:00
|
|
|
BlockingCall(io_thread_get(), [](){
|
|
|
|
delete curl.sockets;
|
|
|
|
});
|
2011-08-24 02:54:05 +02:00
|
|
|
|
|
|
|
curl_multi_cleanup(curl.multi);
|
|
|
|
|
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
|
|
|
g_free(meta_name);
|
2009-01-03 23:29:45 +01:00
|
|
|
|
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-08-10 18:02:44 +02:00
|
|
|
input_curl_check(struct input_stream *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 *
|
2009-01-03 23:29:45 +01:00
|
|
|
input_curl_tag(struct input_stream *is)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
c->tag = NULL;
|
|
|
|
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-01-10 23:00:23 +01:00
|
|
|
while (c->easy != NULL && 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
|
|
|
|
|
|
|
if (tag == NULL)
|
|
|
|
return;
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
delete c->tag;
|
2009-01-03 23:55:03 +01:00
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
if (c->meta_name != NULL && !tag->HasType(TAG_NAME))
|
|
|
|
tag->AddItem(TAG_NAME, c->meta_name);
|
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
|
|
|
|
input_curl_available(struct input_stream *is)
|
|
|
|
{
|
|
|
|
struct input_curl *c = (struct input_curl *)is;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
return c->postponed_error.IsDefined() || c->easy == NULL ||
|
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
|
2009-11-14 23:53:04 +01:00
|
|
|
input_curl_read(struct input_stream *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);
|
|
|
|
|
2009-10-11 23:32:22 +02:00
|
|
|
is->offset += (goffset)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
|
2008-10-26 19:32:43 +01:00
|
|
|
input_curl_close(struct input_stream *is)
|
|
|
|
{
|
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-08-04 23:48:01 +02:00
|
|
|
input_curl_eof(gcc_unused struct input_stream *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-01-10 23:00:23 +01:00
|
|
|
return c->easy == NULL && 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);
|
2008-10-26 21:12:56 +01:00
|
|
|
if (value == NULL || (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 */
|
|
|
|
|
|
|
|
while (value < end && g_ascii_isspace(*value))
|
|
|
|
++value;
|
|
|
|
|
|
|
|
while (end > value && g_ascii_isspace(end[-1]))
|
|
|
|
--end;
|
2008-10-26 19:32:43 +01:00
|
|
|
|
2009-04-28 09:30:12 +02:00
|
|
|
if (g_ascii_strcasecmp(name, "accept-ranges") == 0) {
|
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;
|
2009-04-28 09:30:12 +02:00
|
|
|
} else if (g_ascii_strcasecmp(name, "content-length") == 0) {
|
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
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
c->base.size = c->base.offset + g_ascii_strtoull(buffer, NULL, 10);
|
2009-04-28 09:30:12 +02:00
|
|
|
} else if (g_ascii_strcasecmp(name, "content-type") == 0) {
|
2013-01-28 23:41:45 +01:00
|
|
|
c->base.mime.assign(value, end);
|
2009-04-28 09:30:12 +02:00
|
|
|
} else if (g_ascii_strcasecmp(name, "icy-name") == 0 ||
|
|
|
|
g_ascii_strcasecmp(name, "ice-name") == 0 ||
|
|
|
|
g_ascii_strcasecmp(name, "x-audiocast-name") == 0) {
|
2009-01-03 23:29:45 +01:00
|
|
|
g_free(c->meta_name);
|
|
|
|
c->meta_name = g_strndup(value, end - value);
|
|
|
|
|
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();
|
|
|
|
c->tag->AddItem(TAG_NAME, c->meta_name);
|
2009-04-28 09:30:12 +02:00
|
|
|
} else if (g_ascii_strcasecmp(name, "icy-metaint") == 0) {
|
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;
|
|
|
|
|
|
|
|
icy_metaint = g_ascii_strtoull(buffer, NULL, 10);
|
|
|
|
g_debug("icy-metaint=%zu", icy_metaint);
|
|
|
|
|
|
|
|
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();
|
|
|
|
if (c->easy == NULL) {
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-04-25 13:35:04 +02:00
|
|
|
if (proxy != NULL)
|
|
|
|
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
|
|
|
|
2009-04-25 13:35:04 +02:00
|
|
|
if (proxy_user != NULL && proxy_password != NULL) {
|
2009-01-13 22:57:05 +01:00
|
|
|
char *proxy_auth_str =
|
2009-04-25 13:35:04 +02:00
|
|
|
g_strconcat(proxy_user, ":", proxy_password, NULL);
|
2009-01-13 22:57:05 +01:00
|
|
|
curl_easy_setopt(c->easy, CURLOPT_PROXYUSERPWD, proxy_auth_str);
|
|
|
|
g_free(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
|
|
|
|
|
|
|
c->request_headers = NULL;
|
|
|
|
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
|
2009-11-14 23:53:04 +01:00
|
|
|
input_curl_seek(struct input_stream *is, goffset offset, 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();
|
2009-10-11 23:32:22 +02:00
|
|
|
if (offset - is->offset < (goffset)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) {
|
2008-10-28 20:39:09 +01:00
|
|
|
c->range = g_strdup_printf("%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
|
|
|
}
|
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
static struct input_stream *
|
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-09-03 13:07:33 +02:00
|
|
|
if ((strncmp(url, "http://", 7) != 0) &&
|
|
|
|
(strncmp(url, "https://", 8) != 0))
|
2009-12-30 23:27:37 +01:00
|
|
|
return NULL;
|
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;
|
2009-12-30 23:27:37 +01:00
|
|
|
return NULL;
|
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;
|
2009-12-30 23:27:37 +01:00
|
|
|
return NULL;
|
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
|
|
|
|
|
|
|
const struct input_plugin 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
|
|
|
};
|