neue mongoose api ... :(

könnte man noch einiges verbessern. z.B. die statischen files in
mongoose direkt serven lassen
master
john stone 9 years ago
parent e5af37f305
commit 8df97cdb3a
  1. 2
      mongoose
  2. 2
      src/DynamicObjects.cc
  3. 59
      src/Routerin.H
  4. 91
      src/Routerin.cc
  5. 32
      src/StaticFileHandler.cc
  6. 44
      src/sicmain.cc

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

@ -10,7 +10,7 @@ std::string DynamicObjects::render_object_list()
for (auto& it : this->m_thevec){
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 += u8"</a> <span class=\"size\">";
output += it.get_pretty_sized();

@ -9,6 +9,8 @@
class DynamicObjects;
int event_route_wrap(struct mg_connection *conn, mg_event ev);
class Routerin
{
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;
Routerin *rouri = Routerin::get_instance();
switch (event->type) {
case MG_REQUEST_BEGIN:
return rouri->event_request_route(event);
case MG_THREAD_BEGIN:
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:
switch (event) {
case MG_REQUEST:
return event_request_route(conn);
case MG_AUTH:
return MG_TRUE;
default:
return rouri->event_fallback_route(event);
return event_fallback_route(event, conn);
}
}
std::string const & get_baseurl()const{return baseurl;}
void set_baseurl(std::string baseurl);
@ -66,17 +75,21 @@ private:
std::string file_path;
std::string json_path;
bool hasInstance = false;
// bool hasInstance = false;
int event_request_route ( struct mg_event *event);
int event_newthread_route ( struct mg_event *event);
int event_fallback_route ( struct mg_event *event);
int event_requend_route ( struct mg_event *event);
int event_request_route ( struct mg_connection *conn);
// int event_newthread_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 rq_static(char const * const restrstr , struct mg_event *event);
int rq_file(char const * const reststr , struct mg_event *event);
int rq_date(char const * const reststr , struct mg_event *event);
int rq_upload(char const * const reststr , struct mg_event *event);
int rq_render_mainpage(struct mg_event *event);
int rq_static(char const * const restrstr , struct mg_connection *conn);
int rq_file(char const * const reststr , struct mg_connection *conn);
int rq_date(char const * const reststr , struct mg_connection *conn);
int rq_upload(char const * const reststr , struct mg_connection *conn);
int rq_render_mainpage(struct mg_connection *conn);
int handle_upload(struct mg_connection *conn);
};

@ -70,13 +70,21 @@ void Routerin::start()
_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;
Foohash fh;
const char *reqstr=0;
char *tmpstr = 0,*pntrstr=0;
int retval=-1;
reqstr=event->request_info->uri;
reqstr=conn->uri;
if (reqstr){
tmpstr = strdup(reqstr);
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;
switch(blub->op){
case RouterOpCode::FILES:
retval = rq_file(pntrstr ,event);
retval = rq_file(pntrstr ,conn);
break;
case RouterOpCode::DATE:
retval = rq_date(pntrstr, event);
retval = rq_date(pntrstr, conn);
break;
case RouterOpCode::UPLOAD:
retval = rq_upload(pntrstr, event);
retval = rq_upload(pntrstr, conn);
break;
case RouterOpCode::STATIC:
std::cerr << "(/static/ given)";
retval = rq_static((pntrstr),event);
retval = rq_static((pntrstr),conn);
break;
default:
std::cerr << "fallback ";
retval = rq_static("illegalurl.htm",event);
retval = rq_static("illegalurl.htm",conn);
};
}else{
std::cerr << "no blub! in " << __LINE__ << endl;
//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;
} else {
std::cerr << "event request without uri? ?? hu? [" <<
event->request_info->uri << "]" << endl;
conn->uri << "]" << endl;
}
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;
const std::string fubb =
@ -155,7 +163,7 @@ int Routerin::rq_render_mainpage(struct mg_event * event)
_dyn_obs->render_object_list()+
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-Type: text/html\r\n\r\n%s",
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 << " path: " << reststr << endl;
sfh->answer_pathreq(reststr,event->conn);
return 1;
return sfh->answer_pathreq(reststr,conn);
}
int Routerin::rq_file(char const * const reststr ,
struct mg_event *event __attribute__((unused)))
int Routerin::rq_file(char const * const reststr,
struct mg_connection *conn __attribute__((unused)))
{
std::cerr << " a dynamic file is reqested!" << endl;
std::cerr << " path: " << reststr << endl;
return 1;
return sfh->answer_pathreq(reststr,conn);
}
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 ;
return 1;
}
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 ;
return 1;
}
std::cerr << "here we will get uploadish!" << endl;
std::cerr << "handle: " << handle_upload( conn ) << "fup!" << endl;
int Routerin::event_requend_route(struct mg_event *event __attribute__((unused)) ){
std::cerr << "ended request" <<endl;
// get Dynamic Object form Upload
// Persist
// render thankyou page
return 1;
}
int Routerin::event_fallback_route(struct mg_event *event __attribute__((unused)) ){
/*int Routerin::event_requend_route(struct mg_connection *conn __attribute__((unused)) ){*/
//std::cerr << "ended request" <<endl;
//return 1;
/*}*/
int Routerin::event_fallback_route(enum mg_event event, struct mg_connection *conn __attribute__((unused)) ){
//DEBUG CODE
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;
}
int Routerin::event_newthread_route(struct mg_event *event __attribute__((unused)) ){
//DEBUG CODE
std::cerr << "Started new thread!" <<endl;
return 1;
//int Routerin::event_newthread_route(struct mg_connection *conn __attribute__((unused)) ){
////DEBUG CODE
//std::cerr << "Started new thread!" <<endl;
//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) {

@ -1,17 +1,33 @@
#include "StaticFileHandler.H"
std::string
#include <iostream>
std::string
StaticFileHandler::_cleanpath (const char * const path)
{
//adds no security at all
return "static/"+std::string(path);
//adds no security at all
return "static/"+std::string(path);
}
int
int
StaticFileHandler::answer_pathreq(const char * const path,
struct mg_connection *conn)
struct mg_connection *conn)
{
mg_send_file(conn,_cleanpath(path).c_str());
return 0;
FILE *fp ;
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;
}

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

Loading…
Cancel
Save