2015-01-27 06:35:29 -05:00
|
|
|
#ifndef CONNECTION_HPP
|
|
|
|
#define CONNECTION_HPP
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "server/http/compression_type.hpp"
|
|
|
|
#include "server/http/reply.hpp"
|
|
|
|
#include "server/http/request.hpp"
|
|
|
|
#include "server/request_parser.hpp"
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2015-01-26 11:37:37 -05:00
|
|
|
#include <boost/array.hpp>
|
2013-09-19 05:54:24 -04:00
|
|
|
#include <boost/asio.hpp>
|
2014-05-11 10:51:14 -04:00
|
|
|
#include <boost/config.hpp>
|
|
|
|
#include <boost/version.hpp>
|
|
|
|
|
2015-01-23 12:53:37 -05:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
2014-05-11 10:51:14 -04:00
|
|
|
|
2015-01-23 12:53:37 -05:00
|
|
|
// workaround for incomplete std::shared_ptr compatibility in old boost versions
|
2014-05-11 10:51:14 -04:00
|
|
|
#if BOOST_VERSION < 105300 || defined BOOST_NO_CXX11_SMART_PTR
|
|
|
|
|
2015-01-23 12:53:37 -05:00
|
|
|
namespace boost
|
2014-05-11 10:51:14 -04:00
|
|
|
{
|
2015-01-23 12:53:37 -05:00
|
|
|
template <class T> const T *get_pointer(std::shared_ptr<T> const &p) { return p.get(); }
|
2014-05-11 10:51:14 -04:00
|
|
|
|
2015-01-23 12:53:37 -05:00
|
|
|
template <class T> T *get_pointer(std::shared_ptr<T> &p) { return p.get(); }
|
2014-05-11 10:51:14 -04:00
|
|
|
} // namespace boost
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-12-16 05:29:38 -05:00
|
|
|
class RequestHandler;
|
|
|
|
|
2014-05-07 11:08:30 -04:00
|
|
|
namespace http
|
|
|
|
{
|
2011-01-09 16:42:27 -05:00
|
|
|
|
|
|
|
/// Represents a single connection from a client.
|
2014-05-09 11:04:55 -04:00
|
|
|
class Connection : public std::enable_shared_from_this<Connection>
|
2014-05-07 11:06:28 -04:00
|
|
|
{
|
2014-05-07 11:08:30 -04:00
|
|
|
public:
|
|
|
|
explicit Connection(boost::asio::io_service &io_service, RequestHandler &handler);
|
|
|
|
Connection(const Connection &) = delete;
|
2014-06-12 07:46:07 -04:00
|
|
|
Connection() = delete;
|
2014-05-07 11:08:30 -04:00
|
|
|
|
|
|
|
boost::asio::ip::tcp::socket &socket();
|
|
|
|
|
|
|
|
/// Start the first asynchronous operation for the connection.
|
|
|
|
void start();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void handle_read(const boost::system::error_code &e, std::size_t bytes_transferred);
|
|
|
|
|
|
|
|
/// Handle completion of a write operation.
|
|
|
|
void handle_write(const boost::system::error_code &e);
|
|
|
|
|
2015-01-26 07:35:24 -05:00
|
|
|
std::vector<char> compress_buffers(const std::vector<char> &uncompressed_data,
|
2015-01-27 05:45:33 -05:00
|
|
|
const compression_type compression_type);
|
2014-05-07 11:08:30 -04:00
|
|
|
|
|
|
|
boost::asio::io_service::strand strand;
|
|
|
|
boost::asio::ip::tcp::socket TCP_socket;
|
|
|
|
RequestHandler &request_handler;
|
2015-02-12 03:01:46 -05:00
|
|
|
RequestParser request_parser;
|
2015-01-26 11:37:37 -05:00
|
|
|
boost::array<char, 8192> incoming_data_buffer;
|
2015-01-27 06:52:55 -05:00
|
|
|
request current_request;
|
|
|
|
reply current_reply;
|
Extend compressed output lifetime till the async write function finishes.
This extends the compressed output vector's lifetime, as we issue an
asynchronous write operation that only receives a non-owning buffer to
the compressed data.
When the compressed output vector then goes out of scope, its destructor
is called and the data gets (potentially) destroyed. If the asynchronous
write happens afterwards, it's accessing data that is no longer there.
This is the reason for race conditions --- well, for undefined behavior
in general, but it manifests in the routed _sometimes_ not responding at
all.
The fix works like this: keep the compressed output associated with a
connection. Connections inherit from `std::enable_shared_from_this` and
issues a `shared_from_this()` call, passing a `std::shared_ptr` to the
asynchronous write function, thus extending their lifetime.
Connecitons thus manage their lifetime by themselves, extending it when
needed (and of course via the `std::shared_pointers` pointing to it).
Buffer's non owning property, from the `async_write` documentation:
> One or more buffers containing the data to be written. Although
> the buffers object may be copied as necessary, ownership of the
> underlying memory blocks is retained by the caller, which must
> guarantee that they remain valid until the handler is called.
Reference:
- http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/reference/async_write/overload1.html
2015-09-15 11:59:39 -04:00
|
|
|
std::vector<char> compressed_output;
|
2011-01-09 16:42:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace http
|
|
|
|
|
2015-01-27 06:35:29 -05:00
|
|
|
#endif // CONNECTION_HPP
|