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 | // An example to show how one might use asynchronous responses. |
| 27 | |
| 28 | #include "seasocks/PrintfLogger.h" |
| 29 | #include "seasocks/Server.h" |
| 30 | #include "seasocks/Response.h" |
| 31 | #include "seasocks/ResponseWriter.h" |
| 32 | #include "seasocks/ResponseCode.h" |
| 33 | #include "seasocks/util/RootPageHandler.h" |
| 34 | #include "seasocks/util/PathHandler.h" |
| 35 | |
| 36 | #include <cassert> |
| 37 | #include <cstring> |
| 38 | #include <iostream> |
| 39 | #include <memory> |
| 40 | #include <set> |
| 41 | #include <sstream> |
| 42 | #include <string> |
| 43 | #include <thread> |
| 44 | #include <unistd.h> |
| 45 | |
| 46 | using namespace seasocks; |
| 47 | |
| 48 | // The AsyncResponse does some long-lived "work" (in this case a big sleep...) |
| 49 | // before responding to the ResponseWriter, in chunks. It uses a new thread to |
| 50 | // perform this "work". As responses can be canceled before the work is |
| 51 | // complete, we must ensure the ResponseWriter used to communicate the response |
| 52 | // is kept alive long enough by holding its shared_ptr in the "work" thread. |
| 53 | // Seasocks will tell the response it has been cancelled (if the connection |
| 54 | // associated with the request is closed); but the ResponseWriter is safe in the |
| 55 | // presence of a closed connection so for simplicity this example does nothing |
| 56 | // in the cancel() method. It is assumed the lifetime of the Server object is |
| 57 | // long enough for all requests to complete before it is destroyed. |
| 58 | struct AsyncResponse : Response { |
| 59 | Server& _server; |
| 60 | explicit AsyncResponse(Server& server) |
| 61 | : _server(server) { |
| 62 | } |
| 63 | |
| 64 | // From Response: |
| 65 | virtual void handle(std::shared_ptr<ResponseWriter> writer) override { |
| 66 | auto& server = _server; |
| 67 | std::thread t([&server, writer]() mutable { |
| 68 | usleep(1000000); // A long database query... |
| 69 | std::string response = "some kind of response...beginning<br>"; |
| 70 | server.execute([response, writer] { |
| 71 | writer->begin(ResponseCode::Ok, TransferEncoding::Chunked); |
| 72 | writer->header("Content-type", "application/html"); |
| 73 | writer->payload(response.data(), response.length()); |
| 74 | }); |
| 75 | response = "more data...<br>"; |
| 76 | for (auto i = 0; i < 5; ++i) { |
| 77 | usleep(1000000); // more data |
| 78 | server.execute([response, writer] { |
| 79 | writer->payload(response.data(), response.length()); |
| 80 | }); |
| 81 | } |
| 82 | response = "Done!"; |
| 83 | usleep(100000); // final data |
| 84 | server.execute([response, writer] { |
| 85 | writer->payload(response.data(), response.length()); |
| 86 | writer->finish(true); |
| 87 | }); |
| 88 | }); |
| 89 | t.detach(); |
| 90 | } |
| 91 | virtual void cancel() override { |
| 92 | // If we could cancel the thread, we would do so here. There's no need |
| 93 | // to invalidate the _writer; any writes to it after this will be |
| 94 | // silently dropped. |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | struct DataHandler : CrackedUriPageHandler { |
| 99 | virtual std::shared_ptr<Response> handle( |
| 100 | const CrackedUri& /*uri*/, const Request& request) override { |
| 101 | return std::make_shared<AsyncResponse>(request.server()); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | int main(int /*argc*/, const char* /*argv*/[]) { |
| 106 | auto logger = std::make_shared<PrintfLogger>(Logger::Level::Debug); |
| 107 | |
| 108 | Server server(logger); |
| 109 | auto root = std::make_shared<RootPageHandler>(); |
| 110 | auto pathHandler = std::make_shared<PathHandler>("data", std::make_shared<DataHandler>()); |
| 111 | root->add(pathHandler); |
| 112 | server.addPageHandler(root); |
| 113 | |
| 114 | server.serve("src/async_test_web", 9090); |
| 115 | return 0; |
| 116 | } |