#ifndef EV_HTTP_H #define EV_HTTP_H #include "config.h" #include "ev_dispatch.h" #include namespace EVD { struct HttpClient { HttpClient( Dispatch *disp ); ~HttpClient(); // created to manage the socket transitions struct SocketInfo { SocketInfo( HttpClient *client, curl_socket_t sock, CURL *e, int action ); virtual ~SocketInfo(); static void response_cb(struct ev_loop *loop, struct ev_io *w, int revents); void set_sock( curl_socket_t sock, CURL *e, int action ); void finish(); int m_action; long m_timeout; struct ev_io *m_watcher; CURL *m_handle; HttpClient *m_client; Dispatch *m_dispatch; }; void check_handles(); static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp); static int update_timeout_cb(CURLM *multi, long timeout_ms, void *userp); static void timeout_cb(struct ev_loop *loop, struct ev_timer *w, int revents); int m_active; // number of active requests CURLM *m_handle; Dispatch *m_disp; bool m_timer_set; struct ev_timer m_timer; }; // follow the curl handle for the duration of it's request struct HttpRequest : public Request { HttpRequest( Dispatch &dispatch, const std::string &url ); // use this to have the response be written to the file instead, using a response object. // you can not get the response to return over the normal get_next_response or wait_for_response_by_id method if you use this. HttpRequest( Dispatch &dispatch, const std::string &url, int fd ); virtual ~HttpRequest(); virtual bool enable(); void finish( CURLcode rc ); virtual void set_key( request_t key ); virtual void set_opt( const std::string &key, const std::string &value ); static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data); static size_t header_write_cb(void *ptr, size_t size, size_t nmemb, void *data); static int prog_cb(void *p, double dltotal, double dlnow, double ult, double uln); struct HttpResponse *m_response; CURL *m_handle; HttpClient *m_client; char m_error[CURL_ERROR_SIZE]; private: void init_curl(); }; struct HttpResponse : public Response { HttpResponse( const std::string &url ); HttpResponse( const std::string &url, int fd ); void write( void *ptr, size_t realsize, size_t size, size_t nmemb ); void write_header( void *ptr, size_t realsize, size_t size, size_t nmemb ); void finish( HttpClient *client, CURLcode rc ); int m_fd; std::string m_header; }; } #endif