proxyServerTest/http/connection.hpp

115 lines
2.6 KiB
C++
Raw Normal View History

2018-08-25 18:23:54 +00:00
//
// connection.hpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2018 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)
//
#ifndef HTTP_CONNECTION_HPP
#define HTTP_CONNECTION_HPP
#include <array>
#include <memory>
#include <boost/asio.hpp>
#include "reply.hpp"
#include "request.hpp"
#include "request_handler.hpp"
#include "request_parser.hpp"
2018-08-25 19:32:06 +00:00
#ifndef SSL_R_SHORT_READ
#define SSL_R_SHORT_READ 219
#endif
#include "ssl/ssl_locl.h"
#include <boost/asio/ssl.hpp>
#if defined(close)
#undef close
#endif
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
2018-08-25 18:23:54 +00:00
namespace http {
namespace server {
class connection_manager;
/// Represents a single connection from a client.
class connection
: public std::enable_shared_from_this<connection>
{
public:
connection(const connection&) = delete;
connection& operator=(const connection&) = delete;
/// Construct a connection with the given socket.
2018-08-25 19:32:06 +00:00
explicit connection(std::shared_ptr<ssl_socket> newSocketPtr,
2018-08-25 18:23:54 +00:00
connection_manager& manager, request_handler& handler, boost::asio::io_context& io_context);
/// Start the first asynchronous operation for the connection.
void start();
/// Stop all asynchronous operations associated with the connection.
void stop();
private:
2018-08-25 19:32:06 +00:00
void doHandshake();
2018-08-25 18:23:54 +00:00
/// Perform an asynchronous read operation.
void do_read();
/// Perform an asynchronous write operation.
void do_write();
/// Socket for the connection.
2018-08-25 19:32:06 +00:00
//boost::asio::ip::tcp::socket socket_;
std::shared_ptr<ssl_socket> socketPtr;
2018-08-25 18:23:54 +00:00
/// The manager for this connection.
connection_manager& connection_manager_;
/// The handler used to process the incoming request.
request_handler& request_handler_;
/// Buffer for incoming data.
std::array<char, 8192> buffer_;
/// The incoming request.
request request_;
/// The parser for the incoming request.
request_parser request_parser_;
/// The reply to be sent back to the client.
reply reply_;
boost::asio::io_context& io_context_;
boost::asio::ip::tcp::socket outsideSocket_;
std::array<unsigned char, 8192> forwardBuffer;
std::array<unsigned char, 8192> backwardBuffer;
void do_try_connect_remote(std::string host, std::string port);
void transferDataForward();
void transferDataBackward();
};
typedef std::shared_ptr<connection> connection_ptr;
} // namespace server
} // namespace http
#endif // HTTP_CONNECTION_HPP