Crow  1.1
A C++ microframework for the web
 
Loading...
Searching...
No Matches
app.h
Go to the documentation of this file.
1/**
2 * \file crow/app.h
3 * \brief This file includes the definition of the crow::Crow class,
4 * the crow::App and crow::SimpleApp aliases, and some macros.
5 *
6 * In this file are defined:
7 * - crow::Crow
8 * - crow::App
9 * - crow::SimpleApp
10 * - \ref CROW_ROUTE
11 * - \ref CROW_BP_ROUTE
12 * - \ref CROW_WEBSOCKET_ROUTE
13 * - \ref CROW_MIDDLEWARES
14 * - \ref CROW_CATCHALL_ROUTE
15 * - \ref CROW_BP_CATCHALL_ROUTE
16 */
17
18#pragma once
19
20#include <chrono>
21#include <string>
22#include <functional>
23#include <memory>
24#include <future>
25#include <cstdint>
26#include <type_traits>
27#include <thread>
28#include <condition_variable>
29
30#include "crow/version.h"
31#include "crow/settings.h"
32#include "crow/logging.h"
33#include "crow/utility.h"
34#include "crow/routing.h"
35#include "crow/middleware_context.h"
36#include "crow/http_request.h"
37#include "crow/http_server.h"
38#include "crow/task_timer.h"
39#include "crow/websocket.h"
40#ifdef CROW_ENABLE_COMPRESSION
41#include "crow/compression.h"
42#endif // #ifdef CROW_ENABLE_COMPRESSION
43
44
45#ifdef CROW_MSVC_WORKAROUND
46
47#define CROW_ROUTE(app, url) app.route_dynamic(url) // See the documentation in the comment below.
48#define CROW_BP_ROUTE(blueprint, url) blueprint.new_rule_dynamic(url) // See the documentation in the comment below.
49
50#else // #ifdef CROW_MSVC_WORKAROUND
51
52/**
53 * \def CROW_ROUTE(app, url)
54 * \brief Creates a route for app using a rule.
55 *
56 * It use crow::Crow::route_dynamic or crow::Crow::route to define
57 * a rule for your application. It's usage is like this:
58 *
59 * ```cpp
60 * auto app = crow::SimpleApp(); // or crow::App()
61 * CROW_ROUTE(app, "/")
62 * ([](){
63 * return "<h1>Hello, world!</h1>";
64 * });
65 * ```
66 *
67 * This is the recommended way to define routes in a crow application.
68 * \see [Page of guide "Routes"](https://crowcpp.org/master/guides/routes/).
69 */
70#define CROW_ROUTE(app, url) app.template route<crow::black_magic::get_parameter_tag(url)>(url)
71
72/**
73 * \def CROW_BP_ROUTE(blueprint, url)
74 * \brief Creates a route for a blueprint using a rule.
75 *
76 * It may use crow::Blueprint::new_rule_dynamic or
77 * crow::Blueprint::new_rule_tagged to define a new rule for
78 * an given blueprint. It's usage is similar
79 * to CROW_ROUTE macro:
80 *
81 * ```cpp
82 * crow::Blueprint my_bp();
83 * CROW_BP_ROUTE(my_bp, "/")
84 * ([](){
85 * return "<h1>Hello, world!</h1>";
86 * });
87 * ```
88 *
89 * This is the recommended way to define routes in a crow blueprint
90 * because of its compile-time capabilities.
91 *
92 * \see [Page of the guide "Blueprints"](https://crowcpp.org/master/guides/blueprints/).
93 */
94#define CROW_BP_ROUTE(blueprint, url) blueprint.new_rule_tagged<crow::black_magic::get_parameter_tag(url)>(url)
95
96/**
97 * \def CROW_WEBSOCKET_ROUTE(app, url)
98 * \brief Defines WebSocket route for app.
99 *
100 * It binds a WebSocket route to app. Easy solution to implement
101 * WebSockets in your app. The usage syntax of this macro is
102 * like this:
103 *
104 * ```cpp
105 * auto app = crow::SimpleApp(); // or crow::App()
106 * CROW_WEBSOCKET_ROUTE(app, "/ws")
107 * .onopen([&](crow::websocket::connection& conn){
108 * do_something();
109 * })
110 * .onclose([&](crow::websocket::connection& conn, const std::string& reason, uint16_t){
111 * do_something();
112 * })
113 * .onmessage([&](crow::websocket::connection&, const std::string& data, bool is_binary){
114 * if (is_binary)
115 * do_something(data);
116 * else
117 * do_something_else(data);
118 * });
119 * ```
120 *
121 * \see [Page of the guide "WebSockets"](https://crowcpp.org/master/guides/websockets/).
122 */
123#define CROW_WEBSOCKET_ROUTE(app, url) app.route<crow::black_magic::get_parameter_tag(url)>(url).websocket<std::remove_reference<decltype(app)>::type>(&app)
124
125/**
126 * \def CROW_MIDDLEWARES(app, ...)
127 * \brief Enable a Middleware for an specific route in app
128 * or blueprint.
129 *
130 * It defines the usage of a Middleware in one route. And it
131 * can be used in both crow::SimpleApp (and crow::App) instances and
132 * crow::Blueprint. Its usage syntax is like this:
133 *
134 * ```cpp
135 * auto app = crow::SimpleApp(); // or crow::App()
136 * CROW_ROUTE(app, "/with_middleware")
137 * .CROW_MIDDLEWARES(app, LocalMiddleware) // Can be used more than one
138 * ([]() { // middleware.
139 * return "Hello world!";
140 * });
141 * ```
142 *
143 * \see [Page of the guide "Middlewares"](https://crowcpp.org/master/guides/middleware/).
144 */
145#define CROW_MIDDLEWARES(app, ...) template middlewares<typename std::remove_reference<decltype(app)>::type, __VA_ARGS__>()
146
147#endif // #ifdef CROW_MSVC_WORKAROUND
148
149/**
150 * \def CROW_CATCHALL_ROUTE(app)
151 * \brief Defines a custom catchall route for app using a
152 * custom rule.
153 *
154 * It defines a handler when the client make a request for an
155 * undefined route. Instead of just reply with a `404` status
156 * code (default behavior), you can define a custom handler
157 * using this macro.
158 *
159 * \see [Page of the guide "Routes" (Catchall routes)](https://crowcpp.org/master/guides/routes/#catchall-routes).
160 */
161#define CROW_CATCHALL_ROUTE(app) app.catchall_route()
162
163/**
164 * \def CROW_BP_CATCHALL_ROUTE(blueprint)
165 * \brief Defines a custom catchall route for blueprint
166 * using a custom rule.
167 *
168 * It defines a handler when the client make a request for an
169 * undefined route in the blueprint.
170 *
171 * \see [Page of the guide "Blueprint" (Define a custom Catchall route)](https://crowcpp.org/master/guides/blueprints/#define-a-custom-catchall-route).
172 */
173#define CROW_BP_CATCHALL_ROUTE(blueprint) blueprint.catchall_rule()
174
175
176/**
177 * \namespace crow
178 * \brief The main namespace of the library. In this namespace
179 * is defined the most important classes and functions of the
180 * library.
181 *
182 * Within this namespace, the Crow class, Router class, Connection
183 * class, and other are defined.
184 */
185namespace crow
186{
187#ifdef CROW_ENABLE_SSL
188 using ssl_context_t = asio::ssl::context;
189#endif
190 /**
191 * \class Crow
192 * \brief The main server application class.
193 *
194 * Use crow::SimpleApp or crow::App<Middleware1, Middleware2, etc...> instead of
195 * directly instantiate this class.
196 */
197 template<typename... Middlewares>
198 class Crow
199 {
200 public:
201 /// \brief This is the crow application
202 using self_t = Crow;
203
204 /// \brief The HTTP server
205 using server_t = Server<Crow, TCPAcceptor, SocketAdaptor, Middlewares...>;
206 /// \brief An HTTP server that runs on unix domain socket
208#ifdef CROW_ENABLE_SSL
209 /// \brief An HTTP server that runs on SSL with an SSLAdaptor
210 using ssl_server_t = Server<Crow, TCPAcceptor, SSLAdaptor, Middlewares...>;
211#endif
212 Crow()
213 {}
214
215 /// \brief Construct Crow with a subset of middleware
216 template<typename... Ts>
217 Crow(Ts&&... ts):
218 middlewares_(make_middleware_tuple(std::forward<Ts>(ts)...))
219 {}
220
221 /// \brief Process an Upgrade request
222 ///
223 /// Currently used to upgrade an HTTP connection to a WebSocket connection
224 template<typename Adaptor>
225 void handle_upgrade(const request& req, response& res, Adaptor&& adaptor)
226 {
227 router_.handle_upgrade(req, res, adaptor);
228 }
229
230 /// \brief Process only the method and URL of a request and provide a route (or an error response)
231 std::unique_ptr<routing_handle_result> handle_initial(request& req, response& res)
232 {
233 return router_.handle_initial(req, res);
234 }
235
236 /// \brief Process the fully parsed request and generate a response for it
237 void handle(request& req, response& res, std::unique_ptr<routing_handle_result>& found)
238 {
239 router_.handle<self_t>(req, res, *found);
240 }
241
242 /// \brief Process a fully parsed request from start to finish (primarily used for debugging)
243 void handle_full(request& req, response& res)
244 {
245 auto found = handle_initial(req, res);
246 if (found->rule_index)
247 handle(req, res, found);
248 }
249
250 /// \brief Create a dynamic route using a rule (**Use CROW_ROUTE instead**)
251 DynamicRule& route_dynamic(const std::string& rule)
252 {
253 return router_.new_rule_dynamic(rule);
254 }
255
256 /// \brief Create a route using a rule (**Use CROW_ROUTE instead**)
257 template<uint64_t Tag>
258 auto route(const std::string& rule)
259 -> typename std::invoke_result<decltype(&Router::new_rule_tagged<Tag>), Router, const std::string&>::type
260 {
261 return router_.new_rule_tagged<Tag>(rule);
262 }
263
264 /// \brief Create a route for any requests without a proper route (**Use CROW_CATCHALL_ROUTE instead**)
266 {
267 return router_.catchall_rule();
268 }
269
270 /// \brief Set the default max payload size for websockets
271 self_t& websocket_max_payload(uint64_t max_payload)
272 {
273 max_payload_ = max_payload;
274 return *this;
275 }
276
277 /// \brief Get the default max payload size for websockets
279 {
280 return max_payload_;
281 }
282
283 self_t& signal_clear()
284 {
285 signals_.clear();
286 return *this;
287 }
288
289 self_t& signal_add(int signal_number)
290 {
291 signals_.push_back(signal_number);
292 return *this;
293 }
294
295 std::vector<int> signals()
296 {
297 return signals_;
298 }
299
300 /// \brief Set the port that Crow will handle requests on
301 self_t& port(std::uint16_t port)
302 {
303 port_ = port;
304 return *this;
305 }
306
307 /// \brief Get the port that Crow will handle requests on
308 std::uint16_t port() const
309 {
310 if (!server_started_)
311 {
312 return port_;
313 }
314#ifdef CROW_ENABLE_SSL
315 if (ssl_used_)
316 {
317 return ssl_server_->port();
318 }
319 else
320#endif
321 {
322 return server_->port();
323 }
324 }
325
326 /// \brief Set status variable to note that the address that Crow will handle requests on is bound
328 is_bound_ = true;
329 }
330
331 /// \brief Get whether address that Crow will handle requests on is bound
332 bool is_bound() const {
333 return is_bound_;
334 }
335
336 /// \brief Set the connection timeout in seconds (default is 5)
337 self_t& timeout(std::uint8_t timeout)
338 {
339 timeout_ = timeout;
340 return *this;
341 }
342
343 /// \brief Set the server name included in the 'Server' HTTP response header. If set to an empty string, the header will be omitted by default.
345 {
346 server_name_ = server_name;
347 return *this;
348 }
349
350 /// \brief The IP address that Crow will handle requests on (default is 0.0.0.0)
352 {
353 bindaddr_ = bindaddr;
354 return *this;
355 }
356
357 /// \brief Get the address that Crow will handle requests on
358 std::string bindaddr()
359 {
360 return bindaddr_;
361 }
362
363 /// \brief Disable tcp/ip and use unix domain socket instead
364 self_t& local_socket_path(std::string path)
365 {
366 bindaddr_ = path;
367 use_unix_ = true;
368 return *this;
369 }
370
371 /// \brief Get the unix domain socket path
372 std::string local_socket_path()
373 {
374 return bindaddr_;
375 }
376
377 /// \brief Run the server on multiple threads using all available threads
379 {
380 return concurrency(std::thread::hardware_concurrency());
381 }
382
383 /// \brief Run the server on multiple threads using a specific number
385 {
386 if (concurrency < 2) // Crow can have a minimum of 2 threads running
387 concurrency = 2;
388 concurrency_ = concurrency;
389 return *this;
390 }
391
392 /// \brief Get the number of threads that server is using
393 std::uint16_t concurrency() const
394 {
395 return concurrency_;
396 }
397
398 /// \brief Set the server's log level
399 ///
400 /// Possible values are:
401 /// - crow::LogLevel::Debug (0)
402 /// - crow::LogLevel::Info (1)
403 /// - crow::LogLevel::Warning (2)
404 /// - crow::LogLevel::Error (3)
405 /// - crow::LogLevel::Critical (4)
406 self_t& loglevel(LogLevel level)
407 {
408 crow::logger::setLogLevel(level);
409 return *this;
410 }
411
412 /// \brief Set the response body size (in bytes) beyond which Crow automatically streams responses (Default is 1MiB)
413 ///
414 /// Any streamed response is unaffected by Crow's timer, and therefore won't timeout before a response is fully sent.
415 self_t& stream_threshold(size_t threshold)
416 {
417 res_stream_threshold_ = threshold;
418 return *this;
419 }
420
421 /// \brief Get the response body size (in bytes) beyond which Crow automatically streams responses
423 {
424 return res_stream_threshold_;
425 }
426
427
428 self_t& register_blueprint(Blueprint& blueprint)
429 {
430 router_.register_blueprint(blueprint);
431 return *this;
432 }
433
434 /// \brief Set the function to call to handle uncaught exceptions generated in routes (Default generates error 500).
435 ///
436 /// The function must have the following signature: void(crow::response&).
437 /// It must set the response passed in argument to the function, which will be sent back to the client.
438 /// See Router::default_exception_handler() for the default implementation.
439 template<typename Func>
441 {
442 router_.exception_handler() = std::forward<Func>(f);
443 return *this;
444 }
445
446 std::function<void(crow::response&)>& exception_handler()
447 {
448 return router_.exception_handler();
449 }
450
451 /// \brief Set a custom duration and function to run on every tick
452 template<typename Duration, typename Func>
453 self_t& tick(Duration d, Func f)
454 {
455 tick_interval_ = std::chrono::duration_cast<std::chrono::milliseconds>(d);
456 tick_function_ = f;
457 return *this;
458 }
459
460#ifdef CROW_ENABLE_COMPRESSION
461
462 self_t& use_compression(compression::algorithm algorithm)
463 {
464 comp_algorithm_ = algorithm;
465 compression_used_ = true;
466 return *this;
467 }
468
469 compression::algorithm compression_algorithm()
470 {
471 return comp_algorithm_;
472 }
473
474 bool compression_used() const
475 {
476 return compression_used_;
477 }
478#endif
479
480 /// \brief Apply blueprints
482 {
483#if defined(__APPLE__) || defined(__MACH__)
484 if (router_.blueprints().empty()) return;
485#endif
486
487 for (Blueprint* bp : router_.blueprints())
488 {
489 if (bp->static_dir().empty()) {
490 CROW_LOG_ERROR << "Blueprint " << bp->prefix() << " and its sub-blueprints ignored due to empty static directory.";
491 continue;
492 }
493 auto static_dir_ = crow::utility::normalize_path(bp->static_dir());
494
495 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) {
496 utility::sanitize_filename(file_path_partial);
497 res.set_static_file_info_unsafe(static_dir_ + file_path_partial);
498 res.end();
499 });
500 }
501
502 router_.validate_bp();
503 }
504
505 /// \brief Go through the rules, upgrade them if possible, and add them to the list of rules
507 {
508 if (are_static_routes_added()) return;
509 auto static_dir_ = crow::utility::normalize_path(CROW_STATIC_DIRECTORY);
510
511 route<crow::black_magic::get_parameter_tag(CROW_STATIC_ENDPOINT)>(CROW_STATIC_ENDPOINT)([static_dir_](crow::response& res, std::string file_path_partial) {
512 utility::sanitize_filename(file_path_partial);
513 res.set_static_file_info_unsafe(static_dir_ + file_path_partial);
514 res.end();
515 });
516 set_static_routes_added();
517 }
518
519 /// \brief A wrapper for `validate()` in the router
520 void validate()
521 {
522 router_.validate();
523 }
524
525 /// \brief Run the server
526 void run()
527 {
528#ifndef CROW_DISABLE_STATIC_DIR
531#endif
532 validate();
533
534#ifdef CROW_ENABLE_SSL
535 if (ssl_used_)
536 {
537
538 error_code ec;
539 asio::ip::address addr = asio::ip::make_address(bindaddr_,ec);
540 if (ec){
541 CROW_LOG_ERROR << ec.message() << " - Can not create valid ip address from string: \"" << bindaddr_ << "\"";
542 return;
543 }
544 tcp::endpoint endpoint(addr, port_);
545 router_.using_ssl = true;
546 ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, endpoint, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_)));
547 ssl_server_->set_tick_function(tick_interval_, tick_function_);
548 ssl_server_->signal_clear();
549 for (auto snum : signals_)
550 {
551 ssl_server_->signal_add(snum);
552 }
553 notify_server_start();
554 ssl_server_->run();
555 }
556 else
557#endif
558 {
559 if (use_unix_)
560 {
561 UnixSocketAcceptor::endpoint endpoint(bindaddr_);
562 unix_server_ = std::move(std::unique_ptr<unix_server_t>(new unix_server_t(this, endpoint, server_name_, &middlewares_, concurrency_, timeout_, nullptr)));
563 unix_server_->set_tick_function(tick_interval_, tick_function_);
564 for (auto snum : signals_)
565 {
566 unix_server_->signal_add(snum);
567 }
568 notify_server_start();
569 unix_server_->run();
570 }
571 else
572 {
573 error_code ec;
574 asio::ip::address addr = asio::ip::make_address(bindaddr_,ec);
575 if (ec){
576 CROW_LOG_ERROR << ec.message() << " - Can not create valid ip address from string: \"" << bindaddr_ << "\"";
577 return;
578 }
579 TCPAcceptor::endpoint endpoint(addr, port_);
580 server_ = std::move(std::unique_ptr<server_t>(new server_t(this, endpoint, server_name_, &middlewares_, concurrency_, timeout_, nullptr)));
581 server_->set_tick_function(tick_interval_, tick_function_);
582 for (auto snum : signals_)
583 {
584 server_->signal_add(snum);
585 }
586 notify_server_start();
587 server_->run();
588 }
589 }
590 }
591
592 /// \brief Non-blocking version of \ref run()
593 ///
594 /// The output from this method needs to be saved into a variable!
595 /// Otherwise the call will be made on the same thread.
596 std::future<void> run_async()
597 {
598 return std::async(std::launch::async, [&] {
599 this->run();
600 });
601 }
602
603 /// \brief Stop the server
604 void stop()
605 {
606#ifdef CROW_ENABLE_SSL
607 if (ssl_used_)
608 {
609 if (ssl_server_) { ssl_server_->stop(); }
610 }
611 else
612#endif
613 {
614 close_websockets();
615 if (server_) { server_->stop(); }
616 if (unix_server_) { unix_server_->stop(); }
617 }
618 }
619
620 void close_websockets()
621 {
622 for (auto websocket : websockets_)
623 {
624 CROW_LOG_INFO << "Quitting Websocket: " << websocket;
625 websocket->close("Websocket Closed");
626 }
627 }
628
629
630 void add_websocket(crow::websocket::connection* conn)
631 {
632 websockets_.push_back(conn);
633 }
634
635 void remove_websocket(crow::websocket::connection* conn)
636 {
637 websockets_.erase(std::remove(websockets_.begin(), websockets_.end(), conn), websockets_.end());
638 }
639
640 /// \brief Print the routing paths defined for each HTTP method
642 {
643 CROW_LOG_DEBUG << "Routing:";
644 router_.debug_print();
645 }
646
647
648#ifdef CROW_ENABLE_SSL
649
650 /// \brief Use certificate and key files for SSL
651 self_t& ssl_file(const std::string& crt_filename, const std::string& key_filename)
652 {
653 ssl_used_ = true;
654 ssl_context_.set_verify_mode(asio::ssl::verify_peer);
655 ssl_context_.set_verify_mode(asio::ssl::verify_client_once);
656 ssl_context_.use_certificate_file(crt_filename, ssl_context_t::pem);
657 ssl_context_.use_private_key_file(key_filename, ssl_context_t::pem);
658 ssl_context_.set_options(
659 asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3);
660 return *this;
661 }
662
663 /// \brief Use `.pem` file for SSL
664 self_t& ssl_file(const std::string& pem_filename)
665 {
666 ssl_used_ = true;
667 ssl_context_.set_verify_mode(asio::ssl::verify_peer);
668 ssl_context_.set_verify_mode(asio::ssl::verify_client_once);
669 ssl_context_.load_verify_file(pem_filename);
670 ssl_context_.set_options(
671 asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3);
672 return *this;
673 }
674
675 /// \brief Use certificate chain and key files for SSL
676 self_t& ssl_chainfile(const std::string& crt_filename, const std::string& key_filename)
677 {
678 ssl_used_ = true;
679 ssl_context_.set_verify_mode(asio::ssl::verify_peer);
680 ssl_context_.set_verify_mode(asio::ssl::verify_client_once);
681 ssl_context_.use_certificate_chain_file(crt_filename);
682 ssl_context_.use_private_key_file(key_filename, ssl_context_t::pem);
683 ssl_context_.set_options(
684 asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3);
685 return *this;
686 }
687
688 self_t& ssl(asio::ssl::context&& ctx)
689 {
690 ssl_used_ = true;
691 ssl_context_ = std::move(ctx);
692 return *this;
693 }
694
695 bool ssl_used() const
696 {
697 return ssl_used_;
698 }
699#else
700
701 template<typename T, typename... Remain>
702 self_t& ssl_file(T&&, Remain&&...)
703 {
704 // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined.
705 static_assert(
706 // make static_assert dependent to T; always false
707 std::is_base_of<T, void>::value,
708 "Define CROW_ENABLE_SSL to enable ssl support.");
709 return *this;
710 }
711
712 template<typename T, typename... Remain>
713 self_t& ssl_chainfile(T&&, Remain&&...)
714 {
715 // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined.
716 static_assert(
717 // make static_assert dependent to T; always false
718 std::is_base_of<T, void>::value,
719 "Define CROW_ENABLE_SSL to enable ssl support.");
720 return *this;
721 }
722
723 template<typename T>
724 self_t& ssl(T&&)
725 {
726 // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined.
727 static_assert(
728 // make static_assert dependent to T; always false
729 std::is_base_of<T, void>::value,
730 "Define CROW_ENABLE_SSL to enable ssl support.");
731 return *this;
732 }
733
734 bool ssl_used() const
735 {
736 return false;
737 }
738#endif
739
740 // middleware
741 using context_t = detail::context<Middlewares...>;
742 using mw_container_t = std::tuple<Middlewares...>;
743 template<typename T>
744 typename T::context& get_context(const request& req)
745 {
746 static_assert(black_magic::contains<T, Middlewares...>::value, "App doesn't have the specified middleware type.");
747 auto& ctx = *reinterpret_cast<context_t*>(req.middleware_context);
748 return ctx.template get<T>();
749 }
750
751 template<typename T>
752 T& get_middleware()
753 {
754 return utility::get_element_by_type<T, Middlewares...>(middlewares_);
755 }
756
757 /// \brief Wait until the server has properly started
758 std::cv_status wait_for_server_start(std::chrono::milliseconds wait_timeout = std::chrono::milliseconds(3000))
759 {
760 std::cv_status status = std::cv_status::no_timeout;
761 auto wait_until = std::chrono::steady_clock::now() + wait_timeout;
762 {
763 std::unique_lock<std::mutex> lock(start_mutex_);
764 while (!server_started_ && (status == std::cv_status::no_timeout))
765 {
766 status = cv_started_.wait_until(lock, wait_until);
767 }
768 }
769 if (status == std::cv_status::no_timeout)
770 {
771 if (server_) {
772 status = server_->wait_for_start(wait_until);
773 } else if (unix_server_) {
774 status = unix_server_->wait_for_start(wait_until);
775 }
776#ifdef CROW_ENABLE_SSL
777 else if (ssl_server_)
778 {
779 status = ssl_server_->wait_for_start(wait_until);
780 }
781#endif
782 }
783 return status;
784 }
785
786 private:
787 template<typename... Ts>
788 std::tuple<Middlewares...> make_middleware_tuple(Ts&&... ts)
789 {
790 auto fwd = std::forward_as_tuple((ts)...);
791 return std::make_tuple(
792 std::forward<Middlewares>(
793 black_magic::tuple_extract<Middlewares, decltype(fwd)>(fwd))...);
794 }
795
796 /// \brief Notify anything using \ref wait_for_server_start() to proceed
797 void notify_server_start()
798 {
799 std::unique_lock<std::mutex> lock(start_mutex_);
800 server_started_ = true;
801 cv_started_.notify_all();
802 }
803
804 void set_static_routes_added() {
805 static_routes_added_ = true;
806 }
807
808 bool are_static_routes_added() {
809 return static_routes_added_;
810 }
811
812 private:
813 std::uint8_t timeout_{5};
814 uint16_t port_ = 80;
815 unsigned int concurrency_ = 2;
816 std::atomic_bool is_bound_ = false;
817 uint64_t max_payload_{UINT64_MAX};
818 std::string server_name_ = std::string("Crow/") + VERSION;
819 std::string bindaddr_ = "0.0.0.0";
820 bool use_unix_ = false;
821 size_t res_stream_threshold_ = 1048576;
822 Router router_;
823 bool static_routes_added_{false};
824
825#ifdef CROW_ENABLE_COMPRESSION
826 compression::algorithm comp_algorithm_;
827 bool compression_used_{false};
828#endif
829
830 std::chrono::milliseconds tick_interval_;
831 std::function<void()> tick_function_;
832
833 std::tuple<Middlewares...> middlewares_;
834
835#ifdef CROW_ENABLE_SSL
836 std::unique_ptr<ssl_server_t> ssl_server_;
837 bool ssl_used_{false};
838 ssl_context_t ssl_context_{asio::ssl::context::sslv23};
839#endif
840
841 std::unique_ptr<server_t> server_;
842 std::unique_ptr<unix_server_t> unix_server_;
843
844 std::vector<int> signals_{SIGINT, SIGTERM};
845
846 bool server_started_{false};
847 std::condition_variable cv_started_;
848 std::mutex start_mutex_;
849 std::vector<crow::websocket::connection*> websockets_;
850 };
851
852 /// \brief Alias of Crow<Middlewares...>. Useful if you want
853 /// a instance of an Crow application that require Middlewares
854 template<typename... Middlewares>
855 using App = Crow<Middlewares...>;
856
857 /// \brief Alias of Crow<>. Useful if you want a instance of
858 /// an Crow application that doesn't require of Middlewares
860} // namespace crow
A blueprint can be considered a smaller section of a Crow app, specifically where the router is conce...
Definition routing.h:1101
Definition routing.h:344
The main server application class.
Definition app.h:199
void stop()
Stop the server.
Definition app.h:604
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:415
std::cv_status wait_for_server_start(std::chrono::milliseconds wait_timeout=std::chrono::milliseconds(3000))
Wait until the server has properly started.
Definition app.h:758
void run()
Run the server.
Definition app.h:526
self_t & timeout(std::uint8_t timeout)
Set the connection timeout in seconds (default is 5)
Definition app.h:337
uint64_t websocket_max_payload()
Get the default max payload size for websockets.
Definition app.h:278
void add_static_dir()
Go through the rules, upgrade them if possible, and add them to the list of rules.
Definition app.h:506
Server< Crow, UnixSocketAcceptor, UnixSocketAdaptor, Middlewares... > unix_server_t
An HTTP server that runs on unix domain socket.
Definition app.h:207
Crow self_t
This is the crow application.
Definition app.h:202
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:676
self_t & server_name(std::string server_name)
Set the server name included in the 'Server' HTTP response header. If set to an empty string,...
Definition app.h:344
self_t & ssl_file(const std::string &crt_filename, const std::string &key_filename)
Use certificate and key files for SSL.
Definition app.h:651
void handle_upgrade(const request &req, response &res, Adaptor &&adaptor)
Process an Upgrade request.
Definition app.h:225
self_t & multithreaded()
Run the server on multiple threads using all available threads.
Definition app.h:378
self_t & tick(Duration d, Func f)
Set a custom duration and function to run on every tick.
Definition app.h:453
self_t & local_socket_path(std::string path)
Disable tcp/ip and use unix domain socket instead.
Definition app.h:364
void debug_print()
Print the routing paths defined for each HTTP method.
Definition app.h:641
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:237
std::string bindaddr()
Get the address that Crow will handle requests on.
Definition app.h:358
self_t & port(std::uint16_t port)
Set the port that Crow will handle requests on.
Definition app.h:301
bool is_bound() const
Get whether address that Crow will handle requests on is bound.
Definition app.h:332
std::uint16_t port() const
Get the port that Crow will handle requests on.
Definition app.h:308
self_t & concurrency(unsigned int concurrency)
Run the server on multiple threads using a specific number.
Definition app.h:384
Server< Crow, TCPAcceptor, SSLAdaptor, Middlewares... > ssl_server_t
An HTTP server that runs on SSL with an SSLAdaptor.
Definition app.h:210
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:440
self_t & bindaddr(std::string bindaddr)
The IP address that Crow will handle requests on (default is 0.0.0.0)
Definition app.h:351
Server< Crow, TCPAcceptor, SocketAdaptor, Middlewares... > server_t
The HTTP server.
Definition app.h:205
void validate()
A wrapper for validate() in the router.
Definition app.h:520
Crow(Ts &&... ts)
Construct Crow with a subset of middleware.
Definition app.h:217
CatchallRule & catchall_route()
Create a route for any requests without a proper route (Use CROW_CATCHALL_ROUTE instead)
Definition app.h:265
self_t & websocket_max_payload(uint64_t max_payload)
Set the default max payload size for websockets.
Definition app.h:271
void handle_full(request &req, response &res)
Process a fully parsed request from start to finish (primarily used for debugging)
Definition app.h:243
self_t & ssl_file(const std::string &pem_filename)
Use .pem file for SSL.
Definition app.h:664
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:231
std::string local_socket_path()
Get the unix domain socket path.
Definition app.h:372
DynamicRule & route_dynamic(const std::string &rule)
Create a dynamic route using a rule (Use CROW_ROUTE instead)
Definition app.h:251
self_t & loglevel(LogLevel level)
Set the server's log level.
Definition app.h:406
void add_blueprint()
Apply blueprints.
Definition app.h:481
void address_is_bound()
Set status variable to note that the address that Crow will handle requests on is bound.
Definition app.h:327
auto route(const std::string &rule) -> typename std::invoke_result< decltype(&Router::new_rule_tagged< Tag >), Router, const std::string & >::type
Create a route using a rule (Use CROW_ROUTE instead)
Definition app.h:258
size_t & stream_threshold()
Get the response body size (in bytes) beyond which Crow automatically streams responses.
Definition app.h:422
std::uint16_t concurrency() const
Get the number of threads that server is using.
Definition app.h:393
std::future< void > run_async()
Non-blocking version of run()
Definition app.h:596
A rule that can change its parameters during runtime.
Definition routing.h:578
Handles matching requests to existing rules and upgrade requests.
Definition routing.h:1256
Definition http_server.h:45
The main namespace of the library. In this namespace is defined the most important classes and functi...
Definition socket_adaptors.h:184
A wrapper for the asio::ip::tcp::socket and asio::ssl::stream.
Definition socket_adaptors.h:40
Definition socket_acceptors.h:31
Definition socket_acceptors.h:61
Definition socket_adaptors.h:112
An HTTP request.
Definition http_request.h:36
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:62