Skip to content
main.cpp 4.48 KiB
Newer Older
jan.koester's avatar
jan.koester committed
#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"));
}