-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
62 lines (60 loc) · 2.32 KB
/
main.cpp
File metadata and controls
62 lines (60 loc) · 2.32 KB
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
#include<bits/stdc++.h>
#include"httpd.h"
using namespace std;
string readFile(string path) {
ifstream t;
t.open(path.c_str());
std::stringstream buffer;
buffer << t.rdbuf();
return buffer.str();
}
int main(int argc, char *argv[]) {
app.addRoute("/websocket", [](client_conn conn, http_request request, param argv){
string js = "<script src='./script/main.js'></script>";
putRequest(conn, 200, __default_response);
send(conn, js);
exitRequest(conn);
});
app.addRoute("/style/%s", [](client_conn conn, http_request request, param argv){
string buffer = readFile("./css/" + argv[0]);
putRequest(conn, 200, merge(__default_response, mime(".css")));
send(conn, buffer);
exitRequest(conn);
});
app.addRoute("/script/%s", [](client_conn conn, http_request request, param argv){
string buffer = readFile("./js/" + argv[0]);
putRequest(conn, 200, merge(__default_response, mime(".js")));
send(conn, buffer);
exitRequest(conn);
});
app.addRoute("/index", [](client_conn conn, http_request request, param argv){
stringstream buffer;
argvar $_GET = getParam(request);
argvar $_POST = postParam(request);
argvar $_COOKIE = cookieParam(request);
buffer << "$_GET: " << _endl;
for (auto it : $_GET) buffer << it.first << " " << it.second << _endl;
buffer << "$_POST: " << _endl;
for (auto it : $_POST) buffer << it.first << " " << it.second << _endl;
buffer << "$_COOKIE: " << _endl;
for (auto it : $_COOKIE) buffer << it.first << " " << it.second << _endl;
putRequest(conn, 200, __default_response);
send(conn, buffer.str());
exitRequest(conn);
});
app.addRoute("/%d/%f/%s", [](client_conn conn, http_request request, param argv){
stringstream buffer;
buffer << argv[0] << " " << argv[1] << " " << argv[2] << endl;
putRequest(conn, 200, __default_response);
send(conn, buffer.str());
exitRequest(conn);
});
app.setopt(HTTP_ENABLE_SSL, false);
app.setopt(HTTP_LISTEN_PORT, 8888);
app.setopt(HTTP_LISTEN_HOST, "0.0.0.0");
app.setopt(HTTP_MULTI_THREAD, 3);
app.setopt(LOG_TARGET_TYPE, LOG_TARGET_FILE);
app.setopt(OPEN_DEBUG, false);
app.run();
return 0;
}