5#ifndef WPINET_HTTPUTIL_H_
6#define WPINET_HTTPUTIL_H_
8#include <initializer_list>
40 bool spacePlus =
true);
64 std::string* saveBuf);
96 std::optional<std::string_view>
Get(std::string_view name,
133 explicit operator bool()
const {
return !
empty(); }
151 bool equals(std::initializer_list<std::string_view> match)
const {
152 return equals(0, {match.begin(), match.end()});
154 bool equals(std::span<const std::string_view> match)
const {
157 bool equals(std::string_view match)
const {
return equals(0, {match}); }
168 std::initializer_list<std::string_view> match)
const {
169 return equals(start, {match.begin(), match.end()});
171 bool equals(
size_t start, std::span<const std::string_view> match)
const {
172 if (m_pathEnds.
size() != (start + match.size())) {
177 bool equals(
size_t start, std::string_view match)
const {
178 return equals(start, {match});
188 bool startswith(std::initializer_list<std::string_view> match)
const {
189 return startswith(0, {match.begin(), match.end()});
191 bool startswith(std::span<const std::string_view> match)
const {
207 std::initializer_list<std::string_view> match)
const {
208 return startswith(start, {match.begin(), match.end()});
211 bool startswith(
size_t start, std::span<const std::string_view> match)
const;
213 bool startswith(
size_t start, std::string_view match)
const {
240 : m_path(&path), m_start(start) {}
242 explicit operator bool()
const {
return !
empty(); }
243 bool empty()
const {
return m_path && m_path->
size() == m_start; }
244 size_t size()
const {
return m_path ? m_path->
size() - m_start : 0; }
246 bool equals(std::initializer_list<std::string_view> match)
const {
247 return equals(0, {match.begin(), match.end()});
249 bool equals(std::span<const std::string_view> match)
const {
252 bool equals(std::string_view match)
const {
return equals(0, {match}); }
255 std::initializer_list<std::string_view> match)
const {
256 return equals(start, {match.begin(), match.end()});
258 bool equals(
size_t start, std::span<const std::string_view> match)
const {
259 return m_path ? m_path->
equals(m_start + start, match) :
false;
261 bool equals(
size_t start, std::string_view match)
const {
262 return equals(start, {match});
265 bool startswith(std::initializer_list<std::string_view> match)
const {
266 return startswith(0, {match.begin(), match.end()});
268 bool startswith(std::span<const std::string_view> match)
const {
276 std::initializer_list<std::string_view> match)
const {
277 return startswith(start, {match.begin(), match.end()});
279 bool startswith(
size_t start, std::span<const std::string_view> match)
const {
280 return m_path ? m_path->
startswith(m_start + start, match) :
false;
282 bool startswith(
size_t start, std::string_view match)
const {
287 return m_path ? m_path->operator[](m_start + n) : std::string_view{};
301 HttpLocation(std::string_view url_,
bool* error, std::string* errorMsg);
309 std::vector<std::pair<std::string, std::string>>
params;
323 template <
typename T>
327 for (
const auto& p : loc.
params) {
328 params.
emplace_back(std::pair{GetFirst(p), GetSecond(p)});
330 for (
const auto& p : extraParams) {
331 params.
emplace_back(std::pair{GetFirst(p), GetSecond(p)});
333 SetPath(loc.
path, params);
342 template <
typename T>
345 SetPath(path_, params);
357 template <
typename T>
358 void SetPath(std::string_view path_,
const T& params) {
363 for (
const auto& param : params) {
370 SmallString<64> escapeBuf;
371 pathOs <<
EscapeURI(GetFirst(param), escapeBuf,
false);
372 if (!GetSecond(param).empty()) {
373 pathOs <<
'=' <<
EscapeURI(GetSecond(param), escapeBuf,
false);
378 template <
typename T>
379 static std::string_view GetFirst(
const T& elem) {
382 template <
typename T>
383 static std::string_view GetFirst(
const std::pair<std::string, T>& elem) {
386 template <
typename T>
387 static std::string_view GetSecond(
const T& elem) {
399 std::unique_ptr<wpi::NetworkStream>
stream;
413 bool saveSkipped =
false) {
423 void Reset(
bool saveSkipped =
false);
429 std::string_view
Execute(std::string_view in);
432 bool IsDone()
const {
return m_state == kDone; }
436 return m_saveSkipped ? std::string_view{m_buf} : std::string_view{};
443 enum State { kBoundary, kPadding, kDone };
445 size_t m_posWith, m_posWithout;
446 enum Dashes { kUnknown, kWith, kWithout };
This file defines the SmallString class.
This file defines the SmallVector class.
Definition HttpUtil.h:392
SmallString< 64 > contentType
Definition HttpUtil.h:404
SmallString< 64 > contentLength
Definition HttpUtil.h:405
bool Handshake(const HttpRequest &request, std::string *warnMsg)
wpi::raw_socket_ostream os
Definition HttpUtil.h:401
HttpConnection(std::unique_ptr< wpi::NetworkStream > stream_, int timeout)
Definition HttpUtil.h:394
std::unique_ptr< wpi::NetworkStream > stream
Definition HttpUtil.h:399
wpi::raw_socket_istream is
Definition HttpUtil.h:400
Definition HttpUtil.h:298
std::string user
Definition HttpUtil.h:304
std::string url
Definition HttpUtil.h:303
std::string password
Definition HttpUtil.h:305
HttpLocation(std::string_view url_, bool *error, std::string *errorMsg)
int port
Definition HttpUtil.h:307
std::string fragment
Definition HttpUtil.h:310
std::string host
Definition HttpUtil.h:306
std::vector< std::pair< std::string, std::string > > params
Definition HttpUtil.h:309
std::string path
Definition HttpUtil.h:308
Definition HttpUtil.h:410
HttpMultipartScanner(std::string_view boundary, bool saveSkipped=false)
Definition HttpUtil.h:412
void Reset(bool saveSkipped=false)
std::string_view GetSkipped() const
Definition HttpUtil.h:435
std::string_view Execute(std::string_view in)
bool IsDone() const
Definition HttpUtil.h:432
void SetBoundary(std::string_view boundary)
Class for HTTP path matching.
Definition HttpUtil.h:117
bool empty() const
Returns true if the path has no elements.
Definition HttpUtil.h:138
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.h:188
size_t size() const
Returns number of elements in the path.
Definition HttpUtil.h:143
HttpPathRef drop_front(size_t n) const
Returns a path reference with the first N elements of the path removed.
Definition HttpUtil.h:454
bool equals(std::string_view match) const
Definition HttpUtil.h:157
bool startswith(size_t start, std::string_view match) const
Definition HttpUtil.h:213
bool equals(size_t start, std::span< const std::string_view > match) const
Definition HttpUtil.h:171
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.h:167
bool startswith(std::string_view match) const
Definition HttpUtil.h:194
bool equals(std::span< const std::string_view > match) const
Definition HttpUtil.h:154
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.h:206
bool startswith(std::span< const std::string_view > match) const
Definition HttpUtil.h:191
bool equals(size_t start, std::string_view match) const
Definition HttpUtil.h:177
bool startswith(size_t start, std::span< const std::string_view > match) const
HttpPath()=default
Constructs an empty HTTP path.
HttpPath(std::string_view path)
Constructs a HTTP path from an escaped path string.
bool equals(std::initializer_list< std::string_view > match) const
Returns true if the path exactly matches the provided match list.
Definition HttpUtil.h:151
std::string_view operator[](size_t n) const
Gets a single element of the path.
Proxy reference object for a portion of a HttpPath.
Definition HttpUtil.h:235
bool empty() const
Definition HttpUtil.h:243
bool startswith(size_t start, std::initializer_list< std::string_view > match) const
Definition HttpUtil.h:275
size_t size() const
Definition HttpUtil.h:244
bool equals(size_t start, std::initializer_list< std::string_view > match) const
Definition HttpUtil.h:254
bool equals(std::initializer_list< std::string_view > match) const
Definition HttpUtil.h:246
std::string_view operator[](size_t n) const
Definition HttpUtil.h:286
bool startswith(size_t start, std::string_view match) const
Definition HttpUtil.h:282
bool equals(size_t start, std::span< const std::string_view > match) const
Definition HttpUtil.h:258
bool startswith(std::initializer_list< std::string_view > match) const
Definition HttpUtil.h:265
bool equals(size_t start, std::string_view match) const
Definition HttpUtil.h:261
bool startswith(std::string_view match) const
Definition HttpUtil.h:271
bool startswith(size_t start, std::span< const std::string_view > match) const
Definition HttpUtil.h:279
HttpPathRef drop_front(size_t n) const
Definition HttpUtil.h:289
bool equals(std::span< const std::string_view > match) const
Definition HttpUtil.h:249
bool equals(std::string_view match) const
Definition HttpUtil.h:252
bool startswith(std::span< const std::string_view > match) const
Definition HttpUtil.h:268
HttpPathRef(const HttpPath &path, size_t start=0)
Definition HttpUtil.h:238
Map for looking up elements of the query portion of a URI.
Definition HttpUtil.h:72
HttpQueryMap()=default
Constructs an empty map (with no entries).
std::optional< std::string_view > Get(std::string_view name, SmallVectorImpl< char > &buf) const
Gets an element of the query string.
HttpQueryMap(std::string_view query)
Constructs from an escaped query string.
Definition HttpUtil.h:313
HttpRequest(const HttpLocation &loc, std::string_view path_)
Definition HttpUtil.h:337
HttpRequest(const HttpLocation &loc)
Definition HttpUtil.h:317
SmallString< 128 > path
Definition HttpUtil.h:352
HttpRequest(const HttpLocation &loc, std::string_view path_, const T ¶ms)
Definition HttpUtil.h:343
int port
Definition HttpUtil.h:350
HttpRequest(const HttpLocation &loc, const T &extraParams)
Definition HttpUtil.h:324
SmallString< 128 > host
Definition HttpUtil.h:349
std::string auth
Definition HttpUtil.h:351
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:27
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1212
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition sha1.h:30
reference emplace_back(ArgTypes &&... Args)
Definition SmallVector.h:953
size_t size() const
Definition SmallVector.h:99
bool empty() const
Definition SmallVector.h:102
StringMap is a sorted associative container that contains key-value pairs with unique string keys.
Definition StringMap.h:26
Definition raw_istream.h:22
bool has_error() const
Definition raw_istream.h:114
Definition raw_socket_istream.h:14
Definition raw_socket_ostream.h:14
A raw_ostream that writes to an SmallVector or SmallString.
Definition raw_ostream.h:619
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
Foonathan namespace.
Definition ntcore_cpp.h:26
bool ParseHttpHeaders(raw_istream &is, SmallVectorImpl< char > *contentType, SmallVectorImpl< char > *contentLength)
std::string_view UnescapeURI(std::string_view str, SmallVectorImpl< char > &buf, bool *error)
std::string_view EscapeHTML(std::string_view str, SmallVectorImpl< char > &buf)
bool FindMultipartBoundary(wpi::raw_istream &is, std::string_view boundary, std::string *saveBuf)
std::string_view EscapeURI(std::string_view str, SmallVectorImpl< char > &buf, bool spacePlus=true)