2017-01-03 07:33:00 +01:00
|
|
|
/*
|
2017-01-17 11:54:55 +01:00
|
|
|
* Copyright (C) 2008-2016 Max Kellermann <max.kellermann@gmail.com>
|
2017-01-03 07:33:00 +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 "Global.hxx"
|
|
|
|
#include "Request.hxx"
|
|
|
|
#include "Log.hxx"
|
2017-01-26 09:29:49 +01:00
|
|
|
#include "event/Loop.hxx"
|
2017-01-03 07:33:00 +01:00
|
|
|
#include "event/SocketMonitor.hxx"
|
|
|
|
#include "util/RuntimeError.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
|
|
|
|
|
|
|
static constexpr Domain curlm_domain("curlm");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Monitor for one socket created by CURL.
|
|
|
|
*/
|
|
|
|
class CurlSocket final : SocketMonitor {
|
|
|
|
CurlGlobal &global;
|
|
|
|
|
|
|
|
public:
|
2017-08-10 18:25:22 +02:00
|
|
|
CurlSocket(CurlGlobal &_global, EventLoop &_loop, SocketDescriptor _fd)
|
2017-01-03 07:33:00 +01:00
|
|
|
:SocketMonitor(_fd, _loop), global(_global) {}
|
|
|
|
|
|
|
|
~CurlSocket() {
|
|
|
|
/* 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? */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function for CURLMOPT_SOCKETFUNCTION.
|
|
|
|
*/
|
|
|
|
static int SocketFunction(CURL *easy,
|
|
|
|
curl_socket_t s, int action,
|
2017-06-03 21:33:44 +02:00
|
|
|
void *userp, void *socketp) noexcept;
|
2017-01-03 07:33:00 +01:00
|
|
|
|
2017-11-10 20:20:07 +01:00
|
|
|
bool OnSocketReady(unsigned flags) noexcept override;
|
2017-01-03 07:33:00 +01:00
|
|
|
|
|
|
|
private:
|
2017-11-12 17:49:58 +01:00
|
|
|
static constexpr int FlagsToCurlCSelect(unsigned flags) noexcept {
|
2017-01-03 07:33:00 +01:00
|
|
|
return (flags & (READ | HANGUP) ? CURL_CSELECT_IN : 0) |
|
|
|
|
(flags & WRITE ? CURL_CSELECT_OUT : 0) |
|
|
|
|
(flags & ERROR ? CURL_CSELECT_ERR : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_const
|
2017-06-03 21:33:44 +02:00
|
|
|
static unsigned CurlPollToFlags(int action) noexcept {
|
2017-01-03 07:33:00 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CurlGlobal::CurlGlobal(EventLoop &_loop)
|
2017-09-21 22:44:44 +02:00
|
|
|
:defer_read_info(_loop, BIND_THIS_METHOD(ReadInfo)),
|
2017-08-29 16:33:20 +02:00
|
|
|
timeout_event(_loop, BIND_THIS_METHOD(OnTimeout))
|
2017-01-03 07:33:00 +01:00
|
|
|
{
|
|
|
|
multi.SetOption(CURLMOPT_SOCKETFUNCTION, CurlSocket::SocketFunction);
|
|
|
|
multi.SetOption(CURLMOPT_SOCKETDATA, this);
|
|
|
|
|
|
|
|
multi.SetOption(CURLMOPT_TIMERFUNCTION, TimerFunction);
|
|
|
|
multi.SetOption(CURLMOPT_TIMERDATA, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
CurlSocket::SocketFunction(gcc_unused CURL *easy,
|
|
|
|
curl_socket_t s, int action,
|
2017-06-03 21:33:44 +02:00
|
|
|
void *userp, void *socketp) noexcept {
|
2017-01-03 07:33:00 +01:00
|
|
|
auto &global = *(CurlGlobal *)userp;
|
|
|
|
CurlSocket *cs = (CurlSocket *)socketp;
|
|
|
|
|
2017-01-26 09:29:49 +01:00
|
|
|
assert(global.GetEventLoop().IsInside());
|
2017-01-03 07:33:00 +01:00
|
|
|
|
|
|
|
if (action == CURL_POLL_REMOVE) {
|
|
|
|
delete cs;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cs == nullptr) {
|
2017-08-10 18:25:22 +02:00
|
|
|
cs = new CurlSocket(global, global.GetEventLoop(),
|
|
|
|
SocketDescriptor(s));
|
2017-01-03 07:33:00 +01:00
|
|
|
global.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
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned flags = CurlPollToFlags(action);
|
|
|
|
if (flags != 0)
|
|
|
|
cs->Schedule(flags);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-11-10 20:20:07 +01:00
|
|
|
CurlSocket::OnSocketReady(unsigned flags) noexcept
|
2017-01-03 07:33:00 +01:00
|
|
|
{
|
2017-01-26 09:29:49 +01:00
|
|
|
assert(GetEventLoop().IsInside());
|
2017-01-03 07:33:00 +01:00
|
|
|
|
2017-11-10 20:34:45 +01:00
|
|
|
global.SocketAction(GetSocket().Get(), FlagsToCurlCSelect(flags));
|
2017-01-03 07:33:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs in the I/O thread. No lock needed.
|
|
|
|
*
|
|
|
|
* Throws std::runtime_error on error.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
CurlGlobal::Add(CURL *easy, CurlRequest &request)
|
|
|
|
{
|
2017-01-26 09:29:49 +01:00
|
|
|
assert(GetEventLoop().IsInside());
|
2017-01-03 07:33:00 +01:00
|
|
|
assert(easy != nullptr);
|
|
|
|
|
|
|
|
curl_easy_setopt(easy, CURLOPT_PRIVATE, &request);
|
|
|
|
|
|
|
|
CURLMcode mcode = curl_multi_add_handle(multi.Get(), easy);
|
|
|
|
if (mcode != CURLM_OK)
|
|
|
|
throw FormatRuntimeError("curl_multi_add_handle() failed: %s",
|
|
|
|
curl_multi_strerror(mcode));
|
|
|
|
|
|
|
|
InvalidateSockets();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlGlobal::Remove(CURL *easy) noexcept
|
2017-01-03 07:33:00 +01:00
|
|
|
{
|
2017-01-26 09:29:49 +01:00
|
|
|
assert(GetEventLoop().IsInside());
|
2017-01-03 07:33:00 +01:00
|
|
|
assert(easy != nullptr);
|
|
|
|
|
|
|
|
curl_multi_remove_handle(multi.Get(), easy);
|
2017-01-03 07:30:53 +01:00
|
|
|
|
|
|
|
InvalidateSockets();
|
2017-01-03 07:33:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static CurlRequest *
|
2017-11-12 17:49:58 +01:00
|
|
|
ToRequest(CURL *easy) noexcept
|
2017-01-03 07:33:00 +01:00
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
CURLcode code = curl_easy_getinfo(easy, CURLINFO_PRIVATE, &p);
|
|
|
|
if (code != CURLE_OK)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return (CurlRequest *)p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for finished HTTP responses.
|
|
|
|
*
|
|
|
|
* Runs in the I/O thread. The caller must not hold locks.
|
|
|
|
*/
|
|
|
|
inline void
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlGlobal::ReadInfo() noexcept
|
2017-01-03 07:33:00 +01:00
|
|
|
{
|
2017-01-26 09:29:49 +01:00
|
|
|
assert(GetEventLoop().IsInside());
|
2017-01-03 07:33:00 +01:00
|
|
|
|
|
|
|
CURLMsg *msg;
|
|
|
|
int msgs_in_queue;
|
|
|
|
|
|
|
|
while ((msg = curl_multi_info_read(multi.Get(),
|
|
|
|
&msgs_in_queue)) != nullptr) {
|
|
|
|
if (msg->msg == CURLMSG_DONE) {
|
|
|
|
auto *request = ToRequest(msg->easy_handle);
|
|
|
|
if (request != nullptr)
|
|
|
|
request->Done(msg->data.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 12:41:59 +01:00
|
|
|
inline void
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlGlobal::UpdateTimeout(long timeout_ms) noexcept
|
2017-01-03 07:33:00 +01:00
|
|
|
{
|
|
|
|
if (timeout_ms < 0) {
|
2017-08-29 16:33:20 +02:00
|
|
|
timeout_event.Cancel();
|
2017-01-08 12:41:59 +01:00
|
|
|
return;
|
2017-01-03 07:33:00 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 12:44:04 +01:00
|
|
|
if (timeout_ms < 10)
|
2017-01-03 07:33:00 +01:00
|
|
|
/* 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;
|
|
|
|
|
2017-08-29 16:33:20 +02:00
|
|
|
timeout_event.Schedule(std::chrono::milliseconds(timeout_ms));
|
2017-01-08 12:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlGlobal::TimerFunction(gcc_unused CURLM *_global, long timeout_ms,
|
|
|
|
void *userp) noexcept
|
2017-01-08 12:41:59 +01:00
|
|
|
{
|
|
|
|
auto &global = *(CurlGlobal *)userp;
|
|
|
|
assert(_global == global.multi.Get());
|
|
|
|
|
|
|
|
global.UpdateTimeout(timeout_ms);
|
2017-01-03 07:33:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlGlobal::OnTimeout() noexcept
|
2017-01-03 07:33:00 +01:00
|
|
|
{
|
|
|
|
SocketAction(CURL_SOCKET_TIMEOUT, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-12 17:49:58 +01:00
|
|
|
CurlGlobal::SocketAction(curl_socket_t fd, int ev_bitmask) noexcept
|
2017-01-03 07:33:00 +01:00
|
|
|
{
|
|
|
|
int running_handles;
|
|
|
|
CURLMcode mcode = curl_multi_socket_action(multi.Get(), fd, ev_bitmask,
|
|
|
|
&running_handles);
|
|
|
|
if (mcode != CURLM_OK)
|
|
|
|
FormatError(curlm_domain,
|
|
|
|
"curl_multi_socket_action() failed: %s",
|
|
|
|
curl_multi_strerror(mcode));
|
|
|
|
|
2017-09-21 22:44:44 +02:00
|
|
|
defer_read_info.Schedule();
|
2017-01-03 07:33:00 +01:00
|
|
|
}
|