Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "wpi/HttpServerConnection.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 7 | #include "fmt/format.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | #include "wpi/SmallString.h" |
| 9 | #include "wpi/SmallVector.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 10 | #include "wpi/SpanExtras.h" |
| 11 | #include "wpi/StringExtras.h" |
| 12 | #include "wpi/fmt/raw_ostream.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 13 | #include "wpi/raw_uv_ostream.h" |
| 14 | |
| 15 | using namespace wpi; |
| 16 | |
| 17 | HttpServerConnection::HttpServerConnection(std::shared_ptr<uv::Stream> stream) |
| 18 | : m_stream(*stream) { |
| 19 | // process HTTP messages |
| 20 | m_messageCompleteConn = |
| 21 | m_request.messageComplete.connect_connection([this](bool keepAlive) { |
| 22 | m_keepAlive = keepAlive; |
| 23 | ProcessRequest(); |
| 24 | }); |
| 25 | |
| 26 | // look for Accept-Encoding headers to determine if gzip is acceptable |
| 27 | m_request.messageBegin.connect([this] { m_acceptGzip = false; }); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 28 | m_request.header.connect( |
| 29 | [this](std::string_view name, std::string_view value) { |
| 30 | if (wpi::equals_lower(name, "accept-encoding") && |
| 31 | wpi::contains(value, "gzip")) { |
| 32 | m_acceptGzip = true; |
| 33 | } |
| 34 | }); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | |
| 36 | // pass incoming data to HTTP parser |
| 37 | m_dataConn = |
| 38 | stream->data.connect_connection([this](uv::Buffer& buf, size_t size) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 39 | m_request.Execute({buf.base, size}); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | if (m_request.HasError()) { |
| 41 | // could not parse; just close the connection |
| 42 | m_stream.Close(); |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | // close when remote side closes |
| 47 | m_endConn = |
| 48 | stream->end.connect_connection([h = stream.get()] { h->Close(); }); |
| 49 | |
| 50 | // start reading |
| 51 | stream->StartRead(); |
| 52 | } |
| 53 | |
| 54 | void HttpServerConnection::BuildCommonHeaders(raw_ostream& os) { |
| 55 | os << "Server: WebServer/1.0\r\n" |
| 56 | "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, " |
| 57 | "post-check=0, max-age=0\r\n" |
| 58 | "Pragma: no-cache\r\n" |
| 59 | "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"; |
| 60 | } |
| 61 | |
| 62 | void HttpServerConnection::BuildHeader(raw_ostream& os, int code, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 63 | std::string_view codeText, |
| 64 | std::string_view contentType, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 65 | uint64_t contentLength, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 66 | std::string_view extra) { |
| 67 | fmt::print(os, "HTTP/{}.{} {} {}\r\n", m_request.GetMajor(), |
| 68 | m_request.GetMinor(), code, codeText); |
| 69 | if (contentLength == 0) { |
| 70 | m_keepAlive = false; |
| 71 | } |
| 72 | if (!m_keepAlive) { |
| 73 | os << "Connection: close\r\n"; |
| 74 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 75 | BuildCommonHeaders(os); |
| 76 | os << "Content-Type: " << contentType << "\r\n"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 77 | if (contentLength != 0) { |
| 78 | fmt::print(os, "Content-Length: {}\r\n", contentLength); |
| 79 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 80 | os << "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: *\r\n"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 81 | if (!extra.empty()) { |
| 82 | os << extra; |
| 83 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 84 | os << "\r\n"; // header ends with a blank line |
| 85 | } |
| 86 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 87 | void HttpServerConnection::SendData(span<const uv::Buffer> bufs, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 88 | bool closeAfter) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 89 | m_stream.Write(bufs, [closeAfter, stream = &m_stream](auto bufs, uv::Error) { |
| 90 | for (auto&& buf : bufs) { |
| 91 | buf.Deallocate(); |
| 92 | } |
| 93 | if (closeAfter) { |
| 94 | stream->Close(); |
| 95 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 96 | }); |
| 97 | } |
| 98 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 99 | void HttpServerConnection::SendResponse(int code, std::string_view codeText, |
| 100 | std::string_view contentType, |
| 101 | std::string_view content, |
| 102 | std::string_view extraHeader) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 103 | SmallVector<uv::Buffer, 4> toSend; |
| 104 | raw_uv_ostream os{toSend, 4096}; |
| 105 | BuildHeader(os, code, codeText, contentType, content.size(), extraHeader); |
| 106 | os << content; |
| 107 | // close after write completes if we aren't keeping alive |
| 108 | SendData(os.bufs(), !m_keepAlive); |
| 109 | } |
| 110 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 111 | void HttpServerConnection::SendStaticResponse( |
| 112 | int code, std::string_view codeText, std::string_view contentType, |
| 113 | std::string_view content, bool gzipped, std::string_view extraHeader) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 114 | // TODO: handle remote side not accepting gzip (very rare) |
| 115 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 116 | std::string_view contentEncodingHeader; |
| 117 | if (gzipped /* && m_acceptGzip*/) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 118 | contentEncodingHeader = "Content-Encoding: gzip\r\n"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 119 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 120 | |
| 121 | SmallVector<uv::Buffer, 4> bufs; |
| 122 | raw_uv_ostream os{bufs, 4096}; |
| 123 | BuildHeader(os, code, codeText, contentType, content.size(), |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 124 | fmt::format("{}{}", extraHeader, contentEncodingHeader)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 125 | // can send content without copying |
| 126 | bufs.emplace_back(content); |
| 127 | |
| 128 | m_stream.Write(bufs, [closeAfter = !m_keepAlive, stream = &m_stream]( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 129 | auto bufs, uv::Error) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 130 | // don't deallocate the static content |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 131 | for (auto&& buf : wpi::drop_back(bufs)) { |
| 132 | buf.Deallocate(); |
| 133 | } |
| 134 | if (closeAfter) { |
| 135 | stream->Close(); |
| 136 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 137 | }); |
| 138 | } |
| 139 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 140 | void HttpServerConnection::SendError(int code, std::string_view message) { |
| 141 | std::string_view codeText, extra, baseMessage; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 142 | switch (code) { |
| 143 | case 401: |
| 144 | codeText = "Unauthorized"; |
| 145 | extra = "WWW-Authenticate: Basic realm=\"CameraServer\""; |
| 146 | baseMessage = "401: Not Authenticated!"; |
| 147 | break; |
| 148 | case 404: |
| 149 | codeText = "Not Found"; |
| 150 | baseMessage = "404: Not Found!"; |
| 151 | break; |
| 152 | case 500: |
| 153 | codeText = "Internal Server Error"; |
| 154 | baseMessage = "500: Internal Server Error!"; |
| 155 | break; |
| 156 | case 400: |
| 157 | codeText = "Bad Request"; |
| 158 | baseMessage = "400: Not Found!"; |
| 159 | break; |
| 160 | case 403: |
| 161 | codeText = "Forbidden"; |
| 162 | baseMessage = "403: Forbidden!"; |
| 163 | break; |
| 164 | case 503: |
| 165 | codeText = "Service Unavailable"; |
| 166 | baseMessage = "503: Service Unavailable"; |
| 167 | break; |
| 168 | default: |
| 169 | code = 501; |
| 170 | codeText = "Not Implemented"; |
| 171 | baseMessage = "501: Not Implemented!"; |
| 172 | break; |
| 173 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 174 | SmallString<256> content{baseMessage}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 175 | content += "\r\n"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 176 | content += message; |
| 177 | SendResponse(code, codeText, "text/plain", content.str(), extra); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 178 | } |