Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best Exception Handling and improved getAllVariables #23

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
CMakeLists.txt.user
16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ option (ENABLE_STATS

option (ENABLE_REGEX_URL
"Enable url regex matching dispatcher" OFF)

option (ENABLE_SESSION_BOOST_ANY
"Enable session values with boost any" OFF)

set (JSONCPP_DIR "${PROJECT_SOURCE_DIR}/../jsoncpp"
CACHE STRING "Json C++ directory")
Expand All @@ -40,9 +43,16 @@ endif (ENABLE_STATS)

if (ENABLE_REGEX_URL)
add_definitions("-DENABLE_REGEX_URL")
SET (CMAKE_CXX_FLAGS "-std=c++11")
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif (ENABLE_REGEX_URL)

if (ENABLE_SESSION_BOOST_ANY)
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
set (EXTRA_LIBS ${EXTRA_LIBS} ${Boost_LIBRARIES})
add_definitions("-DENABLE_SESSION_BOOST_ANY")
endif (ENABLE_SESSION_BOOST_ANY)

if (CPP_BINDING)
set (SOURCES
${SOURCES}
Expand Down Expand Up @@ -84,9 +94,9 @@ if (NOT WEBSOCKET)
add_definitions("-DNO_WEBSOCKET")
endif (NOT WEBSOCKET)

# Adding dl
# Adding dl and pthread
if (NOT WIN32)
set (EXTRA_LIBS ${EXTRA_LIBS} dl)
set (EXTRA_LIBS ${EXTRA_LIBS} dl pthread)
endif (NOT WIN32)

# Adding sockets for Win32
Expand Down
4 changes: 3 additions & 1 deletion mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
#include <io.h> // For _lseeki64
#include <direct.h> // For _mkdir
typedef int socklen_t;
typedef HANDLE pid_t;
#ifndef __MINGW32__
typedef HANDLE pid_t;
#endif
typedef SOCKET sock_t;
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
Expand Down
1 change: 1 addition & 0 deletions mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void *mg_start_thread(void *(*func)(void *), void *param);
char *mg_md5(char buf[33], ...);
int mg_authorize_digest(struct mg_connection *c, FILE *fp);
void mg_send_digest_auth_request(struct mg_connection *conn);
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded);

void mg_server_do_i_handle(struct mg_server *server, mg_handler_t handler);

Expand Down
10 changes: 0 additions & 10 deletions mongoose/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@ namespace Mongoose

}

Response *Controller::serverInternalError(string message)
{
StreamResponse *response = new StreamResponse;

response->setCode(HTTP_SERVER_ERROR);
*response << "[500] Server internal error: " << message;

return response;
}

vector<string> Controller::getUrls()
{
return urls;
Expand Down
9 changes: 0 additions & 9 deletions mongoose/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,6 @@ namespace Mongoose
*/
void dumpRoutes();

/**
* Called when an exception occur during the rendering
*
* @param string the error message
*
* @return response a response to send, 404 will occur if NULL
*/
virtual Response *serverInternalError(string message);

/**
* Gets the session for a request/response
*
Expand Down
32 changes: 25 additions & 7 deletions mongoose/Request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>
#include <mongoose.h>
#include "Request.h"
#include "Utils.h"

using namespace std;

Expand Down Expand Up @@ -153,16 +154,17 @@ namespace Mongoose

return false;
}

map<string, string> Request::getAllVariable()
multimap<string, string> Request::getAllVariable()
{
map<string, string> mapKeyValue;
stringstream ss(data);
multimap<string, string> mapKeyValue;
stringstream ss(data + '&' + (connection->query_string ? connection->query_string:"")); //POST + GET
string param;
while(std::getline(ss, param, '&')){ //block for '&'
const string& key = param.substr(0, param.find('='));
const string& value = param.substr(param.find('=')+1);
mapKeyValue[key] = value; // insert map
const size_t& delimitPos = param.find('=');
const string& key = param.substr(0, delimitPos);
const string& value = param.substr(delimitPos+1);
mapKeyValue.insert(make_pair(key, Utils::urlDecode(value) ) ); // insert map
}
return mapKeyValue;
}
Expand Down Expand Up @@ -210,6 +212,22 @@ namespace Mongoose
return output;
}

// Looking on the header
const char* referer = mg_get_header(this->connection, "Referer");
if(referer){
output = referer;
stringstream ss(output.substr(output.find('?')+1));
string param;
while(std::getline(ss, param, '&')){ //block for '&'
const size_t& delimitPos = param.find('=');
const string& name = param.substr(0, delimitPos);
if(name == key){
const string& value = param.substr(delimitPos+1);
return value;
}
}
}

return fallback;
}

Expand Down
2 changes: 1 addition & 1 deletion mongoose/Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace Mongoose
* @brief getAllVariable
* @return map<string, string> with all variables
*/
map<string, string> getAllVariable();
multimap<string, string> getAllVariable();

/**
* Get the value for a certain variable
Expand Down
20 changes: 18 additions & 2 deletions mongoose/RequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ namespace Mongoose
{
}

/**
* Called when an exception occur during the rendering
*
* @param string the error message
*
* @return response a response to send, 404 will occur if NULL
*/
virtual void serverInternalError(Request& request, Response& response, string message)
{
response.setCode(HTTP_SERVER_ERROR);
std::cerr << "Request: " << request.getUrl() << " Method: " << request.getMethod() << std::endl;
std::cerr << "[500] Server internal error: " << message;
}

Response *process(Request &request)
{
R *response = new R;
Expand All @@ -32,9 +46,11 @@ namespace Mongoose
controller->preProcess(request, *response);
(controller->*function)(request, *response);
} catch (string exception) {
return controller->serverInternalError(exception);
serverInternalError(request, *response, exception);
} catch (const std::exception& exception) {
serverInternalError(request, *response, exception.what());
} catch (...) {
return controller->serverInternalError("Unknown error");
serverInternalError(request, *response, "Unknown error");
}

return response;
Expand Down
19 changes: 14 additions & 5 deletions mongoose/Server.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef _MSC_VER
#include <unistd.h>
# include <unistd.h>
#else
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
# include <sys/types.h>
# include <sys/timeb.h>
#endif
#include <time.h>
#include <string>
#include <string.h>
#include <iostream>
Expand Down Expand Up @@ -77,7 +77,7 @@ namespace Mongoose
Server::Server(int port, const char *documentRoot)
:
stopped(false),
destroyed(false),
destroyed(true),
server(NULL)
#ifndef NO_WEBSOCKET
,websockets(NULL)
Expand Down Expand Up @@ -256,6 +256,15 @@ namespace Mongoose
optionsMap[key] = value;
}

string Server::getOption(const string& key)
{
map<string, string>::iterator it = optionsMap.find(key);
if (it != optionsMap.end())
return it->second;

return "";
}

#ifndef NO_WEBSOCKET
WebSockets &Server::getWebSockets()
{
Expand Down
8 changes: 8 additions & 0 deletions mongoose/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ namespace Mongoose
*/
void setOption(string key, string value);

/**
* Get a mongoose extra option
*
* @param string the name of the option
* @return string the value of the option
*/
string getOption(const string& key);

#ifndef NO_WEBSOCKET
/**
* Returns the WebSockets container
Expand Down
7 changes: 4 additions & 3 deletions mongoose/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Mongoose
mutex.unlock();
}

void Session::setValue(string key, string value)
void Session::setValue(string key, Session::TypeValue value)
{
mutex.lock();
values[key] = value;
Expand All @@ -38,11 +38,11 @@ namespace Mongoose
return values.find(key) != values.end();
}

string Session::get(string key, string fallback)
Session::TypeValue Session::get(string key, Session::TypeValue fallback)
{
mutex.lock();
if (hasValue(key)) {
string value = values[key];
TypeValue value = values[key];
mutex.unlock();

return value;
Expand All @@ -56,4 +56,5 @@ namespace Mongoose
{
return time(NULL)-date;
}

}
41 changes: 35 additions & 6 deletions mongoose/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@

#include <map>
#include "Mutex.h"
#ifdef ENABLE_SESSION_BOOST_ANY
# include <boost/any.hpp>
#endif

using namespace std;


/**
* A session contains the user specific values
*/
namespace Mongoose
{
class Session
{
#ifdef ENABLE_SESSION_BOOST_ANY
typedef boost::any TypeValue;
#else
typedef std::string TypeValue;
#endif
public:
Session();

/**
* Sets the value of a session variable
*
* @param string the name of the variable
* @param string the value of the variable
* @param Session::TypeValue the value of the variable
*/
void setValue(string key, string value);
void setValue(string key, Session::TypeValue value);

/**
* Unset a session varaible
Expand All @@ -42,11 +51,31 @@ namespace Mongoose
* Try to get the value for the given variable
*
* @pram string the name of the variable
* @param string the fallback value
* @param Session::TypeValue the fallback value
*
* @return string the value of the variable if it exists, fallback else
* @return Session::TypeValue the value of the variable if it exists, fallback else
*/
string get(string key, string fallback = "");
TypeValue get(string key, Session::TypeValue fallback = Session::TypeValue());

#ifdef ENABLE_SESSION_BOOST_ANY
/**
* Try to get the value for the given variable
*
* @pram string the name of the variable
* @param TypeValue the fallback value
*
* @return Type the value of the variable if it exists, fallback else
*/
template<class Type>
Type get(const string& key, Type fallback = Type())
{
TypeValue any = get(key);
if( ! any.empty() )
return boost::any_cast<Type>(any);
else
return fallback;
}
#endif

/**
* Pings the session, this will update the creation date to now
Expand All @@ -62,7 +91,7 @@ namespace Mongoose
int getAge();

protected:
map<string, string> values;
map<string, TypeValue> values;
int date;
Mutex mutex;
};
Expand Down
Loading