Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #include <curl/curl.h>
- #include <event2\event.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include "http.h"
- #include <string>
- #define MSG_OUT stdout
- /* Global information, common to all connections */
- typedef struct _GlobalInfo {
- struct event_base* base;
- struct event* timer_event;
- CURLM *multi;
- int still_running;
- http_handler_fn callback;
- } GlobalInfo;
- /* Information associated with a specific easy handle */
- typedef struct _ConnInfo {
- CURL *easy;
- char *url;
- GlobalInfo *global;
- char error[CURL_ERROR_SIZE];
- char* data;
- int data_size;
- int data_pos;
- // std::string data;
- } 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_del(g->timer_event);
- if( timeout_ms != -1 ) 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, GlobalInfo *g)
- {
- char error_str[256];
- if ( CURLM_OK != code ) {
- const char *s;
- switch (code) {
- case CURLM_CALL_MULTI_PERFORM: s="CURLM_CALL_MULTI_PERFORM"; break;
- 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";
- //sprintf(error_str, "ERROR: %s returns %s", where, s);
- /* ignore this error */
- return;
- }
- sprintf(error_str, "ERROR: %s returns %s", where, s);
- g->callback(-1, 0, 0, error_str);
- }
- }
- /* 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;
- 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);
- // get http response code
- unsigned long rc = 0;
- curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &rc);
- // call callback before we clean up the connection
- g->callback(rc, conn->data, conn->data_pos, conn->error);
- // clean up connection
- curl_multi_remove_handle(g->multi, easy);
- free(conn->url);
- free(conn->data);
- 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, g);
- 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);
- }
- curl_multi_cleanup(g->multi);
- free(g);
- }
- }
- /* 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, g);
- check_multi_info(g);
- }
- /* Clean up the SockInfo structure */
- static void remsock(SockInfo *f)
- {
- if (f) {
- if (f->evset)
- event_del(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_del(f->ev);
- f->ev = event_new(g->base, 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 = (SockInfo*)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]);
- 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;
- while( conn->data_size - conn->data_pos < realsize )
- {
- fprintf(MSG_OUT, "!! resize buf size %i data_pos %i realsize %i\n", conn->data_size, conn->data_pos, realsize);
- conn->data = (char*)realloc(conn->data, 2*conn->data_size);
- conn->data_size *=2;
- }
- memcpy(conn->data + conn->data_pos, ptr, realsize);
- conn->data_pos += realsize;
- fprintf(MSG_OUT, "write_cb: %i\n", realsize);
- 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(const char *url, GlobalInfo *g )
- {
- ConnInfo *conn;
- CURLMcode rc;
- conn = (ConnInfo *)calloc(1, sizeof(ConnInfo));
- memset(conn, 0, sizeof(ConnInfo));
- conn->error[0]='\0';
- conn->data_size = 4096;
- conn->data_pos = 0;
- conn->data = (char*)malloc(4096);
- conn->easy = curl_easy_init();
- if (!conn->easy)
- {
- g->callback(-1, 0, 0, "curl_easy_init() failed" );
- return;
- }
- 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, g);
- /* 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 */
- }
- typedef void (*http_handler_fn)(int response_code, const char* data, size_t len, const char* error);
- void add_http(const char* url, struct event_base* base, http_handler_fn callback)
- {
- GlobalInfo* g = (GlobalInfo *)calloc(1, sizeof(GlobalInfo));
- memset(g, 0, sizeof(GlobalInfo));
- g->base = base;
- g->multi = curl_multi_init();
- g->callback = callback;
- g->timer_event = event_new(base, -1, EV_PERSIST, timer_cb, g);
- 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);
- new_conn(url, g);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement