1#ifndef ENGINE_SHARED_HTTP_H
2#define ENGINE_SHARED_HTTP_H
10#include <condition_variable>
14#include <unordered_map>
136 size_t OnHeader(
char *pHeader,
size_t HeaderSize);
139 size_t OnData(
char *pData,
size_t DataSize);
141 static int ProgressCallback(
void *pUser,
double DlTotal,
double DlCurr,
double UlTotal,
double UlCurr);
143 static size_t WriteCallback(
char *pData,
size_t Size,
size_t Number,
void *pUser);
163 void Post(
const unsigned char *pData,
size_t DataLength)
167 m_pBody = (
unsigned char *)malloc(std::max((
size_t)1, DataLength));
177 void Header(
const char *pNameColonValue);
181 str_format(aHeader,
sizeof(aHeader),
"%s: %s", pName, pValue);
187 str_format(aHeader,
sizeof(aHeader),
"%s: %d", pName, Value);
204 double Size()
const {
return m_Size.load(std::memory_order_relaxed); }
216 void Result(
unsigned char **ppResult,
size_t *pResultLength)
const;
225inline std::unique_ptr<CHttpRequest>
HttpHead(
const char *pUrl)
227 auto pResult = std::make_unique<CHttpRequest>(pUrl);
232inline std::unique_ptr<CHttpRequest>
HttpGet(
const char *pUrl)
234 return std::make_unique<CHttpRequest>(pUrl);
237inline std::unique_ptr<CHttpRequest>
HttpGetFile(
const char *pUrl,
IStorage *pStorage,
const char *pOutputFile,
int StorageType)
239 std::unique_ptr<CHttpRequest> pResult =
HttpGet(pUrl);
240 pResult->WriteToFile(pStorage, pOutputFile, StorageType);
241 pResult->Timeout(
CTimeout{4000, 0, 500, 5});
245inline std::unique_ptr<CHttpRequest>
HttpPost(
const char *pUrl,
const unsigned char *pData,
size_t DataLength)
247 auto pResult = std::make_unique<CHttpRequest>(pUrl);
248 pResult->Post(pData, DataLength);
249 pResult->Timeout(
CTimeout{4000, 15000, 500, 5});
253inline std::unique_ptr<CHttpRequest>
HttpPostJson(
const char *pUrl,
const char *pJson)
255 auto pResult = std::make_unique<CHttpRequest>(pUrl);
256 pResult->PostJson(pJson);
257 pResult->Timeout(
CTimeout{4000, 15000, 500, 5});
261void EscapeUrl(
char *pBuf,
int Size,
const char *pStr);
284 std::condition_variable
m_Cv{};
289 std::optional<std::chrono::time_point<std::chrono::steady_clock>>
m_ShutdownTime{};
300 bool Init(std::chrono::milliseconds ShutdownDelay);
303 virtual void Run(std::shared_ptr<IHttpRequest> pRequest)
override;
const SHA256_DIGEST SHA256_ZEROED
Definition: hash.cpp:6
void FailOnErrorStatus(bool FailOnErrorStatus)
Definition: http.h:159
static size_t HeaderCallback(char *pData, size_t Size, size_t Number, void *pUser)
Definition: http.cpp:300
const SHA256_DIGEST & ResultSha256() const
Definition: http.cpp:437
int64_t m_MaxResponseSize
Definition: http.h:88
static int ProgressCallback(void *pUser, double DlTotal, double DlCurr, double UlTotal, double UlCurr)
Definition: http.cpp:311
REQUEST
Definition: http.h:57
bool ConfigureHandle(void *pHandle)
Definition: http.cpp:100
HTTPLOG m_LogProgress
Definition: http.h:112
void * m_pHeaders
Definition: http.h:83
bool m_HeadersEnded
Definition: http.h:124
const char * Dest()
Definition: http.h:191
char m_aUrl[256]
Definition: http.h:81
SHA256_DIGEST m_ActualSha256
Definition: http.h:92
std::mutex m_WaitMutex
Definition: http.h:120
void Result(unsigned char **ppResult, size_t *pResultLength) const
Definition: http.cpp:421
virtual ~CHttpRequest()
Definition: http.cpp:72
char m_aDestAbsolute[IO_MAX_PATH_LENGTH]
Definition: http.h:106
std::atomic< int > m_Progress
Definition: http.h:111
void IpResolve(IPRESOLVE IpResolve)
Definition: http.h:158
void Abort()
Definition: http.h:212
std::optional< int64_t > m_ResultLastModified
Definition: http.h:126
REQUEST m_Type
Definition: http.h:90
void Header(const char *pNameColonValue)
Definition: http.cpp:407
static constexpr const char * GetRequestType(REQUEST Type)
Definition: http.h:64
void Wait()
Definition: http.cpp:412
json_value * ResultJson() const
Definition: http.cpp:429
void MaxResponseSize(int64_t MaxResponseSize)
Definition: http.h:155
size_t m_BodyLength
Definition: http.h:85
double Size() const
Definition: http.h:204
IPRESOLVE m_IpResolve
Definition: http.h:113
SHA256_DIGEST m_ExpectedSha256
Definition: http.h:94
std::optional< int64_t > ResultAgeSeconds() const
Definition: http.cpp:449
unsigned char * m_pBuffer
Definition: http.h:102
void OnCompletionInternal(void *pHandle, unsigned int Result)
Definition: http.cpp:321
void WriteToFile(IStorage *pStorage, const char *pDest, int StorageType)
Definition: http.cpp:393
void Timeout(CTimeout Timeout)
Definition: http.h:154
std::atomic< double > m_Size
Definition: http.h:109
int StatusCode() const
Definition: http.cpp:443
EHttpState State() const
Definition: http.h:206
virtual void OnCompletion(EHttpState State)
Definition: http.h:148
void Head()
Definition: http.h:162
void ExpectSha256(const SHA256_DIGEST &Sha256)
Definition: http.h:161
std::atomic< double > m_Current
Definition: http.h:110
std::optional< int64_t > ResultLastModified() const
Definition: http.cpp:459
int Progress() const
Definition: http.h:205
size_t OnData(char *pData, size_t DataSize)
Definition: http.cpp:262
static size_t WriteCallback(char *pData, size_t Size, size_t Number, void *pUser)
Definition: http.cpp:306
std::optional< int64_t > m_ResultDate
Definition: http.h:125
size_t m_BufferSize
Definition: http.h:101
void HeaderInt(const char *pName, int Value)
Definition: http.h:184
virtual void OnProgress()
Definition: http.h:147
void Post(const unsigned char *pData, size_t DataLength)
Definition: http.h:163
CTimeout m_Timeout
Definition: http.h:87
bool m_WriteToFile
Definition: http.h:96
uint64_t m_ResponseLength
Definition: http.h:98
int m_StatusCode
Definition: http.h:123
IOHANDLE m_File
Definition: http.h:105
unsigned char * m_pBody
Definition: http.h:84
std::atomic< EHttpState > m_State
Definition: http.h:118
std::atomic< bool > m_Abort
Definition: http.h:119
std::condition_variable m_WaitCondition
Definition: http.h:121
char m_aDest[IO_MAX_PATH_LENGTH]
Definition: http.h:107
int64_t m_IfModifiedSince
Definition: http.h:89
SHA256_CTX m_ActualSha256Ctx
Definition: http.h:93
void IfModifiedSince(int64_t IfModifiedSince)
Definition: http.h:156
bool BeforeInit()
Definition: http.cpp:80
CHttpRequest(const char *pUrl)
Definition: http.cpp:66
void HeaderString(const char *pName, const char *pValue)
Definition: http.h:178
double Current() const
Definition: http.h:203
char m_aErr[256]
Definition: http.h:117
bool Done() const
Definition: http.h:207
void PostJson(const char *pJson)
Definition: http.h:170
void LogProgress(HTTPLOG LogProgress)
Definition: http.h:157
bool m_FailOnErrorStatus
Definition: http.h:115
size_t OnHeader(char *pHeader, size_t HeaderSize)
Definition: http.cpp:216
std::atomic< bool > m_Shutdown
Definition: http.h:290
virtual void Run(std::shared_ptr< IHttpRequest > pRequest) override
Definition: http.cpp:664
std::chrono::milliseconds m_ShutdownDelay
Definition: http.h:288
~CHttp()
Definition: http.cpp:689
std::atomic< EState > m_State
Definition: http.h:285
std::unordered_map< void *, std::shared_ptr< CHttpRequest > > m_RunningRequests
Definition: http.h:287
EState
Definition: http.h:275
@ ERROR
Definition: http.h:278
@ UNINITIALIZED
Definition: http.h:276
@ RUNNING
Definition: http.h:277
void Shutdown() override
Definition: http.cpp:679
std::optional< std::chrono::time_point< std::chrono::steady_clock > > m_ShutdownTime
Definition: http.h:289
std::condition_variable m_Cv
Definition: http.h:284
std::mutex m_Lock
Definition: http.h:283
std::deque< std::shared_ptr< CHttpRequest > > m_PendingRequests
Definition: http.h:286
static void ThreadMain(void *pUser)
Definition: http.cpp:486
void * m_pMultiH
Definition: http.h:293
void RunLoop()
Definition: http.cpp:492
bool Init(std::chrono::milliseconds ShutdownDelay)
Definition: http.cpp:465
void * m_pThread
Definition: http.h:281
struct _json_value json_value
Definition: serverbrowser.h:17
void mem_copy(void *dest, const void *source, size_t size)
Definition: system.cpp:174
int str_length(const char *str)
Definition: system.cpp:2795
void * IOHANDLE
Definition: logger.h:11
std::unique_ptr< CHttpRequest > HttpGet(const char *pUrl)
Definition: http.h:232
std::unique_ptr< CHttpRequest > HttpHead(const char *pUrl)
Definition: http.h:225
std::unique_ptr< CHttpRequest > HttpPost(const char *pUrl, const unsigned char *pData, size_t DataLength)
Definition: http.h:245
IPRESOLVE
Definition: http.h:38
std::unique_ptr< CHttpRequest > HttpGetFile(const char *pUrl, IStorage *pStorage, const char *pOutputFile, int StorageType)
Definition: http.h:237
void EscapeUrl(char *pBuf, int Size, const char *pStr)
Definition: http.cpp:52
std::unique_ptr< CHttpRequest > HttpPostJson(const char *pUrl, const char *pJson)
Definition: http.h:253
EHttpState
Definition: http.h:22
bool HttpHasIpresolveBug()
Definition: http.cpp:59
HTTPLOG
Definition: http.h:31
long ConnectTimeoutMs
Definition: http.h:46
long LowSpeedLimit
Definition: http.h:48
long LowSpeedTime
Definition: http.h:49
long TimeoutMs
Definition: http.h:47
#define str_format
Definition: system.cpp:2830
@ IO_MAX_PATH_LENGTH
Definition: types.h:43