Add HTTP header and plain HTTP post feature

This commit is contained in:
heinrich5991 2022-05-05 16:06:49 +02:00
parent 40bd85b552
commit c9136121f2
2 changed files with 38 additions and 1 deletions

View file

@ -200,10 +200,14 @@ int CHttpRequest::RunImpl(CURL *pUser)
case REQUEST::HEAD: case REQUEST::HEAD:
curl_easy_setopt(pHandle, CURLOPT_NOBODY, 1L); curl_easy_setopt(pHandle, CURLOPT_NOBODY, 1L);
break; break;
case REQUEST::POST:
case REQUEST::POST_JSON: 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_POSTFIELDS, m_pBody);
curl_easy_setopt(pHandle, CURLOPT_POSTFIELDSIZE, m_BodyLength); curl_easy_setopt(pHandle, CURLOPT_POSTFIELDSIZE, m_BodyLength);
m_pHeaders = curl_slist_append((curl_slist *)m_pHeaders, "Content-Type: application/json");
break; 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 void CHttpRequest::Result(unsigned char **ppResult, size_t *pResultLength) const
{ {
if(m_WriteToFile || State() != HTTP_DONE) if(m_WriteToFile || State() != HTTP_DONE)

View file

@ -43,6 +43,7 @@ class CHttpRequest : public IJob
{ {
GET = 0, GET = 0,
HEAD, HEAD,
POST,
POST_JSON, POST_JSON,
}; };
char m_aUrl[256] = {0}; char m_aUrl[256] = {0};
@ -100,6 +101,13 @@ public:
void IpResolve(IPRESOLVE IpResolve) { m_IpResolve = IpResolve; } void IpResolve(IPRESOLVE IpResolve) { m_IpResolve = IpResolve; }
void WriteToFile(IStorage *pStorage, const char *pDest, int StorageType); void WriteToFile(IStorage *pStorage, const char *pDest, int StorageType);
void Head() { m_Type = REQUEST::HEAD; } 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) void PostJson(const char *pJson)
{ {
m_Type = REQUEST::POST_JSON; m_Type = REQUEST::POST_JSON;
@ -107,6 +115,19 @@ public:
m_pBody = (unsigned char *)malloc(m_BodyLength); m_pBody = (unsigned char *)malloc(m_BodyLength);
mem_copy(m_pBody, pJson, 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() const char *Dest()
{ {
@ -150,6 +171,13 @@ inline std::unique_ptr<CHttpRequest> HttpGetFile(const char *pUrl, IStorage *pSt
return pResult; return pResult;
} }
inline std::unique_ptr<CHttpRequest> HttpPost(const char *pUrl, const unsigned char *pData, size_t DataLength)
{
std::unique_ptr<CHttpRequest> pResult = std::unique_ptr<CHttpRequest>(new CHttpRequest(pUrl));
pResult->Post(pData, DataLength);
return pResult;
}
inline std::unique_ptr<CHttpRequest> HttpPostJson(const char *pUrl, const char *pJson) inline std::unique_ptr<CHttpRequest> HttpPostJson(const char *pUrl, const char *pJson)
{ {
std::unique_ptr<CHttpRequest> pResult = std::unique_ptr<CHttpRequest>(new CHttpRequest(pUrl)); std::unique_ptr<CHttpRequest> pResult = std::unique_ptr<CHttpRequest>(new CHttpRequest(pUrl));