2016-09-21 14:11:06 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <future>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <thread>
|
2017-12-24 15:40:39 +00:00
|
|
|
#include <condition_variable>
|
2016-09-21 14:11:06 +00:00
|
|
|
|
|
|
|
#include "crow/settings.h"
|
|
|
|
#include "crow/logging.h"
|
|
|
|
#include "crow/utility.h"
|
|
|
|
#include "crow/routing.h"
|
|
|
|
#include "crow/middleware_context.h"
|
|
|
|
#include "crow/http_request.h"
|
|
|
|
#include "crow/http_server.h"
|
2020-10-21 13:06:49 +00:00
|
|
|
#include "crow/dumb_timer_queue.h"
|
2021-01-02 19:12:04 +00:00
|
|
|
#include "crow/compression.h"
|
2016-09-21 14:11:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef CROW_MSVC_WORKAROUND
|
|
|
|
#define CROW_ROUTE(app, url) app.route_dynamic(url)
|
|
|
|
#else
|
|
|
|
#define CROW_ROUTE(app, url) app.route<crow::black_magic::get_parameter_tag(url)>(url)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace crow
|
|
|
|
{
|
2021-03-31 17:30:32 +00:00
|
|
|
#ifdef CROW_MAIN
|
2020-10-21 13:06:49 +00:00
|
|
|
int detail::dumb_timer_queue::tick = 5;
|
2021-02-20 21:28:32 +00:00
|
|
|
#endif
|
|
|
|
|
2016-09-21 14:11:06 +00:00
|
|
|
#ifdef CROW_ENABLE_SSL
|
|
|
|
using ssl_context_t = boost::asio::ssl::context;
|
|
|
|
#endif
|
2020-10-22 01:13:57 +00:00
|
|
|
///The main server application
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Use `SimpleApp` or `App<Middleware1, Middleware2, etc...>`
|
2016-09-21 14:11:06 +00:00
|
|
|
template <typename ... Middlewares>
|
|
|
|
class Crow
|
|
|
|
{
|
|
|
|
public:
|
2020-10-22 01:13:57 +00:00
|
|
|
///This crow application
|
2016-09-21 14:11:06 +00:00
|
|
|
using self_t = Crow;
|
2020-10-22 01:13:57 +00:00
|
|
|
///The HTTP server
|
2016-09-21 14:11:06 +00:00
|
|
|
using server_t = Server<Crow, SocketAdaptor, Middlewares...>;
|
|
|
|
#ifdef CROW_ENABLE_SSL
|
2020-10-22 01:13:57 +00:00
|
|
|
///An HTTP server that runs on SSL with an SSLAdaptor
|
2016-09-21 14:11:06 +00:00
|
|
|
using ssl_server_t = Server<Crow, SSLAdaptor, Middlewares...>;
|
|
|
|
#endif
|
|
|
|
Crow()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:13:57 +00:00
|
|
|
///Process an Upgrade request
|
2020-10-21 13:06:49 +00:00
|
|
|
|
2020-10-22 01:13:57 +00:00
|
|
|
///
|
|
|
|
///Currently used to upgrrade an HTTP connection to a WebSocket connection
|
2020-10-21 13:06:49 +00:00
|
|
|
template <typename Adaptor>
|
2016-09-21 14:11:06 +00:00
|
|
|
void handle_upgrade(const request& req, response& res, Adaptor&& adaptor)
|
|
|
|
{
|
|
|
|
router_.handle_upgrade(req, res, adaptor);
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:06:49 +00:00
|
|
|
///Process the request and generate a response for it
|
2016-09-21 14:11:06 +00:00
|
|
|
void handle(const request& req, response& res)
|
|
|
|
{
|
|
|
|
router_.handle(req, res);
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:13:57 +00:00
|
|
|
///Create a dynamic route using a rule (**Use CROW_ROUTE instead**)
|
2016-09-21 14:11:06 +00:00
|
|
|
DynamicRule& route_dynamic(std::string&& rule)
|
|
|
|
{
|
|
|
|
return router_.new_rule_dynamic(std::move(rule));
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:13:57 +00:00
|
|
|
///Create a route using a rule (**Use CROW_ROUTE instead**)
|
2016-09-21 14:11:06 +00:00
|
|
|
template <uint64_t Tag>
|
|
|
|
auto route(std::string&& rule)
|
|
|
|
-> typename std::result_of<decltype(&Router::new_rule_tagged<Tag>)(Router, std::string&&)>::type
|
|
|
|
{
|
|
|
|
return router_.new_rule_tagged<Tag>(std::move(rule));
|
|
|
|
}
|
|
|
|
|
2020-12-19 07:15:57 +00:00
|
|
|
self_t& signal_clear()
|
|
|
|
{
|
|
|
|
signals_.clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
self_t& signal_add(int signal_number)
|
|
|
|
{
|
|
|
|
signals_.push_back(signal_number);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 17:56:12 +00:00
|
|
|
///Set the port that Crow will handle requests on
|
2016-09-21 14:11:06 +00:00
|
|
|
self_t& port(std::uint16_t port)
|
|
|
|
{
|
|
|
|
port_ = port;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:06:49 +00:00
|
|
|
///Set the connection timeout in seconds (default is 5)
|
|
|
|
self_t& timeout(std::uint8_t timeout)
|
|
|
|
{
|
|
|
|
detail::dumb_timer_queue::tick = timeout;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-12-03 01:15:05 +00:00
|
|
|
///Set the server name (default Crow/0.2)
|
|
|
|
self_t& server_name(std::string server_name)
|
|
|
|
{
|
|
|
|
server_name_ = server_name;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:06:49 +00:00
|
|
|
///The IP address that Crow will handle requests on (default is 0.0.0.0)
|
2016-09-21 14:11:06 +00:00
|
|
|
self_t& bindaddr(std::string bindaddr)
|
|
|
|
{
|
|
|
|
bindaddr_ = bindaddr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:06:49 +00:00
|
|
|
///Run the server on multiple threads using all available threads
|
2016-09-21 14:11:06 +00:00
|
|
|
self_t& multithreaded()
|
|
|
|
{
|
|
|
|
return concurrency(std::thread::hardware_concurrency());
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:06:49 +00:00
|
|
|
///Run the server on multiple threads using a specific number
|
2016-09-21 14:11:06 +00:00
|
|
|
self_t& concurrency(std::uint16_t concurrency)
|
|
|
|
{
|
|
|
|
if (concurrency < 1)
|
|
|
|
concurrency = 1;
|
|
|
|
concurrency_ = concurrency;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:13:57 +00:00
|
|
|
///Set the server's log level
|
|
|
|
|
|
|
|
///
|
2020-11-04 03:21:52 +00:00
|
|
|
/// Possible values are:<br>
|
|
|
|
/// crow::LogLevel::Debug (0)<br>
|
|
|
|
/// crow::LogLevel::Info (1)<br>
|
|
|
|
/// crow::LogLevel::Warning (2)<br>
|
|
|
|
/// crow::LogLevel::Error (3)<br>
|
|
|
|
/// crow::LogLevel::Critical (4)<br>
|
2020-10-22 01:13:57 +00:00
|
|
|
self_t& loglevel(crow::LogLevel level)
|
|
|
|
{
|
|
|
|
crow::logger::setLogLevel(level);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
///Set a custom duration and function to run on every tick
|
|
|
|
template <typename Duration, typename Func>
|
|
|
|
self_t& tick(Duration d, Func f) {
|
|
|
|
tick_interval_ = std::chrono::duration_cast<std::chrono::milliseconds>(d);
|
|
|
|
tick_function_ = f;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-01-02 19:12:04 +00:00
|
|
|
self_t& use_compression(compression::algorithm algorithm)
|
|
|
|
{
|
|
|
|
comp_algorithm_ = algorithm;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
compression::algorithm compression_algorithm()
|
|
|
|
{
|
|
|
|
return comp_algorithm_;
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:13:57 +00:00
|
|
|
///A wrapper for `validate()` in the router
|
|
|
|
|
|
|
|
///
|
|
|
|
///Go through the rules, upgrade them if possible, and add them to the list of rules
|
2016-09-21 14:11:06 +00:00
|
|
|
void validate()
|
|
|
|
{
|
|
|
|
router_.validate();
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:13:57 +00:00
|
|
|
///Notify anything using `wait_for_server_start()` to proceed
|
2017-12-24 15:40:39 +00:00
|
|
|
void notify_server_start()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(start_mutex_);
|
|
|
|
server_started_ = true;
|
|
|
|
cv_started_.notify_all();
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:06:49 +00:00
|
|
|
///Run the server
|
2016-09-21 14:11:06 +00:00
|
|
|
void run()
|
|
|
|
{
|
2020-11-10 00:47:30 +00:00
|
|
|
#ifndef CROW_DISABLE_STATIC_DIR
|
2020-11-10 20:56:27 +00:00
|
|
|
route<crow::black_magic::get_parameter_tag(CROW_STATIC_ENDPOINT)>(CROW_STATIC_ENDPOINT)
|
2021-04-03 02:33:40 +00:00
|
|
|
([](crow::response& res, std::string file_path_partial)
|
2020-11-10 00:47:30 +00:00
|
|
|
{
|
2020-11-10 20:56:27 +00:00
|
|
|
res.set_static_file_info(CROW_STATIC_DIRECTORY + file_path_partial);
|
2020-11-10 00:47:30 +00:00
|
|
|
res.end();
|
|
|
|
});
|
2016-09-21 14:11:06 +00:00
|
|
|
validate();
|
2020-11-10 00:47:30 +00:00
|
|
|
#endif
|
|
|
|
|
2016-09-21 14:11:06 +00:00
|
|
|
#ifdef CROW_ENABLE_SSL
|
|
|
|
if (use_ssl_)
|
|
|
|
{
|
2020-12-03 01:15:05 +00:00
|
|
|
ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, &ssl_context_)));
|
2016-09-21 14:11:06 +00:00
|
|
|
ssl_server_->set_tick_function(tick_interval_, tick_function_);
|
2017-12-24 15:40:39 +00:00
|
|
|
notify_server_start();
|
2016-09-21 14:11:06 +00:00
|
|
|
ssl_server_->run();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2020-12-03 01:15:05 +00:00
|
|
|
server_ = std::move(std::unique_ptr<server_t>(new server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, nullptr)));
|
2016-09-21 14:11:06 +00:00
|
|
|
server_->set_tick_function(tick_interval_, tick_function_);
|
2020-12-19 07:15:57 +00:00
|
|
|
server_->signal_clear();
|
|
|
|
for (auto snum : signals_)
|
|
|
|
{
|
|
|
|
server_->signal_add(snum);
|
|
|
|
}
|
2017-12-24 15:40:39 +00:00
|
|
|
notify_server_start();
|
2016-09-21 14:11:06 +00:00
|
|
|
server_->run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:06:49 +00:00
|
|
|
///Stop the server
|
2016-09-21 14:11:06 +00:00
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
#ifdef CROW_ENABLE_SSL
|
|
|
|
if (use_ssl_)
|
|
|
|
{
|
2018-08-30 15:51:54 +00:00
|
|
|
if (ssl_server_) {
|
|
|
|
ssl_server_->stop();
|
|
|
|
}
|
2016-09-21 14:11:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2018-08-30 15:51:54 +00:00
|
|
|
if (server_) {
|
|
|
|
server_->stop();
|
|
|
|
}
|
2016-09-21 14:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void debug_print()
|
|
|
|
{
|
|
|
|
CROW_LOG_DEBUG << "Routing:";
|
|
|
|
router_.debug_print();
|
|
|
|
}
|
|
|
|
|
2017-09-17 17:58:53 +00:00
|
|
|
|
2016-09-21 14:11:06 +00:00
|
|
|
#ifdef CROW_ENABLE_SSL
|
2020-10-22 01:13:57 +00:00
|
|
|
|
|
|
|
///use certificate and key files for SSL
|
2016-09-21 14:11:06 +00:00
|
|
|
self_t& ssl_file(const std::string& crt_filename, const std::string& key_filename)
|
|
|
|
{
|
|
|
|
use_ssl_ = true;
|
|
|
|
ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer);
|
2020-10-20 08:43:43 +00:00
|
|
|
ssl_context_.set_verify_mode(boost::asio::ssl::verify_client_once);
|
2016-09-21 14:11:06 +00:00
|
|
|
ssl_context_.use_certificate_file(crt_filename, ssl_context_t::pem);
|
|
|
|
ssl_context_.use_private_key_file(key_filename, ssl_context_t::pem);
|
|
|
|
ssl_context_.set_options(
|
|
|
|
boost::asio::ssl::context::default_workarounds
|
|
|
|
| boost::asio::ssl::context::no_sslv2
|
|
|
|
| boost::asio::ssl::context::no_sslv3
|
|
|
|
);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:13:57 +00:00
|
|
|
///use .pem file for SSL
|
2016-09-21 14:11:06 +00:00
|
|
|
self_t& ssl_file(const std::string& pem_filename)
|
|
|
|
{
|
|
|
|
use_ssl_ = true;
|
|
|
|
ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer);
|
2020-10-20 08:43:43 +00:00
|
|
|
ssl_context_.set_verify_mode(boost::asio::ssl::verify_client_once);
|
2016-09-21 14:11:06 +00:00
|
|
|
ssl_context_.load_verify_file(pem_filename);
|
|
|
|
ssl_context_.set_options(
|
|
|
|
boost::asio::ssl::context::default_workarounds
|
|
|
|
| boost::asio::ssl::context::no_sslv2
|
|
|
|
| boost::asio::ssl::context::no_sslv3
|
|
|
|
);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
self_t& ssl(boost::asio::ssl::context&& ctx)
|
|
|
|
{
|
|
|
|
use_ssl_ = true;
|
|
|
|
ssl_context_ = std::move(ctx);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool use_ssl_{false};
|
|
|
|
ssl_context_t ssl_context_{boost::asio::ssl::context::sslv23};
|
|
|
|
|
|
|
|
#else
|
|
|
|
template <typename T, typename ... Remain>
|
|
|
|
self_t& ssl_file(T&&, Remain&&...)
|
|
|
|
{
|
|
|
|
// We can't call .ssl() member function unless CROW_ENABLE_SSL is defined.
|
|
|
|
static_assert(
|
|
|
|
// make static_assert dependent to T; always false
|
|
|
|
std::is_base_of<T, void>::value,
|
|
|
|
"Define CROW_ENABLE_SSL to enable ssl support.");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
self_t& ssl(T&&)
|
|
|
|
{
|
|
|
|
// We can't call .ssl() member function unless CROW_ENABLE_SSL is defined.
|
|
|
|
static_assert(
|
|
|
|
// make static_assert dependent to T; always false
|
|
|
|
std::is_base_of<T, void>::value,
|
|
|
|
"Define CROW_ENABLE_SSL to enable ssl support.");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// middleware
|
|
|
|
using context_t = detail::context<Middlewares...>;
|
|
|
|
template <typename T>
|
|
|
|
typename T::context& get_context(const request& req)
|
|
|
|
{
|
|
|
|
static_assert(black_magic::contains<T, Middlewares...>::value, "App doesn't have the specified middleware type.");
|
|
|
|
auto& ctx = *reinterpret_cast<context_t*>(req.middleware_context);
|
|
|
|
return ctx.template get<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T& get_middleware()
|
|
|
|
{
|
|
|
|
return utility::get_element_by_type<T, Middlewares...>(middlewares_);
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:13:57 +00:00
|
|
|
///Wait until the server has properly started
|
2017-12-24 15:40:39 +00:00
|
|
|
void wait_for_server_start()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(start_mutex_);
|
|
|
|
if (server_started_)
|
|
|
|
return;
|
|
|
|
cv_started_.wait(lock);
|
|
|
|
}
|
2021-04-05 07:44:35 +00:00
|
|
|
#ifdef CROW_CATCHALL
|
|
|
|
public:
|
|
|
|
void catchall_handler( catch_all_func_t func )
|
|
|
|
{
|
|
|
|
router_.set_catchall( func );
|
|
|
|
}
|
|
|
|
#endif
|
2016-09-21 14:11:06 +00:00
|
|
|
private:
|
|
|
|
uint16_t port_ = 80;
|
|
|
|
uint16_t concurrency_ = 1;
|
2020-12-03 01:15:05 +00:00
|
|
|
std::string server_name_ = "Crow/0.2";
|
2016-09-21 14:11:06 +00:00
|
|
|
std::string bindaddr_ = "0.0.0.0";
|
|
|
|
Router router_;
|
2021-01-02 19:12:04 +00:00
|
|
|
compression::algorithm comp_algorithm_;
|
2016-09-21 14:11:06 +00:00
|
|
|
|
|
|
|
std::chrono::milliseconds tick_interval_;
|
|
|
|
std::function<void()> tick_function_;
|
|
|
|
|
|
|
|
std::tuple<Middlewares...> middlewares_;
|
|
|
|
|
|
|
|
#ifdef CROW_ENABLE_SSL
|
|
|
|
std::unique_ptr<ssl_server_t> ssl_server_;
|
|
|
|
#endif
|
|
|
|
std::unique_ptr<server_t> server_;
|
2017-12-24 15:40:39 +00:00
|
|
|
|
2020-12-19 07:15:57 +00:00
|
|
|
std::vector<int> signals_{SIGINT, SIGTERM};
|
|
|
|
|
2017-12-24 15:40:39 +00:00
|
|
|
bool server_started_{false};
|
|
|
|
std::condition_variable cv_started_;
|
|
|
|
std::mutex start_mutex_;
|
2016-09-21 14:11:06 +00:00
|
|
|
};
|
|
|
|
template <typename ... Middlewares>
|
|
|
|
using App = Crow<Middlewares...>;
|
|
|
|
using SimpleApp = Crow<>;
|
|
|
|
}
|