-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCivetServer.cpp
More file actions
254 lines (211 loc) · 6.75 KB
/
CivetServer.cpp
File metadata and controls
254 lines (211 loc) · 6.75 KB
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
/*
* Copyright (c) 2013 No Face Press, LLC
* License http://opensource.org/licenses/mit-license.php MIT License
*/
#include "CivetServer.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sstream>
#ifndef UNUSED_PARAMETER
#define UNUSED_PARAMETER(x) (void)(x)
#endif
bool CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn)
{
UNUSED_PARAMETER(server);
UNUSED_PARAMETER(conn);
return false;
}
bool CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn)
{
UNUSED_PARAMETER(server);
UNUSED_PARAMETER(conn);
return false;
}
bool CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn)
{
UNUSED_PARAMETER(server);
UNUSED_PARAMETER(conn);
return false;
}
bool CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn)
{
UNUSED_PARAMETER(server);
UNUSED_PARAMETER(conn);
return false;
}
int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
{
struct mg_request_info *request_info = mg_get_request_info(conn);
CivetServer *me = (CivetServer*) (request_info->user_data);
CivetHandler *handler = (CivetHandler *)cbdata;
if (handler) {
if (strcmp(request_info->request_method, "GET") == 0) {
return handler->handleGet(me, conn) ? 1 : 0;
} else if (strcmp(request_info->request_method, "POST") == 0) {
return handler->handlePost(me, conn) ? 1 : 0;
} else if (strcmp(request_info->request_method, "PUT") == 0) {
return !handler->handlePut(me, conn) ? 1 : 0;
} else if (strcmp(request_info->request_method, "DELETE") == 0) {
return !handler->handleDelete(me, conn) ? 1 : 0;
}
}
return 0; // No handler found
}
CivetServer::CivetServer(const char **options,
const struct mg_callbacks *_callbacks) :
context(0)
{
if (_callbacks) {
context = mg_start(_callbacks, this, options);
} else {
struct mg_callbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
context = mg_start(&callbacks, this, options);
}
}
CivetServer::~CivetServer()
{
close();
}
void CivetServer::addHandler(const std::string &uri, CivetHandler *handler)
{
mg_set_request_handler(context, uri.c_str(), requestHandler, handler);
}
void CivetServer::removeHandler(const std::string &uri)
{
mg_set_request_handler(context, uri.c_str(), NULL, NULL);
}
void CivetServer::close()
{
if (context) {
mg_stop (context);
context = 0;
}
}
int CivetServer::getCookie(struct mg_connection *conn, const std::string &cookieName, std::string &cookieValue)
{
//Maximum cookie length as per microsoft is 4096. http://msdn.microsoft.com/en-us/library/ms178194.aspx
char _cookieValue[4096];
const char *cookie = mg_get_header(conn, "Cookie");
int lRead = mg_get_cookie(cookie, cookieName.c_str(), _cookieValue, sizeof(_cookieValue));
cookieValue.clear();
cookieValue.append(_cookieValue);
return lRead;
}
const char* CivetServer::getHeader(struct mg_connection *conn, const std::string &headerName)
{
return mg_get_header(conn, headerName.c_str());
}
void
CivetServer::urlDecode(const char *src, std::string &dst, bool is_form_url_encoded)
{
urlDecode(src, strlen(src), dst, is_form_url_encoded);
}
void
CivetServer::urlDecode(const char *src, size_t src_len, std::string &dst, bool is_form_url_encoded)
{
int i, j, a, b;
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
dst.clear();
for (i = j = 0; i < (int)src_len; i++, j++) {
if (src[i] == '%' && i < (int)src_len - 2 &&
isxdigit(* (const unsigned char *) (src + i + 1)) &&
isxdigit(* (const unsigned char *) (src + i + 2))) {
a = tolower(* (const unsigned char *) (src + i + 1));
b = tolower(* (const unsigned char *) (src + i + 2));
dst.push_back((char) ((HEXTOI(a) << 4) | HEXTOI(b)));
i += 2;
} else if (is_form_url_encoded && src[i] == '+') {
dst.push_back(' ');
} else {
dst.push_back(src[i]);
}
}
}
bool
CivetServer::getParam(struct mg_connection *conn, const char *name,
std::string &dst, size_t occurrence)
{
const char *query = mg_get_request_info(conn)->query_string;
if (!query)
return false;
return getParam(query, strlen(query), name, dst, occurrence);
}
bool
CivetServer::getParam(const char *data, size_t data_len, const char *name,
std::string &dst, size_t occurrence)
{
const char *p, *e, *s;
size_t name_len;
dst.clear();
if (data == NULL || name == NULL || data_len == 0) {
return false;
}
name_len = strlen(name);
e = data + data_len;
// data is "var1=val1&var2=val2...". Find variable first
for (p = data; p + name_len < e; p++) {
if ((p == data || p[-1] == '&') && p[name_len] == '=' &&
!mg_strncasecmp(name, p, name_len) && 0 == occurrence--) {
// Point p to variable value
p += name_len + 1;
// Point s to the end of the value
s = (const char *) memchr(p, '&', (size_t)(e - p));
if (s == NULL) {
s = e;
}
assert(s >= p);
// Decode variable into destination buffer
urlDecode(p, (int)(s - p), dst, true);
return true;
}
}
return false;
}
void
CivetServer::urlEncode(const char *src, std::string &dst, bool append)
{
urlEncode(src, strlen(src), dst, append);
}
void
CivetServer::urlEncode(const char *src, size_t src_len, std::string &dst, bool append)
{
static const char *dont_escape = "._-$,;~()";
static const char *hex = "0123456789abcdef";
if (!append)
dst.clear();
for (; src_len > 0; src++, src_len--) {
if (isalnum(*(const unsigned char *) src) ||
strchr(dont_escape, * (const unsigned char *) src) != NULL) {
dst.push_back(*src);
} else {
dst.push_back('%');
dst.push_back(hex[(* (const unsigned char *) src) >> 4]);
dst.push_back(hex[(* (const unsigned char *) src) & 0xf]);
}
}
}
HttpResponse::HttpResponse(mg_connection* conn, Code_t code)
: m_conn(conn), m_code(code)
{}
std::string code_name(HttpResponse::Code_t code)
{
switch (code) {
case HttpResponse::OK:
return "OK";
case HttpResponse::INTERNAL_SERVER_ERROR:
return "Internal Server Error";
}
assert(false);
return "";
}
HttpResponse::~HttpResponse()
{
std::stringstream out;
out << "HTTP/1.1 " << int(m_code) << " " << code_name(m_code) << "\r\n\r\n";
if (m_message.size() > 0) {
out << m_message;
}
mg_printf(m_conn, out.str().c_str());
}