From 14c4e44a0a91e1ad701d4ae3c58185d25118e64e Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 5 Jan 2024 15:54:03 +0300 Subject: Netlib: - NETLIBHTTPHEADER & NETLIBHTTPREQUEST obsoleted; - NETLIBHTTPREQUEST divided into MHttpRequest & MHttpResponse; - MHttpHeaders now manager headers both for MHttpRequest & MHttpResponse; --- protocols/Facebook/src/avatars.cpp | 16 +++++++--------- protocols/Facebook/src/http.cpp | 29 +++++++++-------------------- protocols/Facebook/src/proto.h | 4 ++-- protocols/Facebook/src/server.cpp | 10 +++++----- 4 files changed, 23 insertions(+), 36 deletions(-) (limited to 'protocols/Facebook/src') diff --git a/protocols/Facebook/src/avatars.cpp b/protocols/Facebook/src/avatars.cpp index 6759d20468..224bd707b0 100644 --- a/protocols/Facebook/src/avatars.cpp +++ b/protocols/Facebook/src/avatars.cpp @@ -33,7 +33,7 @@ void FacebookProto::GetAvatarFilename(MCONTACT hContact, wchar_t *pwszFileName) void __cdecl FacebookProto::AvatarsUpdate(void *) { - NETLIBHTTPREQUEST req = {}; + MHttpRequest req; req.flags = NLHRF_NODUMP | NLHRF_SSL | NLHRF_HTTP11 | NLHRF_REDIRECT; req.requestType = REQUEST_GET; @@ -48,13 +48,11 @@ void __cdecl FacebookProto::AvatarsUpdate(void *) continue; delSetting(cc, "UpdateNeeded"); - - CMStringA szUrl(FORMAT, "https://graph.facebook.com/%s/picture?%s", getMStringA(cc, DBKEY_ID).c_str(), szParams.c_str()); - req.szUrl = szUrl.GetBuffer(); + req.m_szUrl.Format("https://graph.facebook.com/%s/picture?%s", getMStringA(cc, DBKEY_ID).c_str(), szParams.c_str()); NLHR_PTR pReply(Netlib_HttpTransaction(m_hNetlibUser, &req)); if (pReply == nullptr) { - debugLogA("Failed to retrieve avatar from url: %s", szUrl.c_str()); + debugLogA("Failed to retrieve avatar from url: %s", req.m_szUrl.c_str()); continue; } @@ -64,14 +62,14 @@ void __cdecl FacebookProto::AvatarsUpdate(void *) GetAvatarFilename(cc, ai.filename); bool bSuccess = false; - if (pReply->resultCode == 200 && pReply->pData && pReply->dataLength) { - if (auto *pszHdr = Netlib_GetHeader(pReply, "Content-Type")) + if (pReply->resultCode == 200 && !pReply->body.IsEmpty()) { + if (auto *pszHdr = pReply->FindHeader("Content-Type")) ai.format = ProtoGetAvatarFormatByMimeType(pszHdr); if (ai.format != PA_FORMAT_UNKNOWN) { FILE *fout = _wfopen(ai.filename, L"wb"); if (fout) { - fwrite(pReply->pData, 1, pReply->dataLength, fout); + fwrite(pReply->body, 1, pReply->body.GetLength(), fout); fclose(fout); bSuccess = true; } @@ -79,7 +77,7 @@ void __cdecl FacebookProto::AvatarsUpdate(void *) } else debugLogA("unknown avatar mime type"); } - else debugLogA("Error %d reading avatar from url: %s", pReply->resultCode, szUrl.c_str()); + else debugLogA("Error %d reading avatar from url: %s", pReply->resultCode, req.m_szUrl.c_str()); ProtoBroadcastAck(cc, ACKTYPE_AVATAR, bSuccess ? ACKRESULT_SUCCESS : ACKRESULT_FAILED, &ai); } diff --git a/protocols/Facebook/src/http.cpp b/protocols/Facebook/src/http.cpp index 55e8ce5a5c..caf8113ef7 100644 --- a/protocols/Facebook/src/http.cpp +++ b/protocols/Facebook/src/http.cpp @@ -69,7 +69,7 @@ AsyncHttpRequest* operator<<(AsyncHttpRequest *pReq, const INT_PARAM ¶m) ///////////////////////////////////////////////////////////////////////////////////////// -JsonReply::JsonReply(NETLIBHTTPREQUEST *pReply) +JsonReply::JsonReply(MHttpResponse *pReply) { if (pReply == nullptr) { m_errorCode = 500; @@ -80,7 +80,7 @@ JsonReply::JsonReply(NETLIBHTTPREQUEST *pReply) if (m_errorCode != 200) return; - m_root = json_parse(pReply->pData); + m_root = json_parse(pReply->body); if (m_root == nullptr) { m_errorCode = 500; return; @@ -96,10 +96,10 @@ JsonReply::~JsonReply() ///////////////////////////////////////////////////////////////////////////////////////// -AsyncHttpRequest* FacebookProto::CreateRequest(const char *szUrl, const char *szName, const char *szMethod) +AsyncHttpRequest* FacebookProto::CreateRequest(const char *url, const char *szName, const char *szMethod) { AsyncHttpRequest *pReq = new AsyncHttpRequest(); - pReq->m_szUrl = szUrl; + pReq->m_szUrl = url; pReq->requestType = REQUEST_POST; pReq << CHAR_PARAM("api_key", FB_API_KEY) << CHAR_PARAM("device_id", m_szDeviceID) @@ -160,26 +160,15 @@ AsyncHttpRequest* FacebookProto::CreateRequestGQL(int64_t query_id) { return pReq; } -NETLIBHTTPREQUEST* FacebookProto::ExecuteRequest(AsyncHttpRequest *pReq) +MHttpResponse* FacebookProto::ExecuteRequest(AsyncHttpRequest *pReq) { - CMStringA str; - pReq->flags |= NLHRF_HTTP11; - pReq->szUrl = pReq->m_szUrl.GetBuffer(); - if (!pReq->m_szParam.IsEmpty()) { - if (pReq->requestType == REQUEST_GET) { - str.Format("%s?%s", pReq->m_szUrl.c_str(), pReq->m_szParam.c_str()); - pReq->szUrl = str.GetBuffer(); - } - else { - pReq->dataLength = pReq->m_szParam.GetLength(); - pReq->pData = mir_strdup(pReq->m_szParam); - } - } + if (!pReq->m_szParam.IsEmpty() && pReq->requestType == REQUEST_GET) + pReq->m_szUrl.AppendFormat("?%s", pReq->m_szParam.c_str()); - debugLogA("Executing request:\n%s", pReq->szUrl); + debugLogA("Executing request:\n%s", pReq->m_szUrl.c_str()); - NETLIBHTTPREQUEST *reply = Netlib_HttpTransaction(m_hNetlibUser, pReq); + MHttpResponse *reply = Netlib_HttpTransaction(m_hNetlibUser, pReq); delete pReq; return reply; } diff --git a/protocols/Facebook/src/proto.h b/protocols/Facebook/src/proto.h index e8ea77d658..318ef336d6 100644 --- a/protocols/Facebook/src/proto.h +++ b/protocols/Facebook/src/proto.h @@ -332,7 +332,7 @@ class JsonReply int m_errorCode = 0; public: - JsonReply(NETLIBHTTPREQUEST *); + JsonReply(MHttpResponse *); ~JsonReply(); __forceinline JSONNode &data() const { return *m_root; } @@ -412,7 +412,7 @@ class FacebookProto : public PROTO AsyncHttpRequest *CreateRequest(const char *szUrl, const char *szName, const char *szMethod); AsyncHttpRequest *CreateRequestGQL(int64_t id); - NETLIBHTTPREQUEST *ExecuteRequest(AsyncHttpRequest *pReq); + MHttpResponse *ExecuteRequest(AsyncHttpRequest *pReq); CMStringA GetAgentString(); // Avatars diff --git a/protocols/Facebook/src/server.cpp b/protocols/Facebook/src/server.cpp index 72ff76ac1f..db725ba6a0 100644 --- a/protocols/Facebook/src/server.cpp +++ b/protocols/Facebook/src/server.cpp @@ -760,16 +760,16 @@ void FacebookProto::OnPublishPrivateMessage(const JSONNode &root) // wszFileName.Format(L"%s\\STK{%S}.webp", wszPath.c_str(), stickerId.c_str()); std::string szUrl = sticker["thread_image"]["uri"].as_string(); - NETLIBHTTPREQUEST req = {}; + MHttpRequest req; req.flags = NLHRF_NODUMP | NLHRF_SSL | NLHRF_HTTP11 | NLHRF_REDIRECT; req.requestType = REQUEST_GET; - req.szUrl = (char*)szUrl.c_str(); + req.m_szUrl = szUrl.c_str(); - NETLIBHTTPREQUEST *pReply = Netlib_HttpTransaction(m_hNetlibUser, &req); - if (pReply != nullptr && pReply->resultCode == 200 && pReply->pData && pReply->dataLength) { + MHttpResponse *pReply = Netlib_HttpTransaction(m_hNetlibUser, &req); + if (pReply != nullptr && pReply->resultCode == 200 && !pReply->body.IsEmpty()) { bSuccess = true; FILE *out = _wfopen(wszFileName, L"wb"); - fwrite(pReply->pData, 1, pReply->dataLength, out); + fwrite(pReply->body, 1, pReply->body.GetLength(), out); fclose(out); } } -- cgit v1.2.3