2011-01-09 16:42:27 -05:00
|
|
|
/*
|
2013-10-14 07:42:28 -04:00
|
|
|
|
2015-02-19 03:19:51 -05:00
|
|
|
Copyright (c) 2015, Project OSRM contributors
|
2013-10-14 07:42:28 -04:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2015-01-27 06:35:29 -05:00
|
|
|
#ifndef CONNECTION_HPP
|
|
|
|
#define CONNECTION_HPP
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2015-01-27 05:45:33 -05:00
|
|
|
#include "http/compression_type.hpp"
|
|
|
|
#include "http/reply.hpp"
|
|
|
|
#include "http/request.hpp"
|
2015-02-12 03:01:46 -05:00
|
|
|
#include "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
|