dancing arround class stuff. virtual interface cleaned

This commit is contained in:
john stone 2014-01-25 17:47:37 +01:00
parent 48f43be792
commit 7b64cb5b76
9 changed files with 108 additions and 100 deletions

View File

@ -15,7 +15,9 @@ InputMemoryFile::InputMemoryFile(const char *pathname):
struct stat sbuf; struct stat sbuf;
if (::fstat(file_handle_, &sbuf) == -1) return; if (::fstat(file_handle_, &sbuf) == -1) return;
data_ = static_cast<const char*>(::mmap( data_ = static_cast<const char*>(::mmap(
0, sbuf.st_size, PROT_READ, MAP_SHARED, file_handle_, 0)); 0, sbuf.st_size,
PROT_READ, MAP_SHARED,
file_handle_, 0));
if (data_ == MAP_FAILED) data_ = 0; if (data_ == MAP_FAILED) data_ = 0;
else size_ = sbuf.st_size; else size_ = sbuf.st_size;
} }

View File

@ -8,5 +8,7 @@ class MediocreSimpleStaticFileHandler :public StaticFileHandler
public: public:
int answer_pathreq(const char * const path, struct mg_connection *conn); int answer_pathreq(const char * const path, struct mg_connection *conn);
std::string cleanpath(const char * const); std::string cleanpath(const char * const);
MediocreSimpleStaticFileHandler() {};
~MediocreSimpleStaticFileHandler() {};
}; };

View File

@ -1,11 +1,14 @@
#include "MediocreSimpleStaticFileHandler.H" #include "MediocreSimpleStaticFileHandler.H"
std::string MediocreSimpleStaticFileHandler::cleanpath (const char * const path){ std::string
MediocreSimpleStaticFileHandler::cleanpath (const char * const path)
{
//adds no security at all //adds no security at all
return std::string(path); return std::string(path);
} }
int MediocreSimpleStaticFileHandler::answer_pathreq(const char * const path, int
MediocreSimpleStaticFileHandler::answer_pathreq(const char * const path,
struct mg_connection *conn) struct mg_connection *conn)
{ {
mg_send_file(conn,cleanpath(path).c_str()); mg_send_file(conn,cleanpath(path).c_str());

View File

@ -38,7 +38,7 @@ public:
} }
private: private:
explicit Routerin(); explicit Routerin();
StaticFileHandler sfh; StaticFileHandler && sfh;
bool hasInstance = false; bool hasInstance = false;
int event_request_route ( struct mg_event *event); int event_request_route ( struct mg_event *event);

View File

@ -6,6 +6,7 @@ using std::strcmp;
#include "Foohash.H" #include "Foohash.H"
#include "Routerin.H" #include "Routerin.H"
#include "StaticFileHandler.H"
#include "UltraSimpleStaticFileHandler.H" #include "UltraSimpleStaticFileHandler.H"
using std::cout; using std::cout;
@ -26,56 +27,56 @@ Routerin::~Routerin()
} }
int Routerin::event_request_route(struct mg_event *event){ int Routerin::event_request_route(struct mg_event *event){
const RouterOp *blub=0; const RouterOp *blub=0;
Foohash fh; Foohash fh;
const char *reqstr=0; const char *reqstr=0;
char *tmpstr = 0,*pntrstr=0; char *tmpstr = 0,*pntrstr=0;
int retval=-1; int retval=-1;
reqstr=event->request_info->uri; reqstr=event->request_info->uri;
if (reqstr){ if (reqstr){
tmpstr = strdup(reqstr); tmpstr = strdup(reqstr);
std::cerr << "starting with str as " std::cerr << "starting with str as "
<< tmpstr << endl; //DEBUG CODE << tmpstr << endl; //DEBUG CODE
pntrstr = strchrnul((tmpstr+1),'/'); pntrstr = strchrnul((tmpstr+1),'/');
if (pntrstr) { if (pntrstr) {
*pntrstr = 0; *pntrstr = 0;
std::cerr <<" having reqstr as " << tmpstr std::cerr <<" having reqstr as " << tmpstr
<< " now. " << endl; << " now. " << endl;
std::cerr <<"len of reqstr is " << std::cerr <<"len of reqstr is " <<
( pntrstr - tmpstr) << endl; ( pntrstr - tmpstr) << endl;
blub = fh.in_word_set(tmpstr,((pntrstr++)-tmpstr) ); blub = fh.in_word_set(tmpstr,((pntrstr++)-tmpstr) );
if (blub){ if (blub){
std::cerr << "me has blub!!" <<endl; std::cerr << "me has blub!!" <<endl;
switch(blub->op){ switch(blub->op){
case RouterOpCode::FILES: case RouterOpCode::FILES:
retval = rq_file(pntrstr ,event); retval = rq_file(pntrstr ,event);
break; break;
case RouterOpCode::DATE: case RouterOpCode::DATE:
retval = rq_date(pntrstr, event); retval = rq_date(pntrstr, event);
break; break;
case RouterOpCode::UPLOAD: case RouterOpCode::UPLOAD:
retval = rq_upload(pntrstr, event); retval = rq_upload(pntrstr, event);
break; break;
case RouterOpCode::STATIC: case RouterOpCode::STATIC:
std::cerr << "(/static/ given)"; std::cerr << "(/static/ given)";
retval = rq_static((pntrstr),event); retval = rq_static((pntrstr),event);
break; break;
default: default:
std::cerr << "fallback "; std::cerr << "fallback ";
retval = rq_static((pntrstr),event); retval = rq_static((pntrstr),event);
}; };
}else{ }else{
std::cerr << "no blub! in " << __LINE__ << endl; std::cerr << "no blub! in " << __LINE__ << endl;
} }
} }
std::free(tmpstr); std::free(tmpstr);
tmpstr=0; tmpstr=0;
return retval; return retval;
} else { } else {
std::cerr << "event request without uri? ?? hu? [" << std::cerr << "event request without uri? ?? hu? [" <<
event->request_info->uri << "]" << endl; event->request_info->uri << "]" << endl;
} }
return 0; return 0;
} }
@ -90,32 +91,34 @@ int Routerin::rq_static(char const * const reststr , struct mg_event *event)
int Routerin::rq_file(char const * const reststr , struct mg_event *event) int Routerin::rq_file(char const * const reststr ,
struct mg_event *event __attribute__((unused)))
{ {
std::cerr << " a dynamic file is reqested!" << endl; std::cerr << " a dynamic file is reqested!" << endl;
std::cerr << " path: " << reststr << endl; std::cerr << " path: " << reststr << endl;
return 1; return 1;
} }
int Routerin::rq_date(char const * const reststr , struct mg_event *event) int Routerin::rq_date(char const * const reststr __attribute__((unused)),
struct mg_event *event __attribute__((unused)) )
{ {
std::cerr << "requested filelist ordered by date!" << endl ; std::cerr << "requested filelist ordered by date!" << endl ;
return 1; return 1;
} }
int Routerin::rq_upload(char const * const reststr, struct mg_event *event) int Routerin::rq_upload(char const * const reststr __attribute__((unused)),
struct mg_event *event __attribute__((unused)))
{ {
std::cerr << "here we will get uploadish!" << endl ; std::cerr << "here we will get uploadish!" << endl ;
return 1; return 1;
} }
int Routerin::event_requend_route(struct mg_event *event){ int Routerin::event_requend_route(struct mg_event *event __attribute__((unused)) ){
std::cerr << "ended request" <<endl; std::cerr << "ended request" <<endl;
return 1; return 1;
} }
int Routerin::event_fallback_route(struct mg_event *event){ int Routerin::event_fallback_route(struct mg_event *event __attribute__((unused)) ){
//DEBUG CODE //DEBUG CODE
std::cerr << "Got an unhandled mg_event" << endl; std::cerr << "Got an unhandled mg_event" << endl;
std::cerr << "Its type is: " << event->type << endl; std::cerr << "Its type is: " << event->type << endl;
@ -123,7 +126,7 @@ int Routerin::event_fallback_route(struct mg_event *event){
} }
int Routerin::event_newthread_route(struct mg_event *event){ int Routerin::event_newthread_route(struct mg_event *event __attribute__((unused)) ){
//DEBUG CODE //DEBUG CODE
std::cerr << "Started new thread!" <<endl; std::cerr << "Started new thread!" <<endl;
return 1; return 1;

View File

@ -4,7 +4,6 @@
class StaticFileHandler class StaticFileHandler
{ {
public: public:
virtual ~StaticFileHandler() {}; virtual int answer_pathreq(const char * const path,struct mg_connection *conn)=0;
virtual int answer_pathreq(const char * const path,struct mg_connection *conn) {};
}; };

View File

@ -4,8 +4,8 @@
class UltraSimpleStaticFileHandler :public StaticFileHandler class UltraSimpleStaticFileHandler :public StaticFileHandler
{ {
public: public:
~UltraSimpleStaticFileHandler(); virtual ~UltraSimpleStaticFileHandler();
int answer_pathreq(const char * const path, struct mg_connection *conn); virtual int answer_pathreq(const char * const path, struct mg_connection *conn) override;
}; };

View File

@ -22,14 +22,14 @@ static const char * hello_world_html = u8R"HERE(
</body> </body>
</html>)HERE"; </html>)HERE";
UltraSimpleStaticFileHandler::~UltraSimpleStaticFileHandler() {} UltraSimpleStaticFileHandler::~UltraSimpleStaticFileHandler(){};
int
UltraSimpleStaticFileHandler::answer_pathreq(const char * const path, int UltraSimpleStaticFileHandler::answer_pathreq(const char * const path,
struct mg_connection *conn) struct mg_connection *conn)
{ {
mg_printf(conn, "HTTP/1.0 200 OK\r\n" mg_printf(conn, "HTTP/1.0 200 OK\r\n"
"Content-Length: %d\r\n" "Content-Length: %lu\r\n"
"Content-Type: text/html\r\n\r\n%s", "Content-Type: text/html\r\n\r\n%s",
sizeof(hello_world_html), sizeof(hello_world_html),
hello_world_html); hello_world_html);

View File

@ -15,10 +15,10 @@ using std::cout;
class MongooseHandler{ class MongooseHandler{
private: private:
struct mg_context *ctx; struct mg_context *ctx;
//const char *options[]; //const char *options[];
unsigned short listenport; unsigned short listenport;
// = {"listening_ports", "8080", NULL}; // = {"listening_ports", "8080", NULL};
public: public:
@ -26,14 +26,14 @@ class MongooseHandler{
MongooseHandler(int listenport=8080): MongooseHandler(int listenport=8080):
ctx(nullptr), ctx(nullptr),
listenport(listenport) listenport(listenport)
{ {
char portstring[8]; char portstring[8];
Routerin *routerin = Routerin::get_instance(); Routerin *routerin = Routerin::get_instance();
std::snprintf(portstring,8,"%d",this->listenport); std::snprintf(portstring,8,"%d",this->listenport);
const char *options[] = const char *options[] =
{"listening_ports",portstring,nullptr}; {"listening_ports",portstring,nullptr};
this->ctx= mg_start(options, routerin->event_route, nullptr); this->ctx= mg_start(options, routerin->event_route, nullptr);
} }
//no copy constructor: //no copy constructor:
MongooseHandler( const MongooseHandler &) = delete; MongooseHandler( const MongooseHandler &) = delete;
@ -81,44 +81,43 @@ bool configfile_parsing_action(int& argc, char **argv)
//initialize the parameters structure //initialize the parameters structure
params = cmd_parser_params_create(); params = cmd_parser_params_create();
params->check_required = 0; params->check_required = 0;
if (cmd_parser_config_file("./siccc.conf", &args_info, params) != 0) { if (cmd_parser_config_file("./siccc.conf", &args_info, params) != 0) {
cmd_parser_free(&args_info); cmd_parser_free(&args_info);
free(params); free(params);
return false; return false;
} }
params->initialize = 0; params->initialize = 0;
params->override = 1; params->override = 1;
params->check_required = 1; params->check_required = 1;
//call the command line parser //call the command line parser
if (cmd_parser(argc, argv, &args_info) != 0) { if (cmd_parser(argc, argv, &args_info) != 0) {
cmd_parser_free(&args_info); cmd_parser_free(&args_info);
free(params); free(params);
return false; return false;
} }
return true; return true;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if ( !configfile_parsing_action(argc, argv) ) { if ( !configfile_parsing_action(argc, argv) ) {
std::cerr << "ERROR ERROR BEEP" << std::endl; std::cerr << "ERROR ERROR BEEP" << std::endl;
exit(1); exit(1);
} }
#ifdef DEBUG #ifdef DEBUG
dump_args(); dump_args();
#endif #endif
MongooseHandler m(args_info.port_arg); MongooseHandler m(args_info.port_arg);
if (!args_info.daemonize_flag) { if (!args_info.daemonize_flag) {
while(1) while(1)
getchar(); getchar();
} }
return 0; return 0;
} }