CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
URLSession.HTTP.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "URLSession.hpp"
11
14
17
18#if defined(CURL_FOUND)
19#define CE_HAS_HTTP_URL_PROTOCOL 1
20#endif
21
22#if defined(CE_HAS_HTTP_URL_PROTOCOL)
23
24namespace CeresEngine {
25
32 struct HTTPRequestMethod {
33 String raw;
34
37
40
42 HTTPRequestMethod(const char* raw) noexcept : raw(raw) {}
43
44 public:
46 [[nodiscard]] StringView toString() const noexcept { return raw; }
47
49 friend std::ostream& operator<<(std::ostream&, HTTPRequestMethod code);
50
51 public:
53 friend bool operator==(const HTTPRequestMethod& lhs, const HTTPRequestMethod& rhs) { return lhs.raw == rhs.raw; }
54
56 friend bool operator!=(const HTTPRequestMethod& lhs, const HTTPRequestMethod& rhs) { return lhs.raw != rhs.raw; }
57
58 public:
61 static const HTTPRequestMethod Get;
62
65 static const HTTPRequestMethod Head;
66
69 static const HTTPRequestMethod Post;
70
73 static const HTTPRequestMethod Put;
74
76 static const HTTPRequestMethod Delete;
77
80 static const HTTPRequestMethod Connect;
81
84 static const HTTPRequestMethod Options;
85
88 static const HTTPRequestMethod Trace;
89
91 static const HTTPRequestMethod Patch;
92 };
93
105 struct HTTPStatusCode {
107 UInt16 raw = 0;
108
110 constexpr HTTPStatusCode() noexcept = default;
111
113 constexpr HTTPStatusCode(const UInt16 raw) noexcept : raw(raw) {}
114
115 public:
117 [[nodiscard]] constexpr bool isUndefined() const noexcept { return raw == 0; }
118
121 [[nodiscard]] constexpr bool isInformational() const noexcept { return raw >= 100 && raw <= 199; }
122
125 [[nodiscard]] constexpr bool isSuccess() const noexcept { return raw >= 200 && raw <= 299; }
126
129 [[nodiscard]] constexpr bool isRedirection() const noexcept { return raw >= 300 && raw <= 399; }
130
133 [[nodiscard]] constexpr bool isClientError() const noexcept { return raw >= 400 && raw <= 499; }
134
137 [[nodiscard]] constexpr bool isServerError() const noexcept { return raw >= 500 && raw <= 599; }
138
141
144
147
148 public:
150 friend bool operator==(const HTTPStatusCode lhs, const HTTPStatusCode rhs) { return lhs.raw == rhs.raw; }
151
153 friend bool operator!=(const HTTPStatusCode lhs, const HTTPStatusCode rhs) { return lhs.raw != rhs.raw; }
154
155 public: // Information responses
159 static const HTTPStatusCode Continue;
160
165
169 static const HTTPStatusCode Processing;
170
175 static const HTTPStatusCode EarlyHints;
176
177 public: // Successful responses
190 static const HTTPStatusCode OK;
191
196 static const HTTPStatusCode Created;
197
204 static const HTTPStatusCode Accepted;
205
213
218 static const HTTPStatusCode NoContent;
219
222 static const HTTPStatusCode ResetContent;
223
227 static const HTTPStatusCode PartialContent;
228
229 public: // Redirection messages
235 static const HTTPStatusCode MultipleChoice;
236
240 static const HTTPStatusCode MovedPermanently;
241
247 static const HTTPStatusCode Found;
248
252 static const HTTPStatusCode SeeOther;
253
258 static const HTTPStatusCode NotModified;
259
268
277
278 public: // Client error responses
281 static const HTTPStatusCode BadRequest;
282
287 static const HTTPStatusCode Unauthorized;
288
294 static const HTTPStatusCode Forbidden;
295
304 static const HTTPStatusCode NotFound;
305
310 static const HTTPStatusCode MethodNotAllowed;
311
316 static const HTTPStatusCode NotAcceptable;
317
322
331 static const HTTPStatusCode RequestTimeout;
332
336 static const HTTPStatusCode Conflict;
337
345 static const HTTPStatusCode Gone;
346
350 static const HTTPStatusCode LengthRequired;
351
356
360 static const HTTPStatusCode PayloadTooLarge;
361
365 static const HTTPStatusCode URITooLong;
366
371
377
382
389
393 static const HTTPStatusCode TooEarly;
394
400 static const HTTPStatusCode UpgradeRequired;
401
409
413 static const HTTPStatusCode TooManyRequests;
414
420
425
426 public: // Server error responses
431
436 static const HTTPStatusCode NotImplemented;
437
442 static const HTTPStatusCode BadGateway;
443
455
459 static const HTTPStatusCode GatewayTimeout;
460
464
471
475 static const HTTPStatusCode NotExtended;
476
481 };
482
485 class HTTPURLProtocol final : public TURLProtocol<HTTPURLProtocol, "HTTP"> {
486 private:
487 struct Pimpl;
488 const UPtr<Pimpl> m;
489
491 class Connection;
492
493 public:
495 class Request : public IURLRequest {
496 public:
498 HTTPRequestMethod method = HTTPRequestMethod::Get;
499
501 Map<CiString, String> headers;
502
506 bool withResponseBody = true;
507
511 bool withResponseHeaders = true;
512
513 public:
515 explicit Request(const IURLRequest& other) : IURLRequest(other) {}
516
518 explicit Request(const Request& other, const URL& url)
519 : IURLRequest(other, url), method(other.method), headers(other.headers), withResponseBody(other.withResponseBody),
520 withResponseHeaders(other.withResponseHeaders) {}
521
523 explicit Request(const URL& url, const CachePolicy& cachePolicy = CachePolicy::Default) : IURLRequest(url, cachePolicy) {}
524 };
525
527 class AsyncStream;
528
530 class Stream;
531
533 class Response : public IURLResponse {
534 friend class Connection;
535
537 const RC<Connection> mConnection;
538
539 public:
541 HTTPStatusCode statusCode;
542
555 Map<CiString, String> headers;
556
557 public:
559 explicit Response(const RC<Connection>& connection);
560
561 public:
566 [[nodiscard]] Optional<StringView> getHeader(StringView name) const;
567
571 [[nodiscard]] Optional<StringView> getContentType() const;
572
574 [[nodiscard]] bool isJSON() const;
575
576 public:
578 [[nodiscard]] URLInputStream openInputStream() override;
579 };
580
581 public:
583 HTTPURLProtocol(URLSession& session);
584
587
588 public:
590 [[nodiscard]] bool isRequestSupported(const URLRequest& request) final;
591
593 [[nodiscard]] Async<URLResponse> execute(Task& connection) final;
594 };
595
598
601
605 public:
607 using Request = HTTPURLProtocol::Request;
608
610 using Response = HTTPURLProtocol::Response;
611
613 enum class Gateway {
615 IPFS_IO,
616
619 };
620
621 private:
624
625 public:
627 IPFSURLProtocol(URLSession& session);
628
630 IPFSURLProtocol(URLSession& session, const URL& gatewayURL);
631
633 IPFSURLProtocol(URLSession& session, Gateway gateway);
634
637
638 public:
640 [[nodiscard]] bool isRequestSupported(const URLRequest& request) final;
641
643 [[nodiscard]] Async<URLResponse> execute(Task& connection) final;
644 };
645
648
651
652} // namespace CeresEngine
653
654#endif
Nth< 0, TTypeList > Head
First type of a type list.
Definition TypeListOps.hpp:45
Definition Application.hpp:19
BasicStringView< char > StringView
Narrow string view used for handling narrow encoded text in UTF-8.
Definition String.hpp:190
StringView toString(Button button) noexcept
Returns a string representation for the given button
cti::continuable< Args... > Async
Defines a non-copyable continuation type which uses the function2 backend for type erasure.
Definition Async.hpp:22
constexpr Byte operator<<(const Byte arg, const _IntType shift) noexcept
Definition DataTypes.hpp:44
@ Created
A file or directory has been created.
bool operator!=(const ShortAllocator< T, N, A1 > &x, const ShortAllocator< U, M, A2 > &y) noexcept
Definition Allocator.hpp:416
std::uint16_t UInt16
Definition DataTypes.hpp:20
bool operator==(const ShortAllocator< T, N, A1 > &x, const ShortAllocator< U, M, A2 > &y) noexcept
Definition Allocator.hpp:411
InputStream URLInputStream
A stream that provides read-only stream functionality.
Definition URLSession.hpp:43
@ Stream
A audio stream is a larger piece of audio that will be streammed for the device (instead of feeding a...
URI URL
A Uniform Resource Identifier (URI) is a unique sequence of characters that identifies a logical or p...
Definition URLSession.hpp:40
BasicString< char > String
Narrow string used for handling narrow encoded text in UTF-8.
Definition String.hpp:163
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668