Loading CMakeLists.txt 0 → 100644 +19 −0 Original line number Diff line number Diff line 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) guest.html 0 → 100644 +37 −0 Original line number Diff line number Diff line <!DOCTYPE html> <html lang=""> <head> <meta charset="utf-8"> <title>Guestbook</title> <style> body { background: rgb(61, 174, 233); } #book div{ background: rgb(252, 233, 20); margin:10px 0; } #book li{ list-style:none; } #book ul{ margin:0; padding:0; } </style> </head> <body> <div id="gform"> <form action="" method="POST"> <h1>Bitte Schreiben sie etwas.</h1> <label for="fname">Name:</label> <input type="text" name="guestname" id="guestname" /> <label for="fname">Text:</label> <input type="text" name="guestentry" id="guestentry"/> <input type="submit" value="Posten"/> </form> </div> <div id="book" > </div> </body> </html> guest.txt 0 → 100644 +8 −0 Original line number Diff line number Diff line Jan|testeintrag Jonas|testeintrag2 Torben|testeintrag3 Peter|Viele Grüße Anna|Liebe Grüße Anna|Liebe Grüße Helmut|Tolle Sonnenbrillen Helmut|Tolle Sonnenbrillen main.cpp 0 → 100644 +129 −0 Original line number Diff line number Diff line #include <algorithm> #include <cstring> #include <iostream> #include <fstream> #include <mutex> #include <httppp/http.h> #include <httppp/httpd.h> #include <htmlpp/html.h> 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 << "<div><span class=\"guest_author\" >" << name <<":<br></span><span class=\"guest_entry\">" << entry << "</span></div>"; 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")); } Loading
CMakeLists.txt 0 → 100644 +19 −0 Original line number Diff line number Diff line 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)
guest.html 0 → 100644 +37 −0 Original line number Diff line number Diff line <!DOCTYPE html> <html lang=""> <head> <meta charset="utf-8"> <title>Guestbook</title> <style> body { background: rgb(61, 174, 233); } #book div{ background: rgb(252, 233, 20); margin:10px 0; } #book li{ list-style:none; } #book ul{ margin:0; padding:0; } </style> </head> <body> <div id="gform"> <form action="" method="POST"> <h1>Bitte Schreiben sie etwas.</h1> <label for="fname">Name:</label> <input type="text" name="guestname" id="guestname" /> <label for="fname">Text:</label> <input type="text" name="guestentry" id="guestentry"/> <input type="submit" value="Posten"/> </form> </div> <div id="book" > </div> </body> </html>
guest.txt 0 → 100644 +8 −0 Original line number Diff line number Diff line Jan|testeintrag Jonas|testeintrag2 Torben|testeintrag3 Peter|Viele Grüße Anna|Liebe Grüße Anna|Liebe Grüße Helmut|Tolle Sonnenbrillen Helmut|Tolle Sonnenbrillen
main.cpp 0 → 100644 +129 −0 Original line number Diff line number Diff line #include <algorithm> #include <cstring> #include <iostream> #include <fstream> #include <mutex> #include <httppp/http.h> #include <httppp/httpd.h> #include <htmlpp/html.h> 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 << "<div><span class=\"guest_author\" >" << name <<":<br></span><span class=\"guest_entry\">" << entry << "</span></div>"; 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")); }