finish rewriting of http client code
[citadel.git] / citadel / event_client.h
1 #include <ev.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netdb.h>
5 #include <arpa/nameser.h>
6 #include <ares.h>
7 #include <curl/curl.h>
8
9 typedef struct AsyncIO AsyncIO;
10
11 typedef enum _eNextState {
12         eSendDNSQuery,
13         eReadDNSReply,
14         eConnect,
15         eSendReply, 
16         eSendMore,
17         eReadMessage, 
18         eTerminateConnection,
19         eAbort
20 }eNextState;
21
22 typedef eNextState (*IO_CallBack)(AsyncIO *IO);
23 typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO);
24 typedef void (*ParseDNSAnswerCb)(AsyncIO*, unsigned char*, int);
25 typedef void (*FreeDNSReply)(void *DNSData);
26
27 typedef struct _DNSQueryParts {
28         ParseDNSAnswerCb DNS_CB;
29         IO_CallBack PostDNS;
30
31         int DNSStatus;
32         void *VParsedDNSReply;
33         FreeDNSReply DNSReplyFree;
34         void *Data;
35 } DNSQueryParts;
36
37 typedef struct _evcurl_request_data 
38 {
39         CURL              *chnd;
40         struct curl_slist *headers;
41         char               errdesc[CURL_ERROR_SIZE];
42
43         int                attached;
44
45         char              *PlainPostData;
46         long               PlainPostDataLen;
47         StrBuf            *PostData;
48
49         StrBuf            *ReplyData;
50         long               httpcode;
51 } evcurl_request_data;
52
53 struct AsyncIO {
54         eNextState NextState;
55
56         /* connection related */
57         ParsedURL *ConnectMe;
58         
59         /* read/send related... */
60         StrBuf *IOBuf;
61         IOBuffer SendBuf, 
62                 RecvBuf;
63
64         /* our events... */
65         ev_cleanup abort_by_shutdown; /* server wants to go down... */
66         ev_timer conn_fail,           /* connection establishing timed out */
67                 rw_timeout;           /* timeout while sending data */
68         ev_idle unwind_stack;         /* get c-ares out of the stack */
69         ev_io recv_event,             /* receive data from the client */
70                 send_event,           /* send more data to the client */
71                 conn_event;           /* Connection successfully established */
72
73         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
74
75         /* Citadel application callbacks... */
76         IO_CallBack ReadDone, /* Theres new data to read... */
77                 SendDone,     /* we may send more data */
78                 Terminate,    /* shutting down... */
79                 Timeout,      /* Timeout handler; may also be connection timeout */
80                 ConnFail,     /* What to do when one connection failed? */
81                 ShutdownAbort;/* we're going down. make your piece. */ 
82
83         IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
84
85         /* DNS Related */
86         ev_io dns_recv_event, 
87                 dns_send_event;
88         struct ares_options DNSOptions;
89         ares_channel DNSChannel;
90         DNSQueryParts *DNSQuery;
91         
92         evcurl_request_data HttpReq;
93
94         /* Custom data; its expected to contain  AsyncIO so we can save malloc()s... */
95         void *Data;        /* application specific data */
96         void *CitContext;  /* Citadel Session context... */
97 };
98
99 typedef struct _IOAddHandler {
100         AsyncIO *IO;
101         IO_CallBack EvAttch;
102 }IOAddHandler; 
103
104 void FreeAsyncIOContents(AsyncIO *IO);
105
106 int QueueEventContext(AsyncIO *IO, IO_CallBack CB);
107 int ShutDownEventQueue(void);
108
109 eNextState InitEventIO(AsyncIO *IO, 
110                        void *pData, 
111                        double conn_timeout, 
112                        double first_rw_timeout,
113                        int ReadFirst);
114 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
115
116 int QueueQuery(ns_type Type, const char *name, AsyncIO *IO, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
117 void QueueGetHostByName(AsyncIO *IO, const char *Hostname, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
118
119 void QueryCbDone(AsyncIO *IO);
120
121 void StopClient(AsyncIO *IO);
122
123 void StopClientWatchers(AsyncIO *IO);
124
125 void SetNextTimeout(AsyncIO *IO, double timeout);
126
127 void InitC_ares_dns(AsyncIO *IO);
128
129 #include <curl/curl.h>
130
131 #define OPT(s, v) \
132         do { \
133                 sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v)); \
134                 if (sta)  {                                             \
135                         CtdlLogPrintf(CTDL_ERR, "error setting option " #s " on curl handle: %s", curl_easy_strerror(sta)); \
136         } } while (0)
137
138
139 int evcurl_init(AsyncIO *IO, 
140                 void *CustomData, 
141                 const char* Desc,
142                 IO_CallBack CallBack);
143
144 void evcurl_handle_start(AsyncIO *IO);