summaryrefslogtreecommitdiff
path: root/include/modules/httpd.h
blob: 3d524a27e06262013819478e9e3b35e9b56e944e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 *
 * (C) 2012-2025 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 */

#pragma once

namespace HTTP
{
	class Reply;
	class Message;
	class Page;
	class Client;
	class Provider;

	enum Error
	{
		OK = 200,
		FOUND = 302,
		BAD_REQUEST = 400,
		PAGE_NOT_FOUND = 404,
		NOT_SUPPORTED = 505,
	};
}

/* A message to someone */
struct HTTP::Reply final
{
	HTTP::Error error = HTTP::OK;
	Anope::string content_type;
	std::map<Anope::string, Anope::string, ci::less> headers;
	typedef std::list<std::pair<Anope::string, Anope::string> > cookie;
	std::vector<cookie> cookies;

	Reply() = default;
	Reply &operator=(const HTTP::Reply &) = default;

	Reply(const HTTP::Reply &other)
		: error(other.error)
		, length(other.length)
	{
		content_type = other.content_type;
		headers = other.headers;
		cookies = other.cookies;

		for (const auto &datum : other.out)
			out.push_back(new Data(datum->buf, datum->len));
	}

	~Reply()
	{
		for (const auto *datum : out)
			delete datum;
		out.clear();
	}

	struct Data final
	{
		char *buf;
		size_t len;

		Data(const char *b, size_t l)
		{
			this->buf = new char[l];
			memcpy(this->buf, b, l);
			this->len = l;
		}

		~Data()
		{
			delete [] buf;
		}
	};

	std::deque<Data *> out;
	size_t length = 0;

	void Write(const Anope::string &message)
	{
		this->out.push_back(new Data(message.c_str(), message.length()));
		this->length += message.length();
	}

	void Write(const char *b, size_t l)
	{
		this->out.push_back(new Data(b, l));
		this->length += l;
	}
};

/* A message from someone */
struct HTTP::Message final
{
	std::map<Anope::string, Anope::string, ci::less> headers;
	std::map<Anope::string, Anope::string, ci::less> cookies;
	std::map<Anope::string, Anope::string, ci::less> get_data;
	std::map<Anope::string, Anope::string, ci::less> post_data;
	Anope::string content;
};

class HTTP::Page
	: public virtual Base
{
	Anope::string url;
	Anope::string content_type;

public:
	Page(const Anope::string &u, const Anope::string &ct = "text/html")
		: url(u)
		, content_type(ct)
	{
	}

	const Anope::string &GetURL() const { return this->url; }

	const Anope::string &GetContentType() const { return this->content_type; }

	/** Called when this page is requested
	 * @param The server this page is on
	 * @param The page name
	 * @param The client requesting the page
	 * @param The HTTP header sent from the client to request the page
	 * @param The HTTP header that will be sent back to the client
	 */
	virtual bool OnRequest(HTTP::Provider *, const Anope::string &, HTTP::Client *, HTTP::Message &, HTTP::Reply &) = 0;
};

class HTTP::Client
	: public ClientSocket
	, public BinarySocket
	, public Base
{
protected:
	void WriteClient(const Anope::string &message)
	{
		BinarySocket::Write(message + "\r\n");
	}

public:
	Client(ListenSocket *l, int f, const sockaddrs &a)
		: ClientSocket(l, a)
		, BinarySocket()
	{
	}

	virtual const Anope::string GetIP()
	{
		return this->clientaddr.addr();
	}

	virtual void SendError(HTTP::Error err, const Anope::string &msg) = 0;
	virtual void SendReply(HTTP::Reply *) = 0;
};

#define HTTP_PROVIDER "HTTP::Provider"

class HTTP::Provider
	: public ListenSocket
	, public Service
{
	Anope::string ip;
	unsigned short port;
	bool ssl;
public:
	std::vector<Anope::string> ext_ips;
	std::vector<Anope::string> ext_headers;

	Provider(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, HTTP_PROVIDER, n)
		, ip(i)
		, port(p)
		, ssl(s)
	{
	}

	const Anope::string &GetIP() const
	{
		return this->ip;
	}

	unsigned short GetPort() const
	{
		return this->port;
	}

	bool IsSSL() const
	{
		return this->ssl;
	}

	virtual bool RegisterPage(HTTP::Page *page) = 0;
	virtual void UnregisterPage(HTTP::Page *page) = 0;
	virtual HTTP::Page *FindPage(const Anope::string &name) = 0;
};

namespace HTTP
{
	inline Anope::string URLDecode(const Anope::string &url)
	{
		Anope::string decoded;

		for (unsigned i = 0; i < url.length(); ++i)
		{
			const char &c = url[i];

			if (c == '%' && i + 2 < url.length())
			{
				Anope::string dest;
				Anope::Unhex(url.substr(i + 1, 2), dest);
				decoded += dest;
				i += 2;
			}
			else if (c == '+')
				decoded += ' ';
			else
				decoded += c;
		}

		return decoded;
	}

	inline Anope::string URLEncode(const Anope::string &url)
	{
		Anope::string encoded;

		for (const auto c : url)
		{
			if (isalnum(c) || c == '.' || c == '-' || c == '*' || c == '_')
				encoded += c;
			else if (c == ' ')
				encoded += '+';
			else
				encoded += "%" + Anope::Hex(c);
		}

		return encoded;
	}

	inline Anope::string Escape(const Anope::string &src)
	{
		Anope::string dst;

		for (const auto c : src)
		{
			switch (c)
			{
				case '<':
					dst += "&lt;";
					break;
				case '>':
					dst += "&gt;";
					break;
				case '"':
					dst += "&quot;";
					break;
				case '&':
					dst += "&amp;";
					break;
				default:
					dst += c;
			}
		}

		return dst;
	}
}