From c9136121f2fc72844c8d9b770d1de3c15c40d8b1 Mon Sep 17 00:00:00 2001 From: heinrich5991 Date: Thu, 5 May 2022 16:06:49 +0200 Subject: [PATCH] Add HTTP header and plain HTTP post feature --- src/engine/shared/http.cpp | 11 ++++++++++- src/engine/shared/http.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/engine/shared/http.cpp b/src/engine/shared/http.cpp index 7603615fd..7fc5f5fc7 100644 --- a/src/engine/shared/http.cpp +++ b/src/engine/shared/http.cpp @@ -200,10 +200,14 @@ int CHttpRequest::RunImpl(CURL *pUser) case REQUEST::HEAD: curl_easy_setopt(pHandle, CURLOPT_NOBODY, 1L); break; + case REQUEST::POST: case REQUEST::POST_JSON: + if(m_Type == REQUEST::POST_JSON) + { + Header("Content-Type: application/json"); + } curl_easy_setopt(pHandle, CURLOPT_POSTFIELDS, m_pBody); curl_easy_setopt(pHandle, CURLOPT_POSTFIELDSIZE, m_BodyLength); - m_pHeaders = curl_slist_append((curl_slist *)m_pHeaders, "Content-Type: application/json"); break; } @@ -307,6 +311,11 @@ void CHttpRequest::WriteToFile(IStorage *pStorage, const char *pDest, int Storag } } +void CHttpRequest::Header(const char *pNameColonValue) +{ + m_pHeaders = curl_slist_append((curl_slist *)m_pHeaders, pNameColonValue); +} + void CHttpRequest::Result(unsigned char **ppResult, size_t *pResultLength) const { if(m_WriteToFile || State() != HTTP_DONE) diff --git a/src/engine/shared/http.h b/src/engine/shared/http.h index b06cf29d7..fd790eb70 100644 --- a/src/engine/shared/http.h +++ b/src/engine/shared/http.h @@ -43,6 +43,7 @@ class CHttpRequest : public IJob { GET = 0, HEAD, + POST, POST_JSON, }; char m_aUrl[256] = {0}; @@ -100,6 +101,13 @@ public: void IpResolve(IPRESOLVE IpResolve) { m_IpResolve = IpResolve; } void WriteToFile(IStorage *pStorage, const char *pDest, int StorageType); void Head() { m_Type = REQUEST::HEAD; } + void Post(const unsigned char *pData, size_t DataLength) + { + m_Type = REQUEST::POST; + m_BodyLength = DataLength; + m_pBody = (unsigned char *)malloc(DataLength); + mem_copy(m_pBody, pData, DataLength); + } void PostJson(const char *pJson) { m_Type = REQUEST::POST_JSON; @@ -107,6 +115,19 @@ public: m_pBody = (unsigned char *)malloc(m_BodyLength); mem_copy(m_pBody, pJson, m_BodyLength); } + void Header(const char *pNameColonValue); + void HeaderString(const char *pName, const char *pValue) + { + char aHeader[256]; + str_format(aHeader, sizeof(aHeader), "%s: %s", pName, pValue); + Header(aHeader); + } + void HeaderInt(const char *pName, int Value) + { + char aHeader[256]; + str_format(aHeader, sizeof(aHeader), "%s: %d", pName, Value); + Header(aHeader); + } const char *Dest() { @@ -150,6 +171,13 @@ inline std::unique_ptr HttpGetFile(const char *pUrl, IStorage *pSt return pResult; } +inline std::unique_ptr HttpPost(const char *pUrl, const unsigned char *pData, size_t DataLength) +{ + std::unique_ptr pResult = std::unique_ptr(new CHttpRequest(pUrl)); + pResult->Post(pData, DataLength); + return pResult; +} + inline std::unique_ptr HttpPostJson(const char *pUrl, const char *pJson) { std::unique_ptr pResult = std::unique_ptr(new CHttpRequest(pUrl));