7#include <initializer_list>
20#include "wpi/util/SmallVector.hpp"
40 bool spacePlus =
true);
66 std::string_view boundary, std::string* saveBuf);
98 std::optional<std::string_view>
Get(
135 explicit operator bool()
const {
return !
empty(); }
140 bool empty()
const {
return m_pathEnds.empty(); }
145 size_t size()
const {
return m_pathEnds.size(); }
153 bool equals(std::initializer_list<std::string_view> match)
const {
154 return equals(0, {match.begin(), match.end()});
156 bool equals(std::span<const std::string_view> match)
const {
159 bool equals(std::string_view match)
const {
return equals(0, {match}); }
170 std::initializer_list<std::string_view> match)
const {
171 return equals(start, {match.begin(), match.end()});
173 bool equals(
size_t start, std::span<const std::string_view> match)
const {
174 if (m_pathEnds.size() != (start + match.size())) {
179 bool equals(
size_t start, std::string_view match)
const {
180 return equals(start, {match});
190 bool startswith(std::initializer_list<std::string_view> match)
const {
191 return startswith(0, {match.begin(), match.end()});
193 bool startswith(std::span<const std::string_view> match)
const {
209 std::initializer_list<std::string_view> match)
const {
210 return startswith(start, {match.begin(), match.end()});
213 bool startswith(
size_t start, std::span<const std::string_view> match)
const;
215 bool startswith(
size_t start, std::string_view match)
const {
231 wpi::util::SmallVector<size_t, 16> m_pathEnds;
242 : m_path(&path), m_start(start) {}
244 explicit operator bool()
const {
return !
empty(); }
245 bool empty()
const {
return m_path && m_path->size() == m_start; }
246 size_t size()
const {
return m_path ? m_path->size() - m_start : 0; }
248 bool equals(std::initializer_list<std::string_view> match)
const {
249 return equals(0, {match.begin(), match.end()});
251 bool equals(std::span<const std::string_view> match)
const {
254 bool equals(std::string_view match)
const {
return equals(0, {match}); }
257 std::initializer_list<std::string_view> match)
const {
258 return equals(start, {match.begin(), match.end()});
260 bool equals(
size_t start, std::span<const std::string_view> match)
const {
261 return m_path ? m_path->equals(m_start + start, match) :
false;
263 bool equals(
size_t start, std::string_view match)
const {
264 return equals(start, {match});
267 bool startswith(std::initializer_list<std::string_view> match)
const {
268 return startswith(0, {match.begin(), match.end()});
270 bool startswith(std::span<const std::string_view> match)
const {
278 std::initializer_list<std::string_view> match)
const {
279 return startswith(start, {match.begin(), match.end()});
281 bool startswith(
size_t start, std::span<const std::string_view> match)
const {
282 return m_path ? m_path->startswith(m_start + start, match) :
false;
284 bool startswith(
size_t start, std::string_view match)
const {
289 return m_path ? m_path->operator[](m_start + n) : std::string_view{};
303 HttpLocation(std::string_view url_,
bool* error, std::string* errorMsg);
311 std::vector<std::pair<std::string, std::string>>
params;
325 template <
typename T>
328 wpi::util::SmallVector<std::pair<std::string_view, std::string_view>, 4>
330 for (
const auto&
p : loc.
params) {
331 params.emplace_back(std::pair{GetFirst(
p), GetSecond(
p)});
333 for (
const auto&
p : extraParams) {
334 params.emplace_back(std::pair{GetFirst(
p), GetSecond(
p)});
336 SetPath(loc.
path, params);
345 template <
typename T>
348 SetPath(path_, params);
360 template <
typename T>
361 void SetPath(std::string_view path_,
const T& params) {
363 wpi::util::raw_svector_ostream pathOs{
path};
366 for (
const auto& param : params) {
374 pathOs <<
EscapeURI(GetFirst(param), escapeBuf,
false);
375 if (!GetSecond(param).empty()) {
376 pathOs <<
'=' <<
EscapeURI(GetSecond(param), escapeBuf,
false);
381 template <
typename T>
382 static std::string_view GetFirst(
const T& elem) {
385 template <
typename T>
386 static std::string_view GetFirst(
const std::pair<std::string, T>& elem) {
389 template <
typename T>
390 static std::string_view GetSecond(
const T& elem) {
402 std::unique_ptr<wpi::net::NetworkStream>
stream;
410 explicit operator bool()
const {
return stream && !
is.has_error(); }
416 bool saveSkipped =
false) {
426 void Reset(
bool saveSkipped =
false);
432 std::string_view
Execute(std::string_view in);
435 bool IsDone()
const {
return m_state == kDone; }
439 return m_saveSkipped ? std::string_view{m_buf} : std::string_view{};
446 enum State { kBoundary, kPadding, kDone };
448 size_t m_posWith, m_posWithout;
449 enum Dashes { kUnknown, kWith, kWithout };
This file defines the SmallString class.
@ name
Definition base.h:690
HttpConnection(std::unique_ptr< wpi::net::NetworkStream > stream_, int timeout)
Definition HttpUtil.hpp:397
wpi::net::raw_socket_istream is
Definition HttpUtil.hpp:403
std::unique_ptr< wpi::net::NetworkStream > stream
Definition HttpUtil.hpp:402
bool Handshake(const HttpRequest &request, std::string *warnMsg)
wpi::util::SmallString< 64 > contentLength
Definition HttpUtil.hpp:408
wpi::net::raw_socket_ostream os
Definition HttpUtil.hpp:404
wpi::util::SmallString< 64 > contentType
Definition HttpUtil.hpp:407
Definition HttpUtil.hpp:300
std::vector< std::pair< std::string, std::string > > params
Definition HttpUtil.hpp:311
HttpLocation(std::string_view url_, bool *error, std::string *errorMsg)
std::string fragment
Definition HttpUtil.hpp:312
std::string url
Definition HttpUtil.hpp:305
int port
Definition HttpUtil.hpp:309
std::string host
Definition HttpUtil.hpp:308
std::string path
Definition HttpUtil.hpp:310
std::string password
Definition HttpUtil.hpp:307
std::string user
Definition HttpUtil.hpp:306
std::string_view Execute(std::string_view in)
std::string_view GetSkipped() const
Definition HttpUtil.hpp:438
void Reset(bool saveSkipped=false)
bool IsDone() const
Definition HttpUtil.hpp:435
HttpMultipartScanner(std::string_view boundary, bool saveSkipped=false)
Definition HttpUtil.hpp:415
void SetBoundary(std::string_view boundary)
Class for HTTP path matching.
Definition HttpUtil.hpp:119
bool equals(std::initializer_list< std::string_view > match) const
Returns true if the path exactly matches the provided match list.
Definition HttpUtil.hpp:153
bool equals(std::string_view match) const
Definition HttpUtil.hpp:159
HttpPath()=default
Constructs an empty HTTP path.
bool equals(std::span< const std::string_view > match) const
Definition HttpUtil.hpp:156
bool startswith(size_t start, std::span< const std::string_view > match) const
bool startswith(size_t start, std::string_view match) const
Definition HttpUtil.hpp:215
HttpPath(std::string_view path)
Constructs a HTTP path from an escaped path string.
bool equals(size_t start, std::span< const std::string_view > match) const
Definition HttpUtil.hpp:173
size_t size() const
Returns number of elements in the path.
Definition HttpUtil.hpp:145
bool startswith(std::string_view match) const
Definition HttpUtil.hpp:196
std::string_view operator[](size_t n) const
Gets a single element of the path.
bool startswith(std::span< const std::string_view > match) const
Definition HttpUtil.hpp:193
bool startswith(size_t start, std::initializer_list< std::string_view > match) const
Returns true if the elements of the path starting at the "start" element match the provided match lis...
Definition HttpUtil.hpp:208
bool startswith(std::initializer_list< std::string_view > match) const
Returns true if the first elements of the path match the provided match list.
Definition HttpUtil.hpp:190
bool equals(size_t start, std::initializer_list< std::string_view > match) const
Returns true if the elements of the path starting at the "start" element match the provided match lis...
Definition HttpUtil.hpp:169
bool empty() const
Returns true if the path has no elements.
Definition HttpUtil.hpp:140
bool equals(size_t start, std::string_view match) const
Definition HttpUtil.hpp:179
HttpPathRef drop_front(size_t n) const
Returns a path reference with the first N elements of the path removed.
Definition HttpUtil.hpp:457
Proxy reference object for a portion of a HttpPath.
Definition HttpUtil.hpp:237
bool equals(std::string_view match) const
Definition HttpUtil.hpp:254
bool startswith(std::string_view match) const
Definition HttpUtil.hpp:273
bool startswith(std::initializer_list< std::string_view > match) const
Definition HttpUtil.hpp:267
bool empty() const
Definition HttpUtil.hpp:245
std::string_view operator[](size_t n) const
Definition HttpUtil.hpp:288
size_t size() const
Definition HttpUtil.hpp:246
bool equals(std::span< const std::string_view > match) const
Definition HttpUtil.hpp:251
bool startswith(size_t start, std::initializer_list< std::string_view > match) const
Definition HttpUtil.hpp:277
bool equals(std::initializer_list< std::string_view > match) const
Definition HttpUtil.hpp:248
bool startswith(std::span< const std::string_view > match) const
Definition HttpUtil.hpp:270
bool startswith(size_t start, std::string_view match) const
Definition HttpUtil.hpp:284
bool equals(size_t start, std::span< const std::string_view > match) const
Definition HttpUtil.hpp:260
HttpPathRef(const HttpPath &path, size_t start=0)
Definition HttpUtil.hpp:240
bool startswith(size_t start, std::span< const std::string_view > match) const
Definition HttpUtil.hpp:281
bool equals(size_t start, std::string_view match) const
Definition HttpUtil.hpp:263
HttpPathRef drop_front(size_t n) const
Definition HttpUtil.hpp:291
bool equals(size_t start, std::initializer_list< std::string_view > match) const
Definition HttpUtil.hpp:256
HttpQueryMap(std::string_view query)
Constructs from an escaped query string.
std::optional< std::string_view > Get(std::string_view name, wpi::util::SmallVectorImpl< char > &buf) const
Gets an element of the query string.
HttpQueryMap()=default
Constructs an empty map (with no entries).
Definition HttpUtil.hpp:315
wpi::util::SmallString< 128 > path
Definition HttpUtil.hpp:355
HttpRequest(const HttpLocation &loc, std::string_view path_, const T ¶ms)
Definition HttpUtil.hpp:346
HttpRequest(const HttpLocation &loc)
Definition HttpUtil.hpp:319
wpi::util::SmallString< 128 > host
Definition HttpUtil.hpp:352
HttpRequest(const HttpLocation &loc, std::string_view path_)
Definition HttpUtil.hpp:340
int port
Definition HttpUtil.hpp:353
HttpRequest(const HttpLocation &loc, const T &extraParams)
Definition HttpUtil.hpp:326
std::string auth
Definition HttpUtil.hpp:354
Definition raw_socket_istream.hpp:13
Definition raw_socket_ostream.hpp:13
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.hpp:27
Definition BooleanTopic.hpp:24
StringMap is a sorted associative container that contains key-value pairs with unique string keys.
Definition StringMap.hpp:26
Definition raw_istream.hpp:21
Definition StringMap.hpp:773
Definition raw_socket_ostream.hpp:9
bool FindMultipartBoundary(wpi::util::raw_istream &is, std::string_view boundary, std::string *saveBuf)
std::string_view UnescapeURI(std::string_view str, wpi::util::SmallVectorImpl< char > &buf, bool *error)
bool ParseHttpHeaders(wpi::util::raw_istream &is, wpi::util::SmallVectorImpl< char > *contentType, wpi::util::SmallVectorImpl< char > *contentLength)
std::string_view EscapeURI(std::string_view str, wpi::util::SmallVectorImpl< char > &buf, bool spacePlus=true)
std::string_view EscapeHTML(std::string_view str, wpi::util::SmallVectorImpl< char > &buf)