5#include "../Foundation/Compiler.h"
6#ifndef SC_EXPORT_LIBRARY_HTTP_CLIENT
7#define SC_EXPORT_LIBRARY_HTTP_CLIENT 0
9#define SC_HTTP_CLIENT_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_HTTP_CLIENT)
11#include "../Foundation/Result.h"
12#include "../Foundation/Span.h"
13#include "../Foundation/StringSpan.h"
14#include "Internal/HttpClientThreading.h"
35 Method method = HttpGET;
47 bool allowRedirects =
false;
64 size_t headersLength = 0;
65 Protocol negotiatedProtocol = Protocol::Unknown;
104struct SC_HTTP_CLIENT_EXPORT HttpClientOperation;
137 Type type = Type::ResponseHead;
139 size_t bufferIndex = 0;
157#pragma warning(disable : 4324)
169 [[nodiscard]]
Result init();
170 [[nodiscard]]
Result close();
172 [[nodiscard]]
bool isInitialized()
const {
return initialized; }
188 friend struct Internal;
194 bool initialized =
false;
197 alignas(
uint64_t)
char storage[128];
198#elif SC_PLATFORM_WINDOWS
199 alignas(
uint64_t)
char storage[128];
200#elif SC_PLATFORM_LINUX
201 alignas(
uint64_t)
char storage[512];
219 [[nodiscard]]
Result close();
220 [[nodiscard]]
Result cancel();
241 [[nodiscard]]
bool isInitialized()
const {
return initialized; }
242 [[nodiscard]]
bool isRequestInFlight()
const {
return requestInFlight; }
244 friend struct Internal;
248 friend struct HttpClientAppleCallbacks;
249 friend struct HttpClientLinuxCallbacks;
250 friend struct HttpClientWindowsCallbacks;
252 Result platformInit();
253 Result platformClose();
254 Result platformStart();
255 Result platformCancel();
257 Result enqueueEvent(
const HttpClientOperationEvent& event);
258 bool dequeueEvent(HttpClientOperationEvent& event);
260 Result allocateResponseBuffer(
size_t minimumSizeInBytes,
size_t& bufferIndex, Span<char>& data);
261 void releaseResponseBuffer(
size_t bufferIndex);
262 Result enqueueResponseDataCopy(Span<const char> data);
264 void enqueueResponseHead();
265 void enqueueResponseBuffer(
size_t bufferIndex,
size_t size);
266 void enqueueResponseComplete();
267 void enqueueError(Result error);
269 void resetResponseState(HttpClientResponse& response);
270 void resetRequestBodyState();
272 size_t readRequestBodyChunk(Span<char> dest, Result& outError,
bool& outEnd);
273 bool hasPendingEvents()
const;
274 Result processPendingEvents();
276 HttpClient* client =
nullptr;
277 HttpClientResponse* currentResponse =
nullptr;
278 HttpClientOperationListener* currentListener =
nullptr;
279 HttpClientRequestBodyProvider* currentBodyProvider =
nullptr;
280 HttpClientOperationNotifier* notifier =
nullptr;
281 HttpClientRequest currentRequest;
283 Span<HttpClientResponseBuffer> responseBuffers;
284 Span<HttpClientOperationEvent> eventQueue;
286 Span<char> responseHeaders;
287 Span<char> backendScratch;
289 mutable HttpClientLocalMutex eventMutex;
290 HttpClientLocalConditionVariable eventCV;
291 HttpClientOperationEvent dequeuedEvent;
293 size_t eventHead = 0;
294 size_t eventTail = 0;
295 size_t eventCount = 0;
296 bool requestBodyFinished =
false;
297 Result requestBodyError = Result(
true);
299 bool initialized =
false;
300 bool requestInFlight =
false;
303 alignas(
uint64_t)
char storage[512];
304#elif SC_PLATFORM_WINDOWS
305 alignas(
uint64_t)
char storage[256];
306#elif SC_PLATFORM_LINUX
307 alignas(
uint64_t)
char storage[512];
#define SC_COMPILER_UNUSED(param)
Silence an unused variable or unused parameter warning.
Definition Compiler.h:148
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition PrimitiveTypes.h:27
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:33
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:29
Event slot storage used by HttpClientOperation to hand off backend notifications.
Definition HttpClient.h:128
Listener receiving response notifications during HttpClientOperation::poll.
Definition HttpClient.h:85
virtual void onError(Result error)
Called when the request fails.
Definition HttpClient.h:101
virtual void onResponseComplete()
Called when the response body completed successfully.
Definition HttpClient.h:97
virtual void onResponseHead(HttpClientResponse &response)
Called once the response status code and headers are available.
Definition HttpClient.h:90
virtual void onResponseBody(Span< const char > data)
Called for each response body chunk delivered by poll()
Definition HttpClient.h:94
Caller-owned memory for one HttpClientOperation.
Definition HttpClient.h:145
Span< char > responseBufferMemory
Optional; split equally into responseBuffers if non-empty.
Definition HttpClient.h:149
Optional notifier used by external adapters to wake up their own event loop.
Definition HttpClient.h:108
virtual void notifyHttpClientOperation(HttpClientOperation &operation)=0
Notifies an external adapter that the operation has queued new events.
One in-flight HTTP request/response operation.
Definition HttpClient.h:209
Result start(const HttpClientRequest &request, HttpClientResponse &response, HttpClientOperationListener *listener=nullptr, HttpClientRequestBodyProvider *bodyProvider=nullptr)
Starts a new request on this operation.
void setNotifier(HttpClientOperationNotifier *notifierValue)
Registers an optional notifier used by adapters such as HttpClientAsyncT.
Definition HttpClient.h:239
Result poll(uint32_t timeoutMilliseconds=0)
Processes queued backend events and optionally waits for more work.
Pull-based provider for streamed request bodies.
Definition HttpClient.h:72
virtual Result pullRequestBody(Span< char > dest, size_t &bytesWritten, bool &endReached)=0
Writes the next request body chunk in dest.
Configuration for an outgoing HTTP request.
Definition HttpClient.h:23
Span< const char > body
Fixed request body.
Definition HttpClient.h:42
StringSpan url
Full URL including scheme (e.g. "https://example.com/path")
Definition HttpClient.h:37
Caller-owned response buffer descriptor for one HttpClientOperation.
Definition HttpClient.h:118
Parsed response metadata filled when headers arrive.
Definition HttpClient.h:52
Reusable HTTP backend/session owner.
Definition HttpClient.h:160
static Result executeBlocking(const HttpClientRequest &request, HttpClientResponse &response, Span< char > bodyBuffer, size_t &bodyLength, const HttpClientOperationMemory &memory)
Convenience helper executing a request synchronously on top of HttpClientOperation::poll.
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37