neue mongoose api ... :(

könnte man noch einiges verbessern. z.B. die statischen files in
mongoose direkt serven lassen
This commit is contained in:
john stone 2014-05-05 00:45:39 +02:00
parent e5af37f305
commit 8df97cdb3a
6 changed files with 151 additions and 81 deletions

@ -1 +1 @@
Subproject commit 03d846529d4baf96675858849000219dae91c072 Subproject commit 63490249ba5a2301f9c710d22ee819c0de060e66

View File

@ -10,7 +10,7 @@ std::string DynamicObjects::render_object_list()
for (auto& it : this->m_thevec){ for (auto& it : this->m_thevec){
output += u8"<li>\n"; output += u8"<li>\n";
output += u8"<a href=\"" + m_merouterin->get_baseurl() + it.get_filename() + u8"\">"; //we could include baseurl here? output += u8"<a href=\"" + m_merouterin->get_baseurl() + u8"files/"+ it.get_filename() + u8"\">"; //we could include baseurl here?
output += it.get_filename(); output += it.get_filename();
output += u8"</a> <span class=\"size\">"; output += u8"</a> <span class=\"size\">";
output += it.get_pretty_sized(); output += it.get_pretty_sized();

View File

@ -9,6 +9,8 @@
class DynamicObjects; class DynamicObjects;
int event_route_wrap(struct mg_connection *conn, mg_event ev);
class Routerin class Routerin
{ {
public: public:
@ -29,25 +31,32 @@ public:
/*enum mg_result { MG_FALSE, MG_TRUE, MG_MORE };*/
//enum mg_event {
//MG_POLL = 100, // Callback return value is ignored
//MG_CONNECT, // If callback returns MG_FALSE, connect fails
//MG_AUTH, // If callback returns MG_FALSE, authentication fails
//MG_REQUEST, // If callback returns MG_FALSE, Mongoose continues with req
//MG_REPLY, // If callback returns MG_FALSE, Mongoose closes connection
//MG_CLOSE, // Connection is closed, callback return value is ignored
//MG_WS_HANDSHAKE, // New websocket connection, handshake request
//MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err
//};
static int event_route(struct mg_event *event)
int event_route(struct mg_connection *conn, enum mg_event event)
{ {
// Foohash fh; // Foohash fh;
Routerin *rouri = Routerin::get_instance(); switch (event) {
switch (event->type) { case MG_REQUEST:
case MG_REQUEST_BEGIN: return event_request_route(conn);
return rouri->event_request_route(event); case MG_AUTH:
case MG_THREAD_BEGIN: return MG_TRUE;
return rouri->event_newthread_route(event);
case MG_REQUEST_END:
return rouri->event_requend_route(event);
case MG_HTTP_ERROR:
case MG_EVENT_LOG:
case MG_THREAD_END:
default: default:
return rouri->event_fallback_route(event); return event_fallback_route(event, conn);
} }
} }
std::string const & get_baseurl()const{return baseurl;} std::string const & get_baseurl()const{return baseurl;}
void set_baseurl(std::string baseurl); void set_baseurl(std::string baseurl);
@ -66,17 +75,21 @@ private:
std::string file_path; std::string file_path;
std::string json_path; std::string json_path;
bool hasInstance = false; // bool hasInstance = false;
int event_request_route ( struct mg_event *event); int event_request_route ( struct mg_connection *conn);
int event_newthread_route ( struct mg_event *event); // int event_newthread_route ( struct mg_event *event);
int event_fallback_route ( struct mg_event *event); int event_fallback_route ( enum mg_event event, struct mg_connection *conn );
int event_requend_route ( struct mg_event *event); // int event_requend_route ( struct mg_event *event);
int rq_static(char const * const restrstr , struct mg_event *event); int rq_static(char const * const restrstr , struct mg_connection *conn);
int rq_file(char const * const reststr , struct mg_event *event); int rq_file(char const * const reststr , struct mg_connection *conn);
int rq_date(char const * const reststr , struct mg_event *event); int rq_date(char const * const reststr , struct mg_connection *conn);
int rq_upload(char const * const reststr , struct mg_event *event); int rq_upload(char const * const reststr , struct mg_connection *conn);
int rq_render_mainpage(struct mg_event *event); int rq_render_mainpage(struct mg_connection *conn);
int handle_upload(struct mg_connection *conn);
}; };

View File

@ -70,13 +70,21 @@ void Routerin::start()
_dyn_obs = new DynamicObjects(this); _dyn_obs = new DynamicObjects(this);
} }
int Routerin::event_request_route(struct mg_event *event){
int event_route_wrap(struct mg_connection *conn, mg_event ev)
{
Routerin *rouri = Routerin::get_instance();
return rouri->event_route(conn,ev);
}
int Routerin::event_request_route(struct mg_connection *conn){
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=conn->uri;
if (reqstr){ if (reqstr){
tmpstr = strdup(reqstr); tmpstr = strdup(reqstr);
std::cerr << "starting with str as " std::cerr << "starting with str as "
@ -93,26 +101,26 @@ int Routerin::event_request_route(struct mg_event *event){
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 ,conn);
break; break;
case RouterOpCode::DATE: case RouterOpCode::DATE:
retval = rq_date(pntrstr, event); retval = rq_date(pntrstr, conn);
break; break;
case RouterOpCode::UPLOAD: case RouterOpCode::UPLOAD:
retval = rq_upload(pntrstr, event); retval = rq_upload(pntrstr, conn);
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),conn);
break; break;
default: default:
std::cerr << "fallback "; std::cerr << "fallback ";
retval = rq_static("illegalurl.htm",event); retval = rq_static("illegalurl.htm",conn);
}; };
}else{ }else{
std::cerr << "no blub! in " << __LINE__ << endl; std::cerr << "no blub! in " << __LINE__ << endl;
//retval = rq_static("illegalurl.htm",event); //retval = rq_static("illegalurl.htm",event);
retval = rq_render_mainpage(event); retval = rq_render_mainpage(conn);
} }
} }
@ -121,7 +129,7 @@ int Routerin::event_request_route(struct mg_event *event){
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; conn->uri << "]" << endl;
} }
return 0; return 0;
} }
@ -146,7 +154,7 @@ void Routerin::set_json_path(std::string json_path)
int Routerin::rq_render_mainpage(struct mg_event * event) int Routerin::rq_render_mainpage(struct mg_connection *conn)
{ {
std::cerr << "rendering main page!" << endl; std::cerr << "rendering main page!" << endl;
const std::string fubb = const std::string fubb =
@ -155,7 +163,7 @@ int Routerin::rq_render_mainpage(struct mg_event * event)
_dyn_obs->render_object_list()+ _dyn_obs->render_object_list()+
std_footer.c_str() ; std_footer.c_str() ;
mg_printf(event->conn, "HTTP/1.0 200 OK\r\n" mg_printf(conn, "HTTP/1.0 200 OK\r\n"
"Content-Length: %lu\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",
fubb.size(), fubb.size(),
@ -166,61 +174,78 @@ int Routerin::rq_render_mainpage(struct mg_event * event)
int Routerin::rq_static(char const * const reststr , struct mg_event *event) int Routerin::rq_static(char const * const reststr , struct mg_connection *conn)
{ {
std::cerr << " a static file is reqested!" << endl; std::cerr << " a static file is reqested!" << endl;
std::cerr << " path: " << reststr << endl; std::cerr << " path: " << reststr << endl;
sfh->answer_pathreq(reststr,event->conn); return sfh->answer_pathreq(reststr,conn);
return 1;
} }
int Routerin::rq_file(char const * const reststr , int Routerin::rq_file(char const * const reststr,
struct mg_event *event __attribute__((unused))) struct mg_connection *conn __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 sfh->answer_pathreq(reststr,conn);
} }
int Routerin::rq_date(char const * const reststr __attribute__((unused)), int Routerin::rq_date(char const * const reststr __attribute__((unused)),
struct mg_event *event __attribute__((unused)) ) struct mg_connection *conn __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 __attribute__((unused)), int Routerin::rq_upload(char const * const reststr __attribute__((unused)),
struct mg_event *event __attribute__((unused))) struct mg_connection *conn __attribute__((unused)))
{ {
std::cerr << "here we will get uploadish!" << endl ; std::cerr << "here we will get uploadish!" << endl;
std::cerr << "handle: " << handle_upload( conn ) << "fup!" << endl;
// get Dynamic Object form Upload
// Persist
// render thankyou page
return 1; return 1;
} }
int Routerin::event_requend_route(struct mg_event *event __attribute__((unused)) ){ /*int Routerin::event_requend_route(struct mg_connection *conn __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 __attribute__((unused)) ){ int Routerin::event_fallback_route(enum mg_event event, struct mg_connection *conn __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 << endl;
return 1; return 1;
} }
int Routerin::event_newthread_route(struct mg_event *event __attribute__((unused)) ){ //int Routerin::event_newthread_route(struct mg_connection *conn __attribute__((unused)) ){
//DEBUG CODE ////DEBUG CODE
std::cerr << "Started new thread!" <<endl; //std::cerr << "Started new thread!" <<endl;
return 1; //return 1;
//}
int Routerin::handle_upload(struct mg_connection *conn){
const char *data;
int data_len;
int ret;
char var_name[100], file_name[100];
ret = mg_parse_multipart(conn->content, conn->content_len,
var_name, sizeof(var_name),
file_name, sizeof(file_name),
&data, &data_len);
if (ret > 0) {
std::cerr << " got " << file_name << "with len of " << data_len << endl;
}
return ret;
} }
/* /*
* *
static int event_handler(struct mg_event *event) { static int event_handler(struct mg_event *event) {

View File

@ -1,17 +1,33 @@
#include "StaticFileHandler.H" #include "StaticFileHandler.H"
std::string #include <iostream>
std::string
StaticFileHandler::_cleanpath (const char * const path) StaticFileHandler::_cleanpath (const char * const path)
{ {
//adds no security at all //adds no security at all
return "static/"+std::string(path); return "static/"+std::string(path);
} }
int int
StaticFileHandler::answer_pathreq(const char * const path, StaticFileHandler::answer_pathreq(const char * const path,
struct mg_connection *conn) struct mg_connection *conn)
{ {
mg_send_file(conn,_cleanpath(path).c_str()); FILE *fp ;
return 0; char buf[1024];
size_t n = 0;
fp = fopen(_cleanpath(path).c_str(), "r");
if (fp != NULL) {
std::cerr << "outfile" << std::endl;
while ((n =fread(buf,1,sizeof(buf),fp)) > 0){
mg_send_data(conn,buf,n);
}
fclose(fp);
return MG_TRUE;
}
return MG_FALSE;
} }

View File

@ -1,6 +1,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <cassert>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
@ -20,30 +21,40 @@ class MongooseHandler{
//const char *options[]; //const char *options[];
unsigned short listenport; unsigned short listenport;
// = {"listening_ports", "8080", NULL}; // = {"listening_ports", "8080", NULL};
//
struct mg_server * srv;
public: public:
//default listenport:8080 //default listenport:8080
MongooseHandler(int listenport=8080): MongooseHandler(int listenport=8080):
ctx(nullptr), listenport(listenport),
listenport(listenport) srv(nullptr)
{ {
char portstring[8]; char portstring[8];
mg_handler_t mh = & event_route_wrap;
srv = mg_create_server(NULL, mh);
std::snprintf(portstring,8,"%d",this->listenport); std::snprintf(portstring,8,"%d",this->listenport);
const char *options[] = mg_set_option(srv,"listening_port",portstring);
{"listening_ports",portstring,nullptr}; //this->ctx= mg_start(options, Routerin::event_route, nullptr);
this->ctx= mg_start(options, Routerin::event_route, nullptr);
}
}
void serveloop(const int milisectimeout){
assert(srv);
mg_poll_server(srv,milisectimeout);
return;
}
//no copy constructor: //no copy constructor:
MongooseHandler( const MongooseHandler &) = delete; MongooseHandler( const MongooseHandler &) = delete;
//destructor stops the server //destructor stops the server
~MongooseHandler(){ ~MongooseHandler(){
if (this->ctx){ if (this->srv){
mg_stop(this->ctx); mg_destroy_server(&(this->srv));
} }
} }
@ -105,10 +116,13 @@ bool configfile_parsing_action(int& argc, char **argv)
return true; return true;
} }
bool killme;
static void signalhandler(int signum){ static void signalhandler(int signum){
switch(signum){ switch(signum){
case SIGTERM:
case SIGINT: case SIGINT:
exit(0); killme=true;
break; break;
default: default:
std::cout << "called sighandler with signal " << signum << " FUPP! " << std::endl; std::cout << "called sighandler with signal " << signum << " FUPP! " << std::endl;
@ -119,9 +133,10 @@ static void exithandlerfunc(){
std::cout << "alles hat ein ende" << std::endl; std::cout << "alles hat ein ende" << std::endl;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
killme = false;
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);
@ -136,6 +151,7 @@ int main(int argc, char **argv)
struct sigaction sa; struct sigaction sa;
sa.sa_handler = signalhandler; sa.sa_handler = signalhandler;
sigaction(SIGINT, &sa ,nullptr); sigaction(SIGINT, &sa ,nullptr);
sigaction(SIGTERM, &sa ,nullptr);
Routerin * merouterin = Routerin::get_instance(); Routerin * merouterin = Routerin::get_instance();
@ -156,8 +172,8 @@ int main(int argc, char **argv)
if (!args_info.daemonize_flag) { if (!args_info.daemonize_flag) {
while(1) while((not killme))
getchar(); m->serveloop(1000);
} }
delete m; delete m;