forked from fkie/async_web_server_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_connection.cpp
More file actions
158 lines (143 loc) · 4.41 KB
/
http_connection.cpp
File metadata and controls
158 lines (143 loc) · 4.41 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
#include "async_web_server_cpp/http_reply.hpp"
#include <boost/bind.hpp>
#include <boost/make_shared.hpp>
namespace async_web_server_cpp
{
HttpConnection::HttpConnection(boost::asio::io_context& io_service,
HttpServerRequestHandler handler)
: strand_(io_service), socket_(io_service), request_handler_(handler),
write_in_progress_(false)
{
}
boost::asio::ip::tcp::socket& HttpConnection::socket()
{
return socket_;
}
void HttpConnection::start()
{
async_read(
boost::bind(&HttpConnection::handle_read, shared_from_this(), _1, _2));
}
void HttpConnection::handle_read(const char* begin, const char* end)
{
boost::tribool result;
const char* parse_end;
boost::tie(result, parse_end) = request_parser_.parse(request_, begin, end);
if (result)
{
request_.parse_uri();
try
{
request_handler_(request_, shared_from_this(), parse_end, end);
}
catch (...)
{
// error constructing request
// just kill the connection as the handler may have already started
// writing stuff out
}
}
else if (!result)
{
HttpReply::stock_reply(HttpReply::bad_request)(
request_, shared_from_this(), begin, end);
}
else
{
async_read(boost::bind(&HttpConnection::handle_read, shared_from_this(),
_1, _2));
}
}
void HttpConnection::handle_read_raw(ReadHandler callback,
const boost::system::error_code& e,
std::size_t bytes_transferred)
{
if (!e)
{
callback(buffer_.data(), buffer_.data() + bytes_transferred);
}
else
{
last_error_ = e;
}
}
void HttpConnection::async_read(ReadHandler callback)
{
if (last_error_)
{
boost::throw_exception(boost::system::system_error(last_error_));
}
socket_.async_read_some(
boost::asio::buffer(buffer_),
strand_.wrap(
boost::bind(&HttpConnection::handle_read_raw, shared_from_this(),
callback, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
void HttpConnection::write_and_clear(std::vector<unsigned char>& data)
{
std::shared_ptr<std::vector<unsigned char>> buffer(
new std::vector<unsigned char>());
buffer->swap(data);
write(boost::asio::buffer(*buffer), buffer);
}
void HttpConnection::write(const std::string& content)
{
std::shared_ptr<std::string> str(new std::string(content));
write(boost::asio::buffer(*str), str);
}
void HttpConnection::write(const boost::asio::const_buffer& buffer,
ResourcePtr resource)
{
boost::mutex::scoped_lock lock(write_mutex_);
pending_write_buffers_.push_back(buffer);
if (resource)
pending_write_resources_.push_back(resource);
if (!write_in_progress_)
write_pending();
}
void HttpConnection::write(
const std::vector<boost::asio::const_buffer>& buffers, ResourcePtr resource)
{
boost::mutex::scoped_lock lock(write_mutex_);
pending_write_buffers_.insert(pending_write_buffers_.end(), buffers.begin(),
buffers.end());
if (resource)
pending_write_resources_.push_back(resource);
if (!write_in_progress_)
write_pending();
}
// Must be called while holding write lock
void HttpConnection::write_pending()
{
if (last_error_)
{
boost::throw_exception(boost::system::system_error(last_error_));
}
write_in_progress_ = true;
boost::asio::async_write(socket_, pending_write_buffers_,
boost::bind(&HttpConnection::handle_write,
shared_from_this(),
boost::asio::placeholders::error,
pending_write_resources_));
pending_write_buffers_.clear();
pending_write_resources_.clear();
}
void HttpConnection::handle_write(const boost::system::error_code& e,
std::vector<ResourcePtr> resources)
{
boost::mutex::scoped_lock lock(write_mutex_);
write_in_progress_ = false;
if (!e)
{
if (!pending_write_buffers_.empty())
{
write_pending();
}
}
else
{
last_error_ = e;
}
}
} // namespace async_web_server_cpp