From 20955d917b5043889009a43bf769835639008655 Mon Sep 17 00:00:00 2001 From: "jan.koester" Date: Thu, 4 Jan 2024 16:17:20 +0100 Subject: [PATCH] initial commit --- CMakeLists.txt | 19 ++++++++ guest.html | 37 ++++++++++++++ guest.txt | 8 +++ main.cpp | 129 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 193 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 guest.html create mode 100644 guest.txt create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6d33dc6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.0) + +find_package(libnetplus REQUIRED) +find_package(libcryptplus REQUIRED) +find_package(httppp REQUIRED) + +project(guestbook) + +add_executable(guestbook main.cpp) + +target_include_directories(guestbook + PUBLIC + ${httppp_INCLUDE_DIRECTORIES} + ${htmlpp_INCLUDE_DIRECTORIES} + ${netplus_INCLUDE_DIRECTORIES} +) + +target_link_libraries(guestbook ${httppp_LIBRARIES} netplus htmlpp) + diff --git a/guest.html b/guest.html new file mode 100644 index 0000000..f99bbe6 --- /dev/null +++ b/guest.html @@ -0,0 +1,37 @@ + + + + + Guestbook + + + +
+
+

Bitte Schreiben sie etwas.

+ + + + + +
+
+
+
+ + diff --git a/guest.txt b/guest.txt new file mode 100644 index 0000000..0c4136b --- /dev/null +++ b/guest.txt @@ -0,0 +1,8 @@ +Jan|testeintrag +Jonas|testeintrag2 +Torben|testeintrag3 +Peter|Viele Grüße +Anna|Liebe Grüße +Anna|Liebe Grüße +Helmut|Tolle Sonnenbrillen +Helmut|Tolle Sonnenbrillen diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..acad59a --- /dev/null +++ b/main.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + +#include +#include + +#include + +std::mutex file_mutex; + +class Controller : public netplus::event { +public: + Controller(netplus::socket* serversocket,libhtmlpp::HtmlElement *tpl) : event(serversocket){ + _tpl=tpl; + }; + + void RequestEvent(netplus::con *curcon){ + libhttppp::HttpRequest curreq; + try { + curreq.parse(curcon); + libhttppp::HttpForm gform; + + try{ + std::string guestname,guestentry; + gform.parse(&curreq); + for (libhttppp::HttpForm::UrlcodedFormData* cururlform = gform.getUrlcodedFormData(); cururlform; + cururlform = cururlform->nextUrlcodedFormData()) { + if(strcmp("guestname",cururlform->getKey())==0){ + guestname=cururlform->getValue(); + }else if(strcmp("guestentry",cururlform->getKey())==0){ + guestentry=cururlform->getValue(); + } + std::cout << cururlform->getKey() << std::endl; + } + if(!guestname.empty() && !guestentry.empty()){ + std::string entry; + entry = guestname; entry += "|"; entry += guestentry; + file_mutex.lock(); + std::ofstream guestfile_out ("guest.txt",std::ios::app); + if(guestfile_out.is_open()) + guestfile_out << entry << "\n"; + guestfile_out.close(); + file_mutex.unlock(); + } + }catch(...){ + } + + libhtmlpp::HtmlElement site; + site=_tpl; + libhttppp::HttpResponse resp; + resp.setState(HTTP200); + resp.setVersion(HTTPVERSION(2.0)); + resp.setContentLength(0); + + file_mutex.lock(); + std::string line; + std::ifstream guestfile ("guest.txt"); + + libhtmlpp::HtmlElement guestul("ul"); + + if(!guestfile.is_open()){ + libhttppp::HTTPException e; + e[libhttppp::HTTPException::Error] << "could not open guest.txt"; + throw e; + } + + while ( getline (guestfile,line) ){ + std::string name,entry; + int deli=line.find('|'); + name=line.substr(0,deli); + entry=line.substr(deli+1,line.length()-(deli+1)); + libhtmlpp::HtmlElement guestli("li"); + libhtmlpp::HtmlString gentry; + gentry << "
" + << name <<":
" + << entry << "
"; + guestli.appendChild(gentry.parse()); + guestul.appendChild(&guestli); + } + + guestfile.close(); + + file_mutex.unlock(); + + libhtmlpp::HtmlElement *book=site.getElementbyID("book"); + + if(book){ + book->appendChild(&guestul); + } + + std::string hdoc; + libhtmlpp::print(&site,nullptr,hdoc); + resp.send(curcon,hdoc.c_str(),hdoc.length()); + } catch (libhttppp::HTTPException& e) { + if (e.getErrorType() != libhttppp::HTTPException::Note) { + libhttppp::HttpResponse curres; + curres.setState(HTTP500); + curres.setVersion(HTTPVERSION(2.0)); + curres.setContentLength(0); + curres.send(curcon, e.what(), strlen(e.what())); + } + } + } + +private: + libhtmlpp::HtmlElement *_tpl; +}; + +class HttpConD : public libhttppp::HttpD { +public: + HttpConD(int argc, char** argv,libhtmlpp::HtmlElement *tpl) : HttpD(argc,argv){ + libhttppp::HTTPException httpexception; + try { + Controller controller(getServerSocket(),tpl); + controller.runEventloop(); + }catch(libhttppp::HTTPException &e){ + std::cerr << e.what() << std::endl; + } + }; +private: +}; + +int main(int argc, char** argv){ + libhtmlpp::HtmlPage page; + HttpConD(argc,argv,page.loadFile("guest.html")); +} -- GitLab