6 #include "CivetServer.h"
12 #ifndef UNUSED_PARAMETER
13 #define UNUSED_PARAMETER(x) (void)(x)
16 bool CivetHandler::handleGet(CivetServer *server,
struct mg_connection *conn)
18 UNUSED_PARAMETER(server);
19 UNUSED_PARAMETER(conn);
23 bool CivetHandler::handlePost(CivetServer *server,
struct mg_connection *conn)
25 UNUSED_PARAMETER(server);
26 UNUSED_PARAMETER(conn);
30 bool CivetHandler::handlePut(CivetServer *server,
struct mg_connection *conn)
32 UNUSED_PARAMETER(server);
33 UNUSED_PARAMETER(conn);
37 bool CivetHandler::handleDelete(CivetServer *server,
struct mg_connection *conn)
39 UNUSED_PARAMETER(server);
40 UNUSED_PARAMETER(conn);
44 int CivetServer::requestHandler(
struct mg_connection *conn,
void *cbdata)
46 struct mg_request_info *request_info = mg_get_request_info(conn);
47 CivetServer *me = (CivetServer*) (request_info->user_data);
48 CivetHandler *handler = (CivetHandler *)cbdata;
51 if (strcmp(request_info->request_method,
"GET") == 0) {
52 return handler->handleGet(me, conn) ? 1 : 0;
53 }
else if (strcmp(request_info->request_method,
"POST") == 0) {
54 return handler->handlePost(me, conn) ? 1 : 0;
55 }
else if (strcmp(request_info->request_method,
"PUT") == 0) {
56 return handler->handlePut(me, conn) ? 1 : 0;
57 }
else if (strcmp(request_info->request_method,
"DELETE") == 0) {
58 return handler->handleDelete(me, conn) ? 1 : 0;
66 CivetServer::CivetServer(
const char **options,
67 const struct mg_callbacks *_callbacks) :
73 context = mg_start(_callbacks,
this, options);
75 struct mg_callbacks callbacks;
76 memset(&callbacks, 0,
sizeof(callbacks));
77 context = mg_start(&callbacks,
this, options);
81 CivetServer::~CivetServer()
86 void CivetServer::addHandler(
const std::string &uri, CivetHandler *handler)
88 mg_set_request_handler(context, uri.c_str(), requestHandler, handler);
91 void CivetServer::removeHandler(
const std::string &uri)
93 mg_set_request_handler(context, uri.c_str(), NULL, NULL);
96 void CivetServer::close()
104 int CivetServer::getCookie(
struct mg_connection *conn,
const std::string &cookieName, std::string &cookieValue)
107 char _cookieValue[4096];
108 const char *cookie = mg_get_header(conn,
"Cookie");
109 int lRead = mg_get_cookie(cookie, cookieName.c_str(), _cookieValue,
sizeof(_cookieValue));
111 cookieValue.append(_cookieValue);
115 const char* CivetServer::getHeader(
struct mg_connection *conn,
const std::string &headerName)
117 return mg_get_header(conn, headerName.c_str());
121 CivetServer::urlDecode(
const char *src, std::string &dst,
bool is_form_url_encoded)
123 urlDecode(src, strlen(src), dst, is_form_url_encoded);
127 CivetServer::urlDecode(
const char *src,
size_t src_len, std::string &dst,
bool is_form_url_encoded)
130 #define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
133 for (i = j = 0; i < (int)src_len; i++, j++) {
134 if (src[i] ==
'%' && i < (
int)src_len - 2 &&
135 isxdigit(* (
const unsigned char *) (src + i + 1)) &&
136 isxdigit(* (
const unsigned char *) (src + i + 2))) {
137 a = tolower(* (
const unsigned char *) (src + i + 1));
138 b = tolower(* (
const unsigned char *) (src + i + 2));
139 dst.push_back((
char) ((HEXTOI(a) << 4) | HEXTOI(b)));
141 }
else if (is_form_url_encoded && src[i] ==
'+') {
144 dst.push_back(src[i]);
150 CivetServer::getParam(
struct mg_connection *conn,
const char *name,
151 std::string &dst,
size_t occurrence)
153 const char *query = mg_get_request_info(conn)->query_string;
154 return getParam(query, strlen(query), name, dst, occurrence);
158 CivetServer::getParam(
const char *data,
size_t data_len,
const char *name,
159 std::string &dst,
size_t occurrence)
161 const char *p, *e, *s;
165 if (data == NULL || name == NULL || data_len == 0) {
168 name_len = strlen(name);
172 for (p = data; p + name_len < e; p++) {
173 if ((p == data || p[-1] ==
'&') && p[name_len] ==
'=' &&
174 !mg_strncasecmp(name, p, name_len) && 0 == occurrence--) {
180 s = (
const char *) memchr(p,
'&', (
size_t)(e - p));
187 urlDecode(p, (
int)(s - p), dst,
true);
195 CivetServer::urlEncode(
const char *src, std::string &dst,
bool append)
197 urlEncode(src, strlen(src), dst, append);
201 CivetServer::urlEncode(
const char *src,
size_t src_len, std::string &dst,
bool append)
203 static const char *dont_escape =
"._-$,;~()";
204 static const char *hex =
"0123456789abcdef";
209 for (; src_len > 0; src++, src_len--) {
210 if (isalnum(*(
const unsigned char *) src) ||
211 strchr(dont_escape, * (
const unsigned char *) src) != NULL) {
215 dst.push_back(hex[(* (
const unsigned char *) src) >> 4]);
216 dst.push_back(hex[(* (
const unsigned char *) src) & 0xf]);