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
74 lines (55 loc) · 1.64 KB
/
Test.cpp
File metadata and controls
74 lines (55 loc) · 1.64 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
#include "Test.h"
bool test(HttpServer::ServerRequest &request, HttpServer::ServerResponse &response)
{
const std::unordered_map<std::string, std::string> &incoming_headers = request.headers;
const std::unordered_multimap<std::string, std::string> &incoming_data = request.data;
HttpServer::Socket &socket = response.socket;
std::map<std::string, std::string> &headers_outgoing = response.headers;
std::string s;
for (auto h = incoming_headers.cbegin(); h != incoming_headers.cend(); ++h)
{
s += h->first + ":" + h->second + "\n";
}
s += "\n";
for (auto v = incoming_data.cbegin(); v != incoming_data.cend(); ++v)
{
s += v->first + ": " + v->second + "\n";
}
/* s = "\
<table width=\"100%\">\
<tr>\
<td>1<td>\
<td>2<td>\
</tr>\
<tr>\
<td>3<td>\
<td>4<td>\
</tr>\
</table>";*/
headers_outgoing[""] = "HTTP/1.1 200 OK";
headers_outgoing["Content-Type"] = "text/plain; charset=utf-8";
// headers_outgoing["Access-Control-Allow-Origin"] = "*";
// headers_outgoing["Content-Type"] = "text/html; charset=utf-8";
headers_outgoing["Content-Length"] = std::to_string(s.length() );
headers_outgoing["Connection"] = "keep-alive";
headers_outgoing["Date"] = Utils::getDatetimeStringValue();
std::string headers;
for (auto h = headers_outgoing.cbegin(); h != headers_outgoing.cend(); ++h)
{
if (h->first.length() )
{
headers += h->first + ": ";
}
headers += h->second + "\r\n";
}
headers += "\r\n";
headers_outgoing.clear();
// s = headers + s;
std::chrono::milliseconds timeout(5000);
socket.nonblock_send(headers, timeout);
if ( (size_t)~0 == socket.nonblock_send(s, timeout) )
{
socket.close();
}
return EXIT_SUCCESS;
}