Commit e2203582 authored by jan.koester's avatar jan.koester
Browse files

some ext

parent 5f22e36c
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -8,6 +8,12 @@ find_package(libhtmlpp REQUIRED)

add_executable(guestbook main.cpp)

add_definitions(
    -D${CMAKE_HOST_SYSTEM}
    -DHPAGE="${CMAKE_INSTALL_PREFIX}/share/guestbook/guest.html"
    -DRHPAGE="${CMAKE_INSTALL_PREFIX}/share/guestbook/guest.html"
)

target_include_directories(guestbook
    PUBLIC
    ${HTTPPP_INCLUDE_DIR}
@@ -22,3 +28,15 @@ target_link_libraries(guestbook
    cmdplus::cmdplus
)

install(TARGETS
        guestbook
    DESTINATION
        bin
)

install(FILES
        guest.html
        retro.html
    DESTINATION
        share/guestbook
)
+47 −13
Original line number Diff line number Diff line
@@ -10,8 +10,17 @@
#include <htmlpp/html.h>
#include <htmlpp/exception.h>


#include <netplus/exception.h>

#ifndef Windows
#include <unistd.h>
#include <sys/stat.h>
#define libpath(V)  ("/var/lib/guestbook/"#V)
#else
#define libpath(V)  ""(#V)""
#endif

std::mutex   file_mutex;

struct Browser {
@@ -19,16 +28,17 @@ struct Browser {
    float       Version;
};


//#define DEBUG

class Controller : public libhttppp::HttpEvent {
public:
    Controller(netplus::socket* serversocket) : HttpEvent(serversocket){
        try{
            page.loadFile(index,"guest.html");
            rpage.loadFile(rindex,"retro.html");
            page.loadFile(index,HPAGE);
            rpage.loadFile(rindex,RHPAGE);
        }catch(libhtmlpp::HTMLException &e){
            netplus::NetException ee;
            ee[netplus::NetException::Error] << e.what();
            throw ee.what();
            throw e.what();
        }
    };

@@ -75,7 +85,8 @@ public:

    }

    void RequestEvent(libhttppp::HttpRequest *curreq,int tid,ULONG_PTR arg){

    void RequestEvent(libhttppp::HttpRequest *curreq,int tid,ULONG_PTR args){
            try {
                libhttppp::HttpForm gform;

@@ -95,7 +106,7 @@ public:
                                 std::string entry;
                                 entry = guestname; entry += "|"; entry += guestentry;
                                 file_mutex.lock();
                                 std::ofstream guestfile_out ("guest.txt",std::ios::app);
                                 std::ofstream guestfile_out (libpath(guest.txt),std::ios::app);
                                 if(guestfile_out.is_open())
                                     guestfile_out << entry << "\n";
                                 guestfile_out.close();
@@ -125,7 +136,7 @@ public:

                file_mutex.lock();
                std::string line;
                std::ifstream guestfile ("guest.txt");
                std::ifstream guestfile (libpath(guest.txt));

                libhtmlpp::HtmlElement guestul("ul");

@@ -180,13 +191,36 @@ private:

class HttpConD : public libhttppp::HttpD {
public:
  HttpConD(int cargc, char** cargv) : HttpD(cargc,cargv){
    HttpConD(int argc, char** argv) : HttpD(argc,argv){
        libhttppp::HTTPException httpexception;
        try {
#ifndef Windows
            if(getuid()!=0){
                throw "must be run as root!";;
            }

            std::cerr << HPAGE << std::endl;

            mkdir(libpath(""),755);

            int pid = fork();

            if(pid==0){
             return;
            }

            std::ofstream pidf("/var/run/guestbook.pid");
            pidf << pid;
            pidf.close();

            Controller ctl(getServerSocket());
            ctl.runEventloop();
#else
        Controller ctl(getServerSocket());
        ctl.runEventloop();
    }catch(netplus::NetException &e){
        std::cerr << e.what() << std::endl;
#endif
    }catch(const char *e){
        std::cerr << e << std::endl;
    }
  };
private: