Skip to content

Commit eb44ec0

Browse files
authored
Merge pull request #1241 from AndreasAakesson/dev
Made it easier to create WebSocket from HTTP Request/Response
2 parents b116650 + 511334a commit eb44ec0

16 files changed

Lines changed: 657 additions & 210 deletions

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ if(tests)
138138
ExternalProject_Add(unittests
139139
PREFIX unittests
140140
SOURCE_DIR ${INCLUDEOS_ROOT}/test
141+
BINARY_DIR unittests
141142
CMAKE_ARGS -DINCLUDEOS_ROOT=${INCLUDEOS_ROOT} -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
142143
)
143144
#add_subdirectory(test)

api/net/http/client.hpp

Lines changed: 11 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;
@@ -164,6 +166,15 @@ namespace http {
164166
void on_send(Request_handler cb)
165167
{ on_send_ = std::move(cb); }
166168

169+
/**
170+
* @brief Returns the Origin for the Client as a string.
171+
* Currently returns the IP address to the stack.
172+
*
173+
* @return The origin as a string
174+
*/
175+
std::string origin() const
176+
{ return tcp_.stack().ip_addr().to_string(); }
177+
167178
private:
168179
friend class Client_connection;
169180

api/net/http/header_fields.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern Field If_None_Match;
4242
extern Field If_Range;
4343
extern Field If_Unmodified_Since;
4444
extern Field Max_Forwards;
45+
extern Field Origin;
4546
extern Field Proxy_Authorization;
4647
extern Field Range;
4748
extern Field Referer;

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: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ 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:
42+
using Request_handler = http::Request_handler;
3943
using TCP = net::TCP;
4044
using TCP_conn = net::tcp::Connection_ptr;
4145

@@ -63,7 +67,7 @@ namespace http {
6367
*
6468
* @param[in] port The port to listen on
6569
*/
66-
virtual void listen(uint16_t port);
70+
void listen(const uint16_t port);
6771

6872
/**
6973
* @brief Setup handler for when a Request is received
@@ -93,13 +97,34 @@ namespace http {
9397
virtual ~Server();
9498

9599
protected:
96-
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+
*/
97123
void connect(Connection::Stream_ptr stream);
98124

99125
private:
100126
friend class Server_connection;
101127

102-
TCP& tcp_;
103128
Request_handler on_request_;
104129
Connection_set connections_;
105130
Index_set free_idx_;
@@ -113,14 +138,27 @@ namespace http {
113138
Stat& stat_req_bad_;
114139
Stat& stat_timeouts_;
115140

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

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

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+
*/
124162
void receive(Request_ptr, status_t code, Server_connection&);
125163

126164
}; // < class Server

0 commit comments

Comments
 (0)