2014-01-17 23:47:14 +01:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
#include "Foohash.H"
|
|
|
|
#include "Routerin.H"
|
2014-05-02 00:01:01 +02:00
|
|
|
#include "DynamicObjects.H"
|
2014-01-18 15:10:23 +01:00
|
|
|
|
2014-02-22 18:03:05 +01:00
|
|
|
using std::strcmp;
|
2014-01-17 23:47:14 +01:00
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
|
|
|
|
2014-02-22 18:03:05 +01:00
|
|
|
//put this in class ?? Util??
|
|
|
|
//FIXME:
|
|
|
|
static const std::string std_html_header= u8R"HUENERBLUBBE(<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
|
|
<title>Sharing is Caring</title>
|
2014-02-23 18:04:52 +01:00
|
|
|
<link rel="stylesheet" type="text/css" href="static/style.css">
|
2014-02-22 18:03:05 +01:00
|
|
|
</head>
|
|
|
|
)HUENERBLUBBE" ;
|
|
|
|
|
|
|
|
|
|
|
|
static const std::string std_mainpage= u8R"HASENBLUBBE(
|
|
|
|
<body>
|
|
|
|
<div id="page">
|
|
|
|
<h1>
|
|
|
|
<span style="color: red">Sharing</span>
|
|
|
|
<span style="color: blue">is</span>
|
|
|
|
<span style="color: green">Caring</span>
|
|
|
|
</h1>
|
|
|
|
|
|
|
|
<div id="up">
|
|
|
|
<form action="/upload" method="post" enctype="multipart/form-data">
|
|
|
|
<input id="file" name="file" type="file">
|
|
|
|
<input type="submit" value="Hochladen">
|
|
|
|
</form>
|
|
|
|
<p class="hint">
|
|
|
|
Wie wärs mit einem schicken Foto? Dem Flyer der nächsten
|
|
|
|
Party? Oder gar einem gesprochenen Kommentar?
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
)HASENBLUBBE" ;
|
|
|
|
|
|
|
|
|
|
|
|
static const std::string std_footer= u8R"FISCHBLUBBE(
|
|
|
|
<p class="foot">
|
|
|
|
Eine Guerilla-Kunstaktion von <b><<</>></b>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
)FISCHBLUBBE" ;
|
|
|
|
|
2014-01-17 23:47:14 +01:00
|
|
|
|
2014-01-18 14:48:52 +01:00
|
|
|
Routerin::Routerin():
|
2014-01-26 13:02:12 +01:00
|
|
|
sfh( new StaticFileHandler())
|
2014-01-18 14:48:52 +01:00
|
|
|
{
|
2014-02-22 18:03:05 +01:00
|
|
|
cout << "Konstr Routering" << endl;
|
2014-01-18 14:48:52 +01:00
|
|
|
}
|
2014-01-17 23:47:14 +01:00
|
|
|
|
2014-05-02 01:15:14 +02:00
|
|
|
Routerin::~Routerin()
|
|
|
|
{
|
|
|
|
delete _dyn_obs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Routerin::start()
|
|
|
|
{
|
|
|
|
_dyn_obs = new DynamicObjects(this);
|
|
|
|
}
|
|
|
|
|
2014-05-05 00:45:39 +02:00
|
|
|
|
|
|
|
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){
|
2014-01-25 17:47:37 +01:00
|
|
|
const RouterOp *blub=0;
|
|
|
|
Foohash fh;
|
|
|
|
const char *reqstr=0;
|
|
|
|
char *tmpstr = 0,*pntrstr=0;
|
|
|
|
int retval=-1;
|
2014-05-05 00:45:39 +02:00
|
|
|
reqstr=conn->uri;
|
2014-01-25 17:47:37 +01:00
|
|
|
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:
|
2014-05-05 00:45:39 +02:00
|
|
|
retval = rq_file(pntrstr ,conn);
|
2014-01-25 17:47:37 +01:00
|
|
|
break;
|
|
|
|
case RouterOpCode::DATE:
|
2014-05-05 00:45:39 +02:00
|
|
|
retval = rq_date(pntrstr, conn);
|
2014-01-25 17:47:37 +01:00
|
|
|
break;
|
|
|
|
case RouterOpCode::UPLOAD:
|
2014-05-05 00:45:39 +02:00
|
|
|
retval = rq_upload(pntrstr, conn);
|
2014-01-25 17:47:37 +01:00
|
|
|
break;
|
|
|
|
case RouterOpCode::STATIC:
|
|
|
|
std::cerr << "(/static/ given)";
|
2014-05-05 00:45:39 +02:00
|
|
|
retval = rq_static((pntrstr),conn);
|
2014-01-25 17:47:37 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
std::cerr << "fallback ";
|
2014-05-05 00:45:39 +02:00
|
|
|
retval = rq_static("illegalurl.htm",conn);
|
2014-01-25 17:47:37 +01:00
|
|
|
};
|
|
|
|
}else{
|
|
|
|
std::cerr << "no blub! in " << __LINE__ << endl;
|
2014-02-22 18:03:05 +01:00
|
|
|
//retval = rq_static("illegalurl.htm",event);
|
2014-05-05 00:45:39 +02:00
|
|
|
retval = rq_render_mainpage(conn);
|
2014-01-26 12:53:04 +01:00
|
|
|
|
2014-01-25 17:47:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::free(tmpstr);
|
|
|
|
tmpstr=0;
|
|
|
|
return retval;
|
|
|
|
} else {
|
|
|
|
std::cerr << "event request without uri? ?? hu? [" <<
|
2014-05-05 00:45:39 +02:00
|
|
|
conn->uri << "]" << endl;
|
2014-01-25 17:47:37 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2014-01-23 02:22:06 +01:00
|
|
|
}
|
2014-01-17 23:47:14 +01:00
|
|
|
|
|
|
|
|
2014-05-02 01:04:24 +02:00
|
|
|
void Routerin::set_baseurl(std::string baseurl)
|
|
|
|
{
|
|
|
|
this->baseurl = baseurl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
void Routerin::set_file_path(std::string file_path)
|
|
|
|
{
|
|
|
|
this->file_path = file_path;
|
2014-02-22 18:03:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-02 01:04:24 +02:00
|
|
|
void Routerin::set_json_path(std::string json_path)
|
|
|
|
{
|
|
|
|
this->json_path = json_path;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-05 00:45:39 +02:00
|
|
|
int Routerin::rq_render_mainpage(struct mg_connection *conn)
|
2014-05-02 01:04:24 +02:00
|
|
|
{
|
2014-02-22 18:03:05 +01:00
|
|
|
std::cerr << "rendering main page!" << endl;
|
|
|
|
const std::string fubb =
|
|
|
|
std_html_header +
|
|
|
|
std_mainpage +
|
|
|
|
_dyn_obs->render_object_list()+
|
|
|
|
std_footer.c_str() ;
|
|
|
|
|
2014-05-05 00:45:39 +02:00
|
|
|
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
|
2014-02-22 18:03:05 +01:00
|
|
|
"Content-Length: %lu\r\n"
|
|
|
|
"Content-Type: text/html\r\n\r\n%s",
|
|
|
|
fubb.size(),
|
|
|
|
fubb.c_str());
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-05 00:45:39 +02:00
|
|
|
int Routerin::rq_static(char const * const reststr , struct mg_connection *conn)
|
2014-01-18 14:48:52 +01:00
|
|
|
{
|
|
|
|
std::cerr << " a static file is reqested!" << endl;
|
|
|
|
std::cerr << " path: " << reststr << endl;
|
2014-05-05 00:45:39 +02:00
|
|
|
return sfh->answer_pathreq(reststr,conn);
|
2014-01-18 14:48:52 +01:00
|
|
|
}
|
2014-01-17 23:47:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-05 00:45:39 +02:00
|
|
|
int Routerin::rq_file(char const * const reststr,
|
|
|
|
struct mg_connection *conn __attribute__((unused)))
|
2014-01-18 14:48:52 +01:00
|
|
|
{
|
|
|
|
std::cerr << " a dynamic file is reqested!" << endl;
|
|
|
|
std::cerr << " path: " << reststr << endl;
|
2014-05-05 00:45:39 +02:00
|
|
|
return sfh->answer_pathreq(reststr,conn);
|
2014-01-18 14:48:52 +01:00
|
|
|
}
|
2014-01-17 23:47:14 +01:00
|
|
|
|
2014-01-25 17:47:37 +01:00
|
|
|
int Routerin::rq_date(char const * const reststr __attribute__((unused)),
|
2014-05-05 00:45:39 +02:00
|
|
|
struct mg_connection *conn __attribute__((unused)) )
|
2014-01-18 14:48:52 +01:00
|
|
|
{
|
|
|
|
std::cerr << "requested filelist ordered by date!" << endl ;
|
|
|
|
return 1;
|
|
|
|
}
|
2014-01-17 23:47:14 +01:00
|
|
|
|
2014-01-25 17:47:37 +01:00
|
|
|
int Routerin::rq_upload(char const * const reststr __attribute__((unused)),
|
2014-05-05 00:45:39 +02:00
|
|
|
struct mg_connection *conn __attribute__((unused)))
|
2014-01-18 14:48:52 +01:00
|
|
|
{
|
2014-05-05 00:45:39 +02:00
|
|
|
std::cerr << "here we will get uploadish!" << endl;
|
|
|
|
std::cerr << "handle: " << handle_upload( conn ) << "fup!" << endl;
|
2014-01-17 23:47:14 +01:00
|
|
|
|
2014-05-05 00:45:39 +02:00
|
|
|
// get Dynamic Object form Upload
|
|
|
|
// Persist
|
|
|
|
// render thankyou page
|
2014-05-29 18:58:20 +02:00
|
|
|
return MG_TRUE;
|
2014-01-18 14:48:52 +01:00
|
|
|
}
|
2014-01-17 23:47:14 +01:00
|
|
|
|
2014-05-05 00:45:39 +02:00
|
|
|
/*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)) ){
|
2014-01-18 14:48:52 +01:00
|
|
|
//DEBUG CODE
|
|
|
|
std::cerr << "Got an unhandled mg_event" << endl;
|
2014-05-05 00:45:39 +02:00
|
|
|
std::cerr << "Its type is: " << event << endl;
|
2014-01-18 14:48:52 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-01-17 23:47:14 +01:00
|
|
|
|
|
|
|
|
2014-05-05 00:45:39 +02:00
|
|
|
//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;
|
2014-01-18 14:48:52 +01:00
|
|
|
}
|
2014-01-17 23:47:14 +01:00
|
|
|
|
|
|
|
|
2014-01-18 14:48:52 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
static int event_handler(struct mg_event *event) {
|
2014-01-17 23:47:14 +01:00
|
|
|
|
2014-01-18 14:48:52 +01:00
|
|
|
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);
|
2014-01-17 23:47:14 +01:00
|
|
|
} else {
|
2014-01-18 14:48:52 +01:00
|
|
|
mg_printf(event->conn,"%s",
|
|
|
|
"HTTP/1.0 200 OK\r\n\r\nNo files sent");
|
2014-01-17 23:47:14 +01:00
|
|
|
}
|
2014-01-18 14:48:52 +01:00
|
|
|
} 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);
|
2014-01-17 23:47:14 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 14:48:52 +01:00
|
|
|
// Mark request as processed
|
2014-01-17 23:47:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-01-18 14:48:52 +01:00
|
|
|
// All other events left unprocessed
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*/
|
2014-01-17 23:47:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
//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)
|
|
|
|
|
|
|
|
|