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

fixes

parent 4226b967
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -6,3 +6,27 @@ Anna|Liebe Grüße
Anna|Liebe Grüße
Helmut|Tolle Sonnenbrillen
Helmut|Tolle Sonnenbrillen
test|tes
test|tes
test|test
test|test
test|test
tes|test
tes|test
tes|test
tes|test
tes|test
tes|test
tes|test
test|test
testneu|testneu
testneu|testneu
tes|test
gdrgd|drgdrg
gdrgd|drgdrg
gdrgd|drgdrg
gdrgd|drgdrg
win98|win98
gdrgd|drgdrg
gdrgd|drgdrg
gdrgd|drgdrg
+99 −35
Original line number Diff line number Diff line
@@ -8,19 +8,74 @@
#include <httppp/httpd.h>

#include <htmlpp/html.h>
#include <htmlpp/exception.h>

std::mutex   file_mutex;

struct Browser {
    std::string Name;
    float       Version;
};

class Controller : public libhttppp::HttpEvent {
public:
    Controller(netplus::socket* serversocket,libhtmlpp::HtmlElement *tpl) : HttpEvent(serversocket){
        _tpl=tpl;
    Controller(netplus::socket* serversocket) : HttpEvent(serversocket){
        try{
            page.loadFile(index,"guest.html");
            rpage.loadFile(rindex,"retro.html");
        }catch(libhtmlpp::HTMLException &e){
            throw e.what();
        }
    };

    void BrowserDetect(libhttppp::HttpRequest *curreq,Browser &bws){
        std::string txt=curreq->getValue(curreq->getData("user-agent"));

        size_t pos=0;
        char buf[255];

        if( (pos=txt.find("msie")) != std::string::npos){
            bws.Name="msie";
            pos+=4;

            while(txt[pos]==' '){++pos;};

            int ii=0;

            for(size_t i=pos; i<txt.length(); ++i){
                if(ii>255 || txt[i]==' ' || txt[i] == ';'){
                    break;
                }
                buf[ii++]=txt[i];
            }
            buf[ii]='\0';
            bws.Version=atof(buf);
        }else if( ( pos=txt.find("gecko") ) != std::string::npos ){
            bws.Name="gecko";
            pos=txt.find("rv:");
            pos+=3;

            while(txt[pos]==' '){++pos;};

            int ii=0;

            for(size_t i=pos; i<txt.length(); ++i){
                if(ii>255 || !(isdigit(txt[i]) || txt[i]=='.') ){
                    break;
                }
                buf[ii++]=txt[i];
            }
            buf[ii]='\0';
            bws.Version=atof(buf);
        }

    }

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

                if(curreq->getRequestType()==POSTREQUEST){
                    try{
                        std::string guestname,guestentry;
                        gform.parse(curreq);
@@ -31,7 +86,6 @@ public:
                            }else if(strcmp("guestentry",cururlform->getKey())==0){
                                guestentry=cururlform->getValue();
                            }
                        std::cout << cururlform->getKey() << std::endl;
                             }
                             if(!guestname.empty() && !guestentry.empty()){
                                 std::string entry;
@@ -43,11 +97,23 @@ public:
                                 guestfile_out.close();
                                 file_mutex.unlock();
                             }
                }catch(...){
                    }catch(libhttppp::HTTPException &e){
                        std::cout << e.what() << std::endl;
                        std::cout.write(curreq->RecvData.data(),curreq->RecvData.size()) <<std::endl;
                        return;
                    }
                }

                libhtmlpp::HtmlElement site;
                site=_tpl;
                Browser cbws;
                BrowserDetect(curreq,cbws);

                if( (cbws.Name=="msie" && cbws.Version <9) ||
                    (cbws.Name=="gecko" && cbws.Version < 3.5)
                )
                    site=rindex;
                else
                    site=index;

                libhttppp::HttpResponse resp;
                resp.setState(HTTP200);
                resp.setVersion(HTTPVERSION(2.0));
@@ -104,26 +170,24 @@ public:
    }

private:
    libhtmlpp::HtmlElement *_tpl;
    libhtmlpp::HtmlElement index,rindex;
    libhtmlpp::HtmlPage    page,rpage;
};

class HttpConD : public libhttppp::HttpD {
public:
  HttpConD(int argc, char** argv,libhtmlpp::HtmlElement *tpl) : HttpD(argc,argv){
  HttpConD(int argc, char** argv) : HttpD(argc,argv){
    libhttppp::HTTPException httpexception;
    try {
        Controller ctl(getServerSocket(),tpl);
        Controller ctl(getServerSocket());
        ctl.runEventloop();
    }catch(libhttppp::HTTPException &e){
        std::cerr << e.what() << std::endl;
    }catch(const char *e){
        std::cerr << e << std::endl;
    }
  };
private:
};

int main(int argc, char** argv){
    libhtmlpp::HtmlPage page;
    libhtmlpp::HtmlElement index;
    page.loadFile(index,"guest.html");
    HttpConD(argc,argv,&index);
    HttpConD(argc,argv);
}

retro.html

0 → 100644
+39 −0
Original line number Diff line number Diff line
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//DE"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <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>