Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013-2017, Martin Charles |
| 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/StringUtil.h" |
| 30 | #include "seasocks/SimpleResponse.h" |
| 31 | |
| 32 | #include <cstring> |
| 33 | #include <iostream> |
| 34 | #include <memory> |
| 35 | #include <set> |
| 36 | #include <sstream> |
| 37 | #include <string> |
| 38 | |
| 39 | using namespace seasocks; |
| 40 | |
| 41 | size_t streamlen(std::shared_ptr<std::istream> stream) { |
| 42 | stream->seekg(0, stream->end); |
| 43 | size_t len = stream->tellg(); |
| 44 | stream->seekg(0, stream->beg); |
| 45 | return len; |
| 46 | } |
| 47 | |
| 48 | class MyPageHandler : public PageHandler { |
| 49 | public: |
| 50 | virtual std::shared_ptr<Response> handle(const Request& request) override { |
| 51 | if (request.verb() != Request::Verb::Get) { |
| 52 | return Response::unhandled(); |
| 53 | } |
| 54 | |
| 55 | auto ss = std::make_shared<std::stringstream>(); |
| 56 | (*ss) << "<html><head><title>seasocks example</title></head>" |
| 57 | "<body>Hello, " |
| 58 | << request.credentials()->attributes["fullName"] << "! You asked for " << request.getRequestUri() |
| 59 | << " and your ip is " << request.getRemoteAddress().sin_addr.s_addr |
| 60 | << " and a random number is " << rand() |
| 61 | << "</body></html>"; |
| 62 | |
| 63 | auto headers = SimpleResponse::Headers({ |
| 64 | {"Content-Type", "text/html"}, |
| 65 | {"Content-Length", std::to_string(streamlen(ss))}, |
| 66 | {"Connection", "keep-alive"}, |
| 67 | }); |
| 68 | |
| 69 | const auto response = std::make_shared<SimpleResponse>( |
| 70 | ResponseCode::Ok, // response code |
| 71 | ss, // stream |
| 72 | headers, // headers |
| 73 | true, // keep alive |
| 74 | true // flush instantly |
| 75 | ); |
| 76 | return response; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | int main(int /*argc*/, const char* /*argv*/[]) { |
| 81 | auto logger = std::make_shared<PrintfLogger>(Logger::Level::Info); |
| 82 | |
| 83 | Server server(logger); |
| 84 | server.addPageHandler(std::make_shared<MyPageHandler>()); |
| 85 | |
| 86 | server.serve("", 9090); |
| 87 | return 0; |
| 88 | } |