-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathTest.cpp
More file actions
148 lines (124 loc) · 2.78 KB
/
Test.cpp
File metadata and controls
148 lines (124 loc) · 2.78 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "Test.h"
#include "../utils/Utils.h"
namespace Application
{
bool test(
HttpServer::Request &request,
HttpServer::Response &response
) {
// Output incoming headers
std::string s = R"(<table>
<thead>
<tr>
<th colspan="2">Incoming headers</th>
</tr>
<tr>
<th>Header name</th>
<th>Header value</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 url parameters
s += R"( </tbody>
<thead>
<tr>
<th colspan="2">Incoming url parameters</th>
</tr>
<tr>
<th>Parameter name</th>
<th>Parameter value</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 incoming form data
s += R"( </tbody>
<thead>
<tr>
<th colspan="2">Incoming form data</th>
</tr>
<tr>
<th>Form field</th>
<th>Field value</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 info about incoming files
s += R"( </tbody>
<thead>
<tr>
<th colspan="5">Incoming files</th>
</tr>
<tr>
<th>Form field</th>
<th>Original name</th>
<th>File type</th>
<th>File size</th>
<th>Uploaded name</th>
</tr>
</thead>
<tbody>
)";
for (auto const &pair : request.files)
{
const std::string &field_name = pair.first;
const Transfer::FileIncoming &file = pair.second;
s += R"( <tr>
<td>)" + field_name + R"(</td>
<td>)" + file.getName() + R"(</td>
<td>)" + file.getType() + R"(</td>
<td>)" + std::to_string(file.getSize() ) + R"(</td>
<td>)" + file.getTmpName() + R"(</td>
</tr>
)";
}
s += R"( </tbody>
</table>)";
std::unordered_map<std::string, std::string> &headers = response.headers;
// Set outgoing headers
response.setStatusCode(Http::StatusCode::OK);
headers["content-type"] = "text/html; charset=utf-8";
headers["accept-ranges"] = "bytes";
headers["content-length"] = std::to_string(s.size() );
headers["connection"] = "keep-alive";
headers["date"] = Utils::getDatetimeAsString();
// Additional headers
// In additional headers may be placed values of cookies
std::vector<std::pair<std::string, std::string> > additional;
// additional.emplace_back("set-cookie", "key=value; expires=; path=/; domain=*");
// Send headers and data
const std::chrono::milliseconds timeout(5000);
if (response.sendHeaders(additional, timeout, s.empty() ) ) {
if (s.empty() == false) {
response.sendData(s.data(), s.size(), timeout, true);
}
}
return EXIT_SUCCESS;
}
}