Skip to content

Commit c1c0635

Browse files
http: Refactored and added comments to servers
1 parent 19888ed commit c1c0635

5 files changed

Lines changed: 184 additions & 67 deletions

File tree

api/net/http/client.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
namespace http {
3030

31+
using Response_handler = Client_connection::Response_handler;
32+
3133
class Client {
3234
public:
3335
using TCP = net::TCP;

api/net/http/secure_server.hpp

Lines changed: 96 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,55 +25,117 @@
2525

2626
namespace http {
2727

28+
/**
29+
* @brief A secure HTTPS server.
30+
*/
2831
class Secure_server : public http::Server
2932
{
3033
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(
3248
const std::string& name,
3349
fs::Dirent& ca_key,
3450
fs::Dirent& ca_cert,
3551
fs::Dirent& server_key,
36-
TCP& tcp,
37-
Request_handler cb);
52+
net::TCP& tcp,
53+
Server_args&&... server_args);
3854

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(
4767
Botan::Credentials_Manager* in_credman,
4868
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);
5671

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);
7185

7286
private:
7387
Botan::RandomNumberGenerator& rng;
7488
std::unique_ptr<Botan::Credentials_Manager> credman;
75-
};
7689

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
78140

79141
#endif

api/net/http/server.hpp

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ namespace http {
3434
// Used in HTTP server - invoked when a Request is received
3535
using Request_handler = delegate<void(Request_ptr, Response_writer_ptr)>;
3636

37+
/**
38+
* @brief A simple HTTP server.
39+
*/
3740
class Server {
3841
public:
3942
using Request_handler = http::Request_handler;
@@ -64,7 +67,7 @@ namespace http {
6467
*
6568
* @param[in] port The port to listen on
6669
*/
67-
virtual void listen(uint16_t port);
70+
void listen(const uint16_t port);
6871

6972
/**
7073
* @brief Setup handler for when a Request is received
@@ -94,13 +97,34 @@ namespace http {
9497
virtual ~Server();
9598

9699
protected:
97-
delegate<void(TCP_conn)> on_connect;
100+
TCP& tcp_;
101+
102+
/**
103+
* @brief Binds to a TCP port and sets up a connect event.
104+
* This is called from listen()
105+
*
106+
* @param[in] port The port
107+
*/
108+
virtual void bind(const uint16_t port);
109+
110+
/**
111+
* @brief Handle a newly connected TCP client.
112+
*
113+
* @param[in] conn The TCP connection
114+
*/
115+
virtual void on_connect(TCP_conn conn)
116+
{ connect(std::make_unique<Connection::Stream>(std::move(conn))); }
117+
118+
/**
119+
* @brief Connect the stream to the server.
120+
*
121+
* @param[in] stream The stream
122+
*/
98123
void connect(Connection::Stream_ptr stream);
99124

100125
private:
101126
friend class Server_connection;
102127

103-
TCP& tcp_;
104128
Request_handler on_request_;
105129
Connection_set connections_;
106130
Index_set free_idx_;
@@ -114,14 +138,27 @@ namespace http {
114138
Stat& stat_req_bad_;
115139
Stat& stat_timeouts_;
116140

117-
void connected(TCP_conn conn) {
118-
connect(std::make_unique<Connection::Stream>(conn));
119-
}
120-
141+
/**
142+
* @brief Close the given Server_connection
143+
*
144+
* @param <unnamed> The server connection to be closed
145+
*/
121146
void close(Server_connection&);
122147

148+
/**
149+
* @brief Timeout (close) all clients that been idle for more than limit.
150+
*
151+
* @param[in] <unnamed> A timer id (unused)
152+
*/
123153
void timeout_clients(int32_t);
124154

155+
/**
156+
* @brief Receive a incoming HTTP request
157+
*
158+
* @param[in] <unnamed> The HTTP reuqest
159+
* @param[in] code The HTTP status code
160+
* @param <unnamed> The server connection which the req arrived from
161+
*/
125162
void receive(Request_ptr, status_t code, Server_connection&);
126163

127164
}; // < class Server

src/net/http/secure_server.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is a part of the IncludeOS unikernel - www.includeos.org
22
//
3-
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
3+
// Copyright 2017 Oslo and Akershus University College of Applied Sciences
44
// and Alfred Bratterud
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -31,18 +31,18 @@ inline std::unique_ptr<Botan::Private_Key> read_pkey(fs::Dirent& key_file)
3131

3232
namespace http
3333
{
34-
Secure_server::Secure_server(
35-
const std::string& server_name,
36-
fs::Dirent& file_ca_key,
37-
fs::Dirent& file_ca_cert,
38-
fs::Dirent& file_server_key,
39-
TCP& tcp,
40-
Request_handler cb)
41-
: http::Server(tcp, cb),
42-
rng(get_rng())
34+
35+
Botan::RandomNumberGenerator& Secure_server::get_rng()
4336
{
44-
on_connect = {this, &Secure_server::secure_connect};
37+
return ::get_rng();
38+
}
4539

40+
void Secure_server::load_credentials(
41+
const std::string& server_name,
42+
fs::Dirent& file_ca_key,
43+
fs::Dirent& file_ca_cert,
44+
fs::Dirent& file_server_key)
45+
{
4646
// load CA certificate
4747
assert(file_ca_cert.is_valid());
4848
auto ca_cert = file_ca_cert.read();
@@ -62,16 +62,28 @@ namespace http
6262
this->credman.reset(credman);
6363
}
6464

65-
Secure_server::Secure_server(
66-
const std::string& server_name,
67-
fs::Dirent& file_ca_key,
68-
fs::Dirent& file_ca_cert,
69-
fs::Dirent& file_server_key,
70-
TCP& tcp)
71-
: Secure_server(
72-
server_name,
73-
file_ca_key, file_ca_cert, file_server_key,
74-
tcp, nullptr)
75-
{}
65+
void Secure_server::bind(const uint16_t port)
66+
{
67+
tcp_.bind(port).on_connect({this, &Secure_server::on_connect});
68+
INFO("HTTPS Server", "Listening on port %u", port);
69+
}
70+
71+
void Secure_server::on_connect(TCP_conn conn)
72+
{
73+
auto* ptr = new net::tls::Server(std::move(conn), rng, *credman);
74+
75+
ptr->on_connect(
76+
[this, ptr] (net::Stream&)
77+
{
78+
// create and pass TLS socket
79+
connect(std::unique_ptr<net::tls::Server>(ptr));
80+
});
81+
82+
// this is ok due to the created Server_connection inside
83+
// connect assigns a new on_close
84+
ptr->on_close([ptr] {
85+
delete ptr;
86+
});
87+
}
7688

7789
}

src/net/http/server.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ namespace http {
2222
const Server::idle_duration Server::DEFAULT_IDLE_TIMEOUT{std::chrono::seconds(60)};
2323

2424
Server::Server(TCP& tcp, Request_handler cb, idle_duration timeout)
25-
: on_connect{this, &Server::connected},
26-
tcp_(tcp),
25+
: tcp_(tcp),
2726
on_request_(std::move(cb)),
2827
keep_alive_(true),
2928
timer_id_(Timers::UNUSED_ID),
@@ -39,8 +38,7 @@ namespace http {
3938
{
4039
Expects(on_request_ != nullptr);
4140

42-
tcp_.bind(port).on_connect(this->on_connect);
43-
INFO("HTTP Server", "Listening on port %u", port);
41+
bind(port);
4442

4543
using namespace std::chrono;
4644

@@ -63,6 +61,12 @@ namespace http {
6361
}
6462
}
6563

64+
void Server::bind(const uint16_t port)
65+
{
66+
tcp_.bind(port).on_connect({this, &Server::on_connect});
67+
INFO("HTTP Server", "Listening on port %u", port);
68+
}
69+
6670
void Server::connect(Connection::Stream_ptr stream)
6771
{
6872
debug("Connection attempt from %s\n", stream->remote().to_string().c_str());

0 commit comments

Comments
 (0)