161 lines
3.6 KiB
C++
161 lines
3.6 KiB
C++
#include <cstdlib>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
|
|
#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 =
|
|
"<html><body>Upload example."
|
|
"<form method=\"POST\" action=\"/handle_post_request\" "
|
|
" enctype=\"multipart/form-data\">"
|
|
"<input type=\"file\" name=\"file\" /> <br/>"
|
|
"<input type=\"submit\" value=\"Upload\" />"
|
|
"</form></body></html>";
|
|
|
|
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;
|
|
|
|
//initialize the parameters structure
|
|
params = cmd_parser_params_create();
|
|
|
|
//call the command line parser
|
|
if (cmd_parser(argc, argv, &args_info) != 0) {
|
|
result = 1;
|
|
goto stop;
|
|
}
|
|
|
|
//override command line options,
|
|
//but do not initialize args_info, check for required options.
|
|
//NOTICE: we must NOT skip the 0 assignment to initialize,
|
|
//since its default value is 1 and override defaults to 0
|
|
//while check_required is already set to its default value, 1
|
|
params->initialize = 1;
|
|
params->override = 0;
|
|
|
|
//call the config file parser
|
|
if (cmd_parser_config_file
|
|
(args_info.conffile_arg, &args_info, params) != 0) {
|
|
result = 1;
|
|
goto stop;
|
|
}
|
|
|
|
|
|
//debugcode:
|
|
cout << "value of port: " << args_info.port_arg << std::endl;
|
|
cout << "value of daemonize: " <<
|
|
static_cast<bool>(args_info.daemonize_flag) <<
|
|
std::endl;
|
|
|
|
cout << "value of listen_given: "<<
|
|
args_info.listen_given << std::endl ;
|
|
for (unsigned int i = 0; i < args_info.listen_given; i++)
|
|
cout << "value of listen: " <<
|
|
args_info.listen_arg[i] <<std::endl;
|
|
|
|
if (args_info.saveconf_given) {
|
|
if (cmd_parser_file_save(args_info.conffile_arg,
|
|
&args_info) == EXIT_FAILURE)
|
|
result = 1;
|
|
else
|
|
cout << "saved configuration file "<<
|
|
args_info.conffile_arg <<
|
|
", \n" << std::endl;
|
|
}
|
|
m = MongooseHandler(args_info.port_arg);
|
|
if (!args_info.daemonize_flag) {
|
|
while(1)
|
|
getchar();
|
|
}
|
|
|
|
stop:
|
|
//deallocate structures
|
|
cmd_parser_free(&args_info);
|
|
free(params);
|
|
|
|
return result;
|
|
}
|