Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "http_server.h"
- #include <boost/beast/core.hpp>
- void fail(boost::system::error_code ec, const char *what)
- {
- std::cerr << what << ": " << ec.message() << "\n";
- }
- boost::beast::string_view mime_type(boost::beast::string_view path)
- {
- using boost::beast::iequals;
- const auto ext = [&path]
- {
- const auto pos = path.rfind(".");
- if (pos == boost::beast::string_view::npos)
- {
- return boost::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";
- }
- std::string path_cat(boost::beast::string_view base, boost::beast::string_view path)
- {
- if (base.empty())
- {
- return path.to_string();
- }
- std::string result = base.to_string();
- #if BOOST_MSVC
- constexpr char 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;
- }
- void session::on_read(boost::system::error_code ec, std::size_t bytes_transferred)
- {
- boost::ignore_unused(bytes_transferred);
- // This means they closed the connection
- if (ec == http::error::end_of_stream)
- {
- return do_close();
- }
- if (ec)
- {
- return fail(ec, "read");
- }
- // Send the response
- handle_request(doc_root_, std::move(req_), lambda_);
- }
- void session::on_write(boost::system::error_code ec, std::size_t bytes_transferred, bool close)
- {
- boost::ignore_unused(bytes_transferred);
- if (ec)
- {
- return fail(ec, "write");
- }
- if (close)
- {
- // This means we should close the connection, usually because
- // the response indicated the "Connection: close" semantic.
- return do_close();
- }
- // We're done with the response so delete it
- res_ = nullptr;
- // Read another request
- do_read();
- }
- listener::listener(boost::asio::io_context& ioc, tcp::endpoint endpoint, const std::string &doc_root)
- : acceptor_{ ioc }
- , socket_{ ioc }
- , doc_root_{ doc_root }
- {
- boost::system::error_code ec;
- // Open the acceptor
- acceptor_.open(endpoint.protocol(), ec);
- if (ec)
- {
- fail(ec, "open");
- return;
- }
- // Bind to the server address
- acceptor_.bind(endpoint, ec);
- if (ec)
- {
- fail(ec, "bind");
- return;
- }
- // Start listening for connections
- acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
- if (ec)
- {
- fail(ec, "listen");
- return;
- }
- }
- void listener::on_accept(boost::system::error_code ec)
- {
- if (ec)
- {
- fail(ec, "accept");
- }
- else
- {
- // Create the session and run it
- std::make_shared<session>(std::move(socket_), doc_root_)->run();
- }
- // Accept another connection
- do_accept();
- }
Add Comment
Please, Sign In to add comment