forked from BoostGSoC14/boost.http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspawn-with-runtime4.cpp
More file actions
165 lines (140 loc) · 5.86 KB
/
Copy pathspawn-with-runtime4.cpp
File metadata and controls
165 lines (140 loc) · 5.86 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <algorithm>
#include <boost/utility/string_view.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/http/buffered_socket.hpp>
#include <boost/http/socket_adaptor.hpp>
#include <boost/http/algorithm.hpp>
using namespace std;
using namespace boost;
class connection: public std::enable_shared_from_this<connection>
{
public:
void operator()(asio::yield_context yield)
{
auto self = shared_from_this();
try {
while (self->socket.is_open()) {
cout << "--\n[" << self->counter
<< "] About to receive a new message" << endl;
self->socket.async_read_request(self->request, yield);
//self->request.body().clear(); // freeing not used resources
if (http::request_continue_required(self->request)) {
cout << '[' << self->counter
<< "] Continue required. About to send"
" \"100-continue\""
<< std::endl;
self->socket.async_write_response_continue(yield);
}
while (self->socket.read_state()
!= http::read_state::finished) {
cout << '[' << self->counter
<< "] Message not fully received" << endl
<< '[' << self->counter
<< "] About to receive some data" << endl;
http::request_response_wrapper<http::request,
http::response>
wreq(self->request);
self->socket.async_read_some(wreq, yield);
}
//cout << "BODY:==";
//for (const auto &e: self->request.body()) {
// cout << char(e);
//}
//cout << "==" << endl;
cout << '[' << self->counter << "] Message received. State = "
<< int(self->socket.read_state()) << endl;
cout << '[' << self->counter << "] Method: "
<< self->request.method() << endl;
cout << '[' << self->counter << "] Path: "
<< self->request.target() << endl;
{
auto host = self->request.headers().find("host");
if (host != self->request.headers().end())
cout << '[' << self->counter << "] Host header: "
<< host->second << endl;
}
std::cout << '[' << self->counter << "] Write state = "
<< int(self->socket.write_state()) << std::endl;
cout << '[' << self->counter << "] About to send a reply"
<< endl;
http::response reply;
reply.status_code() = 200;
reply.reason_phrase() = "OK";
//reply.headers().emplace("connection", "close");
const char body[] = "Hello World\n";
std::copy(body, body + sizeof(body) - 1,
std::back_inserter(reply.body()));
self->socket.async_write_response(reply, yield);
}
} catch (system::system_error &e) {
if (e.code() != system::error_code{asio::error::connection_reset}
&& e.code() != system::error_code{asio::error::broken_pipe}
&& e.code() != system::error_code{asio::error::eof}) {
cerr << '[' << self->counter << "] Aborting on exception: "
<< e.what() << endl;
std::exit(1);
}
cout << '[' << self->counter << "] Error: " << e.what() << endl;
} catch (std::exception &e) {
cerr << '[' << self->counter << "] Aborting on exception: "
<< e.what() << endl;
std::exit(1);
}
}
asio::ip::tcp::socket &tcp_layer()
{
return underlying_socket.next_layer();
}
static std::shared_ptr<connection> make_connection(asio::io_context &ios,
int counter)
{
return std::shared_ptr<connection>{new connection{ios, counter}};
}
private:
connection(asio::io_context &ios, int counter)
: underlying_socket(ios)
, adaptor(underlying_socket)
, socket(adaptor)
, counter(counter)
{}
http::buffered_socket underlying_socket;
http::socket_adaptor<std::reference_wrapper<http::buffered_socket>> adaptor;
// just to ensure we're not using non-virtual methods
http::poly_socket &socket;
int counter;
http::request request;
};
int main()
{
asio::io_context ios;
asio::ip::tcp::acceptor acceptor(ios,
asio::ip::tcp
::endpoint(asio::ip::tcp::v6(), 8080));
auto work = [&acceptor](asio::yield_context yield) {
int counter = 0;
for ( ; true ; ++counter ) {
try {
auto connection = connection::make_connection(
acceptor.get_executor().context(), counter
);
cout << "About to accept a new connection" << endl;
acceptor.async_accept(connection->tcp_layer(), yield);
auto handle_connection
= [connection](asio::yield_context yield) mutable {
(*connection)(yield);
};
spawn(acceptor.get_executor(), handle_connection);
} catch (std::exception &e) {
cerr << "Aborting on exception: " << e.what() << endl;
std::exit(1);
}
}
};
cout << "About to spawn" << endl;
spawn(ios, work);
cout << "About to run" << endl;
ios.run();
return 0;
}