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/HttpUtil.h" |
| 6 | |
| 7 | #include <cctype> |
| 8 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 9 | #include "fmt/format.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include "wpi/Base64.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | #include "wpi/StringExtras.h" |
| 12 | #include "wpi/TCPConnector.h" |
| 13 | #include "wpi/raw_ostream.h" |
| 14 | |
| 15 | namespace wpi { |
| 16 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 17 | std::string_view UnescapeURI(std::string_view str, SmallVectorImpl<char>& buf, |
| 18 | bool* error) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | buf.clear(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 20 | for (auto i = str.begin(), end = str.end(); i != end; ++i) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 21 | // pass non-escaped characters to output |
| 22 | if (*i != '%') { |
| 23 | // decode + to space |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 24 | if (*i == '+') { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 25 | buf.push_back(' '); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 26 | } else { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | buf.push_back(*i); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 28 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 29 | continue; |
| 30 | } |
| 31 | |
| 32 | // are there enough characters left? |
| 33 | if (i + 2 >= end) { |
| 34 | *error = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 35 | return {}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // replace %xx with the corresponding character |
| 39 | unsigned val1 = hexDigitValue(*++i); |
| 40 | if (val1 == -1U) { |
| 41 | *error = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 42 | return {}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | } |
| 44 | unsigned val2 = hexDigitValue(*++i); |
| 45 | if (val2 == -1U) { |
| 46 | *error = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 47 | return {}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 48 | } |
| 49 | buf.push_back((val1 << 4) | val2); |
| 50 | } |
| 51 | |
| 52 | *error = false; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 53 | return {buf.data(), buf.size()}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 56 | std::string_view EscapeURI(std::string_view str, SmallVectorImpl<char>& buf, |
| 57 | bool spacePlus) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 58 | static const char* const hexLut = "0123456789ABCDEF"; |
| 59 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 60 | buf.clear(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 61 | for (auto i = str.begin(), end = str.end(); i != end; ++i) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 62 | // pass unreserved characters to output |
| 63 | if (std::isalnum(*i) || *i == '-' || *i == '_' || *i == '.' || *i == '~') { |
| 64 | buf.push_back(*i); |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | // encode space to + |
| 69 | if (spacePlus && *i == ' ') { |
| 70 | buf.push_back('+'); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | // convert others to %xx |
| 75 | buf.push_back('%'); |
| 76 | buf.push_back(hexLut[((*i) >> 4) & 0x0f]); |
| 77 | buf.push_back(hexLut[(*i) & 0x0f]); |
| 78 | } |
| 79 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 80 | return {buf.data(), buf.size()}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 83 | HttpQueryMap::HttpQueryMap(std::string_view query) { |
| 84 | SmallVector<std::string_view, 16> queryElems; |
| 85 | split(query, queryElems, '&', 100, false); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 86 | for (auto elem : queryElems) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 87 | auto [nameEsc, valueEsc] = split(elem, '='); |
| 88 | SmallString<64> nameBuf; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 89 | bool err = false; |
| 90 | auto name = wpi::UnescapeURI(nameEsc, nameBuf, &err); |
| 91 | // note: ignores duplicates |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 92 | if (!err) { |
| 93 | m_elems.try_emplace(name, valueEsc); |
| 94 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 98 | std::optional<std::string_view> HttpQueryMap::Get( |
| 99 | std::string_view name, wpi::SmallVectorImpl<char>& buf) const { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 100 | auto it = m_elems.find(name); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 101 | if (it == m_elems.end()) { |
| 102 | return {}; |
| 103 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 104 | bool err = false; |
| 105 | auto val = wpi::UnescapeURI(it->second, buf, &err); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 106 | if (err) { |
| 107 | return {}; |
| 108 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 109 | return val; |
| 110 | } |
| 111 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 112 | HttpPath::HttpPath(std::string_view path) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 113 | // special-case root path to be a single empty element |
| 114 | if (path == "/") { |
| 115 | m_pathEnds.emplace_back(0); |
| 116 | return; |
| 117 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 118 | wpi::SmallVector<std::string_view, 16> pathElems; |
| 119 | split(path, pathElems, '/', 100, false); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 120 | for (auto elem : pathElems) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 121 | SmallString<64> buf; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 122 | bool err = false; |
| 123 | auto val = wpi::UnescapeURI(elem, buf, &err); |
| 124 | if (err) { |
| 125 | m_pathEnds.clear(); |
| 126 | return; |
| 127 | } |
| 128 | m_pathBuf += val; |
| 129 | m_pathEnds.emplace_back(m_pathBuf.size()); |
| 130 | } |
| 131 | } |
| 132 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 133 | bool HttpPath::startswith(size_t start, |
| 134 | span<const std::string_view> match) const { |
| 135 | if (m_pathEnds.size() < (start + match.size())) { |
| 136 | return false; |
| 137 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 138 | bool first = start == 0; |
| 139 | auto p = m_pathEnds.begin() + start; |
| 140 | for (auto m : match) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 141 | auto val = slice(m_pathBuf, first ? 0 : *(p - 1), *p); |
| 142 | if (val != m) { |
| 143 | return false; |
| 144 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 145 | first = false; |
| 146 | ++p; |
| 147 | } |
| 148 | return true; |
| 149 | } |
| 150 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 151 | std::string_view HttpPath::operator[](size_t n) const { |
| 152 | return slice(m_pathBuf, n == 0 ? 0 : m_pathEnds[n - 1], m_pathEnds[n]); |
| 153 | } |
| 154 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 155 | bool ParseHttpHeaders(raw_istream& is, SmallVectorImpl<char>* contentType, |
| 156 | SmallVectorImpl<char>* contentLength) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 157 | if (contentType) { |
| 158 | contentType->clear(); |
| 159 | } |
| 160 | if (contentLength) { |
| 161 | contentLength->clear(); |
| 162 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 163 | |
| 164 | bool inContentType = false; |
| 165 | bool inContentLength = false; |
| 166 | SmallString<64> lineBuf; |
| 167 | for (;;) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 168 | std::string_view line = rtrim(is.getline(lineBuf, 1024)); |
| 169 | if (is.has_error()) { |
| 170 | return false; |
| 171 | } |
| 172 | if (line.empty()) { |
| 173 | return true; // empty line signals end of headers |
| 174 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 175 | |
| 176 | // header fields start at the beginning of the line |
| 177 | if (!std::isspace(line[0])) { |
| 178 | inContentType = false; |
| 179 | inContentLength = false; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 180 | std::string_view field; |
| 181 | std::tie(field, line) = split(line, ':'); |
| 182 | field = rtrim(field); |
| 183 | if (equals_lower(field, "content-type")) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 184 | inContentType = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 185 | } else if (equals_lower(field, "content-length")) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 186 | inContentLength = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 187 | } else { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 188 | continue; // ignore other fields |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 189 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // collapse whitespace |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 193 | line = ltrim(line); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 194 | |
| 195 | // save field data |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 196 | if (inContentType && contentType) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 197 | contentType->append(line.begin(), line.end()); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 198 | } else if (inContentLength && contentLength) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 199 | contentLength->append(line.begin(), line.end()); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 200 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 204 | bool FindMultipartBoundary(raw_istream& is, std::string_view boundary, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 205 | std::string* saveBuf) { |
| 206 | SmallString<64> searchBuf; |
| 207 | searchBuf.resize(boundary.size() + 2); |
| 208 | size_t searchPos = 0; |
| 209 | |
| 210 | // Per the spec, the --boundary should be preceded by \r\n, so do a first |
| 211 | // pass of 1-byte reads to throw those away (common case) and keep the |
| 212 | // last non-\r\n character in searchBuf. |
| 213 | if (!saveBuf) { |
| 214 | do { |
| 215 | is.read(searchBuf.data(), 1); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 216 | if (is.has_error()) { |
| 217 | return false; |
| 218 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 219 | } while (searchBuf[0] == '\r' || searchBuf[0] == '\n'); |
| 220 | searchPos = 1; |
| 221 | } |
| 222 | |
| 223 | // Look for --boundary. Read boundarysize+2 bytes at a time |
| 224 | // during the search to speed up the reads, then fast-scan for -, |
| 225 | // and only then match the entire boundary. This will be slow if |
| 226 | // there's a bunch of continuous -'s in the output, but that's unlikely. |
| 227 | for (;;) { |
| 228 | is.read(searchBuf.data() + searchPos, searchBuf.size() - searchPos); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 229 | if (is.has_error()) { |
| 230 | return false; |
| 231 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 232 | |
| 233 | // Did we find the boundary? |
| 234 | if (searchBuf[0] == '-' && searchBuf[1] == '-' && |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 235 | searchBuf.substr(2) == boundary) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 236 | return true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 237 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 238 | |
| 239 | // Fast-scan for '-' |
| 240 | size_t pos = searchBuf.find('-', searchBuf[0] == '-' ? 1 : 0); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 241 | if (pos == std::string_view::npos) { |
| 242 | if (saveBuf) { |
| 243 | saveBuf->append(searchBuf.data(), searchBuf.size()); |
| 244 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 245 | } else { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 246 | if (saveBuf) { |
| 247 | saveBuf->append(searchBuf.data(), pos); |
| 248 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 249 | |
| 250 | // move '-' and following to start of buffer (next read will fill) |
| 251 | std::memmove(searchBuf.data(), searchBuf.data() + pos, |
| 252 | searchBuf.size() - pos); |
| 253 | searchPos = searchBuf.size() - pos; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 258 | HttpLocation::HttpLocation(std::string_view url_, bool* error, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 259 | std::string* errorMsg) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 260 | : url{url_} { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 261 | // Split apart into components |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 262 | std::string_view query{url}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 263 | |
| 264 | // scheme: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 265 | std::string_view scheme; |
| 266 | std::tie(scheme, query) = split(query, ':'); |
| 267 | if (!equals_lower(scheme, "http")) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 268 | *errorMsg = "only supports http URLs"; |
| 269 | *error = true; |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | // "//" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 274 | if (!starts_with(query, "//")) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 275 | *errorMsg = "expected http://..."; |
| 276 | *error = true; |
| 277 | return; |
| 278 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 279 | query.remove_prefix(2); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 280 | |
| 281 | // user:password@host:port/ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 282 | std::string_view authority; |
| 283 | std::tie(authority, query) = split(query, '/'); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 284 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 285 | auto [userpass, hostport] = split(authority, '@'); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 286 | // split leaves the RHS empty if the split char isn't present... |
| 287 | if (hostport.empty()) { |
| 288 | hostport = userpass; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 289 | userpass = {}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | if (!userpass.empty()) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 293 | auto [rawUser, rawPassword] = split(userpass, ':'); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 294 | SmallString<64> userBuf, passBuf; |
| 295 | user = UnescapeURI(rawUser, userBuf, error); |
| 296 | if (*error) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 297 | *errorMsg = fmt::format("could not unescape user \"{}\"", rawUser); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | password = UnescapeURI(rawPassword, passBuf, error); |
| 301 | if (*error) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 302 | *errorMsg = |
| 303 | fmt::format("could not unescape password \"{}\"", rawPassword); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | } |
| 307 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 308 | std::string_view portStr; |
| 309 | std::tie(host, portStr) = rsplit(hostport, ':'); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 310 | if (host.empty()) { |
| 311 | *errorMsg = "host is empty"; |
| 312 | *error = true; |
| 313 | return; |
| 314 | } |
| 315 | if (portStr.empty()) { |
| 316 | port = 80; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 317 | } else if (auto p = parse_integer<int>(portStr, 10)) { |
| 318 | port = p.value(); |
| 319 | } else { |
| 320 | *errorMsg = fmt::format("port \"{}\" is not an integer", portStr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 321 | *error = true; |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | // path?query#fragment |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 326 | std::tie(query, fragment) = split(query, '#'); |
| 327 | std::tie(path, query) = split(query, '?'); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 328 | |
| 329 | // Split query string into parameters |
| 330 | while (!query.empty()) { |
| 331 | // split out next param and value |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 332 | std::string_view rawParam, rawValue; |
| 333 | std::tie(rawParam, query) = split(query, '&'); |
| 334 | if (rawParam.empty()) { |
| 335 | continue; // ignore "&&" |
| 336 | } |
| 337 | std::tie(rawParam, rawValue) = split(rawParam, '='); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 338 | |
| 339 | // unescape param |
| 340 | *error = false; |
| 341 | SmallString<64> paramBuf; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 342 | std::string_view param = UnescapeURI(rawParam, paramBuf, error); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 343 | if (*error) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 344 | *errorMsg = fmt::format("could not unescape parameter \"{}\"", rawParam); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 345 | return; |
| 346 | } |
| 347 | |
| 348 | // unescape value |
| 349 | SmallString<64> valueBuf; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 350 | std::string_view value = UnescapeURI(rawValue, valueBuf, error); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 351 | if (*error) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 352 | *errorMsg = fmt::format("could not unescape value \"{}\"", rawValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
| 356 | params.emplace_back(std::make_pair(param, value)); |
| 357 | } |
| 358 | |
| 359 | *error = false; |
| 360 | } |
| 361 | |
| 362 | void HttpRequest::SetAuth(const HttpLocation& loc) { |
| 363 | if (!loc.user.empty()) { |
| 364 | SmallString<64> userpass; |
| 365 | userpass += loc.user; |
| 366 | userpass += ':'; |
| 367 | userpass += loc.password; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 368 | Base64Encode(userpass.str(), &auth); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | bool HttpConnection::Handshake(const HttpRequest& request, |
| 373 | std::string* warnMsg) { |
| 374 | // send GET request |
| 375 | os << "GET /" << request.path << " HTTP/1.1\r\n"; |
| 376 | os << "Host: " << request.host << "\r\n"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 377 | if (!request.auth.empty()) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 378 | os << "Authorization: Basic " << request.auth << "\r\n"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 379 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 380 | os << "\r\n"; |
| 381 | os.flush(); |
| 382 | |
| 383 | // read first line of response |
| 384 | SmallString<64> lineBuf; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 385 | std::string_view line = rtrim(is.getline(lineBuf, 1024)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 386 | if (is.has_error()) { |
| 387 | *warnMsg = "disconnected before response"; |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | // see if we got a HTTP 200 response |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 392 | std::string_view httpver, code, codeText; |
| 393 | std::tie(httpver, line) = split(line, ' '); |
| 394 | std::tie(code, codeText) = split(line, ' '); |
| 395 | if (!starts_with(httpver, "HTTP")) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 396 | *warnMsg = "did not receive HTTP response"; |
| 397 | return false; |
| 398 | } |
| 399 | if (code != "200") { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 400 | *warnMsg = fmt::format("received {} {} response", code, codeText); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 401 | return false; |
| 402 | } |
| 403 | |
| 404 | // Parse headers |
| 405 | if (!ParseHttpHeaders(is, &contentType, &contentLength)) { |
| 406 | *warnMsg = "disconnected during headers"; |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | return true; |
| 411 | } |
| 412 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 413 | void HttpMultipartScanner::SetBoundary(std::string_view boundary) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 414 | m_boundaryWith = "\n--"; |
| 415 | m_boundaryWith += boundary; |
| 416 | m_boundaryWithout = "\n"; |
| 417 | m_boundaryWithout += boundary; |
| 418 | m_dashes = kUnknown; |
| 419 | } |
| 420 | |
| 421 | void HttpMultipartScanner::Reset(bool saveSkipped) { |
| 422 | m_saveSkipped = saveSkipped; |
| 423 | m_state = kBoundary; |
| 424 | m_posWith = 0; |
| 425 | m_posWithout = 0; |
| 426 | m_buf.resize(0); |
| 427 | } |
| 428 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 429 | std::string_view HttpMultipartScanner::Execute(std::string_view in) { |
| 430 | if (m_state == kDone) { |
| 431 | Reset(m_saveSkipped); |
| 432 | } |
| 433 | if (m_saveSkipped) { |
| 434 | m_buf += in; |
| 435 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 436 | |
| 437 | size_t pos = 0; |
| 438 | if (m_state == kBoundary) { |
| 439 | for (char ch : in) { |
| 440 | ++pos; |
| 441 | if (m_dashes != kWithout) { |
| 442 | if (ch == m_boundaryWith[m_posWith]) { |
| 443 | ++m_posWith; |
| 444 | if (m_posWith == m_boundaryWith.size()) { |
| 445 | // Found the boundary; transition to padding |
| 446 | m_state = kPadding; |
| 447 | m_dashes = kWith; // no longer accept plain 'boundary' |
| 448 | break; |
| 449 | } |
| 450 | } else if (ch == m_boundaryWith[0]) { |
| 451 | m_posWith = 1; |
| 452 | } else { |
| 453 | m_posWith = 0; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (m_dashes != kWith) { |
| 458 | if (ch == m_boundaryWithout[m_posWithout]) { |
| 459 | ++m_posWithout; |
| 460 | if (m_posWithout == m_boundaryWithout.size()) { |
| 461 | // Found the boundary; transition to padding |
| 462 | m_state = kPadding; |
| 463 | m_dashes = kWithout; // no longer accept '--boundary' |
| 464 | break; |
| 465 | } |
| 466 | } else if (ch == m_boundaryWithout[0]) { |
| 467 | m_posWithout = 1; |
| 468 | } else { |
| 469 | m_posWithout = 0; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (m_state == kPadding) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 476 | for (char ch : drop_front(in, pos)) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 477 | ++pos; |
| 478 | if (ch == '\n') { |
| 479 | // Found the LF; return remaining input buffer (following it) |
| 480 | m_state = kDone; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 481 | if (m_saveSkipped) { |
| 482 | m_buf.resize(m_buf.size() - in.size() + pos); |
| 483 | } |
| 484 | return drop_front(in, pos); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | // We consumed the entire input |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 490 | return {}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | } // namespace wpi |