-
Notifications
You must be signed in to change notification settings - Fork 34
/
simple_echo_server.cpp
82 lines (72 loc) · 2.28 KB
/
simple_echo_server.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
/*
* Part of HTTPP.
*
* Distributed under the 2-clause BSD licence (See LICENCE.TXT file at the
* project root).
*
* Copyright (c) 2014 Thomas Sanchez. All rights reserved.
*
*/
#include <chrono>
#include <iostream>
#include <string>
#include <httpp/HttpServer.hpp>
#include <httpp/http/Connection.hpp>
#include <httpp/http/Utils.hpp>
#include <httpp/utils/Exception.hpp>
using HTTPP::HttpServer;
using HTTPP::HTTP::Connection;
using HTTPP::HTTP::HttpCode;
using HTTPP::HTTP::Request;
void handler(Connection* connection)
{
read_whole_request(
connection,
[](std::unique_ptr<HTTPP::HTTP::helper::ReadWholeRequest> hndl,
const boost::system::error_code& ec)
{
const auto& body = hndl->body;
const auto& connection = hndl->connection;
const auto& request = connection->request();
if (ec)
{
throw HTTPP::UTILS::convert_boost_ec_to_std_ec(ec);
}
std::ostringstream out;
out << request;
if (body.empty())
{
connection->response()
.setCode(HttpCode::Ok)
.setBody("request received: " + out.str());
}
else
{
connection->response()
.setCode(HttpCode::Ok)
.setBody(
"request received entirely: " + out.str()
+ ", body size: " + std::to_string(body.size())
);
}
HTTPP::HTTP::setShouldConnectionBeClosed(request, connection->response());
connection->sendResponse(); // connection pointer may become invalid
auto end = Request::Clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(
end - request.received
);
std::cout << "Request handled in: " << elapsed.count() << "us" << std::endl;
}
);
}
int main(int, char**)
{
HttpServer server;
server.start();
server.setSink(&handler);
server.bind("localhost", "8080");
server.bind("localhost", "8081");
server.bind("localhost", "8082");
while (true)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}