Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013-2017, Matt Godbolt |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // Redistributions of source code must retain the above copyright notice, this |
| 8 | // list of conditions and the following disclaimer. |
| 9 | // |
| 10 | // Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | // POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | #include "seasocks/PageHandler.h" |
| 27 | #include "seasocks/PrintfLogger.h" |
| 28 | #include "seasocks/Server.h" |
| 29 | #include "seasocks/WebSocket.h" |
| 30 | #include "seasocks/StringUtil.h" |
| 31 | |
| 32 | #include <memory> |
| 33 | #include <set> |
| 34 | #include <string> |
| 35 | |
| 36 | // Simple chatroom server, showing how one might use authentication. |
| 37 | |
| 38 | using namespace seasocks; |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | struct Handler : WebSocket::Handler { |
| 43 | std::set<WebSocket*> _cons; |
| 44 | |
| 45 | void onConnect(WebSocket* con) override { |
| 46 | _cons.insert(con); |
| 47 | send(con->credentials()->username + " has joined"); |
| 48 | } |
| 49 | void onDisconnect(WebSocket* con) override { |
| 50 | _cons.erase(con); |
| 51 | send(con->credentials()->username + " has left"); |
| 52 | } |
| 53 | |
| 54 | void onData(WebSocket* con, const char* data) override { |
| 55 | send(con->credentials()->username + ": " + data); |
| 56 | } |
| 57 | |
| 58 | void send(const std::string& msg) { |
| 59 | for (auto* con : _cons) { |
| 60 | con->send(msg); |
| 61 | } |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | struct MyAuthHandler : PageHandler { |
| 66 | std::shared_ptr<Response> handle(const Request& request) override { |
| 67 | // Here one would handle one's authentication system, for example; |
| 68 | // * check to see if the user has a trusted cookie: if so, accept it. |
| 69 | // * if not, redirect to a login handler page, and await a redirection |
| 70 | // back here with relevant URL parameters indicating success. Then, |
| 71 | // set the cookie. |
| 72 | // For this example, we set the user's authentication information purely |
| 73 | // from their connection. |
| 74 | request.credentials()->username = formatAddress(request.getRemoteAddress()); |
| 75 | return Response::unhandled(); // cause next handler to run |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | } |
| 80 | |
| 81 | int main(int /*argc*/, const char* /*argv*/[]) { |
| 82 | Server server(std::make_shared<PrintfLogger>()); |
| 83 | server.addPageHandler(std::make_shared<MyAuthHandler>()); |
| 84 | server.addWebSocketHandler("/chat", std::make_shared<Handler>()); |
| 85 | server.serve("src/ws_chatroom_web", 9000); |
| 86 | return 0; |
| 87 | } |