forked from jgarzik/rpcsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.cc
213 lines (184 loc) · 5.85 KB
/
connection.cc
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// connection.cpp
// ~~~~~~~~~~~~~~
//
// Copyright 2012 Red Hat, Inc.
// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <stdio.h>
#include <sstream>
#include <vector>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "connection.h"
#include "request_handler.h"
namespace http {
namespace server3 {
std::string FormatTime(std::string fmt, boost::posix_time::ptime now)
{
static std::locale loc(std::cout.getloc(),
new boost::posix_time::time_facet(fmt.c_str()));
std::stringstream ss;
ss.imbue(loc);
ss << now;
return ss.str();
}
void base_connection::log_request()
{
using namespace boost::posix_time;
using namespace boost::gregorian;
std::string addrstr = peer.address().to_string();
std::string timestr = FormatTime("%d/%b/%Y:%H:%M:%S", request_.tstamp);
printf("%s - - [%s -0000] \"%s %s HTTP/%d.%d\" %d %lu\n",
addrstr.c_str(),
timestr.c_str(),
request_.method.c_str(),
request_.uri.c_str(),
request_.http_version_major,
request_.http_version_minor,
reply_.status,
reply_.content.size());
}
boost::asio::ip::tcp::socket& connection::socket()
{
return socket_;
}
void connection::start()
{
read_more();
}
void connection::read_more()
{
socket_.async_read_some(boost::asio::buffer(buffer_),
strand_.wrap(
boost::bind(&connection::handle_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
void connection::handle_read(const boost::system::error_code& e,
std::size_t bytes_transferred)
{
if (e) {
// If an error occurs then no new asynchronous operations are started. This
// means that all shared_ptr references to the connection object will
// disappear and the object will be destroyed automatically after this
// handler returns. The connection class's destructor closes the socket.
return;
}
boost::tribool result;
boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
request_, buffer_.data(), buffer_.data() + bytes_transferred);
if (result) {
keepalive_ = request_.want_keepalive();
request_.tstamp =
boost::posix_time::second_clock::universal_time();
request_handler_.handle_request(request_, reply_, keepalive_);
boost::asio::async_write(socket_, reply_.to_buffers(),
strand_.wrap(
boost::bind(&connection::handle_write, shared_from_this(),
boost::asio::placeholders::error)));
log_request();
} else if (!result) {
reply_ = reply::stock_reply(reply::bad_request);
boost::asio::async_write(socket_, reply_.to_buffers(),
strand_.wrap(
boost::bind(&connection::handle_write, shared_from_this(),
boost::asio::placeholders::error)));
} else {
read_more();
}
}
void connection::handle_write(const boost::system::error_code& e)
{
if (e) {
// No new asynchronous operations are started. This means that all shared_ptr
// references to the connection object will disappear and the object will be
// destroyed automatically after this handler returns. The connection class's
// destructor closes the socket.
return;
}
if (keepalive_) {
reset();
read_more();
} else {
// Initiate graceful connection closure.
boost::system::error_code ignored_ec;
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
}
}
void ssl_connection::start()
{
socket_.async_handshake(boost::asio::ssl::stream_base::server,
boost::bind(&ssl_connection::handle_handshake, shared_from_this(),
boost::asio::placeholders::error));
}
void ssl_connection::read_more()
{
socket_.async_read_some(boost::asio::buffer(buffer_),
strand_.wrap(
boost::bind(&ssl_connection::handle_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
void ssl_connection::handle_handshake(const boost::system::error_code& e)
{
if (!e)
read_more();
}
void ssl_connection::handle_read(const boost::system::error_code& e,
std::size_t bytes_transferred)
{
if (e) {
// If an error occurs then no new asynchronous operations are started. This
// means that all shared_ptr references to the connection object will
// disappear and the object will be destroyed automatically after this
// handler returns. The connection class's destructor closes the socket.
return;
}
boost::tribool result;
boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
request_, buffer_.data(), buffer_.data() + bytes_transferred);
if (result)
{
keepalive_ = request_.want_keepalive();
request_.tstamp =
boost::posix_time::second_clock::universal_time();
request_handler_.handle_request(request_, reply_, keepalive_);
boost::asio::async_write(socket_, reply_.to_buffers(),
strand_.wrap(
boost::bind(&ssl_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error)));
log_request();
} else if (!result) {
reply_ = reply::stock_reply(reply::bad_request);
boost::asio::async_write(socket_, reply_.to_buffers(),
strand_.wrap(
boost::bind(&ssl_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error)));
} else {
read_more();
}
}
void ssl_connection::handle_write(const boost::system::error_code& e)
{
if (e) {
// No new asynchronous operations are started. This means that all shared_ptr
// references to the connection object will disappear and the object will be
// destroyed automatically after this handler returns. The connection class's
// destructor closes the socket.
return;
}
if (keepalive_) {
reset();
read_more();
} else {
// Initiate graceful connection closure.
boost::system::error_code ignored_ec;
socket_.shutdown(ignored_ec);
}
}
} // namespace server3
} // namespace http