aboutsummaryrefslogtreecommitdiff
path: root/test-curl/test/src
diff options
context:
space:
mode:
Diffstat (limited to 'test-curl/test/src')
-rw-r--r--test-curl/test/src/example.c383
-rw-r--r--test-curl/test/src/test.c197
2 files changed, 580 insertions, 0 deletions
diff --git a/test-curl/test/src/example.c b/test-curl/test/src/example.c
new file mode 100644
index 0000000..f5496af
--- /dev/null
+++ b/test-curl/test/src/example.c
@@ -0,0 +1,383 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/poll.h>
+#include <curl/curl.h>
+#include <event2/event.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+
+#define MSG_OUT stdout
+
+
+/* Global information, common to all connections */
+typedef struct _GlobalInfo
+{
+ struct event_base *evbase;
+ struct event *fifo_event;
+ struct event *timer_event;
+ CURLM *multi;
+ int still_running;
+ FILE *input;
+} GlobalInfo;
+
+
+/* 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 event *ev;
+ int evset;
+ GlobalInfo *global;
+} SockInfo;
+
+
+
+/* Update the event timer after curl_multi library calls */
+static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
+{
+ struct timeval timeout;
+ (void)multi; /* unused */
+
+ timeout.tv_sec = timeout_ms/1000;
+ timeout.tv_usec = (timeout_ms%1000)*1000;
+ fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
+ evtimer_add(g->timer_event, &timeout);
+ return 0;
+}
+
+/* Die if we get a bad CURLMcode somewhere */
+static 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(MSG_OUT, "ERROR: %s returns %s\n", where, s);
+ /* ignore this error */
+ return;
+ }
+ fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
+ exit(code);
+ }
+}
+
+
+
+/* Check for completed transfers, and remove their easy handles */
+static void check_multi_info(GlobalInfo *g)
+{
+ char *eff_url;
+ CURLMsg *msg;
+ int msgs_left;
+ ConnInfo *conn;
+ CURL *easy;
+ CURLcode res;
+
+ fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
+ while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
+ if(msg->msg == CURLMSG_DONE) {
+ easy = msg->easy_handle;
+ res = msg->data.result;
+ curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
+ curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
+ fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
+ curl_multi_remove_handle(g->multi, easy);
+ free(conn->url);
+ curl_easy_cleanup(easy);
+ free(conn);
+ }
+ }
+}
+
+
+
+/* Called by libevent when we get action on a multi socket */
+static void event_cb(int fd, short kind, void *userp)
+{
+ GlobalInfo *g = (GlobalInfo*) userp;
+ CURLMcode rc;
+
+ int action =
+ (kind & EV_READ ? CURL_CSELECT_IN : 0) |
+ (kind & EV_WRITE ? CURL_CSELECT_OUT : 0);
+
+ rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
+ mcode_or_die("event_cb: curl_multi_socket_action", rc);
+
+ check_multi_info(g);
+ if(g->still_running <= 0) {
+ fprintf(MSG_OUT, "last transfer done, kill timeout\n");
+ if(evtimer_pending(g->timer_event, NULL)) {
+ evtimer_del(g->timer_event);
+ }
+ }
+}
+
+
+
+/* Called by libevent when our timeout expires */
+static void timer_cb(int fd, short kind, void *userp)
+{
+ GlobalInfo *g = (GlobalInfo *)userp;
+ CURLMcode rc;
+ (void)fd;
+ (void)kind;
+
+ 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)
+{
+ if(f) {
+ if(f->evset)
+ event_free(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)
+{
+ int kind =
+ (act&CURL_POLL_IN?EV_READ:0)|(act&CURL_POLL_OUT?EV_WRITE:0)|EV_PERSIST;
+
+ f->sockfd = s;
+ f->action = act;
+ f->easy = e;
+ if(f->evset)
+ event_free(f->ev);
+ f->ev = event_new(g->evbase, f->sockfd, kind, event_cb, g);
+ f->evset = 1;
+ event_add(f->ev, NULL);
+}
+
+
+
+/* Initialize a new SockInfo structure */
+static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
+{
+ SockInfo *fdp = calloc(sizeof(SockInfo), 1);
+
+ 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)
+{
+ 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);
+ }
+ 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;
+}
+
+
+
+/* CURLOPT_WRITEFUNCTION */
+static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
+{
+ size_t realsize = size * nmemb;
+ ConnInfo *conn = (ConnInfo*) data;
+ (void)ptr;
+ (void)conn;
+ return realsize;
+}
+
+
+/* CURLOPT_PROGRESSFUNCTION */
+static int prog_cb(void *p, double dltotal, double dlnow, double ult,
+ double uln)
+{
+ ConnInfo *conn = (ConnInfo *)p;
+ (void)ult;
+ (void)uln;
+
+ fprintf(MSG_OUT, "Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal);
+ return 0;
+}
+
+
+/* Create a new easy handle, and add it to the global curl_multi */
+static void new_conn(char *url, GlobalInfo *g)
+{
+ ConnInfo *conn;
+ CURLMcode rc;
+
+ conn = calloc(1, sizeof(ConnInfo));
+ memset(conn, 0, sizeof(ConnInfo));
+ conn->error[0]='\0';
+
+ conn->easy = curl_easy_init();
+ if(!conn->easy) {
+ fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
+ exit(2);
+ }
+ conn->global = g;
+ conn->url = strdup(url);
+ curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url);
+ curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb);
+ curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, conn);
+ curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1L);
+ curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error);
+ curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn);
+ curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0L);
+ curl_easy_setopt(conn->easy, CURLOPT_PROGRESSFUNCTION, prog_cb);
+ curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn);
+ fprintf(MSG_OUT,
+ "Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url);
+ rc = curl_multi_add_handle(g->multi, conn->easy);
+ mcode_or_die("new_conn: curl_multi_add_handle", rc);
+
+ /* note that the add_handle() will set a time-out to trigger very soon so
+ that the necessary socket_action() call will be called by this app */
+}
+
+/* This gets called whenever data is received from the fifo */
+static void fifo_cb(int fd, short event, void *arg)
+{
+ char s[1024];
+ long int rv=0;
+ int n=0;
+ GlobalInfo *g = (GlobalInfo *)arg;
+ (void)fd; /* unused */
+ (void)event; /* unused */
+
+ do {
+ s[0]='\0';
+ rv=fscanf(g->input, "%1023s%n", s, &n);
+ s[n]='\0';
+ if(n && s[0]) {
+ new_conn(s, arg); /* if we read a URL, go get it! */
+ }
+ else
+ break;
+ } while(rv != EOF);
+}
+
+/* Create a named pipe and tell libevent to monitor it */
+static const char *fifo = "hiper.fifo";
+static int init_fifo(GlobalInfo *g)
+{
+ struct stat st;
+ curl_socket_t sockfd;
+
+ fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
+ if(lstat (fifo, &st) == 0) {
+ if((st.st_mode & S_IFMT) == S_IFREG) {
+ errno = EEXIST;
+ perror("lstat");
+ exit(1);
+ }
+ }
+ unlink(fifo);
+ if(mkfifo (fifo, 0600) == -1) {
+ perror("mkfifo");
+ exit(1);
+ }
+ sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
+ if(sockfd == -1) {
+ perror("open");
+ exit(1);
+ }
+ g->input = fdopen(sockfd, "r");
+
+ fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
+ g->fifo_event = event_new(g->evbase, sockfd, EV_READ|EV_PERSIST, fifo_cb, g);
+ event_add(g->fifo_event, NULL);
+ return (0);
+}
+
+static void clean_fifo(GlobalInfo *g)
+{
+ event_free(g->fifo_event);
+ fclose(g->input);
+ unlink(fifo);
+}
+
+int main(int argc, char **argv)
+{
+ GlobalInfo g;
+ (void)argc;
+ (void)argv;
+
+ memset(&g, 0, sizeof(GlobalInfo));
+ g.evbase = event_base_new();
+ init_fifo(&g);
+ g.multi = curl_multi_init();
+ g.timer_event = evtimer_new(g.evbase, timer_cb, &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 don't call any curl_multi_socket*() function yet as we have no handles
+ added! */
+
+ event_base_dispatch(g.evbase);
+
+ /* this, of course, won't get called since only way to stop this program is
+ via ctrl-C, but it is here to show how cleanup /would/ be done. */
+ clean_fifo(&g);
+ event_free(g.timer_event);
+ event_base_free(g.evbase);
+ curl_multi_cleanup(g.multi);
+ return 0;
+}
diff --git a/test-curl/test/src/test.c b/test-curl/test/src/test.c
new file mode 100644
index 0000000..52b1473
--- /dev/null
+++ b/test-curl/test/src/test.c
@@ -0,0 +1,197 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <stdbool.h>
+
+#include <curl/curl.h>
+#include <event2/event.h>
+#include <event2/bufferevent.h>
+#include <event2/buffer.h>
+
+#define URIS_S 2
+const char *uris[] = {
+ "https://api.turris.cz/updater-defs/3.5/omnia/dev-karel/base.lua",
+ "https://api.turris.cz/updater-defs/3.5/omnia/dev-karel/base.lua.sig"
+};
+
+CURLM *curl_multi;
+struct event_base *e_base;
+struct event *e_timer;
+
+struct dwn_data {
+ const char *err;
+};
+
+static void check() {
+ CURLMsg *msg;
+ int msgs_left;
+ struct dwn_data *data;
+ char *url;
+
+ while ((msg = curl_multi_info_read(curl_multi, &msgs_left))) {
+ if (msg->msg != CURLMSG_DONE)
+ continue; // No other message types are defined in libcurl. We check just because of compatibility with possible future versions.
+ curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &data);
+ curl_easy_getinfo(msg->easy_handle, CURLINFO_EFFECTIVE_URL, &url);
+ if (msg->data.result == CURLE_OK) {
+ printf("Download succesfull (%s)\n", url);
+ } else {
+ printf("Download failed (%s): %s\n", url, data->err);
+ exit(2);
+ }
+ }
+}
+
+static size_t clb_write(char *ptr, size_t size, size_t nmemb, void *userd) {
+ // Drop all data
+ return size * nmemb;
+}
+
+static void clb_timer(int fd __attribute__((unused)), short kind __attribute__((unused)), void *userp) {
+ printf("Timer callback\n");
+ int running = 0;
+ curl_multi_socket_action(curl_multi, CURL_SOCKET_TIMEOUT, 0, &running);
+ check();
+}
+
+static int clb_timer_set(CURLM *curl_multi __attribute__((unused)), long timeout_ms, void *userp) {
+ printf("Timer set %ld\n", timeout_ms);
+ struct timeval timeout;
+ timeout.tv_sec = timeout_ms / 1000;
+ timeout.tv_usec = (timeout_ms % 1000) * 1000;
+ evtimer_add(e_timer, &timeout);
+ return 0;
+}
+
+struct dt_s {
+ size_t id;
+ struct event *e;
+};
+
+static void clb_socket(int fd, short kind, void *userp) {
+ printf("Socket callback\n");
+ int action = ((kind & EV_READ) ? CURL_CSELECT_IN : 0) | ((kind & EV_WRITE) ? CURL_CSELECT_OUT : 0);
+ int running = 0;
+ curl_multi_socket_action(curl_multi, fd, action, &running);
+ check();
+ if (running <= 0 && evtimer_pending(e_timer, NULL))
+ evtimer_del(e_timer);
+}
+
+size_t id_next = 0;
+
+static int clb_socket_set(CURL *curl_easy, curl_socket_t s, int what, void *userp, void *socketp) {
+ struct dt_s *sdata = socketp;
+ if (what == CURL_POLL_REMOVE) {
+ printf("Socket remove %ld\n", sdata->id);
+ event_free(sdata->e);
+ free(sdata);
+ } else {
+ if (!sdata) { // New socket. No data associated.
+ printf("Socket new %ld\n", id_next);
+ sdata = malloc(sizeof *sdata);
+ sdata->e = NULL;
+ sdata->id = id_next++;
+ curl_multi_assign(curl_multi, s, sdata);
+ } else
+ printf("Socket change %ld\n", sdata->id);
+ short kind = ((what & CURL_POLL_IN) ? EV_READ : 0) | ((what & CURL_POLL_OUT) ? EV_WRITE : 0) | EV_PERSIST;
+ if (sdata->e) {
+ event_del(sdata->e);
+ event_assign(sdata->e, e_base, s, kind, clb_socket, sdata);
+ } else
+ sdata->e = event_new(e_base, s, kind, clb_socket, sdata);
+ event_add(sdata->e, NULL);
+ }
+ return 0;
+}
+
+static CURL *new_curl(const char *uri, bool simple) {
+ CURL *curl = curl_easy_init();
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
+ curl_easy_setopt(curl, CURLOPT_URL, uri);
+ curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
+ curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca.pem");
+ curl_easy_setopt(curl, CURLOPT_CRLFILE, "./crl.pem");
+ curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
+ curl_easy_setopt(curl, CURLOPT_TIMEOUT, 120);
+ curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
+ curl_easy_setopt(curl, CURLOPT_CAPATH, NULL);
+ curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, NULL);
+
+ if (!simple) {
+ struct dwn_data *data = malloc(sizeof *data);
+ curl_multi_add_handle(curl_multi, curl);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, clb_write);
+ curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, data->err);
+ curl_easy_setopt(curl, CURLOPT_PRIVATE, data);
+ }
+
+ return curl;
+}
+
+
+int main(int argc, char *argv[]) {
+ bool simple = (argc > 1 && !strcmp(argv[1], "-s"));
+ bool multi = (argc > 1 && !strcmp(argv[1], "-m"));
+ curl_global_init(CURL_GLOBAL_SSL);
+ if (!simple) {
+ struct event_config *config = event_config_new();
+ event_config_require_features(config, EV_FEATURE_FDS);
+ event_config_set_flag(config, EVENT_BASE_FLAG_NOLOCK);
+ e_base = event_base_new_with_config(config);
+ event_config_free(config);
+ e_timer = evtimer_new(e_base, clb_timer, NULL);
+
+ curl_multi = curl_multi_init();
+ curl_multi_setopt(curl_multi, CURLMOPT_MAX_TOTAL_CONNECTIONS, 1);
+ curl_multi_setopt(curl_multi, CURLMOPT_SOCKETFUNCTION, clb_socket_set);
+ curl_multi_setopt(curl_multi, CURLMOPT_TIMERFUNCTION, clb_timer_set);
+ }
+
+ size_t i;
+ CURL *curls[URIS_S];
+ for (i = 0; i < URIS_S; i++) {
+ curls[i] = new_curl(uris[i], simple);
+ }
+ if (simple) {
+ for (i = 0; i < URIS_S; i++) {
+ CURLcode res = curl_easy_perform(curls[i]);
+ if (res == CURLE_OK)
+ printf("Download succesfull (%s)\n", uris[i]);
+ else
+ printf("Download failed (%s): %s\n", uris[i], curl_easy_strerror(res));
+ }
+ } else if (multi) {
+ int sr = 0;
+ CURLMcode cd;
+ do {
+ cd = curl_multi_perform(curl_multi, &sr);
+ } while(sr > 0 || cd == CURLM_CALL_MULTI_PERFORM);
+ if (cd != CURLM_OK) {
+ printf("Download failed: %s\n", curl_multi_strerror(cd));
+ exit(3);
+ } else
+ printf("Download succesfull\n");
+ } else
+ event_base_dispatch(e_base);
+
+ for (i = 0; i < URIS_S; i++) {
+ if (!simple)
+ curl_multi_remove_handle(curl_multi, curls[i]);
+ curl_easy_cleanup(curls[i]);
+ }
+
+ if (!simple) {
+ curl_multi_cleanup(curl_multi);
+
+ event_free(e_timer);
+ event_base_free(e_base);
+ }
+ curl_global_cleanup();
+
+ return 0;
+}