1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-09-27 02:56:18 -04:00

[curl] Added libev code based on curl's evhiperfifo.c

This commit is contained in:
Witold Filipczyk 2023-07-09 15:52:16 +02:00
parent ae8f6abbe2
commit 868234cb13
10 changed files with 247 additions and 12 deletions

View File

@ -80,7 +80,6 @@ do { \
#endif
#if defined(CONFIG_LIBEVENT) && defined(CONFIG_LIBCURL)
/* Information associated with a specific easy handle */
typedef struct _ConnInfo
{
@ -268,6 +267,197 @@ sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
}
#endif
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEV)
/* Information associated with a specific easy handle */
typedef struct _ConnInfo
{
CURL *easy;
char *url;
GlobalInfo *global;
char error[CURL_ERROR_SIZE];
} ConnInfo;
/* Information associated with a specific socket */
typedef struct _SockInfo
{
curl_socket_t sockfd;
CURL *easy;
int action;
long timeout;
struct ev_io ev;
int evset;
GlobalInfo *global;
} SockInfo;
GlobalInfo g;
static void timer_cb(EV_P_ struct ev_timer *w, int revents);
/* Update the event timer after curl_multi library calls */
static int
multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
{
//DPRINT("%s %li\n", __PRETTY_FUNCTION__, timeout_ms);
ev_timer_stop(g->loop, &g->timer_event);
if (timeout_ms >= 0) {
/* -1 means delete, other values are timeout times in milliseconds */
double t = timeout_ms / 1000;
ev_timer_init(&g->timer_event, timer_cb, t, 0.);
ev_timer_start(g->loop, &g->timer_event);
}
return 0;
}
/* Die if we get a bad CURLMcode somewhere */
void
mcode_or_die(const char *where, CURLMcode code)
{
if (CURLM_OK != code) {
const char *s;
switch(code) {
case CURLM_BAD_HANDLE:
s = "CURLM_BAD_HANDLE";
break;
case CURLM_BAD_EASY_HANDLE:
s = "CURLM_BAD_EASY_HANDLE";
break;
case CURLM_OUT_OF_MEMORY:
s = "CURLM_OUT_OF_MEMORY";
break;
case CURLM_INTERNAL_ERROR:
s = "CURLM_INTERNAL_ERROR";
break;
case CURLM_UNKNOWN_OPTION:
s = "CURLM_UNKNOWN_OPTION";
break;
case CURLM_LAST:
s = "CURLM_LAST";
break;
default:
s = "CURLM_unknown";
break;
case CURLM_BAD_SOCKET:
s = "CURLM_BAD_SOCKET";
fprintf(stderr, "ERROR: %s returns %s\n", where, s);
/* ignore this error */
return;
}
fprintf(stderr, "ERROR: %s returns %s\n", where, s);
//exit(code);
}
}
/* Called by libevent when we get action on a multi socket */
static void
event_cb(EV_P_ struct ev_io *w, int revents)
{
//DPRINT("%s w %p revents %i\n", __PRETTY_FUNCTION__, w, revents);
GlobalInfo *g = (GlobalInfo*) w->data;
CURLMcode rc;
int action = ((revents & EV_READ) ? CURL_POLL_IN : 0) | ((revents & EV_WRITE) ? CURL_POLL_OUT : 0);
rc = curl_multi_socket_action(g->multi, w->fd, action, &g->still_running);
mcode_or_die("event_cb: curl_multi_socket_action", rc);
check_multi_info(g);
#if 0
if (g->still_running <= 0) {
fprintf(MSG_OUT, "last transfer done, kill timeout\n");
ev_timer_stop(g->loop, &g->timer_event);
}
#endif
}
/* Called by libevent when our timeout expires */
static void
timer_cb(EV_P_ struct ev_timer *w, int revents)
{
//DPRINT("%s w %p revents %i\n", __PRETTY_FUNCTION__, w, revents);
GlobalInfo *g = (GlobalInfo *)w->data;
CURLMcode rc;
rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, &g->still_running);
mcode_or_die("timer_cb: curl_multi_socket_action", rc);
check_multi_info(g);
}
/* Clean up the SockInfo structure */
static void
remsock(SockInfo *f, GlobalInfo *g)
{
//printf("%s \n", __PRETTY_FUNCTION__);
if (f) {
if (f->evset) {
ev_io_stop(g->loop, &f->ev);
}
free(f);
}
}
/* Assign information to a SockInfo structure */
static void
setsock(SockInfo *f, curl_socket_t s, CURL *e, int act, GlobalInfo *g)
{
//printf("%s \n", __PRETTY_FUNCTION__);
int kind = ((act & CURL_POLL_IN) ? EV_READ : 0) | ((act & CURL_POLL_OUT) ? EV_WRITE : 0);
f->sockfd = s;
f->action = act;
f->easy = e;
if (f->evset) {
ev_io_stop(g->loop, &f->ev);
}
ev_io_init(&f->ev, event_cb, f->sockfd, kind);
f->ev.data = g;
f->evset = 1;
ev_io_start(g->loop, &f->ev);
}
/* Initialize a new SockInfo structure */
static void
addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
{
SockInfo *fdp = calloc(1, sizeof(SockInfo));
fdp->global = g;
setsock(fdp, s, easy, action, g);
curl_multi_assign(g->multi, s, fdp);
}
/* CURLMOPT_SOCKETFUNCTION */
static int
sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
{
//DPRINT("%s e %p s %i what %i cbp %p sockp %p\n", __PRETTY_FUNCTION__, e, s, what, cbp, sockp);
GlobalInfo *g = (GlobalInfo*) cbp;
SockInfo *fdp = (SockInfo*) sockp;
//const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE"};
//fprintf(MSG_OUT, "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
if (what == CURL_POLL_REMOVE) {
//fprintf(MSG_OUT, "\n");
remsock(fdp, g);
} else {
if (!fdp) {
//fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
addsock(s, e, what, g);
} else {
//fprintf(MSG_OUT, "Changing action from %s to %s\n", whatstr[fdp->action], whatstr[what]);
setsock(fdp, s, e, what, g);
}
}
return 0;
}
#endif
#ifdef USE_LIBEVENT
const char *
get_libevent_version(void)
@ -741,6 +931,24 @@ select_loop(void (*init)(void))
/* we do not call any curl_multi_socket*() function yet as we have no handles added! */
#endif
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEV)
memset(&g, 0, sizeof(GlobalInfo));
g.loop = ev_default_loop(0);
curl_global_init(CURL_GLOBAL_DEFAULT);
g.multi = curl_multi_init();
//fprintf(stderr, "multi_init\n");
ev_timer_init(&g.timer_event, timer_cb, 0., 0.);
g.timer_event.data = &g;
/* setup the generic multi interface options we want */
curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
/* we do not call any curl_multi_socket*() function yet as we have no handles added! */
#endif
while (!program.terminate) {
check_signals();
if (1 /*(!F)*/) {
@ -758,6 +966,13 @@ select_loop(void (*init)(void))
curl_multi_cleanup(g.multi);
curl_global_cleanup();
#endif
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEV)
//ev_loop(g.loop, 0);
//event_base_free(g.evbase);
curl_multi_cleanup(g.multi);
curl_global_cleanup();
#endif
return;
} else
#endif

View File

@ -27,7 +27,7 @@
extern "C" {
#endif
#ifdef USE_LIBEVENT
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEVENT)
/* Global information, common to all connections */
typedef struct _GlobalInfo
{
@ -47,6 +47,25 @@ void check_multi_info(GlobalInfo *g);
void mcode_or_die(const char *where, CURLMcode code);
#endif
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEV)
/* Global information, common to all connections */
typedef struct _GlobalInfo
{
struct ev_loop *loop;
struct ev_io fifo_event;
struct ev_timer timer_event;
CURLM *multi;
int still_running;
FILE *input;
} GlobalInfo;
extern GlobalInfo g;
void check_multi_info(GlobalInfo *g);
void mcode_or_die(const char *where, CURLMcode code);
#endif
typedef void (*select_handler_T)(void *);
/* Start the select loop after calling the passed @init() function. */

View File

@ -2,5 +2,6 @@ top_builddir=../../..
include $(top_builddir)/Makefile.config
OBJS-$(CONFIG_LIBEVENT) = ftp.o http.o
OBJS-$(CONFIG_LIBEV) = ftp.o http.o
include $(top_srcdir)/Makefile.lib

View File

@ -10,7 +10,7 @@ extern "C" {
extern struct module ftpes_protocol_module;
#if defined(CONFIG_FTP) && defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEVENT)
#if defined(CONFIG_FTP) && defined(CONFIG_LIBCURL) && (defined(CONFIG_LIBEVENT) || defined(CONFIG_LIBEV))
extern protocol_handler_T ftpes_protocol_handler;
#else
#define ftpes_protocol_handler NULL

View File

@ -12,7 +12,7 @@
extern "C" {
#endif
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEVENT)
#if defined(CONFIG_LIBCURL) && (defined(CONFIG_LIBEVENT) || defined(CONFIG_LIBEV))
struct connection;
extern protocol_handler_T http_curl_protocol_handler;

View File

@ -10,7 +10,7 @@ extern "C" {
extern struct module sftp_protocol_module;
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEVENT)
#if defined(CONFIG_LIBCURL) && (defined(CONFIG_LIBEVENT) || defined(CONFIG_LIBEV))
extern protocol_handler_T sftp_protocol_handler;
#else
#define sftp_protocol_handler NULL

View File

@ -72,7 +72,7 @@ union option_info ftp_options[] = {
"anon_passwd", OPT_ZERO, "some@host.domain",
N_("FTP anonymous password to be sent.")),
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEVENT)
#if defined(CONFIG_LIBCURL) && (defined(CONFIG_LIBEVENT) || defined(CONFIG_LIBEV))
INIT_OPT_BOOL("protocol.ftp", N_("Use libcurl"),
"use_curl", OPT_ZERO, 0,
N_("Use libcurl implementation of ftp.")),
@ -289,7 +289,7 @@ ok:
void
ftp_protocol_handler(struct connection *conn)
{
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEVENT)
#if defined(CONFIG_LIBCURL) && (defined(CONFIG_LIBEVENT) || defined(CONFIG_LIBEV))
if (get_opt_bool("protocol.ftp.use_curl", NULL)) {
ftpes_protocol_handler(conn);
return;

View File

@ -197,7 +197,7 @@ static union option_info http_options[] = {
"--- the server only returns the client's request back to "
"the client verbatim. Note that this type of request may "
"not be enabled on all servers.")),
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEVENT)
#if defined(CONFIG_LIBCURL) && (defined(CONFIG_LIBEVENT) || defined(CONFIG_LIBEV))
INIT_OPT_BOOL("protocol.http", N_("Use libcurl"),
"use_curl", OPT_ZERO, 0,
N_("Use libcurl implementation of http(s).")),
@ -533,7 +533,7 @@ static void http_send_header(struct socket *);
void
http_protocol_handler(struct connection *conn)
{
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEVENT)
#if defined(CONFIG_LIBCURL) && (defined(CONFIG_LIBEVENT) || defined(CONFIG_LIBEV))
if (get_opt_bool("protocol.http.use_curl", NULL)) {
http_curl_protocol_handler(conn);
return;

View File

@ -1,7 +1,7 @@
if conf_data.get('CONFIG_BITTORRENT')
subdir('bittorrent')
endif
if conf_data.get('CONFIG_LIBCURL') and conf_data.get('CONFIG_LIBEVENT')
if conf_data.get('CONFIG_LIBCURL') and (conf_data.get('CONFIG_LIBEVENT') or conf_data.get('CONFIG_LIBEV'))
subdir('curl')
endif
if conf_data.get('CONFIG_FINGER')

View File

@ -318,7 +318,7 @@ static struct module *protocol_submodules[] = {
#endif
#ifdef CONFIG_FTP
&ftp_protocol_module,
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEVENT)
#if defined(CONFIG_LIBCURL) && (defined(CONFIG_LIBEVENT) || defined(CONFIG_LIBEV))
&ftpes_protocol_module,
#endif
#endif
@ -332,7 +332,7 @@ static struct module *protocol_submodules[] = {
#ifdef CONFIG_NNTP
&nntp_protocol_module,
#endif
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEVENT)
#if defined(CONFIG_LIBCURL) && (defined(CONFIG_LIBEVENT) || defined(CONFIG_LIBEV))
&sftp_protocol_module,
#endif
#ifdef CONFIG_SMB