Skip to content

Commit f274539

Browse files
authored
Merge pull request #1235 from fwsGonzo/dev
WebSockets
2 parents 2f6653c + d46be4b commit f274539

6 files changed

Lines changed: 621 additions & 4 deletions

File tree

api/net/http/secure_server.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ class Secure_server : public http::Server
3333
fs::Dirent& ca_cert,
3434
fs::Dirent& server_key,
3535
TCP& tcp,
36-
Request_handler cb
37-
);
36+
Request_handler cb);
37+
38+
Secure_server(
39+
fs::Dirent& ca_key,
40+
fs::Dirent& ca_cert,
41+
fs::Dirent& server_key,
42+
TCP& tcp);
3843

3944
Secure_server(
4045
Botan::Credentials_Manager* in_credman,
@@ -57,7 +62,6 @@ class Secure_server : public http::Server
5762
Server::connect(std::unique_ptr<net::tls::Server>(ptr));
5863
});
5964
ptr->on_close([ptr] {
60-
printf("Secure_HTTP::on_close on %s\n", ptr->to_string().c_str());
6165
delete ptr;
6266
});
6367
}

api/net/http/websocket.hpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// This file is a part of the IncludeOS unikernel - www.includeos.org
2+
//
3+
// Copyright 2016 Oslo and Akershus University College of Applied Sciences
4+
// and Alfred Bratterud
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
#pragma once
19+
#ifndef NET_HTTP_WEBSOCKET_HPP
20+
#define NET_HTTP_WEBSOCKET_HPP
21+
22+
#include <net/http/server.hpp>
23+
#include <net/http/client.hpp>
24+
25+
namespace http {
26+
27+
class WebSocket {
28+
public:
29+
typedef std::unique_ptr<WebSocket> WebSocket_ptr;
30+
// client connected
31+
typedef delegate<void(WebSocket_ptr)> connect_func;
32+
// server new client
33+
typedef delegate<bool(net::tcp::Socket, std::string)> accept_func;
34+
// data read (data, length)
35+
typedef delegate<void(const char*, size_t)> read_func;
36+
// closed (status code)
37+
typedef delegate<void(uint16_t)> close_func;
38+
// error (reason)
39+
typedef delegate<void(std::string)> error_func;
40+
41+
/// Server-side connection
42+
WebSocket(http::Request_ptr request,
43+
http::Response_writer_ptr response,
44+
accept_func = nullptr);
45+
/// Client-side connection
46+
static void
47+
connect(http::Client& client,
48+
std::string origin,
49+
uri::URI dest,
50+
connect_func callback);
51+
52+
enum mode_t {
53+
TEXT,
54+
BINARY
55+
};
56+
void write(const char* buffer, size_t len, mode_t = TEXT);
57+
void write(net::tcp::buffer_t, size_t len, mode_t = TEXT);
58+
59+
void write(const std::string& text)
60+
{
61+
write(text.c_str(), text.size(), TEXT);
62+
}
63+
64+
// close the websocket
65+
void close();
66+
67+
// user callbacks
68+
close_func on_close = nullptr;
69+
error_func on_error = nullptr;
70+
read_func on_read = nullptr;
71+
72+
bool is_alive() const noexcept {
73+
return this->conn != nullptr;
74+
}
75+
bool is_client() const noexcept {
76+
return this->clientside;
77+
}
78+
const auto& get_connection() const noexcept {
79+
return this->conn;
80+
}
81+
82+
// string description for status codes
83+
static const char* status_code(uint16_t code);
84+
85+
WebSocket(net::Stream_ptr, bool);
86+
WebSocket(WebSocket&&);
87+
~WebSocket();
88+
89+
private:
90+
net::Stream_ptr conn;
91+
bool clientside;
92+
93+
WebSocket(const WebSocket&) = delete;
94+
WebSocket& operator= (const WebSocket&) = delete;
95+
WebSocket& operator= (WebSocket&&) = delete;
96+
void read_data(net::tcp::buffer_t, size_t);
97+
bool write_opcode(uint8_t code, const char*, size_t);
98+
void failure(const std::string&);
99+
void tcp_closed();
100+
void reset();
101+
};
102+
using WebSocket_ptr = WebSocket::WebSocket_ptr;
103+
104+
} // http
105+
106+
#endif

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ set(OS_OBJECTS
5252
net/http/header.cpp net/http/header_fields.cpp net/http/message.cpp net/http/request.cpp
5353
net/http/response.cpp net/http/status_codes.cpp net/http/time.cpp net/http/version.cpp
5454
net/http/mime_types.cpp net/http/cookie.cpp net/http/secure_server.cpp
55-
net/http/client_connection.cpp net/http/client.cpp
55+
net/http/client_connection.cpp net/http/client.cpp net/http/websocket.cpp
5656
net/http/server_connection.cpp net/http/server.cpp net/http/response_writer.cpp
5757
fs/disk.cpp fs/filesystem.cpp fs/mbr.cpp fs/path.cpp
5858
fs/fat.cpp fs/fat_async.cpp fs/fat_sync.cpp fs/memdisk.cpp

src/net/http/secure_server.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,13 @@ namespace http
5959

6060
this->credman.reset(credman);
6161
}
62+
63+
Secure_server::Secure_server(
64+
fs::Dirent& file_ca_key,
65+
fs::Dirent& file_ca_cert,
66+
fs::Dirent& file_server_key,
67+
TCP& tcp)
68+
: Secure_server(file_ca_key, file_ca_cert, file_server_key, tcp, nullptr)
69+
{}
70+
6271
}

0 commit comments

Comments
 (0)