Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
HttpClient.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Async/Async.h"
5#include "../Foundation/StringSpan.h"
6#include "../Memory/String.h"
7#include "../Socket/Socket.h"
8#include "HttpParser.h"
9namespace SC
10{
12struct HttpClient;
13} // namespace SC
14
17
20
23{
29
31
33 [[nodiscard]] StringSpan getResponse() const;
34
35 Result setCustomDebugName(const StringSpan debugName) { return Result(customDebugName.assign(debugName)); }
36
37 private:
38 void onConnected(AsyncSocketConnect::Result& result);
39 void onAfterSend(AsyncSocketSend::Result& result);
40 void onAfterRead(AsyncSocketReceive::Result& result);
41
42 HttpParser parser;
43
44 SmallBuffer<1024> content;
45
46 bool headersReceived = false;
47
48 size_t receivedBytes = 0;
49 size_t parsedBytes = 0;
50 size_t contentLen = 0;
51 String customDebugName;
52
53 AsyncSocketConnect connectAsync;
54 AsyncSocketSend sendAsync;
55 AsyncSocketReceive receiveAsync;
56 SocketDescriptor clientSocket;
57 AsyncEventLoop* eventLoop = nullptr;
58};
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1198
Helper holding CompletionData for a specific AsyncRequest-derived class.
Definition Async.h:288
Starts a socket connect operation, connecting to a remote endpoint.
Definition Async.h:489
Definition Async.h:609
Starts a socket receive operation, receiving bytes from a remote endpoint.
Definition Async.h:599
Starts a socket send operation, sending bytes to a remote endpoint.
Definition Async.h:523
Wraps function pointers, member functions and lambdas without ever allocating.
Definition Function.h:19
Http async client.
Definition HttpClient.h:23
Delegate< HttpClient & > callback
The callback that is called after GET operation succeeded.
Definition HttpClient.h:30
Result get(AsyncEventLoop &loop, StringSpan url)
Setups this client to execute a GET request on the given url.
StringSpan getResponse() const
Get the response StringSpan sent by the server.
Incremental HTTP request or response parser.
Definition HttpParser.h:17
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:12
A SC::Buffer with a dedicated custom inline buffer to avoid heap allocation.
Definition Buffer.h:38
Low-level OS socket handle.
Definition Socket.h:153
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37
A non-modifiable owning string with associated encoding.
Definition String.h:29
bool assign(StringSpan sv)
Assigns a StringSpan to this String, replacing existing contents.