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){
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, SocketAdaptor, Middlewares...>;
206
207#ifdef CROW_ENABLE_SSL
208 /// \brief An HTTP server that runs on SSL with an SSLAdaptor
209 using ssl_server_t = Server<Crow, SSLAdaptor, Middlewares...>;
210#endif
211 Crow()
212 {}
213
214 /// \brief Construct Crow with a subset of middleware
215 template<typename... Ts>
216 Crow(Ts&&... ts):
217 middlewares_(make_middleware_tuple(std::forward<Ts>(ts)...))
218 {}
219
220 /// \brief Process an Upgrade request
221 ///
222 /// Currently used to upgrade an HTTP connection to a WebSocket connection
223 template<typename Adaptor>
224 void handle_upgrade(const request& req, response& res, Adaptor&& adaptor)
225 {
226 router_.handle_upgrade(req, res, adaptor);
227 }
228
229 /// \brief Process only the method and URL of a request and provide a route (or an error response)
230 std::unique_ptr<routing_handle_result> handle_initial(request& req, response& res)
231 {
232 return router_.handle_initial(req, res);
233 }
234
235 /// \brief Process the fully parsed request and generate a response for it
236 void handle(request& req, response& res, std::unique_ptr<routing_handle_result>& found)
237 {
238 router_.handle<self_t>(req, res, *found);
239 }
240
241 /// \brief Process a fully parsed request from start to finish (primarily used for debugging)
242 void handle_full(request& req, response& res)
243 {
244 auto found = handle_initial(req, res);
245 if (found->rule_index)
246 handle(req, res, found);
247 }
248
249 /// \brief Create a dynamic route using a rule (**Use CROW_ROUTE instead**)
250 DynamicRule& route_dynamic(const std::string& rule)
251 {
252 return router_.new_rule_dynamic(rule);
253 }
254
255 /// \brief Create a route using a rule (**Use CROW_ROUTE instead**)
256 template<uint64_t Tag>
257#ifdef CROW_GCC83_WORKAROUND
258 auto& route(const std::string& rule)
259#else
260 auto route(const std::string& rule)
261#endif
262#if defined CROW_CAN_USE_CPP17 && !defined CROW_GCC83_WORKAROUND
263 -> typename std::invoke_result<decltype(&Router::new_rule_tagged<Tag>), Router, const std::string&>::type
264#elif !defined CROW_GCC83_WORKAROUND
265 -> typename std::result_of<decltype (&Router::new_rule_tagged<Tag>)(Router, const std::string&)>::type
266#endif
267 {
268 return router_.new_rule_tagged<Tag>(rule);
269 }
270
271 /// \brief Create a route for any requests without a proper route (**Use CROW_CATCHALL_ROUTE instead**)
273 {
274 return router_.catchall_rule();
275 }
276
277 /// \brief Set the default max payload size for websockets
278 self_t& websocket_max_payload(uint64_t max_payload)
279 {
280 max_payload_ = max_payload;
281 return *this;
282 }
283
284 /// \brief Get the default max payload size for websockets
286 {
287 return max_payload_;
288 }
289
290 self_t& signal_clear()
291 {
292 signals_.clear();
293 return *this;
294 }
295
296 self_t& signal_add(int signal_number)
297 {
298 signals_.push_back(signal_number);
299 return *this;
300 }
301
302 std::vector<int> signals()
303 {
304 return signals_;
305 }
306
307 /// \brief Set the port that Crow will handle requests on
308 self_t& port(std::uint16_t port)
309 {
310 port_ = port;
311 return *this;
312 }
313
314 /// \brief Get the port that Crow will handle requests on
315 std::uint16_t port()
316 {
317 return port_;
318 }
319
320 /// \brief Set the connection timeout in seconds (default is 5)
321 self_t& timeout(std::uint8_t timeout)
322 {
323 timeout_ = timeout;
324 return *this;
325 }
326
327 /// \brief Set the server name
329 {
330 server_name_ = server_name;
331 return *this;
332 }
333
334 /// \brief The IP address that Crow will handle requests on (default is 0.0.0.0)
336 {
337 bindaddr_ = bindaddr;
338 return *this;
339 }
340
341 /// \brief Get the address that Crow will handle requests on
342 std::string bindaddr()
343 {
344 return bindaddr_;
345 }
346
347 /// \brief Run the server on multiple threads using all available threads
349 {
350 return concurrency(std::thread::hardware_concurrency());
351 }
352
353 /// \brief Run the server on multiple threads using a specific number
355 {
356 if (concurrency < 2) // Crow can have a minimum of 2 threads running
357 concurrency = 2;
358 concurrency_ = concurrency;
359 return *this;
360 }
361
362 /// \brief Get the number of threads that server is using
363 std::uint16_t concurrency()
364 {
365 return concurrency_;
366 }
367
368 /// \brief Set the server's log level
369 ///
370 /// Possible values are:
371 /// - crow::LogLevel::Debug (0)
372 /// - crow::LogLevel::Info (1)
373 /// - crow::LogLevel::Warning (2)
374 /// - crow::LogLevel::Error (3)
375 /// - crow::LogLevel::Critical (4)
376 self_t& loglevel(LogLevel level)
377 {
378 crow::logger::setLogLevel(level);
379 return *this;
380 }
381
382 /// \brief Set the response body size (in bytes) beyond which Crow automatically streams responses (Default is 1MiB)
383 ///
384 /// Any streamed response is unaffected by Crow's timer, and therefore won't timeout before a response is fully sent.
385 self_t& stream_threshold(size_t threshold)
386 {
387 res_stream_threshold_ = threshold;
388 return *this;
389 }
390
391 /// \brief Get the response body size (in bytes) beyond which Crow automatically streams responses
393 {
394 return res_stream_threshold_;
395 }
396
397
398 self_t& register_blueprint(Blueprint& blueprint)
399 {
400 router_.register_blueprint(blueprint);
401 return *this;
402 }
403
404 /// \brief Set the function to call to handle uncaught exceptions generated in routes (Default generates error 500).
405 ///
406 /// The function must have the following signature: void(crow::response&).
407 /// It must set the response passed in argument to the function, which will be sent back to the client.
408 /// See Router::default_exception_handler() for the default implementation.
409 template<typename Func>
411 {
412 router_.exception_handler() = std::forward<Func>(f);
413 return *this;
414 }
415
416 std::function<void(crow::response&)>& exception_handler()
417 {
418 return router_.exception_handler();
419 }
420
421 /// \brief Set a custom duration and function to run on every tick
422 template<typename Duration, typename Func>
423 self_t& tick(Duration d, Func f)
424 {
425 tick_interval_ = std::chrono::duration_cast<std::chrono::milliseconds>(d);
426 tick_function_ = f;
427 return *this;
428 }
429
430#ifdef CROW_ENABLE_COMPRESSION
431
432 self_t& use_compression(compression::algorithm algorithm)
433 {
434 comp_algorithm_ = algorithm;
435 compression_used_ = true;
436 return *this;
437 }
438
439 compression::algorithm compression_algorithm()
440 {
441 return comp_algorithm_;
442 }
443
444 bool compression_used() const
445 {
446 return compression_used_;
447 }
448#endif
449
450 /// \brief Apply blueprints
452 {
453#if defined(__APPLE__) || defined(__MACH__)
454 if (router_.blueprints().empty()) return;
455#endif
456
457 for (Blueprint* bp : router_.blueprints())
458 {
459 if (bp->static_dir().empty()) continue;
460
461 auto static_dir_ = crow::utility::normalize_path(bp->static_dir());
462
463 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) {
464 utility::sanitize_filename(file_path_partial);
465 res.set_static_file_info_unsafe(static_dir_ + file_path_partial);
466 res.end();
467 });
468 }
469
470 router_.validate_bp();
471 }
472
473 /// \brief Go through the rules, upgrade them if possible, and add them to the list of rules
475 {
476 if (are_static_routes_added()) return;
477 auto static_dir_ = crow::utility::normalize_path(CROW_STATIC_DIRECTORY);
478
479 route<crow::black_magic::get_parameter_tag(CROW_STATIC_ENDPOINT)>(CROW_STATIC_ENDPOINT)([static_dir_](crow::response& res, std::string file_path_partial) {
480 utility::sanitize_filename(file_path_partial);
481 res.set_static_file_info_unsafe(static_dir_ + file_path_partial);
482 res.end();
483 });
484 set_static_routes_added();
485 }
486
487 /// \brief A wrapper for `validate()` in the router
488 void validate()
489 {
490 router_.validate();
491 }
492
493 /// \brief Run the server
494 void run()
495 {
496#ifndef CROW_DISABLE_STATIC_DIR
499#endif
500 validate();
501
502#ifdef CROW_ENABLE_SSL
503 if (ssl_used_)
504 {
505 ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_)));
506 ssl_server_->set_tick_function(tick_interval_, tick_function_);
507 ssl_server_->signal_clear();
508 for (auto snum : signals_)
509 {
510 ssl_server_->signal_add(snum);
511 }
512 notify_server_start();
513 ssl_server_->run();
514 }
515 else
516#endif
517 {
518 server_ = std::move(std::unique_ptr<server_t>(new server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, nullptr)));
519 server_->set_tick_function(tick_interval_, tick_function_);
520 for (auto snum : signals_)
521 {
522 server_->signal_add(snum);
523 }
524 notify_server_start();
525 server_->run();
526 }
527 }
528
529 /// \brief Non-blocking version of \ref run()
530 ///
531 /// The output from this method needs to be saved into a variable!
532 /// Otherwise the call will be made on the same thread.
533 std::future<void> run_async()
534 {
535 return std::async(std::launch::async, [&] {
536 this->run();
537 });
538 }
539
540 /// \brief Stop the server
541 void stop()
542 {
543#ifdef CROW_ENABLE_SSL
544 if (ssl_used_)
545 {
546 if (ssl_server_) { ssl_server_->stop(); }
547 }
548 else
549#endif
550 {
551 // TODO(EDev): Move these 6 lines to a method in http_server.
552 std::vector<crow::websocket::connection*> websockets_to_close = websockets_;
553 for (auto websocket : websockets_to_close)
554 {
555 CROW_LOG_INFO << "Quitting Websocket: " << websocket;
556 websocket->close("Server Application Terminated");
557 }
558 if (server_) { server_->stop(); }
559 }
560 }
561
562 void add_websocket(crow::websocket::connection* conn)
563 {
564 websockets_.push_back(conn);
565 }
566
567 void remove_websocket(crow::websocket::connection* conn)
568 {
569 websockets_.erase(std::remove(websockets_.begin(), websockets_.end(), conn), websockets_.end());
570 }
571
572 /// \brief Print the routing paths defined for each HTTP method
574 {
575 CROW_LOG_DEBUG << "Routing:";
576 router_.debug_print();
577 }
578
579
580#ifdef CROW_ENABLE_SSL
581
582 /// \brief Use certificate and key files for SSL
583 self_t& ssl_file(const std::string& crt_filename, const std::string& key_filename)
584 {
585 ssl_used_ = true;
586 ssl_context_.set_verify_mode(asio::ssl::verify_peer);
587 ssl_context_.set_verify_mode(asio::ssl::verify_client_once);
588 ssl_context_.use_certificate_file(crt_filename, ssl_context_t::pem);
589 ssl_context_.use_private_key_file(key_filename, ssl_context_t::pem);
590 ssl_context_.set_options(
591 asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3);
592 return *this;
593 }
594
595 /// \brief Use `.pem` file for SSL
596 self_t& ssl_file(const std::string& pem_filename)
597 {
598 ssl_used_ = true;
599 ssl_context_.set_verify_mode(asio::ssl::verify_peer);
600 ssl_context_.set_verify_mode(asio::ssl::verify_client_once);
601 ssl_context_.load_verify_file(pem_filename);
602 ssl_context_.set_options(
603 asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3);
604 return *this;
605 }
606
607 /// \brief Use certificate chain and key files for SSL
608 self_t& ssl_chainfile(const std::string& crt_filename, const std::string& key_filename)
609 {
610 ssl_used_ = true;
611 ssl_context_.set_verify_mode(asio::ssl::verify_peer);
612 ssl_context_.set_verify_mode(asio::ssl::verify_client_once);
613 ssl_context_.use_certificate_chain_file(crt_filename);
614 ssl_context_.use_private_key_file(key_filename, ssl_context_t::pem);
615 ssl_context_.set_options(
616 asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3);
617 return *this;
618 }
619
620 self_t& ssl(asio::ssl::context&& ctx)
621 {
622 ssl_used_ = true;
623 ssl_context_ = std::move(ctx);
624 return *this;
625 }
626
627 bool ssl_used() const
628 {
629 return ssl_used_;
630 }
631#else
632
633 template<typename T, typename... Remain>
634 self_t& ssl_file(T&&, Remain&&...)
635 {
636 // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined.
637 static_assert(
638 // make static_assert dependent to T; always false
639 std::is_base_of<T, void>::value,
640 "Define CROW_ENABLE_SSL to enable ssl support.");
641 return *this;
642 }
643
644 template<typename T, typename... Remain>
645 self_t& ssl_chainfile(T&&, Remain&&...)
646 {
647 // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined.
648 static_assert(
649 // make static_assert dependent to T; always false
650 std::is_base_of<T, void>::value,
651 "Define CROW_ENABLE_SSL to enable ssl support.");
652 return *this;
653 }
654
655 template<typename T>
656 self_t& ssl(T&&)
657 {
658 // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined.
659 static_assert(
660 // make static_assert dependent to T; always false
661 std::is_base_of<T, void>::value,
662 "Define CROW_ENABLE_SSL to enable ssl support.");
663 return *this;
664 }
665
666 bool ssl_used() const
667 {
668 return false;
669 }
670#endif
671
672 // middleware
673 using context_t = detail::context<Middlewares...>;
674 using mw_container_t = std::tuple<Middlewares...>;
675 template<typename T>
676 typename T::context& get_context(const request& req)
677 {
678 static_assert(black_magic::contains<T, Middlewares...>::value, "App doesn't have the specified middleware type.");
679 auto& ctx = *reinterpret_cast<context_t*>(req.middleware_context);
680 return ctx.template get<T>();
681 }
682
683 template<typename T>
684 T& get_middleware()
685 {
686 return utility::get_element_by_type<T, Middlewares...>(middlewares_);
687 }
688
689 /// \brief Wait until the server has properly started
691 {
692 {
693 std::unique_lock<std::mutex> lock(start_mutex_);
694 if (!server_started_)
695 cv_started_.wait(lock);
696 }
697 if (server_)
698 server_->wait_for_start();
699#ifdef CROW_ENABLE_SSL
700 else if (ssl_server_)
701 ssl_server_->wait_for_start();
702#endif
703 }
704
705 private:
706 template<typename... Ts>
707 std::tuple<Middlewares...> make_middleware_tuple(Ts&&... ts)
708 {
709 auto fwd = std::forward_as_tuple((ts)...);
710 return std::make_tuple(
711 std::forward<Middlewares>(
712 black_magic::tuple_extract<Middlewares, decltype(fwd)>(fwd))...);
713 }
714
715 /// \brief Notify anything using \ref wait_for_server_start() to proceed
716 void notify_server_start()
717 {
718 std::unique_lock<std::mutex> lock(start_mutex_);
719 server_started_ = true;
720 cv_started_.notify_all();
721 }
722
723 void set_static_routes_added() {
724 static_routes_added_ = true;
725 }
726
727 bool are_static_routes_added() {
728 return static_routes_added_;
729 }
730
731 private:
732 std::uint8_t timeout_{5};
733 uint16_t port_ = 80;
734 uint16_t concurrency_ = 2;
735 uint64_t max_payload_{UINT64_MAX};
736 std::string server_name_ = std::string("Crow/") + VERSION;
737 std::string bindaddr_ = "0.0.0.0";
738 size_t res_stream_threshold_ = 1048576;
739 Router router_;
740 bool static_routes_added_{false};
741
742#ifdef CROW_ENABLE_COMPRESSION
743 compression::algorithm comp_algorithm_;
744 bool compression_used_{false};
745#endif
746
747 std::chrono::milliseconds tick_interval_;
748 std::function<void()> tick_function_;
749
750 std::tuple<Middlewares...> middlewares_;
751
752#ifdef CROW_ENABLE_SSL
753 std::unique_ptr<ssl_server_t> ssl_server_;
754 bool ssl_used_{false};
755 ssl_context_t ssl_context_{asio::ssl::context::sslv23};
756#endif
757
758 std::unique_ptr<server_t> server_;
759
760 std::vector<int> signals_{SIGINT, SIGTERM};
761
762 bool server_started_{false};
763 std::condition_variable cv_started_;
764 std::mutex start_mutex_;
765 std::vector<crow::websocket::connection*> websockets_;
766 };
767
768 /// \brief Alias of Crow<Middlewares...>. Useful if you want
769 /// a instance of an Crow application that require Middlewares
770 template<typename... Middlewares>
771 using App = Crow<Middlewares...>;
772
773 /// \brief Alias of Crow<>. Useful if you want a instance of
774 /// an Crow application that doesn't require of Middlewares
776} // namespace 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 class.
Definition app.h:199
void stop()
Stop the server.
Definition app.h:541
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:385
void run()
Run the server.
Definition app.h:494
self_t & timeout(std::uint8_t timeout)
Set the connection timeout in seconds (default is 5)
Definition app.h:321
uint64_t websocket_max_payload()
Get the default max payload size for websockets.
Definition app.h:285
void add_static_dir()
Go through the rules, upgrade them if possible, and add them to the list of rules.
Definition app.h:474
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:608
Server< Crow, SocketAdaptor, Middlewares... > server_t
The HTTP server.
Definition app.h:205
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:260
void wait_for_server_start()
Wait until the server has properly started.
Definition app.h:690
self_t & server_name(std::string server_name)
Set the server name.
Definition app.h:328
self_t & ssl_file(const std::string &crt_filename, const std::string &key_filename)
Use certificate and key files for SSL.
Definition app.h:583
void handle_upgrade(const request &req, response &res, Adaptor &&adaptor)
Process an Upgrade request.
Definition app.h:224
self_t & multithreaded()
Run the server on multiple threads using all available threads.
Definition app.h:348
self_t & tick(Duration d, Func f)
Set a custom duration and function to run on every tick.
Definition app.h:423
void debug_print()
Print the routing paths defined for each HTTP method.
Definition app.h:573
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:236
std::uint16_t port()
Get the port that Crow will handle requests on.
Definition app.h:315
std::string bindaddr()
Get the address that Crow will handle requests on.
Definition app.h:342
self_t & port(std::uint16_t port)
Set the port that Crow will handle requests on.
Definition app.h:308
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:410
Server< Crow, SSLAdaptor, Middlewares... > ssl_server_t
An HTTP server that runs on SSL with an SSLAdaptor.
Definition app.h:209
self_t & bindaddr(std::string bindaddr)
The IP address that Crow will handle requests on (default is 0.0.0.0)
Definition app.h:335
void validate()
A wrapper for validate() in the router.
Definition app.h:488
Crow(Ts &&... ts)
Construct Crow with a subset of middleware.
Definition app.h:216
std::uint16_t concurrency()
Get the number of threads that server is using.
Definition app.h:363
CatchallRule & catchall_route()
Create a route for any requests without a proper route (Use CROW_CATCHALL_ROUTE instead)
Definition app.h:272
self_t & websocket_max_payload(uint64_t max_payload)
Set the default max payload size for websockets.
Definition app.h:278
void handle_full(request &req, response &res)
Process a fully parsed request from start to finish (primarily used for debugging)
Definition app.h:242
self_t & ssl_file(const std::string &pem_filename)
Use .pem file for SSL.
Definition app.h:596
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:230
DynamicRule & route_dynamic(const std::string &rule)
Create a dynamic route using a rule (Use CROW_ROUTE instead)
Definition app.h:250
self_t & loglevel(LogLevel level)
Set the server's log level.
Definition app.h:376
self_t & concurrency(std::uint16_t concurrency)
Run the server on multiple threads using a specific number.
Definition app.h:354
void add_blueprint()
Apply blueprints.
Definition app.h:451
size_t & stream_threshold()
Get the response body size (in bytes) beyond which Crow automatically streams responses.
Definition app.h:392
std::future< void > run_async()
Non-blocking version of run()
Definition app.h:533
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:43
The main namespace of the library. In this namespace is defined the most important classes and functi...
Definition socket_adaptors.h:107
A wrapper for the asio::ip::tcp::socket and asio::ssl::stream.
Definition socket_adaptors.h:39
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:38