-
Notifications
You must be signed in to change notification settings - Fork 8
/
TodoServerApp.cpp
221 lines (170 loc) · 5.18 KB
/
TodoServerApp.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <string>
#include "TodoServerApp.h"
Mutex TodoServerApp::todoLock;
CTodoList TodoServerApp::todoList;
ostream& operator<<(ostream& os, CTodo& todo)
{
os << "{ \"_id\": "<< todo.getId() << ", \"text\": \"" << todo.getText() << "\" }";
return os;
}
ostream& operator<<(ostream& os, CTodoList& todoList)
{
map<size_t, CTodo> todos = todoList.readList();
os << "[";
if(!todos.empty())
{
if(todos.size() == 1)
os << todos.begin()->second;
else
for ( map<size_t, CTodo>::iterator it = todos.begin();;)
{
os << it->second ;
if(++it != todos.end())
os << ',';
else
break;
}
}
os << "]\n";
return os;
}
class CTodoHandler : public HTTPRequestHandler
{
public:
void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp)
{
URI uri(req.getURI());
string method = req.getMethod();
cerr << "URI: " << uri.toString() << endl;
cerr << "Method: " << req.getMethod() << endl;
StringTokenizer tokenizer(uri.getPath(), "/", StringTokenizer::TOK_TRIM);
HTMLForm form(req,req.stream());
if(!method.compare("POST"))
{
cerr << "Create:" << form.get("text") << endl;
CTodo todo(form.get("text"));
TodoServerApp::createTodo(todo);
}
else if(!method.compare("PUT"))
{
cerr << "Update id:" << *(--tokenizer.end()) << endl;
cerr << "Update text:" << form.get("text") << endl;
//size_t id=stoull(*(--tokenizer.end()));
//TodoServerApp::updateTodo(id, form.get("text"));
}
else if(!method.compare("DELETE"))
{
cerr << "Delete id:" << *(--tokenizer.end()) << endl;
size_t id=stoull(*(--tokenizer.end()));
TodoServerApp::deleteTodo(id);
}
resp.setStatus(HTTPResponse::HTTP_OK);
resp.setContentType("application/json");
//resp.setContentLength(...);
ostream& out = resp.send();
cerr << TodoServerApp::readTodoList() << endl;
out << TodoServerApp::readTodoList() << endl;
out.flush();
}
};
#include <iostream> // std::cout
#include <fstream> // std::ifstream
#include <map> // std::ifstream
class CFileHandler : public HTTPRequestHandler
{
typedef std::map<const std::string, const std::string> TStrStrMap;
TStrStrMap CONTENT_TYPE = {
#include "MimeTypes.h"
};
string getPath(string& path){
if(path == "/"){
path="/index.html";
}
path.insert(0, "./www");
return path;
}
string getContentType(string& path){
string contentType("text/plain");
Poco::Path p(path);
TStrStrMap::const_iterator i=CONTENT_TYPE.find(p.getExtension());
if (i != CONTENT_TYPE.end())
{ /* Found, i->first is f, i->second is ++-- */
contentType = i->second;
}
if(contentType.find("text/") != std::string::npos)
{
contentType+="; charset=utf-8";
}
cerr << path << " : " << contentType << endl;
return contentType;
}
public:
void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp)
{
cerr << "Get static page: ";
//system("echo -n '1. Current Directory is '; pwd");
URI uri(req.getURI());
string path(uri.getPath());
ifstream ifs (getPath(path).c_str(), ifstream::in);
if(ifs)
{
resp.setStatus(HTTPResponse::HTTP_OK);
resp.setContentType(getContentType(path));
ostream& out = resp.send();
char c = ifs.get();
while (ifs.good()) {
out << c;
c = ifs.get();
}
out.flush();
}
else
{
resp.setStatus(HTTPResponse::HTTP_NOT_FOUND);
ostream& out = resp.send();
out << "File not found" << endl;
out.flush();
}
ifs.close();
}
};
class TodoRequestHandlerFactory : public HTTPRequestHandlerFactory
{
public:
virtual HTTPRequestHandler* createRequestHandler(const HTTPServerRequest & request)
{
if (!request.getURI().find("/api/"))
return new CTodoHandler;
else
return new CFileHandler;
}
};
void TodoServerApp::createTodo(CTodo& todo)
{
ScopedLock<Mutex> lock(todoLock);
todoList.create(todo);
}
CTodoList& TodoServerApp::readTodoList()
{
ScopedLock<Mutex> lock(todoLock);
return todoList;
}
void TodoServerApp::deleteTodo(size_t id)
{
ScopedLock<Mutex> lock(todoLock);
todoList.del(id);
}
int TodoServerApp::main(const vector<string> &)
{
HTTPServerParams* pParams = new HTTPServerParams;
pParams->setMaxQueued(100);
pParams->setMaxThreads(16);
HTTPServer s(new TodoRequestHandlerFactory, ServerSocket(8000), pParams);
s.start();
cerr << "Server started" << endl;
waitForTerminationRequest(); // wait for CTRL-C or kill
cerr << "Shutting down..." << endl;
s.stop();
return Application::EXIT_OK;
}