From 959c8b134c903a6d121c455d96da196aa9999019 Mon Sep 17 00:00:00 2001 From: john stone Date: Sat, 18 Jan 2014 14:48:52 +0100 Subject: [PATCH] =?UTF-8?q?klassenhierarchie=20aufr=C3=A4umung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MediocreSimpleStaticFileHandler.H | 0 src/MediocreSimpleStaticFileHandler.cc | 20 ++ src/Routerin.H | 4 +- src/Routerin.cc | 199 +++++++++--------- src/StaticFileHandler.H | 4 - src/UltraSimpleStaticFileHandler.H | 10 + ...ler.cc => UltraSimpleStaticFileHandler.cc} | 23 +- 7 files changed, 132 insertions(+), 128 deletions(-) create mode 100644 src/MediocreSimpleStaticFileHandler.H create mode 100644 src/MediocreSimpleStaticFileHandler.cc create mode 100644 src/UltraSimpleStaticFileHandler.H rename src/{StaticFileHandler.cc => UltraSimpleStaticFileHandler.cc} (66%) diff --git a/src/MediocreSimpleStaticFileHandler.H b/src/MediocreSimpleStaticFileHandler.H new file mode 100644 index 0000000..e69de29 diff --git a/src/MediocreSimpleStaticFileHandler.cc b/src/MediocreSimpleStaticFileHandler.cc new file mode 100644 index 0000000..52a7f3a --- /dev/null +++ b/src/MediocreSimpleStaticFileHandler.cc @@ -0,0 +1,20 @@ +#include "MediocreSimpleStaticFileHandler.H" +//#include "InputMemmoryFile.H" +class MediocreSimpleStaticFileHandler :StaticFileHandler +{ + private: + + std::string cleanpath (const char * const path){ + //adds no security at all + return std::string(path); + } + + public: + int answer_pathreq(const char * const path, + struct mg_connection *conn) + { + mg_send_file(conn,cleanpath(path).to_cstring()); + return 0; + } +} + diff --git a/src/Routerin.H b/src/Routerin.H index c942bf0..4253b89 100644 --- a/src/Routerin.H +++ b/src/Routerin.H @@ -6,8 +6,8 @@ class Routerin { public: - Routerin()=delete; - Routerin(StaticFileHandler &sfh); + Routerin(); + //Routerin(StaticFileHandler &sfh); int event_route(struct mg_event *event); diff --git a/src/Routerin.cc b/src/Routerin.cc index a6284d5..b2f8079 100644 --- a/src/Routerin.cc +++ b/src/Routerin.cc @@ -9,33 +9,32 @@ using std::strcmp; using std::cout; using std::endl; -class Routerin { - Routerin(): - sfh(new UltraSimpleStaticFileHandler()) - { - cout << "Konstr Routering" << std::endl; - } +Routerin::Routerin(): + sfh(new UltraSimpleStaticFileHandler()) +{ + cout << "Konstr Routering" << std::endl; +} - int event_route(struct mg_event *event) - { +int Routerin::event_route(struct mg_event *event) +{ // Foohash fh; - switch(event->type){ - case MG_REQUEST_BEGIN: - return event_request_route(event); - case MG_THREAD_BEGIN : - return event_newthread_route(event); - case MG_REQUEST_END: - return event_requend_route(event); - case MG_HTTP_ERROR: - case MG_EVENT_LOG : - case MG_THREAD_END: - default: - return event_fallback_route(event); - } + switch(event->type){ + case MG_REQUEST_BEGIN: + return event_request_route(event); + case MG_THREAD_BEGIN : + return event_newthread_route(event); + case MG_REQUEST_END: + return event_requend_route(event); + case MG_HTTP_ERROR: + case MG_EVENT_LOG : + case MG_THREAD_END: + default: + return event_fallback_route(event); } +} - int event_request_route(struct mg_event *event){ + int Routerin::event_request_route(struct mg_event *event){ const RouterOp *blub=0; Foohash fh; const char *reqstr=0; @@ -90,112 +89,108 @@ class Routerin { - int rq_static(char const * const reststr , struct mg_event *event) - { - std::cerr << " a static file is reqested!" << endl; - std::cerr << " path: " << reststr << endl; - - - return 1; - } +int Routerin::rq_static(char const * const reststr , struct mg_event *event) +{ + std::cerr << " a static file is reqested!" << endl; + std::cerr << " path: " << reststr << endl; + sfh->answer_pathreq(reststr,event->conn); + return 1; +} - int rq_file(char const * const reststr , struct mg_event *event) - { - std::cerr << " a dynamic file is reqested!" << endl; - std::cerr << " path: " << reststr << endl; +int Routerin::rq_file(char const * const reststr , struct mg_event *event) +{ + std::cerr << " a dynamic file is reqested!" << endl; + std::cerr << " path: " << reststr << endl; - return 1; - } + return 1; +} - int rq_date(char const * const reststr , struct mg_event *event) - { - std::cerr << "requested filelist ordered by date!" << endl ; - return 1; - } +int Routerin::rq_date(char const * const reststr , struct mg_event *event) +{ + std::cerr << "requested filelist ordered by date!" << endl ; + return 1; +} - int rq_upload(char const * const reststr, struct mg_event *event) - { - std::cerr << "here we will get uploadish!" << endl ; - return 1; - } +int Routerin::rq_upload(char const * const reststr, struct mg_event *event) +{ + std::cerr << "here we will get uploadish!" << endl ; + return 1; +} - int event_requend_route(struct mg_event *event){ - std::cerr << "ended request" <type << endl; - return 1; - } - - - int event_newthread_route(struct mg_event *event){ +int Routerin::event_fallback_route(struct mg_event *event){ //DEBUG CODE - std::cerr << "Started new thread!" <type << endl; + return 1; +} + + +int Routerin::event_newthread_route(struct mg_event *event){ + //DEBUG CODE + std::cerr << "Started new thread!" <type == MG_REQUEST_BEGIN) { - if (!strcmp( - event->request_info->uri, - "/handle_post_request")) { - char path[200]; - FILE *fp = mg_upload(event->conn, - "/tmp", - path, - sizeof(path)); - if (fp != NULL) { - fclose(fp); - mg_printf(event->conn, - "HTTP/1.0 200 OK\r\n\r\nSaved: [%s]", - path); - } else { - mg_printf(event->conn,"%s", - "HTTP/1.0 200 OK\r\n\r\nNo files sent"); - } + if (event->type == MG_REQUEST_BEGIN) { + if (!strcmp( + event->request_info->uri, + "/handle_post_request")) { + char path[200]; + FILE *fp = mg_upload(event->conn, + "/tmp", + path, + sizeof(path)); + if (fp != NULL) { + fclose(fp); + mg_printf(event->conn, + "HTTP/1.0 200 OK\r\n\r\nSaved: [%s]", + path); } else { - // Show HTML form. Make sure it has enctype="multipart/form-data" attr. - static const char *html_form = - "Upload example." - "
" - "
" - "" - "
"; - - mg_printf(event->conn, "HTTP/1.0 200 OK\r\n" - "Content-Length: %d\r\n" - "Content-Type: text/html\r\n\r\n%s", - (int) strlen(html_form), html_form); + mg_printf(event->conn,"%s", + "HTTP/1.0 200 OK\r\n\r\nNo files sent"); } + } else { + // Show HTML form. Make sure it has enctype="multipart/form-data" attr. + static const char *html_form = + "Upload example." + "
" + "
" + "" + "
"; - // Mark request as processed - return 1; + mg_printf(event->conn, "HTTP/1.0 200 OK\r\n" + "Content-Length: %d\r\n" + "Content-Type: text/html\r\n\r\n%s", + (int) strlen(html_form), html_form); } - // All other events left unprocessed + // Mark request as processed return 1; } - */ + // All other events left unprocessed + return 1; +} +*/ -}; //end of class Routerin - //REST API Documentation ///files/ (index in vector) diff --git a/src/StaticFileHandler.H b/src/StaticFileHandler.H index 4bdaea3..7b2c9ee 100644 --- a/src/StaticFileHandler.H +++ b/src/StaticFileHandler.H @@ -8,7 +8,3 @@ class StaticFileHandler virtual int answer_pathreq(const char * const path,struct mg_connection *conn); }; - -class UltraSimpleStaticFileHandler; - - diff --git a/src/UltraSimpleStaticFileHandler.H b/src/UltraSimpleStaticFileHandler.H new file mode 100644 index 0000000..fda0870 --- /dev/null +++ b/src/UltraSimpleStaticFileHandler.H @@ -0,0 +1,10 @@ + +#include "SimpleStaticFileHandler.h" + +class UltraSimpleStaticFileHandler : StaticFileHandler +{ + public: + int answer_pathreq(const char * const path, struct mg_connection *conn); +}; + + diff --git a/src/StaticFileHandler.cc b/src/UltraSimpleStaticFileHandler.cc similarity index 66% rename from src/StaticFileHandler.cc rename to src/UltraSimpleStaticFileHandler.cc index b604272..90ddb20 100644 --- a/src/StaticFileHandler.cc +++ b/src/UltraSimpleStaticFileHandler.cc @@ -1,7 +1,7 @@ -#include "StaticFileHandler.H" -//#include "InputMemmoryFile.H" - +#include +#include "UltraSimpleStaticFileHandler.H" +#include static const char * hello_world_html = u8R"HERE( @@ -38,21 +38,4 @@ class UltraSimpleStaticFileHandler : StaticFileHandler }; -class MediocreSimpleStaticFileHandler :StaticFileHandler -{ - private: - std::string cleanpath (const char * const path){ - //adds no security at all - return std::string(path); - } - - public: - int answer_pathreq(const char * const path, - struct mg_connection *conn) - { - mg_send_file(conn,cleanpath(path).to_cstring()); - return 0; - } -} -