connwrap - initialize gnutls session in cw_connect
[centerim.git] / src / hooks / HTTPClient.h
blob2512a0026feab528d1733d80bccae5f0f94f8f88
1 #ifndef HTTPCLIENT_H
2 #define HTTPCLIENT_H
4 #include "icqcommon.h"
6 #ifdef BUILD_RSS
8 #include "src/SocketClient.h"
9 #include "src/buffer.h"
11 #include "libicq2000/exceptions.h"
13 using namespace ICQ2000;
15 class HTTPRequestEvent : public MessageEvent {
16 friend class HTTPClient;
18 public:
19 enum RequestMethod {
20 GET,
21 POST
24 enum AuthMethod {
25 Basic,
26 User,
27 Digest
30 private:
31 string m_content, m_url, m_httpresp, m_user, m_pass;
32 vector<pair<string, string> > params;
33 map<string, string> authparams;
34 int authTries, connectTries;
36 RequestMethod method;
37 AuthMethod authmethod;
39 public:
40 HTTPRequestEvent(const string &url, RequestMethod rt = GET)
41 : MessageEvent(ContactRef()), m_url(url),
42 authTries(0), connectTries(0), method(rt) { }
44 string getContent() const { return m_content; }
45 string getURL() const { return m_url; }
46 string getHTTPResp() const { return m_httpresp; }
48 MessageType getType() const { return Normal; }
49 RequestMethod getMethod() const { return method; }
51 void setContent(const string &content) { m_content = content; }
52 void setHTTPResp(const string &resp) { m_httpresp = resp; }
54 void setAuth(AuthMethod am, const string &user, const string &pass)
55 { authmethod = am; m_user = user; m_pass = pass; }
57 void addParam(const string pname, const string pval)
58 { params.push_back(make_pair(mime(pname), mime(pval))); }
60 vector<pair<string, string> >::const_iterator pbegin() const { return params.begin(); }
61 vector<pair<string, string> >::const_iterator pend() const { return params.end(); }
63 bool operator == (HTTPRequestEvent &ev) const
64 { return m_url == ev.m_url && params == ev.params && method == ev.method; }
66 bool operator != (HTTPRequestEvent &ev) const
67 { return !(*this == ev); }
70 class HTTPClient : public SocketClient {
71 private:
72 enum State {
73 NOT_CONNECTED,
74 WAITING_FOR_CONNECT,
75 WAITING_FOR_HEADER,
76 RECEIVING_HEADER,
77 RECEIVING_CONTENT,
78 DISCONNECTING
81 list<HTTPRequestEvent*> m_queue;
82 State m_state;
84 Buffer m_recv;
86 string m_hostname, m_proxy_hostname;
87 string m_proxy_user, m_proxy_passwd;
88 string m_resource, m_content, m_redirect;
89 unsigned short m_port, m_proxy_port;
90 unsigned long m_length;
91 time_t m_last_operation, m_timeout;
92 int m_code;
94 void Init();
95 void Parse();
96 void Send(Buffer &b);
98 void SendRequest();
99 void Disconnect();
101 void check_timeout();
102 string strMethod(HTTPRequestEvent::RequestMethod m);
104 public:
105 HTTPClient();
106 ~HTTPClient();
108 void Connect();
109 void FinishNonBlockingConnect();
110 void Recv();
112 void setTimeout(time_t t);
113 time_t getTimeout() const;
115 void clearoutMessagesPoll();
116 void SendEvent(MessageEvent* ev);
118 void socket_cb(int fd, SocketEvent::Mode m);
120 void setProxyServerHost(const string& host) { m_proxy_hostname = host; }
121 string getProxyServerHost() const { return m_proxy_hostname; }
123 void setProxyServerPort(const unsigned short& port) { m_proxy_port = port; }
124 unsigned short getProxyServerPort() const { return m_proxy_port; }
126 void setProxyServerUser(const string& user) { m_proxy_user = user; }
127 string getProxyServerUser() const { return m_proxy_user; }
129 void setProxyServerPasswd(const string& passwd) { m_proxy_passwd = passwd; }
130 string getProxyServerPasswd() const { return m_proxy_passwd; }
133 class HTTPException : public exception {
134 private:
135 string m_errortext;
137 public:
138 HTTPException() {}
139 HTTPException(const string& text): m_errortext(text) {};
140 ~HTTPException() throw() {}
142 const char* what() const throw() { return m_errortext.c_str(); }
145 #endif
147 #endif