4#include "HttpConnection.h"
6#include "HttpURLParser.h"
13template <
int ReadQueue,
int WriteQueue,
int HeaderBytes,
int StreamBytes>
15 :
public HttpStaticConnection<ReadQueue, WriteQueue, HeaderBytes, StreamBytes, 8, HttpConnectionBase>
17 static constexpr int ExtraBuffers = 8;
21 this->readableSocketStream.setAutoDestroy(
false);
22 this->writableSocketStream.setAutoDestroy(
false);
66 return put(loop, url, body.
toCharSpan(), keepAlive);
73 return post(loop, url, body.
toCharSpan(), keepAlive);
106 bool keepAlive =
false;
107 bool autoSend =
false;
111 BodyMode bodyMode = BodyMode::None;
127 Result startRequest(AsyncEventLoop& loop,
const RequestPreset& preset);
128 Result prepareRequest(
const RequestPreset& preset);
129 Result startPreparedRequest(
const RequestPreset& preset);
130 Result ensureConnected();
131 Result beginSocketConnection();
132 Result beginResponseRead();
133 Result beginRequestSend();
134 Result onResponseBodyStreamRead();
135 Result validateActiveRequest()
const;
137 void finalizeResponse(
bool finishBodyStream);
138 void closeConnection();
139 void finishResponse();
140 void fail(Result error);
142 void onConnected(AsyncSocketConnect::Result& result);
143 void onReadableError(Result result);
144 void onWritableError(Result result);
145 void onPipelineError(Result result);
146 void onReadableEnd();
147 void onHeadersBufferWritten(AsyncBufferView::ID bufferID);
148 void onResponseData(AsyncBufferView::ID bufferID);
149 void onResponseBodyData(AsyncBufferView::ID bufferID);
151 [[nodiscard]]
bool canReuseConnectionFor(StringSpan host, uint16_t port)
const;
152 [[nodiscard]]
bool responseMustNotHaveBody()
const;
153 [[nodiscard]]
bool responseHasKnownLength()
const;
155 HttpConnectionBase* connection =
nullptr;
157 AsyncEventLoop* eventLoop =
nullptr;
158 HttpAsyncClientRequest* currentRequest =
nullptr;
159 HttpAsyncClientRequest request;
160 HttpAsyncClientResponse response;
161 AsyncSocketConnect connectAsync;
162 RequestPreset currentPreset;
164 State state = State::Idle;
166 StringSpan currentHost;
167 char currentHostStorage[256] = {0};
170 HttpURLParser currentURL;
173 bool hasOpenConnection =
false;
174 bool responseDelivered =
false;
175 bool responseFinalized =
false;
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition PrimitiveTypes.h:37
struct SC_FOUNDATION_EXPORT Function
Wraps function pointers, member functions and lambdas without ever allocating.
Definition Function.h:19
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition PrimitiveTypes.h:36
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:42
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:38
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1394
Async source abstraction emitting data events in caller provided byte buffers.
Definition AsyncStreams.h:220
Definition HttpAsyncClient.h:16
Outgoing HTTP request sent by the client.
Definition HttpConnection.h:356
Incoming HTTP response received by the client.
Definition HttpConnection.h:226
Asynchronous HTTP/1.1 client using caller-provided fixed storage.
Definition HttpAsyncClient.h:46
Function< void(HttpAsyncClientResponse &)> onResponse
Called after the response headers have been parsed.
Definition HttpAsyncClient.h:86
Result start(AsyncEventLoop &loop, HttpParser::Method method, StringSpan url, bool keepAlive=false)
Starts a request that must be configured inside onPrepareRequest onPrepareRequest must send the heade...
Function< void(Result)> onError
Called on connection, protocol or streaming errors.
Definition HttpAsyncClient.h:89
Result postMultipart(AsyncEventLoop &loop, StringSpan url, HttpMultipartWriter &writer, bool keepAlive=false)
Convenience wrapper for a multipart/form-data POST request.
Result close()
Closes any active connection and releases references to the initialized storage.
Result get(AsyncEventLoop &loop, StringSpan url, bool keepAlive=false)
Convenience wrapper for a GET request without a request body.
Result init(HttpConnectionBase &storage)
Initializes the client with caller-provided connection storage The storage must outlive the client an...
Result post(AsyncEventLoop &loop, StringSpan url, Span< const char > body, bool keepAlive=false)
Convenience wrapper for a POST request with a fixed in-memory body.
Function< void(HttpAsyncClientRequest &)> onPrepareRequest
Called after the request has been created and can still be customized.
Definition HttpAsyncClient.h:83
Result put(AsyncEventLoop &loop, StringSpan url, Span< const char > body, bool keepAlive=false)
Convenience wrapper for a PUT request with a fixed in-memory body.
Shared async transport storage for HTTP client and server endpoints.
Definition HttpConnection.h:62
Definition HttpConnection.h:29
Method
Method of the current request / response.
Definition HttpParser.h:19
Adds compile-time configurable read and write queues to any class subclassing HttpConnectionBase.
Definition HttpConnection.h:558
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
Span< const char > toCharSpan() const
Obtain a const char Span from this StringView.
Definition StringSpan.h:82