diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..bd43bce --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "mongoose"] + path = mongoose + url = git@github.com:cesanta/mongoose.git diff --git a/Makefile.am b/Makefile.am index ab2ca96..2e2b120 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,11 +3,13 @@ ## to provide a way for the user to supply additional arguments. ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} +AM_CXXFLAGS = -std=c++11 bin_PROGRAMS=siccc -siccc_SOURCES = src/options.c src/sicmain.cc +siccc_SOURCES = src/options.c src/sicmain.cc mongoose/mongoose.c siccc_CFLAGS = -siccc_LDADD = +sicc_CXXFLAGS = -ggdb -I ../mongoose/mongoose.c +siccc_LDFLAGS = -ldl -pthread options.c: options.ggo options.h: options.ggo diff --git a/mongoose b/mongoose new file mode 160000 index 0000000..03d8465 --- /dev/null +++ b/mongoose @@ -0,0 +1 @@ +Subproject commit 03d846529d4baf96675858849000219dae91c072 diff --git a/src/MultipartParser.h b/src/multipartdings/MultipartParser.h similarity index 100% rename from src/MultipartParser.h rename to src/multipartdings/MultipartParser.h diff --git a/src/MultipartReader.h b/src/multipartdings/MultipartReader.h similarity index 100% rename from src/MultipartReader.h rename to src/multipartdings/MultipartReader.h diff --git a/src/multipart.cc b/src/multipartdings/multipart.cc similarity index 100% rename from src/multipart.cc rename to src/multipartdings/multipart.cc diff --git a/src/multipartLICENSE b/src/multipartdings/multipartLICENSE similarity index 100% rename from src/multipartLICENSE rename to src/multipartdings/multipartLICENSE diff --git a/src/options.ggo b/src/options.ggo index fcf7321..44b0400 100644 --- a/src/options.ggo +++ b/src/options.ggo @@ -1,20 +1,23 @@ -package "" -version "" +package "siccc" +version "0" purpose "This is our port of the famous „sharingg is carin“ application by Astro of C3D2: Instant filesharing made simple for $HOME networks. The Port to C++ is meant as a C++ training and to allow SIC to be deployed to small and embedded plattforms" -args "-C -a sic_conf -f cmd_parser" +args "-C -a sic_conf -f cmd_parser --default-optional" -option "listen" l "Specifies the adress to bind to" default="::1" string required multiple -option "port" p "The portnumber listened to" default="12380" int +option "listen" l "Specifies the adress to bind to" string multiple default="::1" +option "port" p "The portnumber listened to" default="12380" int optional option "saveconf" s "Save the actual configuration to a file" flag off -option "conffile" f "The Configurationfile to use" string default="/etc/triggerd.conf" +option "conffile" c "The Configurationfile to use" string default="./siccc.conf" option "verbose" v "increas the verbosity" flag off option "daemonize" D "Detach from calling terminal" flag off +option "dns" d "also start an dns fakeserver" + +option "filedir" f "Directory for data storage. Musst be r/w able" string default="./files/" diff --git a/src/sicmain.cc b/src/sicmain.cc index d9144f5..0439cbe 100644 --- a/src/sicmain.cc +++ b/src/sicmain.cc @@ -1,16 +1,101 @@ #include #include +#include #include +#include "mongoose.h" + #include "options.h" +//globally accessible config static struct sic_conf args_info; using std::cout; +static int event_handler(struct mg_event *event) { + + 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 { + 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." + "
" + "
" + "" + "
"; + + 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); + } + + // Mark request as processed + return 1; + } + + // All other events left unprocessed + return 1; +} + + +class MongooseHandler{ + private: + struct mg_context *ctx; + //const char *options[]; + unsigned short listenport; + + // = {"listening_ports", "8080", NULL}; + + public: + + //default listenport:8080 + MongooseHandler(int listenport=8080): + ctx(nullptr), + listenport(listenport) + { + char portstring[8]; + std::snprintf(portstring,8,"%d",this->listenport); + const char *options[] = + {"listening_ports",portstring,nullptr}; + this->ctx= mg_start(options, &event_handler,nullptr); + } + + //no copy constructor: + MongooseHandler( const MongooseHandler &) = delete; + + //destructor stops the server + ~MongooseHandler(){ + if (this->ctx){ + mg_stop(this->ctx); + } + } + +}; + + int main(int argc, char **argv) { int result = 0; + MongooseHandler m; struct cmd_parser_params *params; @@ -52,14 +137,19 @@ int main(int argc, char **argv) args_info.listen_arg[i] <