|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2017 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 | +#include <net/inet4> |
| 19 | +#include <service> |
| 20 | + |
| 21 | +#include <net/ws/websocket.hpp> |
| 22 | +void handle_ws(net::WebSocket_ptr ws) |
| 23 | +{ |
| 24 | + static std::map<int, net::WebSocket_ptr> websockets; |
| 25 | + static int idx = 0; |
| 26 | + |
| 27 | + // nullptr means the WS attempt failed |
| 28 | + if(not ws) { |
| 29 | + printf("WS failed\n"); |
| 30 | + return; |
| 31 | + } |
| 32 | + printf("WS Connected: %s\n", ws->to_string().c_str()); |
| 33 | + |
| 34 | + // Write a welcome message |
| 35 | + ws->write("Welcome"); |
| 36 | + ws->write(ws->to_string()); |
| 37 | + // Setup echo reply |
| 38 | + ws->on_read = [ws = ws.get()](auto msg) { |
| 39 | + auto str = msg->as_text(); |
| 40 | + printf("WS Recv: %s\n", str.c_str()); |
| 41 | + ws->write(str); |
| 42 | + }; |
| 43 | + |
| 44 | + websockets[idx] = std::move(ws); |
| 45 | + // Notify on close |
| 46 | + websockets[idx]->on_close = [key = idx](auto code) { |
| 47 | + printf("WS Closing (%u) %s\n", code, websockets[key]->to_string().c_str()); |
| 48 | + }; |
| 49 | + idx++; |
| 50 | +} |
| 51 | + |
| 52 | +#include <net/http/server.hpp> |
| 53 | +#include <memdisk> |
| 54 | +std::unique_ptr<http::Server> server; |
| 55 | + |
| 56 | +void Service::start() |
| 57 | +{ |
| 58 | + // Retreive the stack (configured from outside) |
| 59 | + auto& inet = net::Inet4::stack<0>(); |
| 60 | + Expects(inet.is_configured()); |
| 61 | + |
| 62 | + // Init the memdisk |
| 63 | + auto& disk = fs::memdisk(); |
| 64 | + disk.init_fs([] (auto err, auto&) { |
| 65 | + Expects(not err); |
| 66 | + }); |
| 67 | + // Retreive the HTML page from the disk |
| 68 | + auto file = disk.fs().read_file("/index.html"); |
| 69 | + Expects(file.is_valid()); |
| 70 | + Chunk html{file.data(), file.size()}; |
| 71 | + |
| 72 | + // Create a HTTP Server and setup request handling |
| 73 | + server = std::make_unique<http::Server>(inet.tcp()); |
| 74 | + server->on_request([html] (auto req, auto rw) |
| 75 | + { |
| 76 | + // We only support get |
| 77 | + if(req->method() != http::GET) { |
| 78 | + rw->write_header(http::Not_Found); |
| 79 | + return; |
| 80 | + } |
| 81 | + // Serve HTML on / |
| 82 | + if(req->uri() == "/") { |
| 83 | + rw->write(html); |
| 84 | + } |
| 85 | + // WebSockets go here |
| 86 | + else if(req->uri() == "/ws") { |
| 87 | + auto ws = net::WebSocket::upgrade(*req, *rw); |
| 88 | + handle_ws(std::move(ws)); |
| 89 | + } |
| 90 | + else { |
| 91 | + rw->write_header(http::Not_Found); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + // Start listening on port 80 |
| 96 | + server->listen(80); |
| 97 | +} |
0 commit comments