mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
Add HTTP header and plain HTTP post feature
This commit is contained in:
parent
40bd85b552
commit
c9136121f2
|
@ -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)
|
||||
|
|
|
@ -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<CHttpRequest> HttpGetFile(const char *pUrl, IStorage *pSt
|
|||
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)
|
||||
{
|
||||
std::unique_ptr<CHttpRequest> pResult = std::unique_ptr<CHttpRequest>(new CHttpRequest(pUrl));
|
||||
|
|
Loading…
Reference in a new issue