123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- /* Copyright (c) 2013-2014 the Civetweb developers
- * 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>
- #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);
- assert(request_info != NULL);
- CivetServer *me = (CivetServer*) (request_info->user_data);
- assert(me != NULL);
- 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), postData(0)
- {
- struct mg_callbacks callbacks;
- memset(&callbacks, 0, sizeof(callbacks));
- if (_callbacks) {
- callbacks = *_callbacks;
- userCloseHandler = _callbacks->connection_close;
- } else {
- userCloseHandler = NULL;
- }
- callbacks.connection_close = closeHandler;
- context = mg_start(&callbacks, this, options);
- }
- CivetServer::~CivetServer()
- {
- close();
- }
- void CivetServer::closeHandler(struct mg_connection *conn)
- {
- struct mg_request_info *request_info = mg_get_request_info(conn);
- assert(request_info != NULL);
- CivetServer *me = (CivetServer*) (request_info->user_data);
- assert(me != NULL);
- if (me->userCloseHandler) me->userCloseHandler(conn);
- if (me->postData) {
- delete [] (me->postData);
- me->postData = 0;
- }
- }
- 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 *formParams = NULL;
- struct mg_request_info *ri = mg_get_request_info(conn);
- assert(ri != NULL);
- CivetServer *me = (CivetServer*) (ri->user_data);
- assert(me != NULL);
- if (me->postData != NULL) {
- formParams = me->postData;
- } else {
- const char * con_len_str = mg_get_header(conn, "Content-Length");
- if (con_len_str) {
- unsigned long con_len = atoi(con_len_str);
- if (con_len>0) {
- me->postData = new char[con_len];
- if (me->postData != NULL) {
- /* NULL check is not required according to current C++ standard */
- mg_read(conn, me->postData, con_len);
- formParams = me->postData;
- }
- }
- }
- }
- if (formParams == NULL) {
- // get requests do store html <form> field values in the http query_string
- formParams = ri->query_string;
- }
- if (formParams != NULL) {
- return getParam(formParams, strlen(formParams), name, dst, occurrence);
- }
- return false;
- }
- 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]);
- }
- }
- }
|