// // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail 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) // // Official repository: https://github.com/boostorg/beast // //------------------------------------------------------------------------------ // // Example: HTTP SSL server, synchronous // //------------------------------------------------------------------------------ //#include "example/common/server_certificate.hpp" #include #include #include #include #include #include #include #include #include #include #include #include namespace beast = boost::beast; // from namespace http = beast::http; // from namespace net = boost::asio; // from namespace ssl = boost::asio::ssl; // from using tcp = boost::asio::ip::tcp; // from // Return a reasonable mime type based on the extension of a file. beast::string_view mime_type(beast::string_view path) { using beast::iequals; auto const ext = [&path] { auto const pos = path.rfind("."); if(pos == beast::string_view::npos) return beast::string_view{}; return path.substr(pos); }(); if(iequals(ext, ".htm")) return "text/html"; if(iequals(ext, ".html")) return "text/html"; if(iequals(ext, ".php")) return "text/html"; if(iequals(ext, ".css")) return "text/css"; if(iequals(ext, ".txt")) return "text/plain"; if(iequals(ext, ".js")) return "application/javascript"; if(iequals(ext, ".json")) return "application/json"; if(iequals(ext, ".xml")) return "application/xml"; if(iequals(ext, ".swf")) return "application/x-shockwave-flash"; if(iequals(ext, ".flv")) return "video/x-flv"; if(iequals(ext, ".png")) return "image/png"; if(iequals(ext, ".jpe")) return "image/jpeg"; if(iequals(ext, ".jpeg")) return "image/jpeg"; if(iequals(ext, ".jpg")) return "image/jpeg"; if(iequals(ext, ".gif")) return "image/gif"; if(iequals(ext, ".bmp")) return "image/bmp"; if(iequals(ext, ".ico")) return "image/vnd.microsoft.icon"; if(iequals(ext, ".tiff")) return "image/tiff"; if(iequals(ext, ".tif")) return "image/tiff"; if(iequals(ext, ".svg")) return "image/svg+xml"; if(iequals(ext, ".svgz")) return "image/svg+xml"; return "application/text"; } // Append an HTTP rel-path to a local filesystem path. // The returned path is normalized for the platform. std::string path_cat( beast::string_view base, beast::string_view path) { if(base.empty()) return std::string(path); std::string result(base); #ifdef BOOST_MSVC char constexpr path_separator = '\\'; if(result.back() == path_separator) result.resize(result.size() - 1); result.append(path.data(), path.size()); for(auto& c : result) if(c == '/') c = path_separator; #else char constexpr path_separator = '/'; if(result.back() == path_separator) result.resize(result.size() - 1); result.append(path.data(), path.size()); #endif return result; } // This function produces an HTTP response for the given // request. The type of the response object depends on the // contents of the request, so the interface requires the // caller to pass a generic lambda for receiving the response. template< class Body, class Allocator, class Send> void handle_request( http::request>&& req, Send&& send, int payloadProcessStatus) { // Returns a bad request response auto const bad_request = [&req](beast::string_view why) { http::response res; res.version(11); res.result(http::status::bad_request); res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, "text/html"); res.keep_alive(req.keep_alive()); res.body() = std::string(why); res.prepare_payload(); return res; }; // Returns a server error response auto const server_error = [&req](beast::string_view what) { http::response res; res.version(11); res.result(http::status::internal_server_error); res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, "text/html"); res.keep_alive(req.keep_alive()); res.body() = "An error occurred: '" + std::string(what) + "'"; res.prepare_payload(); return res; }; // Make sure we can handle the method if( req.method() != http::verb::post ) return send(bad_request("Unknown HTTP-method")); if (payloadProcessStatus == -1) return send(server_error("Internal server error")); http::response res; res.version(11); res.result(http::status::ok); res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, "application/text"); res.keep_alive(req.keep_alive()); res.body() = "Collection request succeeded"; res.prepare_payload(); return send(std::move(res)); } //------------------------------------------------------------------------------ // Report a failure void fail(beast::error_code ec, char const* what) { std::cerr << what << ": " << ec.message() << "\n"; } // This is the C++11 equivalent of a generic lambda. // The function object is used to send an HTTP message. template struct send_lambda { Stream& stream_; bool& close_; beast::error_code& ec_; explicit send_lambda( Stream& stream, bool& close, beast::error_code& ec) : stream_(stream) , close_(close) , ec_(ec) { } template void operator()(http::message&& msg) const { // Determine if we should close the connection after close_ = msg.need_eof(); // We need the serializer here because the serializer requires // a non-const file_body, and the message oriented version of // http::write only works with const messages. http::serializer sr{msg}; http::write(stream_, sr, ec_); } }; // Handles an HTTP server connection void do_session( tcp::socket& socket, ssl::context& ctx, std::shared_ptr const& doc_root) { bool close = false; beast::error_code ec; // Construct the stream around the socket beast::ssl_stream stream{socket, ctx}; // Perform the SSL handshake stream.handshake(ssl::stream_base::server, ec); if(ec) return fail(ec, "handshake"); // This buffer is required to persist across reads beast::flat_buffer buffer; // This lambda is used to send messages send_lambda> lambda{stream, close, ec}; for(;;) { // Read a request http::request req; http::read(stream, buffer, req, ec); if(ec == http::error::end_of_stream) break; if(ec) return fail(ec, "read"); std::string payload = req.body(); std::cout<<"Payload : "< \n" << "Example:\n" << " http-server-sync-ssl 0.0.0.0 8080 .\n"; return EXIT_FAILURE; } auto const address = net::ip::make_address(argv[1]); auto const port = static_cast(std::atoi(argv[2])); auto const doc_root = std::make_shared(argv[3]); // The io_context is required for all I/O net::io_context ioc{1}; // The SSL context is required, and holds certificates ssl::context ctx{ssl::context::tlsv12}; ctx.load_verify_file("/tmp/ca.crt"); ctx.use_certificate_file("/tmp/server.crt", ssl::context::file_format::pem); ctx.use_rsa_private_key_file("/tmp/server.key", ssl::context::file_format::pem); // The acceptor receives incoming connections tcp::acceptor acceptor{ioc, {address, port}}; for(;;) { // This will receive the new connection tcp::socket socket{ioc}; // Block until we get a connection acceptor.accept(socket); // Launch the session, transferring ownership of the socket std::thread{std::bind( &do_session, std::move(socket), std::ref(ctx), doc_root)}.detach(); } } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; return EXIT_FAILURE; } }