Added https support

This commit is contained in:
vladislav.khorev committed 2018-08-25 22:32:06 +03:00
1 parent 0a14ec2fff
commit 04945291c1
9 files changed
+115 -810

No files matched your search

+38 -11
View File
@@ -38,9 +38,9 @@ public:
namespace http {
namespace server {
connection::connection(boost::asio::ip::tcp::socket socket,
connection::connection(std::shared_ptr<ssl_socket> newSocketPtr,
connection_manager& manager, request_handler& handler, boost::asio::io_context& io_context)
: socket_(std::move(socket)),
: socketPtr(newSocketPtr),
connection_manager_(manager),
request_handler_(handler),
io_context_(io_context),
@@ -50,18 +50,45 @@ connection::connection(boost::asio::ip::tcp::socket socket,
void connection::start()
{
do_read();
doHandshake();
}
void connection::stop()
{
socket_.close();
socketPtr->lowest_layer().close();
//socketPtr.reset();
}
void connection::doHandshake()
{
std::cout << "ProxySession::asyncHandshake" << std::endl;
auto self(shared_from_this());
socketPtr->async_handshake(boost::asio::ssl::stream_base::server,
[this, self](boost::system::error_code ec) {
std::cout << "ProxySession::asyncHandshake inner" << std::endl;
if (!ec)
{
std::cout << "ProxySession::asyncHandshake inner2" << std::endl;
do_read();
}
else
{
if (ec != boost::asio::error::operation_aborted)
{
connection_manager_.stop(shared_from_this());
}
}
});
}
void connection::do_read()
{
auto self(shared_from_this());
socket_.async_read_some(boost::asio::buffer(buffer_),
socketPtr->async_read_some(boost::asio::buffer(buffer_),
[this, self](boost::system::error_code ec, std::size_t bytes_transferred)
{
if (!ec)
@@ -137,15 +164,15 @@ void connection::do_read()
void connection::do_write()
{
auto self(shared_from_this());
boost::asio::async_write(socket_, reply_.to_buffers(),
boost::asio::async_write((*socketPtr), reply_.to_buffers(),
[this, self](boost::system::error_code ec, std::size_t)
{
if (!ec)
{
// Initiate graceful connection closure.
boost::system::error_code ignored_ec;
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both,
ignored_ec);
socketPtr->shutdown(ignored_ec);
}
if (ec != boost::asio::error::operation_aborted)
@@ -172,7 +199,7 @@ void connection::do_try_connect_remote(std::string host, std::string port)
if (!ec)
{
auto self(shared_from_this());
boost::asio::async_write(socket_, boost::asio::buffer(okReply),
boost::asio::async_write((*socketPtr), boost::asio::buffer(okReply),
[this, self](boost::system::error_code ec, std::size_t)
{
if (!ec)
@@ -208,7 +235,7 @@ void connection::transferDataForward()
{
auto self(shared_from_this());
socket_.async_read_some(boost::asio::buffer(forwardBuffer),
socketPtr->async_read_some(boost::asio::buffer(forwardBuffer),
[this, self](boost::system::error_code ec, std::size_t length)
{
if (!ec)
@@ -283,7 +310,7 @@ void connection::transferDataBackward()
std::cout << boost::lexical_cast<std::string>(int(backwardBuffer[length - 1])) << "\n";
boost::asio::async_write(socket_,
boost::asio::async_write((*socketPtr),
boost::asio::buffer(*data),
[this, self, data](boost::system::error_code ec, std::size_t length)
{
+23 -2
View File
@@ -19,6 +19,23 @@
#include "request_handler.hpp"
#include "request_parser.hpp"
#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;
namespace http {
namespace server {
@@ -33,7 +50,7 @@ public:
connection& operator=(const connection&) = delete;
/// Construct a connection with the given socket.
explicit connection(boost::asio::ip::tcp::socket socket,
explicit connection(std::shared_ptr<ssl_socket> newSocketPtr,
connection_manager& manager, request_handler& handler, boost::asio::io_context& io_context);
/// Start the first asynchronous operation for the connection.
@@ -43,6 +60,9 @@ public:
void stop();
private:
void doHandshake();
/// Perform an asynchronous read operation.
void do_read();
@@ -50,7 +70,8 @@ private:
void do_write();
/// Socket for the connection.
boost::asio::ip::tcp::socket socket_;
//boost::asio::ip::tcp::socket socket_;
std::shared_ptr<ssl_socket> socketPtr;
/// The manager for this connection.
connection_manager& connection_manager_;
-43
View File
@@ -1,43 +0,0 @@
//
// main.cpp
// ~~~~~~~~
//
// 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)
//
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include "server.hpp"
int main_http(int argc, char* argv[])
{
try
{
// Check command line arguments.
if (argc != 4)
{
std::cerr << "Usage: http_server <address> <port> <doc_root>\n";
std::cerr << " For IPv4, try:\n";
std::cerr << " receiver 0.0.0.0 80 .\n";
std::cerr << " For IPv6, try:\n";
std::cerr << " receiver 0::0 80 .\n";
return 1;
}
// Initialise the server.
http::server::server s(argv[1], argv[2], argv[3]);
// Run the server until stopped.
s.run();
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << "\n";
}
return 0;
}
+10 -5
View File
@@ -16,12 +16,14 @@ namespace http {
namespace server {
server::server(const std::string& address, const std::string& port,
const std::string& doc_root)
const std::string& doc_root, boost::asio::ssl::context& inSslContext)
: io_context_(1),
signals_(io_context_),
acceptor_(io_context_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 8043)),
connection_manager_(),
request_handler_(doc_root)
request_handler_(doc_root),
sslContext(inSslContext),
tempSocketPtr(std::make_shared<ssl_socket>(io_context_, inSslContext))
{
// Register to handle the signals that indicate when the server should exit.
// It is safe to register for the same signal multiple times in a program,
@@ -48,8 +50,9 @@ void server::run()
void server::do_accept()
{
acceptor_.async_accept(
[this](boost::system::error_code ec, boost::asio::ip::tcp::socket socket)
auto newSocketPtr = tempSocketPtr;
acceptor_.async_accept(tempSocketPtr->lowest_layer(),
[newSocketPtr, this](boost::system::error_code ec)
{
// Check whether the server was stopped by a signal before this
// completion handler had a chance to run.
@@ -61,9 +64,11 @@ void server::do_accept()
if (!ec)
{
connection_manager_.start(std::make_shared<connection>(
std::move(socket), connection_manager_, request_handler_, io_context_));
newSocketPtr, connection_manager_, request_handler_, io_context_));
}
this->tempSocketPtr = std::make_shared<ssl_socket>(io_context_, sslContext);
do_accept();
});
}
+20 -1
View File
@@ -17,6 +17,19 @@
#include "connection_manager.hpp"
#include "request_handler.hpp"
#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
namespace http {
namespace server {
@@ -30,7 +43,7 @@ public:
/// Construct the server to listen on the specified TCP address and port, and
/// serve up files from the given directory.
explicit server(const std::string& address, const std::string& port,
const std::string& doc_root);
const std::string& doc_root, boost::asio::ssl::context& inSslContext);
/// Run the server's io_context loop.
void run();
@@ -56,6 +69,12 @@ private:
/// The handler for all incoming requests.
request_handler request_handler_;
boost::asio::ssl::context& sslContext;
std::shared_ptr<ssl_socket> tempSocketPtr;
};
} // namespace server