Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#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"));
}