11 #include <condition_variable>
13 #include "crow/version.h"
14 #include "crow/settings.h"
15 #include "crow/logging.h"
16 #include "crow/utility.h"
17 #include "crow/routing.h"
18 #include "crow/middleware_context.h"
19 #include "crow/http_request.h"
20 #include "crow/http_server.h"
21 #include "crow/task_timer.h"
22 #include "crow/websocket.h"
23 #ifdef CROW_ENABLE_COMPRESSION
24 #include "crow/compression.h"
27 #ifdef CROW_MSVC_WORKAROUND
28 #define CROW_ROUTE(app, url) app.route_dynamic(url)
29 #define CROW_BP_ROUTE(blueprint, url) blueprint.new_rule_dynamic(url)
31 #define CROW_ROUTE(app, url) app.template route<crow::black_magic::get_parameter_tag(url)>(url)
32 #define CROW_BP_ROUTE(blueprint, url) blueprint.new_rule_tagged<crow::black_magic::get_parameter_tag(url)>(url)
33 #define CROW_WEBSOCKET_ROUTE(app, url) app.route<crow::black_magic::get_parameter_tag(url)>(url).websocket<std::remove_reference<decltype(app)>::type>(&app)
34 #define CROW_MIDDLEWARES(app, ...) template middlewares<typename std::remove_reference<decltype(app)>::type, __VA_ARGS__>()
36 #define CROW_CATCHALL_ROUTE(app) app.catchall_route()
37 #define CROW_BP_CATCHALL_ROUTE(blueprint) blueprint.catchall_rule()
41 #ifdef CROW_ENABLE_SSL
42 using ssl_context_t = asio::ssl::context;
48 template<
typename... Middlewares>
56 #ifdef CROW_ENABLE_SSL
64 template<
typename... Ts>
66 middlewares_(make_middleware_tuple(std::forward<Ts>(ts)...))
73 template<
typename Adaptor>
76 router_.handle_upgrade(req, res, adaptor);
82 return router_.handle_initial(req, res);
88 router_.handle<
self_t>(req, res, *found);
95 if (found->rule_index)
102 return router_.new_rule_dynamic(rule);
106 template<u
int64_t Tag>
107 #ifdef CROW_GCC83_WORKAROUND
108 auto&
route(
const std::string& rule)
112 #if defined CROW_CAN_USE_CPP17 && !defined CROW_GCC83_WORKAROUND
113 ->
typename std::invoke_result<decltype(&Router::new_rule_tagged<Tag>),
Router,
const std::string&>::type
114 #elif !defined CROW_GCC83_WORKAROUND
115 ->
typename std::result_of<decltype (&Router::new_rule_tagged<Tag>)(
Router,
const std::string&)>::type
118 return router_.new_rule_tagged<Tag>(rule);
124 return router_.catchall_rule();
130 max_payload_ = max_payload;
146 self_t& signal_add(
int signal_number)
148 signals_.push_back(signal_number);
152 std::vector<int> signals()
200 return concurrency(std::thread::hardware_concurrency());
229 crow::logger::setLogLevel(level);
239 res_stream_threshold_ = threshold;
246 return res_stream_threshold_;
251 router_.register_blueprint(blueprint);
261 template<
typename Func>
264 router_.exception_handler() = std::forward<Func>(f);
270 return router_.exception_handler();
274 template<
typename Duration,
typename Func>
277 tick_interval_ = std::chrono::duration_cast<std::chrono::milliseconds>(d);
282 #ifdef CROW_ENABLE_COMPRESSION
283 self_t& use_compression(compression::algorithm algorithm)
285 comp_algorithm_ = algorithm;
286 compression_used_ =
true;
290 compression::algorithm compression_algorithm()
292 return comp_algorithm_;
295 bool compression_used()
const
297 return compression_used_;
304 #if defined(__APPLE__) || defined(__MACH__)
305 if (router_.blueprints().empty())
return;
308 for (
Blueprint* bp : router_.blueprints())
310 if (bp->static_dir().empty())
continue;
312 auto static_dir_ = crow::utility::normalize_path(bp->static_dir());
314 bp->new_rule_tagged<crow::black_magic::get_parameter_tag(CROW_STATIC_ENDPOINT)>(CROW_STATIC_ENDPOINT)([static_dir_](
crow::response& res, std::string file_path_partial) {
315 utility::sanitize_filename(file_path_partial);
321 router_.validate_bp();
327 if (are_static_routes_added())
return;
328 auto static_dir_ = crow::utility::normalize_path(CROW_STATIC_DIRECTORY);
330 route<crow::black_magic::get_parameter_tag(CROW_STATIC_ENDPOINT)>(CROW_STATIC_ENDPOINT)([static_dir_](
crow::response& res, std::string file_path_partial) {
331 utility::sanitize_filename(file_path_partial);
335 set_static_routes_added();
347 #ifndef CROW_DISABLE_STATIC_DIR
353 #ifdef CROW_ENABLE_SSL
356 ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(
new ssl_server_t(
this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_)));
357 ssl_server_->set_tick_function(tick_interval_, tick_function_);
358 ssl_server_->signal_clear();
359 for (
auto snum : signals_)
361 ssl_server_->signal_add(snum);
363 notify_server_start();
369 server_ = std::move(std::unique_ptr<server_t>(
new server_t(
this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_,
nullptr)));
370 server_->set_tick_function(tick_interval_, tick_function_);
371 for (
auto snum : signals_)
373 server_->signal_add(snum);
375 notify_server_start();
386 return std::async(std::launch::async, [&] {
394 #ifdef CROW_ENABLE_SSL
397 if (ssl_server_) { ssl_server_->stop(); }
403 std::vector<crow::websocket::connection*> websockets_to_close = websockets_;
404 for (
auto websocket : websockets_to_close)
406 CROW_LOG_INFO <<
"Quitting Websocket: " << websocket;
407 websocket->close(
"Server Application Terminated");
409 if (server_) { server_->stop(); }
415 websockets_.push_back(conn);
420 websockets_.erase(std::remove(websockets_.begin(), websockets_.end(), conn), websockets_.end());
426 CROW_LOG_DEBUG <<
"Routing:";
427 router_.debug_print();
431 #ifdef CROW_ENABLE_SSL
434 self_t&
ssl_file(
const std::string& crt_filename,
const std::string& key_filename)
437 ssl_context_.set_verify_mode(asio::ssl::verify_peer);
438 ssl_context_.set_verify_mode(asio::ssl::verify_client_once);
439 ssl_context_.use_certificate_file(crt_filename, ssl_context_t::pem);
440 ssl_context_.use_private_key_file(key_filename, ssl_context_t::pem);
441 ssl_context_.set_options(
442 asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3);
450 ssl_context_.set_verify_mode(asio::ssl::verify_peer);
451 ssl_context_.set_verify_mode(asio::ssl::verify_client_once);
452 ssl_context_.load_verify_file(pem_filename);
453 ssl_context_.set_options(
454 asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3);
462 ssl_context_.set_verify_mode(asio::ssl::verify_peer);
463 ssl_context_.set_verify_mode(asio::ssl::verify_client_once);
464 ssl_context_.use_certificate_chain_file(crt_filename);
465 ssl_context_.use_private_key_file(key_filename, ssl_context_t::pem);
466 ssl_context_.set_options(
467 asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3);
471 self_t& ssl(asio::ssl::context&& ctx)
474 ssl_context_ = std::move(ctx);
478 bool ssl_used()
const
483 template<
typename T,
typename... Remain>
489 std::is_base_of<T, void>::value,
490 "Define CROW_ENABLE_SSL to enable ssl support.");
494 template<
typename T,
typename... Remain>
500 std::is_base_of<T, void>::value,
501 "Define CROW_ENABLE_SSL to enable ssl support.");
511 std::is_base_of<T, void>::value,
512 "Define CROW_ENABLE_SSL to enable ssl support.");
516 bool ssl_used()
const
523 using context_t = detail::context<Middlewares...>;
524 using mw_container_t = std::tuple<Middlewares...>;
526 typename T::context& get_context(
const request& req)
528 static_assert(black_magic::contains<T, Middlewares...>::value,
"App doesn't have the specified middleware type.");
529 auto& ctx = *
reinterpret_cast<context_t*
>(req.middleware_context);
530 return ctx.template get<T>();
536 return utility::get_element_by_type<T, Middlewares...>(middlewares_);
543 std::unique_lock<std::mutex> lock(start_mutex_);
544 if (!server_started_)
545 cv_started_.wait(lock);
548 server_->wait_for_start();
549 #ifdef CROW_ENABLE_SSL
550 else if (ssl_server_)
551 ssl_server_->wait_for_start();
556 template<
typename... Ts>
557 std::tuple<Middlewares...> make_middleware_tuple(Ts&&... ts)
559 auto fwd = std::forward_as_tuple((ts)...);
560 return std::make_tuple(
561 std::forward<Middlewares>(
562 black_magic::tuple_extract<Middlewares, decltype(fwd)>(fwd))...);
566 void notify_server_start()
568 std::unique_lock<std::mutex> lock(start_mutex_);
569 server_started_ =
true;
570 cv_started_.notify_all();
573 void set_static_routes_added() {
574 static_routes_added_ =
true;
577 bool are_static_routes_added() {
578 return static_routes_added_;
582 std::uint8_t timeout_{5};
584 uint16_t concurrency_ = 2;
585 uint64_t max_payload_{UINT64_MAX};
586 std::string server_name_ = std::string(
"Crow/") + VERSION;
587 std::string bindaddr_ =
"0.0.0.0";
588 size_t res_stream_threshold_ = 1048576;
590 bool static_routes_added_{
false};
592 #ifdef CROW_ENABLE_COMPRESSION
593 compression::algorithm comp_algorithm_;
594 bool compression_used_{
false};
597 std::chrono::milliseconds tick_interval_;
598 std::function<void()> tick_function_;
600 std::tuple<Middlewares...> middlewares_;
602 #ifdef CROW_ENABLE_SSL
603 std::unique_ptr<ssl_server_t> ssl_server_;
604 bool ssl_used_{
false};
605 ssl_context_t ssl_context_{asio::ssl::context::sslv23};
608 std::unique_ptr<server_t> server_;
610 std::vector<int> signals_{SIGINT, SIGTERM};
612 bool server_started_{
false};
613 std::condition_variable cv_started_;
614 std::mutex start_mutex_;
615 std::vector<crow::websocket::connection*> websockets_;
617 template<
typename... Middlewares>
618 using App = Crow<Middlewares...>;
619 using SimpleApp = Crow<>;
A blueprint can be considered a smaller section of a Crow app, specifically where the router is conec...
Definition: routing.h:1104
Definition: routing.h:342
The main server application.
Definition: app.h:50
self_t & multithreaded()
Run the server on multiple threads using all available threads.
Definition: app.h:198
void stop()
Stop the server.
Definition: app.h:392
self_t & tick(Duration d, Func f)
Set a custom duration and function to run on every tick.
Definition: app.h:275
DynamicRule & route_dynamic(const std::string &rule)
Create a dynamic route using a rule (Use CROW_ROUTE instead)
Definition: app.h:100
size_t & stream_threshold()
Get the response body size (in bytes) beyond which Crow automatically streams responses.
Definition: app.h:244
void run()
Run the server.
Definition: app.h:345
self_t & websocket_max_payload(uint64_t max_payload)
Set the default max payload size for websockets.
Definition: app.h:128
self_t & port(std::uint16_t port)
Set the port that Crow will handle requests on.
Definition: app.h:158
uint64_t websocket_max_payload()
Get the default max payload size for websockets.
Definition: app.h:135
void add_static_dir()
Go through the rules, upgrade them if possible, and add them to the list of rules.
Definition: app.h:325
self_t & ssl_file(const std::string &pem_filename)
Use .pem file for SSL.
Definition: app.h:447
self_t & concurrency(std::uint16_t concurrency)
Run the server on multiple threads using a specific number.
Definition: app.h:204
self_t & timeout(std::uint8_t timeout)
Set the connection timeout in seconds (default is 5)
Definition: app.h:171
Crow self_t
This crow application.
Definition: app.h:53
Server< Crow, SocketAdaptor, Middlewares... > server_t
The HTTP server.
Definition: app.h:55
auto route(const std::string &rule) -> typename std::result_of< decltype(&Router::new_rule_tagged< Tag >)(Router, const std::string &)>::type
Create a route using a rule (Use CROW_ROUTE instead)
Definition: app.h:110
void wait_for_server_start()
Wait until the server has properly started.
Definition: app.h:540
void handle_upgrade(const request &req, response &res, Adaptor &&adaptor)
Process an Upgrade request.
Definition: app.h:74
self_t & bindaddr(std::string bindaddr)
The IP address that Crow will handle requests on (default is 0.0.0.0)
Definition: app.h:185
std::unique_ptr< routing_handle_result > handle_initial(request &req, response &res)
Process only the method and URL of a request and provide a route (or an error response)
Definition: app.h:80
self_t & loglevel(LogLevel level)
Set the server's log level.
Definition: app.h:227
void debug_print()
Print the routing paths defined for each HTTP method.
Definition: app.h:424
void handle(request &req, response &res, std::unique_ptr< routing_handle_result > &found)
Process the fully parsed request and generate a response for it.
Definition: app.h:86
self_t & exception_handler(Func &&f)
Set the function to call to handle uncaught exceptions generated in routes (Default generates error 5...
Definition: app.h:262
std::uint16_t port()
Get the port that Crow will handle requests on.
Definition: app.h:165
std::string bindaddr()
Get the address that Crow will handle requests on.
Definition: app.h:192
self_t & ssl_file(const std::string &crt_filename, const std::string &key_filename)
Use certificate and key files for SSL.
Definition: app.h:434
Server< Crow, SSLAdaptor, Middlewares... > ssl_server_t
An HTTP server that runs on SSL with an SSLAdaptor.
Definition: app.h:58
std::future< void > run_async()
Definition: app.h:384
CatchallRule & catchall_route()
Create a route for any requests without a proper route (Use CROW_CATCHALL_ROUTE instead)
Definition: app.h:122
void validate()
A wrapper for validate() in the router.
Definition: app.h:339
Crow(Ts &&... ts)
Construct Crow with a subset of middleware.
Definition: app.h:65
std::uint16_t concurrency()
Get the number of threads that server is using.
Definition: app.h:213
void handle_full(request &req, response &res)
Process a fully parsed request from start to finish (primarily used for debugging)
Definition: app.h:92
self_t & ssl_chainfile(const std::string &crt_filename, const std::string &key_filename)
Use certificate chain and key files for SSL.
Definition: app.h:459
self_t & server_name(std::string server_name)
Set the server name.
Definition: app.h:178
self_t & stream_threshold(size_t threshold)
Set the response body size (in bytes) beyond which Crow automatically streams responses (Default is 1...
Definition: app.h:237
void add_blueprint()
Apply blueprints.
Definition: app.h:302
A rule that can change its parameters during runtime.
Definition: routing.h:575
Handles matching requests to existing rules and upgrade requests.
Definition: routing.h:1254
Definition: http_server.h:28
Definition: socket_adaptors.h:90
A wrapper for the asio::ip::tcp::socket and asio::ssl::stream.
Definition: socket_adaptors.h:22
An HTTP request.
Definition: http_request.h:28
HTTP response.
Definition: http_response.h:34
void end()
Set the response completion flag and call the handler (to send the response).
Definition: http_response.h:237
void set_static_file_info_unsafe(std::string path)
Return a static file as the response body without sanitizing the path (use set_static_file_info inste...
Definition: http_response.h:295
A base class for websocket connection.
Definition: websocket.h:24