Anope IRC Services  Version 2.0
httpd.h
Go to the documentation of this file.
1 #ifndef ANOPE_HTTPD_H
2 #define ANOPE_HTTPD_H
3 
5 {
6  HTTP_ERROR_OK = 200,
7  HTTP_FOUND = 302,
11 };
12 
13 /* A message to someone */
14 struct HTTPReply
15 {
18  std::map<Anope::string, Anope::string> headers;
19  typedef std::list<std::pair<Anope::string, Anope::string> > cookie;
20  std::vector<cookie> cookies;
21 
22  HTTPReply() : error(HTTP_ERROR_OK), length(0) { }
23 
24  HTTPReply(const HTTPReply& other) : error(other.error), length(other.length)
25  {
26  content_type = other.content_type;
27  headers = other.headers;
28  cookies = other.cookies;
29 
30  for (unsigned i = 0; i < other.out.size(); ++i)
31  out.push_back(new Data(other.out[i]->buf, other.out[i]->len));
32  }
33 
35  {
36  for (unsigned i = 0; i < out.size(); ++i)
37  delete out[i];
38  out.clear();
39  }
40 
41  struct Data
42  {
43  char *buf;
44  size_t len;
45 
46  Data(const char *b, size_t l)
47  {
48  this->buf = new char[l];
49  memcpy(this->buf, b, l);
50  this->len = l;
51  }
52 
54  {
55  delete [] buf;
56  }
57  };
58 
59  std::deque<Data *> out;
60  size_t length;
61 
62  void Write(const Anope::string &message)
63  {
64  this->out.push_back(new Data(message.c_str(), message.length()));
65  this->length += message.length();
66  }
67 
68  void Write(const char *b, size_t l)
69  {
70  this->out.push_back(new Data(b, l));
71  this->length += l;
72  }
73 };
74 
75 /* A message from soneone */
77 {
78  std::map<Anope::string, Anope::string> headers;
79  std::map<Anope::string, Anope::string> cookies;
80  std::map<Anope::string, Anope::string> get_data;
81  std::map<Anope::string, Anope::string> post_data;
83 };
84 
85 class HTTPClient;
86 class HTTPProvider;
87 
88 class HTTPPage : public Base
89 {
92 
93  public:
94  HTTPPage(const Anope::string &u, const Anope::string &ct = "text/html") : url(u), content_type(ct) { }
95 
96  const Anope::string &GetURL() const { return this->url; }
97 
98  const Anope::string &GetContentType() const { return this->content_type; }
99 
107  virtual bool OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &) = 0;
108 };
109 
110 class HTTPClient : public ClientSocket, public BinarySocket, public Base
111 {
112  protected:
113  void WriteClient(const Anope::string &message)
114  {
115  BinarySocket::Write(message + "\r\n");
116  }
117 
118  public:
119  HTTPClient(ListenSocket *l, int f, const sockaddrs &a) : ClientSocket(l, a), BinarySocket() { }
120 
121  virtual const Anope::string GetIP()
122  {
123  return this->clientaddr.addr();
124  }
125 
126  virtual void SendError(HTTPError err, const Anope::string &msg) = 0;
127  virtual void SendReply(HTTPReply *) = 0;
128 };
129 
130 class HTTPProvider : public ListenSocket, public Service
131 {
133  unsigned short port;
134  bool ssl;
135  public:
137  std::vector<Anope::string> ext_headers;
138 
139  HTTPProvider(Module *c, const Anope::string &n, const Anope::string &i, const unsigned short p, bool s) : ListenSocket(i, p, i.find(':') != Anope::string::npos), Service(c, "HTTPProvider", n), ip(i), port(p), ssl(s) { }
140 
141  const Anope::string &GetIP() const
142  {
143  return this->ip;
144  }
145 
146  unsigned short GetPort() const
147  {
148  return this->port;
149  }
150 
151  bool IsSSL() const
152  {
153  return this->ssl;
154  }
155 
156  virtual bool RegisterPage(HTTPPage *page) = 0;
157  virtual void UnregisterPage(HTTPPage *page) = 0;
158  virtual HTTPPage* FindPage(const Anope::string &name) = 0;
159 };
160 
161 namespace HTTPUtils
162 {
164  {
165  Anope::string decoded;
166 
167  for (unsigned i = 0; i < url.length(); ++i)
168  {
169  const char& c = url[i];
170 
171  if (c == '%' && i + 2 < url.length())
172  {
173  Anope::string dest;
174  Anope::Unhex(url.substr(i + 1, 2), dest);
175  decoded += dest;
176  i += 2;
177  }
178  else if (c == '+')
179  decoded += ' ';
180  else
181  decoded += c;
182  }
183 
184  return decoded;
185  }
186 
188  {
189  Anope::string encoded;
190 
191  for (unsigned i = 0; i < url.length(); ++i)
192  {
193  const char& c = url[i];
194 
195  if (isalnum(c) || c == '.' || c == '-' || c == '*' || c == '_')
196  encoded += c;
197  else if (c == ' ')
198  encoded += '+';
199  else
200  encoded += "%" + Anope::Hex(c);
201  }
202 
203  return encoded;
204  }
205 
206  inline Anope::string Escape(const Anope::string &src)
207  {
208  Anope::string dst;
209 
210  for (unsigned i = 0; i < src.length(); ++i)
211  {
212  switch (src[i])
213  {
214  case '<':
215  dst += "&lt;";
216  break;
217  case '>':
218  dst += "&gt;";
219  break;
220  case '"':
221  dst += "&quot;";
222  break;
223  default:
224  dst += src[i];
225  }
226  }
227 
228  return dst;
229  }
230 }
231 
232 #endif // ANOPE_HTTPD_H
HTTPError
Definition: httpd.h:4
virtual bool RegisterPage(HTTPPage *page)=0
~HTTPReply()
Definition: httpd.h:34
unsigned short GetPort() const
Definition: httpd.h:146
const Anope::string & GetURL() const
Definition: httpd.h:96
std::map< Anope::string, Anope::string > headers
Definition: httpd.h:78
Anope::string content_type
Definition: httpd.h:17
Anope::string content
Definition: httpd.h:82
Definition: base.h:17
virtual void Write(const char *buffer, size_t l)
char * buf
Definition: httpd.h:43
Anope::string Escape(const Anope::string &src)
Definition: httpd.h:206
Anope::string URLEncode(const Anope::string &url)
Definition: httpd.h:187
Anope::string addr() const
Definition: sockets.cpp:73
const Anope::string & GetIP() const
Definition: httpd.h:141
HTTPProvider(Module *c, const Anope::string &n, const Anope::string &i, const unsigned short p, bool s)
Definition: httpd.h:139
Anope::string url
Definition: httpd.h:90
bool IsSSL() const
Definition: httpd.h:151
virtual void SendReply(HTTPReply *)=0
sockaddrs clientaddr
Definition: sockets.h:426
std::list< std::pair< Anope::string, Anope::string > > cookie
Definition: httpd.h:19
unsigned short port
Definition: httpd.h:133
void WriteClient(const Anope::string &message)
Definition: httpd.h:113
CoreExport string Hex(const string &data)
Definition: misc.cpp:546
virtual void UnregisterPage(HTTPPage *page)=0
HTTPClient(ListenSocket *l, int f, const sockaddrs &a)
Definition: httpd.h:119
string substr(size_type pos=0, size_type n=npos) const
Definition: anope.h:277
size_type length() const
Definition: anope.h:131
size_t len
Definition: httpd.h:44
std::map< Anope::string, Anope::string > headers
Definition: httpd.h:18
size_t length
Definition: httpd.h:60
virtual HTTPPage * FindPage(const Anope::string &name)=0
std::map< Anope::string, Anope::string > cookies
Definition: httpd.h:79
HTTPError error
Definition: httpd.h:16
std::vector< cookie > cookies
Definition: httpd.h:20
bool ssl
Definition: httpd.h:134
const Anope::string & GetContentType() const
Definition: httpd.h:98
std::basic_string< char, ci_char_traits, std::allocator< char > > string
Definition: hashcomp.h:133
HTTPReply()
Definition: httpd.h:22
Anope::string ext_ip
Definition: httpd.h:136
Anope::string content_type
Definition: httpd.h:91
std::map< Anope::string, Anope::string > post_data
Definition: httpd.h:81
void Write(const Anope::string &message)
Definition: httpd.h:62
Anope::string name
Definition: service.h:88
void Write(const char *b, size_t l)
Definition: httpd.h:68
HTTPReply(const HTTPReply &other)
Definition: httpd.h:24
std::deque< Data * > out
Definition: httpd.h:59
Data(const char *b, size_t l)
Definition: httpd.h:46
virtual bool OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &)=0
const char * c_str() const
Definition: anope.h:117
virtual const Anope::string GetIP()
Definition: httpd.h:121
Anope::string URLDecode(const Anope::string &url)
Definition: httpd.h:163
Definition: httpd.h:88
Anope::string ip
Definition: httpd.h:132
std::map< Anope::string, Anope::string > get_data
Definition: httpd.h:80
std::vector< Anope::string > ext_headers
Definition: httpd.h:137
Definition: anope.h:20
CoreExport void Unhex(const string &src, string &dest)
Definition: misc.cpp:575
virtual void SendError(HTTPError err, const Anope::string &msg)=0
HTTPPage(const Anope::string &u, const Anope::string &ct="text/html")
Definition: httpd.h:94