2017-01-03 07:30:53 +01:00
|
|
|
/*
|
2019-08-19 18:53:08 +02:00
|
|
|
* Copyright 2008-2018 Max Kellermann <max.kellermann@gmail.com>
|
2017-01-03 07:30:53 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "Request.hxx"
|
|
|
|
#include "Global.hxx"
|
|
|
|
#include "Version.hxx"
|
|
|
|
#include "Handler.hxx"
|
2018-01-11 20:50:16 +01:00
|
|
|
#include "event/Call.hxx"
|
2017-01-03 07:30:53 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2017-07-05 17:20:02 +02:00
|
|
|
#include "util/StringStrip.hxx"
|
2017-01-03 07:30:53 +01:00
|
|
|
#include "util/StringView.hxx"
|
|
|
|
#include "util/CharUtil.hxx"
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-01-15 10:35:48 +01:00
|
|
|
CurlRequest::CurlRequest(CurlGlobal &_global,
|
2017-01-03 07:30:53 +01:00
|
|
|
CurlResponseHandler &_handler)
|
2017-09-21 22:44:44 +02:00
|
|
|
:global(_global), handler(_handler),
|
|
|
|
postpone_error_event(global.GetEventLoop(),
|
|
|
|
BIND_THIS_METHOD(OnPostponeError))
|
2017-01-03 07:30:53 +01:00
|
|
|
{
|
|
|
|
error_buffer[0] = 0;
|
|
|
|
|
|
|
|
easy.SetOption(CURLOPT_PRIVATE, (void *)this);
|
|
|
|
easy.SetOption(CURLOPT_USERAGENT, "Music Player Daemon " VERSION);
|
|
|
|
easy.SetOption(CURLOPT_HEADERFUNCTION, _HeaderFunction);
|
|
|
|
easy.SetOption(CURLOPT_WRITEHEADER, this);
|
|
|
|
easy.SetOption(CURLOPT_WRITEFUNCTION, WriteFunction);
|
|
|
|
easy.SetOption(CURLOPT_WRITEDATA, this);
|
|
|
|
easy.SetOption(CURLOPT_NETRC, 1l);
|
|
|
|
easy.SetOption(CURLOPT_ERRORBUFFER, error_buffer);
|
|
|
|
easy.SetOption(CURLOPT_NOPROGRESS, 1l);
|
|
|
|
easy.SetOption(CURLOPT_NOSIGNAL, 1l);
|
|
|
|
easy.SetOption(CURLOPT_CONNECTTIMEOUT, 10l);
|
2018-02-22 00:15:28 +01:00
|
|
|
easy.SetOption(CURLOPT_HTTPAUTH, (long) CURLAUTH_ANY);
|
2017-01-03 07:30:53 +01:00
|
|
|
}
|
|
|
|
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlRequest::~CurlRequest() noexcept
|
2017-01-03 07:30:53 +01:00
|
|
|
{
|
|
|
|
FreeEasy();
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:01:58 +01:00
|
|
|
void
|
2017-11-14 20:05:44 +01:00
|
|
|
CurlRequest::Start()
|
2017-01-07 16:01:58 +01:00
|
|
|
{
|
|
|
|
assert(!registered);
|
|
|
|
|
|
|
|
global.Add(easy.Get(), *this);
|
|
|
|
registered = true;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:50:16 +01:00
|
|
|
void
|
|
|
|
CurlRequest::StartIndirect()
|
|
|
|
{
|
|
|
|
BlockingCall(global.GetEventLoop(), [this](){
|
|
|
|
Start();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:01:58 +01:00
|
|
|
void
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlRequest::Stop() noexcept
|
2017-01-07 16:01:58 +01:00
|
|
|
{
|
2017-01-08 13:51:03 +01:00
|
|
|
if (!registered)
|
|
|
|
return;
|
2017-01-07 16:01:58 +01:00
|
|
|
|
|
|
|
global.Remove(easy.Get());
|
|
|
|
registered = false;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:50:16 +01:00
|
|
|
void
|
|
|
|
CurlRequest::StopIndirect()
|
|
|
|
{
|
|
|
|
BlockingCall(global.GetEventLoop(), [this](){
|
|
|
|
Stop();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
void
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlRequest::FreeEasy() noexcept
|
2017-01-03 07:30:53 +01:00
|
|
|
{
|
|
|
|
if (!easy)
|
|
|
|
return;
|
|
|
|
|
2017-01-08 13:51:03 +01:00
|
|
|
Stop();
|
2017-01-03 07:30:53 +01:00
|
|
|
easy = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlRequest::Resume() noexcept
|
2017-01-03 07:30:53 +01:00
|
|
|
{
|
2017-01-07 16:01:58 +01:00
|
|
|
assert(registered);
|
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
curl_easy_pause(easy.Get(), CURLPAUSE_CONT);
|
|
|
|
|
|
|
|
if (IsCurlOlderThan(0x072000))
|
|
|
|
/* libcurl older than 7.32.0 does not update
|
|
|
|
its sockets after curl_easy_pause(); force
|
|
|
|
libcurl to do it now */
|
|
|
|
global.ResumeSockets();
|
|
|
|
|
|
|
|
global.InvalidateSockets();
|
|
|
|
}
|
|
|
|
|
2017-01-08 14:30:34 +01:00
|
|
|
void
|
2017-01-03 07:30:53 +01:00
|
|
|
CurlRequest::FinishHeaders()
|
|
|
|
{
|
|
|
|
if (state != State::HEADERS)
|
2017-01-08 14:30:34 +01:00
|
|
|
return;
|
2017-01-03 07:30:53 +01:00
|
|
|
|
|
|
|
state = State::BODY;
|
|
|
|
|
|
|
|
long status = 0;
|
|
|
|
curl_easy_getinfo(easy.Get(), CURLINFO_RESPONSE_CODE, &status);
|
|
|
|
|
2017-01-08 14:30:34 +01:00
|
|
|
handler.OnHeaders(status, std::move(headers));
|
2017-01-03 07:30:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CurlRequest::FinishBody()
|
|
|
|
{
|
2017-01-08 14:30:34 +01:00
|
|
|
FinishHeaders();
|
2017-01-03 07:30:53 +01:00
|
|
|
|
|
|
|
if (state != State::BODY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
state = State::CLOSED;
|
|
|
|
handler.OnEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlRequest::Done(CURLcode result) noexcept
|
2017-01-03 07:30:53 +01:00
|
|
|
{
|
2017-01-08 13:24:58 +01:00
|
|
|
Stop();
|
2017-01-03 07:30:53 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (result != CURLE_OK) {
|
|
|
|
StripRight(error_buffer);
|
|
|
|
const char *msg = error_buffer;
|
|
|
|
if (*msg == 0)
|
|
|
|
msg = curl_easy_strerror(result);
|
|
|
|
throw FormatRuntimeError("CURL failed: %s", msg);
|
|
|
|
}
|
|
|
|
|
2017-01-08 14:30:34 +01:00
|
|
|
FinishBody();
|
|
|
|
} catch (...) {
|
|
|
|
state = State::CLOSED;
|
|
|
|
handler.OnError(std::current_exception());
|
|
|
|
}
|
2017-01-03 07:30:53 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 16:24:30 +01:00
|
|
|
gcc_pure
|
|
|
|
static bool
|
2017-05-08 14:44:49 +02:00
|
|
|
IsResponseBoundaryHeader(StringView s) noexcept
|
2017-03-10 16:24:30 +01:00
|
|
|
{
|
2018-01-11 14:52:35 +01:00
|
|
|
return s.size > 5 && (s.StartsWith("HTTP/") ||
|
2017-03-10 16:28:02 +01:00
|
|
|
/* the proprietary "ICY 200 OK" is
|
|
|
|
emitted by Shoutcast */
|
2018-01-11 14:52:35 +01:00
|
|
|
s.StartsWith("ICY 2"));
|
2017-03-10 16:24:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 07:30:53 +01:00
|
|
|
inline void
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlRequest::HeaderFunction(StringView s) noexcept
|
2017-01-03 07:30:53 +01:00
|
|
|
{
|
|
|
|
if (state > State::HEADERS)
|
|
|
|
return;
|
|
|
|
|
2017-03-10 16:24:30 +01:00
|
|
|
if (IsResponseBoundaryHeader(s)) {
|
2017-01-03 07:30:53 +01:00
|
|
|
/* this is the boundary to a new response, for example
|
|
|
|
after a redirect */
|
|
|
|
headers.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *header = s.data;
|
|
|
|
const char *end = StripRight(header, header + s.size);
|
|
|
|
|
|
|
|
const char *value = s.Find(':');
|
|
|
|
if (value == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string name(header, value);
|
2017-09-12 18:22:02 +02:00
|
|
|
std::transform(name.begin(), name.end(), name.begin(),
|
|
|
|
static_cast<char(*)(char)>(ToLowerASCII));
|
2017-01-03 07:30:53 +01:00
|
|
|
|
|
|
|
/* skip the colon */
|
|
|
|
|
|
|
|
++value;
|
|
|
|
|
|
|
|
/* strip the value */
|
|
|
|
|
|
|
|
value = StripLeft(value, end);
|
|
|
|
end = StripRight(value, end);
|
|
|
|
|
|
|
|
headers.emplace(std::move(name), std::string(value, end));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlRequest::_HeaderFunction(void *ptr, size_t size, size_t nmemb,
|
|
|
|
void *stream) noexcept
|
2017-01-03 07:30:53 +01:00
|
|
|
{
|
|
|
|
CurlRequest &c = *(CurlRequest *)stream;
|
|
|
|
|
|
|
|
size *= nmemb;
|
|
|
|
|
|
|
|
c.HeaderFunction({(const char *)ptr, size});
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlRequest::DataReceived(const void *ptr, size_t received_size) noexcept
|
2017-01-03 07:30:53 +01:00
|
|
|
{
|
|
|
|
assert(received_size > 0);
|
|
|
|
|
|
|
|
try {
|
2017-01-08 14:30:34 +01:00
|
|
|
FinishHeaders();
|
2017-01-03 07:30:53 +01:00
|
|
|
handler.OnData({ptr, received_size});
|
|
|
|
return received_size;
|
|
|
|
} catch (Pause) {
|
|
|
|
return CURL_WRITEFUNC_PAUSE;
|
|
|
|
} catch (...) {
|
|
|
|
state = State::CLOSED;
|
2017-01-08 14:28:58 +01:00
|
|
|
/* move the CurlResponseHandler::OnError() call into a
|
|
|
|
"safe" stack frame */
|
|
|
|
postponed_error = std::current_exception();
|
2017-09-21 22:44:44 +02:00
|
|
|
postpone_error_event.Schedule();
|
2017-01-08 14:28:58 +01:00
|
|
|
return CURL_WRITEFUNC_PAUSE;
|
2017-01-03 07:30:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlRequest::WriteFunction(void *ptr, size_t size, size_t nmemb,
|
|
|
|
void *stream) noexcept
|
2017-01-03 07:30:53 +01:00
|
|
|
{
|
|
|
|
CurlRequest &c = *(CurlRequest *)stream;
|
|
|
|
|
|
|
|
size *= nmemb;
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return c.DataReceived(ptr, size);
|
|
|
|
}
|
2017-01-08 14:28:58 +01:00
|
|
|
|
|
|
|
void
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlRequest::OnPostponeError() noexcept
|
2017-01-08 14:28:58 +01:00
|
|
|
{
|
|
|
|
assert(postponed_error);
|
|
|
|
|
|
|
|
handler.OnError(postponed_error);
|
|
|
|
}
|