http_connection.cpp
Go to the documentation of this file.
2 
3 #include <boost/bind.hpp>
4 #include <boost/make_shared.hpp>
5 
6 namespace async_web_server_cpp
7 {
8 
9 HttpConnection::HttpConnection(boost::asio::io_service& io_service,
11  : strand_(io_service), socket_(io_service), request_handler_(handler),
12  write_in_progress_(false)
13 {
14 }
15 
16 boost::asio::ip::tcp::socket& HttpConnection::socket()
17 {
18  return socket_;
19 }
20 
22 {
23  async_read(
24  boost::bind(&HttpConnection::handle_read, shared_from_this(), _1, _2));
25 }
26 
27 void HttpConnection::handle_read(const char* begin, const char* end)
28 {
29  boost::tribool result;
30  const char* parse_end;
31  boost::tie(result, parse_end) = request_parser_.parse(request_, begin, end);
32 
33  if (result)
34  {
36  try
37  {
38  request_handler_(request_, shared_from_this(), parse_end, end);
39  }
40  catch (...)
41  {
42  // error constructing request
43  // just kill the connection as the handler may have already started
44  // writing stuff out
45  }
46  }
47  else if (!result)
48  {
50  request_, shared_from_this(), begin, end);
51  }
52  else
53  {
54  async_read(boost::bind(&HttpConnection::handle_read, shared_from_this(),
55  _1, _2));
56  }
57 }
58 
60  const boost::system::error_code& e,
61  std::size_t bytes_transferred)
62 {
63  if (!e)
64  {
65  callback(buffer_.data(), buffer_.data() + bytes_transferred);
66  }
67  else
68  {
69  last_error_ = e;
70  }
71 }
73 {
74  if (last_error_)
75  {
76  boost::throw_exception(boost::system::system_error(last_error_));
77  }
78  socket_.async_read_some(
79  boost::asio::buffer(buffer_),
80  strand_.wrap(
81  boost::bind(&HttpConnection::handle_read_raw, shared_from_this(),
82  callback, boost::asio::placeholders::error,
83  boost::asio::placeholders::bytes_transferred)));
84 }
85 
86 void HttpConnection::write_and_clear(std::vector<unsigned char>& data)
87 {
88  boost::shared_ptr<std::vector<unsigned char>> buffer(
89  new std::vector<unsigned char>());
90  buffer->swap(data);
91  write(boost::asio::buffer(*buffer), buffer);
92 }
93 
94 void HttpConnection::write(const std::string& content)
95 {
96  boost::shared_ptr<std::string> str(new std::string(content));
97  write(boost::asio::buffer(*str), str);
98 }
99 
100 void HttpConnection::write(const boost::asio::const_buffer& buffer,
101  ResourcePtr resource)
102 {
103  boost::mutex::scoped_lock lock(write_mutex_);
104  pending_write_buffers_.push_back(buffer);
105  if (resource)
106  pending_write_resources_.push_back(resource);
107  if (!write_in_progress_)
108  write_pending();
109 }
110 
112  const std::vector<boost::asio::const_buffer>& buffers, ResourcePtr resource)
113 {
114  boost::mutex::scoped_lock lock(write_mutex_);
115  pending_write_buffers_.insert(pending_write_buffers_.end(), buffers.begin(),
116  buffers.end());
117  if (resource)
118  pending_write_resources_.push_back(resource);
119  if (!write_in_progress_)
120  write_pending();
121 }
122 
123 // Must be called while holding write lock
125 {
126  if (last_error_)
127  {
128  boost::throw_exception(boost::system::system_error(last_error_));
129  }
130  write_in_progress_ = true;
131  boost::asio::async_write(socket_, pending_write_buffers_,
132  boost::bind(&HttpConnection::handle_write,
133  shared_from_this(),
134  boost::asio::placeholders::error,
136  pending_write_buffers_.clear();
137  pending_write_resources_.clear();
138 }
139 
140 void HttpConnection::handle_write(const boost::system::error_code& e,
141  std::vector<ResourcePtr> resources)
142 {
143  boost::mutex::scoped_lock lock(write_mutex_);
144  write_in_progress_ = false;
145  if (!e)
146  {
147  if (!pending_write_buffers_.empty())
148  {
149  write_pending();
150  }
151  }
152  else
153  {
154  last_error_ = e;
155  }
156 }
157 
158 } // namespace async_web_server_cpp
async_web_server_cpp::HttpRequest::parse_uri
bool parse_uri()
Definition: http_request.cpp:12
async_web_server_cpp::HttpConnection::write_in_progress_
bool write_in_progress_
Definition: http_connection.hpp:89
async_web_server_cpp::HttpConnection::request_
HttpRequest request_
Definition: http_connection.hpp:85
async_web_server_cpp::HttpServerRequestHandler
boost::function< bool(const HttpRequest &, boost::shared_ptr< HttpConnection >, const char *begin, const char *end)> HttpServerRequestHandler
Definition: http_request_handler.hpp:12
async_web_server_cpp::HttpConnection::pending_write_resources_
std::vector< ResourcePtr > pending_write_resources_
Definition: http_connection.hpp:91
async_web_server_cpp::HttpConnection::handle_write
void handle_write(const boost::system::error_code &e, std::vector< ResourcePtr > resources)
Definition: http_connection.cpp:140
async_web_server_cpp::HttpConnection::strand_
boost::asio::io_service::strand strand_
Definition: http_connection.hpp:81
async_web_server_cpp::HttpConnection::buffer_
boost::array< char, 8192 > buffer_
Definition: http_connection.hpp:84
async_web_server_cpp::HttpConnection::ReadHandler
boost::function< void(const char *begin, const char *end)> ReadHandler
Definition: http_connection.hpp:39
async_web_server_cpp::HttpConnection::request_handler_
HttpServerRequestHandler request_handler_
Definition: http_connection.hpp:83
async_web_server_cpp::HttpConnection::write_and_clear
void write_and_clear(std::vector< unsigned char > &data)
Definition: http_connection.cpp:86
async_web_server_cpp::HttpConnection::handle_read_raw
void handle_read_raw(ReadHandler callback, const boost::system::error_code &e, std::size_t bytes_transferred)
Definition: http_connection.cpp:59
async_web_server_cpp
Definition: http_connection.hpp:14
async_web_server_cpp::HttpReply::stock_reply
static HttpServerRequestHandler stock_reply(status_type status)
Definition: http_reply.cpp:410
async_web_server_cpp::HttpConnection::handle_read
void handle_read(const char *begin, const char *end)
Definition: http_connection.cpp:27
async_web_server_cpp::HttpConnection::ResourcePtr
boost::shared_ptr< const void > ResourcePtr
Definition: http_connection.hpp:40
async_web_server_cpp::HttpConnection::socket_
boost::asio::ip::tcp::socket socket_
Definition: http_connection.hpp:82
async_web_server_cpp::HttpConnection::async_read
void async_read(ReadHandler callback)
Definition: http_connection.cpp:72
async_web_server_cpp::HttpConnection::write_mutex_
boost::mutex write_mutex_
Definition: http_connection.hpp:88
async_web_server_cpp::HttpConnection::request_parser_
HttpRequestParser request_parser_
Definition: http_connection.hpp:86
async_web_server_cpp::HttpConnection::write_pending
void write_pending()
Definition: http_connection.cpp:124
async_web_server_cpp::HttpConnection::start
void start()
Definition: http_connection.cpp:21
async_web_server_cpp::HttpConnection::HttpConnection
HttpConnection(boost::asio::io_service &io_service, HttpServerRequestHandler request_handler)
Definition: http_connection.cpp:9
async_web_server_cpp::HttpRequestParser::parse
boost::tuple< boost::tribool, InputIterator > parse(HttpRequest &req, InputIterator begin, InputIterator end)
Definition: http_request_parser.hpp:37
http_reply.hpp
async_web_server_cpp::HttpConnection::write
void write(const std::string &)
Definition: http_connection.cpp:94
async_web_server_cpp::HttpConnection::pending_write_buffers_
std::vector< boost::asio::const_buffer > pending_write_buffers_
Definition: http_connection.hpp:90
async_web_server_cpp::HttpConnection::last_error_
boost::system::error_code last_error_
Definition: http_connection.hpp:92
async_web_server_cpp::HttpReply::bad_request
@ bad_request
Definition: http_reply.hpp:35
async_web_server_cpp::HttpConnection::socket
boost::asio::ip::tcp::socket & socket()
Definition: http_connection.cpp:16


async_web_server_cpp
Author(s): Mitchell Wills , Russel Toris
autogenerated on Tue Mar 1 2022 23:49:06