207 lines
4.6 KiB
C++
207 lines
4.6 KiB
C++
|
|
#include <cstring>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
using std::strcmp;
|
|
#include "Foohash.H"
|
|
#include "Routerin.H"
|
|
|
|
#include "UltraSimpleStaticFileHandler.H"
|
|
|
|
using std::cout;
|
|
using std::endl;
|
|
|
|
|
|
Routerin::Routerin():
|
|
sfh( UltraSimpleStaticFileHandler())
|
|
{
|
|
cout << "Konstr Routering" << std::endl;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
int Routerin::event_request_route(struct mg_event *event){
|
|
const RouterOp *blub=0;
|
|
Foohash fh;
|
|
const char *reqstr=0;
|
|
char *tmpstr = 0,*pntrstr=0;
|
|
int retval=-1;
|
|
reqstr=event->request_info->uri;
|
|
if (reqstr){
|
|
tmpstr = strdup(reqstr);
|
|
std::cerr << "starting with str as "
|
|
<< tmpstr << endl; //DEBUG CODE
|
|
pntrstr = strchrnul((tmpstr+1),'/');
|
|
if (pntrstr) {
|
|
*pntrstr = 0;
|
|
std::cerr <<" having reqstr as " << tmpstr
|
|
<< " now. " << endl;
|
|
std::cerr <<"len of reqstr is " <<
|
|
( pntrstr - tmpstr) << endl;
|
|
blub = fh.in_word_set(tmpstr,((pntrstr++)-tmpstr) );
|
|
if (blub){
|
|
std::cerr << "me has blub!!" <<endl;
|
|
switch(blub->op){
|
|
case RouterOpCode::FILES:
|
|
retval = rq_file(pntrstr ,event);
|
|
break;
|
|
case RouterOpCode::DATE:
|
|
retval = rq_date(pntrstr, event);
|
|
break;
|
|
case RouterOpCode::UPLOAD:
|
|
retval = rq_upload(pntrstr, event);
|
|
break;
|
|
case RouterOpCode::STATIC:
|
|
std::cerr << "(/static/ given)";
|
|
retval = rq_static((pntrstr),event);
|
|
break;
|
|
default:
|
|
std::cerr << "fallback ";
|
|
retval = rq_static((pntrstr),event);
|
|
};
|
|
}else{
|
|
std::cerr << "no blub! in " << __LINE__ << endl;
|
|
}
|
|
}
|
|
std::free(tmpstr);
|
|
tmpstr=0;
|
|
return retval;
|
|
} else {
|
|
std::cerr << "event request without uri? ?? hu? [" <<
|
|
event->request_info->uri << "]" << endl;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
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 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;
|
|
}
|
|
|
|
|
|
int Routerin::rq_date(char const * const reststr , struct mg_event *event)
|
|
{
|
|
std::cerr << "requested filelist ordered by date!" << 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 Routerin::event_requend_route(struct mg_event *event){
|
|
std::cerr << "ended request" <<endl;
|
|
return 1;
|
|
}
|
|
|
|
int Routerin::event_fallback_route(struct mg_event *event){
|
|
//DEBUG CODE
|
|
std::cerr << "Got an unhandled mg_event" << endl;
|
|
std::cerr << "Its type is: " << event->type << endl;
|
|
return 1;
|
|
}
|
|
|
|
|
|
int Routerin::event_newthread_route(struct mg_event *event){
|
|
//DEBUG CODE
|
|
std::cerr << "Started new thread!" <<endl;
|
|
return 1;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
*
|
|
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;
|
|
}
|
|
*/
|
|
|
|
|
|
//REST API Documentation
|
|
|
|
///files/<zahl> (index in vector<Sicccobjst>)
|
|
|
|
///date/<datestring> (files am date hochgeladen)
|
|
|
|
///static/<mööp.html> (statisches html ausliefern)
|
|
|
|
///uploadmagic/ (upload form request)
|
|
|
|
|