|
25 | 25 |
|
26 | 26 | namespace http { |
27 | 27 |
|
| 28 | +/** |
| 29 | + * @brief A secure HTTPS server. |
| 30 | + */ |
28 | 31 | class Secure_server : public http::Server |
29 | 32 | { |
30 | 33 | public: |
31 | | - Secure_server( |
| 34 | + /** |
| 35 | + * @brief Construct a HTTPS server with the necessary certificates and keys. |
| 36 | + * |
| 37 | + * @param[in] name The name |
| 38 | + * @param ca_key The ca key |
| 39 | + * @param ca_cert The ca cert |
| 40 | + * @param server_key The server key |
| 41 | + * @param tcp The tcp |
| 42 | + * @param[in] server_args A list of args for constructing the underlying HTTP server |
| 43 | + * |
| 44 | + * @tparam Server_args Construct arguments to HTTP Server |
| 45 | + */ |
| 46 | + template <typename... Server_args> |
| 47 | + inline Secure_server( |
32 | 48 | const std::string& name, |
33 | 49 | fs::Dirent& ca_key, |
34 | 50 | fs::Dirent& ca_cert, |
35 | 51 | fs::Dirent& server_key, |
36 | | - TCP& tcp, |
37 | | - Request_handler cb); |
| 52 | + net::TCP& tcp, |
| 53 | + Server_args&&... server_args); |
38 | 54 |
|
39 | | - Secure_server( |
40 | | - const std::string& name, |
41 | | - fs::Dirent& ca_key, |
42 | | - fs::Dirent& ca_cert, |
43 | | - fs::Dirent& server_key, |
44 | | - TCP& tcp); |
45 | | - |
46 | | - Secure_server( |
| 55 | + /** |
| 56 | + * @brief Construct a HTTPS server with a credential manager and rng. |
| 57 | + * |
| 58 | + * @param in_credman In credman |
| 59 | + * @param in_rng In random number generator |
| 60 | + * @param tcp The tcp |
| 61 | + * @param[in] server_args A list of args for constructing the underlying HTTP server |
| 62 | + * |
| 63 | + * @tparam Server_args Server_args Construct arguments to HTTP Server |
| 64 | + */ |
| 65 | + template <typename... Server_args> |
| 66 | + inline Secure_server( |
47 | 67 | Botan::Credentials_Manager* in_credman, |
48 | 68 | Botan::RandomNumberGenerator& in_rng, |
49 | | - TCP& tcp, |
50 | | - Request_handler cb) |
51 | | - : http::Server(tcp, cb), rng(in_rng), credman(in_credman) |
52 | | - { |
53 | | - assert(credman != nullptr); |
54 | | - on_connect = {this, &Secure_server::secure_connect}; |
55 | | - } |
| 69 | + net::TCP& tcp, |
| 70 | + Server_args&&... server_args); |
56 | 71 |
|
57 | | - void secure_connect(TCP_conn conn) |
58 | | - { |
59 | | - auto* ptr = new net::tls::Server(conn, rng, *credman); |
60 | | - |
61 | | - ptr->on_connect( |
62 | | - [this, ptr] (net::Stream&) |
63 | | - { |
64 | | - // create and pass TLS socket |
65 | | - Server::connect(std::unique_ptr<net::tls::Server>(ptr)); |
66 | | - }); |
67 | | - ptr->on_close([ptr] { |
68 | | - delete ptr; |
69 | | - }); |
70 | | - } |
| 72 | + /** |
| 73 | + * @brief Loads credentials. |
| 74 | + * |
| 75 | + * @param[in] name The name |
| 76 | + * @param ca_key The ca key |
| 77 | + * @param ca_cert The ca cert |
| 78 | + * @param server_key The server key |
| 79 | + */ |
| 80 | + void load_credentials( |
| 81 | + const std::string& name, |
| 82 | + fs::Dirent& ca_key, |
| 83 | + fs::Dirent& ca_cert, |
| 84 | + fs::Dirent& server_key); |
71 | 85 |
|
72 | 86 | private: |
73 | 87 | Botan::RandomNumberGenerator& rng; |
74 | 88 | std::unique_ptr<Botan::Credentials_Manager> credman; |
75 | | -}; |
76 | 89 |
|
77 | | -} // http |
| 90 | + /** |
| 91 | + * @brief Binds TCP to pass all new connections to this on_connect. |
| 92 | + * |
| 93 | + * @param[in] port The port |
| 94 | + */ |
| 95 | + void bind(const uint16_t port) override; |
| 96 | + |
| 97 | + /** |
| 98 | + * @brief Try to upgrade a newly established TCP connection to a TLS connection. |
| 99 | + * |
| 100 | + * @param[in] conn The TCP connection |
| 101 | + */ |
| 102 | + void on_connect(TCP_conn conn) override; |
| 103 | + |
| 104 | + /** |
| 105 | + * @brief Gets the random number generator. |
| 106 | + * |
| 107 | + * @return The random number generator. |
| 108 | + */ |
| 109 | + static Botan::RandomNumberGenerator& get_rng(); |
| 110 | + |
| 111 | +}; // < class Secure_server |
| 112 | + |
| 113 | +template <typename... Server_args> |
| 114 | +inline Secure_server::Secure_server( |
| 115 | + const std::string& name, |
| 116 | + fs::Dirent& ca_key, |
| 117 | + fs::Dirent& ca_cert, |
| 118 | + fs::Dirent& server_key, |
| 119 | + net::TCP& tcp, |
| 120 | + Server_args&&... server_args) |
| 121 | + : Server{tcp, std::forward<Server>(server_args)...}, |
| 122 | + rng(get_rng()) |
| 123 | +{ |
| 124 | + load_credentials(name, ca_key, ca_cert, server_key); |
| 125 | +} |
| 126 | + |
| 127 | +template <typename... Server_args> |
| 128 | +inline Secure_server::Secure_server( |
| 129 | + Botan::Credentials_Manager* in_credman, |
| 130 | + Botan::RandomNumberGenerator& in_rng, |
| 131 | + net::TCP& tcp, |
| 132 | + Server_args&&... server_args) |
| 133 | + : Server{tcp, std::forward(server_args)...}, |
| 134 | + rng(in_rng), credman(in_credman) |
| 135 | +{ |
| 136 | + assert(credman != nullptr); |
| 137 | +} |
| 138 | + |
| 139 | +} // < namespace http |
78 | 140 |
|
79 | 141 | #endif |
0 commit comments