forked from awwit/httpserverapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.cpp
More file actions
125 lines (99 loc) · 2.16 KB
/
Test.cpp
File metadata and controls
125 lines (99 loc) · 2.16 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
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
#include "Test.h"
bool test(HttpServer::ServerRequest &request, HttpServer::ServerResponse &response)
{
// Output incoming headers
std::string s = R"(<table>
<thead>
<tr>
<th colspan="2">Incoming headers</th>
</tr>
</thead>
<tbody>
)";
for (auto const &pair : request.headers)
{
s += R"( <tr>
<td>)" + pair.first + R"(</td>
<td>)" + pair.second + R"(</td>
</tr>
)";
}
// Output incoming data
s += R"( </tbody>
<thead>
<tr>
<th colspan="2">Incoming data</th>
</tr>
</thead>
<tbody>
)";
for (auto const &pair : request.data)
{
s += R"( <tr>
<td>)" + pair.first + R"(</td>
<td>)" + pair.second + R"(</td>
</tr>
)";
}
// Output incoming url parameters
s += R"( </tbody>
<thead>
<tr>
<th colspan="2">Incoming url parameters</th>
</tr>
</thead>
<tbody>
)";
for (auto const &pair : request.params)
{
s += R"( <tr>
<td>)" + pair.first + R"(</td>
<td>)" + pair.second + R"(</td>
</tr>
)";
}
// Output info about incoming files
s += R"( </tbody>
<thead>
<tr>
<th colspan="2">Incoming files</th>
</tr>
</thead>
<tbody>
)";
for (auto const &pair : request.files)
{
const HttpServer::FileIncoming &file = pair.second;
s += R"( <tr>
<td>)" + file.getName() + R"(</td>
<td>)" + std::to_string(file.getSize() ) + R"(</td>
</tr>
)";
}
s += R"( </tbody>
</table>)";
HttpServer::Socket &socket = response.socket;
std::map<std::string, std::string> &headers_outgoing = response.headers;
// Set outgoing headers
headers_outgoing[""] = "HTTP/1.1 200 OK";
headers_outgoing["Content-Type"] = "text/html; charset=utf-8";
headers_outgoing["Accept-Ranges"] = "bytes";
headers_outgoing["Content-Length"] = std::to_string(s.length() );
headers_outgoing["Connection"] = "Keep-Alive";
headers_outgoing["Date"] = Utils::getDatetimeAsString();
std::string headers;
for (auto const &h : headers_outgoing)
{
if (h.first.length() )
{
headers += h.first + ": ";
}
headers += h.second + "\r\n";
}
headers += "\r\n";
// Send headers and page
std::chrono::milliseconds timeout(5000);
socket.nonblock_send(headers, timeout);
socket.nonblock_send(s, timeout);
return EXIT_SUCCESS;
}