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; --- .../CloudFile/src/Services/dropbox_service.cpp | 4 +- .../CloudFile/src/Services/google_service.cpp | 8 +- .../CloudFile/src/Services/microsoft_service.cpp | 6 +- .../CloudFile/src/Services/yandex_service.cpp | 6 +- protocols/CloudFile/src/cloud_file.cpp | 12 +- protocols/CloudFile/src/cloud_file.h | 6 +- protocols/CloudFile/src/http_request.h | 82 +---------- protocols/CurrencyRates/src/HTTPSession.cpp | 38 ++--- protocols/Discord/src/avatars.cpp | 6 +- protocols/Discord/src/connection.cpp | 9 +- protocols/Discord/src/gateway.cpp | 18 +-- protocols/Discord/src/http.cpp | 7 +- protocols/Discord/src/proto.cpp | 18 +-- protocols/Discord/src/proto.h | 26 ++-- protocols/Discord/src/server.cpp | 16 +-- protocols/Facebook/src/avatars.cpp | 16 +-- protocols/Facebook/src/http.cpp | 29 ++-- protocols/Facebook/src/proto.h | 4 +- protocols/Facebook/src/server.cpp | 10 +- protocols/Gadu-Gadu/src/avatar.cpp | 104 +++++--------- protocols/Gadu-Gadu/src/oauth.cpp | 155 +++++++++------------ protocols/GmailNotifier/src/check.cpp | 41 ++---- protocols/ICQ-WIM/src/avatars.cpp | 16 +-- protocols/ICQ-WIM/src/file.cpp | 5 +- protocols/ICQ-WIM/src/groupchats.cpp | 4 +- protocols/ICQ-WIM/src/http.cpp | 46 ++---- protocols/ICQ-WIM/src/http.h | 6 +- protocols/ICQ-WIM/src/ignore.cpp | 2 +- protocols/ICQ-WIM/src/mra.cpp | 8 +- protocols/ICQ-WIM/src/options.cpp | 10 +- protocols/ICQ-WIM/src/poll.cpp | 2 +- protocols/ICQ-WIM/src/proto.cpp | 8 +- protocols/ICQ-WIM/src/proto.h | 62 ++++----- protocols/ICQ-WIM/src/server.cpp | 44 +++--- protocols/JabberG/src/jabber_file.cpp | 26 ++-- protocols/JabberG/src/jabber_ft.cpp | 26 ++-- protocols/JabberG/src/jabber_util.cpp | 12 +- protocols/MinecraftDynmap/src/communication.cpp | 130 +++++------------ protocols/MinecraftDynmap/src/proto.h | 5 +- protocols/MinecraftDynmap/src/utils.h | 11 -- protocols/NewsAggregator/Src/Utils.cpp | 118 +++++----------- protocols/Non-IM Contact/src/http.cpp | 15 +- protocols/SkypeWeb/src/request_queue.cpp | 13 +- protocols/SkypeWeb/src/requests/avatars.h | 12 +- protocols/SkypeWeb/src/requests/files.h | 12 +- protocols/SkypeWeb/src/skype_avatars.cpp | 12 +- protocols/SkypeWeb/src/skype_chatrooms.cpp | 4 +- protocols/SkypeWeb/src/skype_contacts.cpp | 8 +- protocols/SkypeWeb/src/skype_files.cpp | 8 +- protocols/SkypeWeb/src/skype_history_sync.cpp | 4 +- protocols/SkypeWeb/src/skype_login.cpp | 30 ++-- protocols/SkypeWeb/src/skype_messages.cpp | 6 +- protocols/SkypeWeb/src/skype_oauth.cpp | 38 ++--- protocols/SkypeWeb/src/skype_polling.cpp | 8 +- protocols/SkypeWeb/src/skype_profile.cpp | 2 +- protocols/SkypeWeb/src/skype_proto.cpp | 2 +- protocols/SkypeWeb/src/skype_proto.h | 56 ++++---- protocols/SkypeWeb/src/skype_search.cpp | 2 +- protocols/SkypeWeb/src/skype_trouter.cpp | 2 +- protocols/SkypeWeb/src/skype_utils.cpp | 4 +- protocols/SkypeWeb/src/skype_utils.h | 2 +- protocols/Steam/src/http_request.h | 14 +- protocols/Steam/src/steam_proto.h | 2 +- protocols/Steam/src/steam_request.cpp | 23 +-- protocols/Tox/src/http_request.h | 41 +----- protocols/Tox/src/tox_bootstrap.cpp | 6 +- protocols/Twitter/src/http.cpp | 4 - protocols/Twitter/src/oauth.cpp | 4 +- protocols/Twitter/src/proto.h | 4 +- protocols/Twitter/src/utility.cpp | 6 +- protocols/VKontakte/src/misc.cpp | 27 ++-- protocols/VKontakte/src/vk_avatars.cpp | 6 +- protocols/VKontakte/src/vk_captcha.cpp | 8 +- protocols/VKontakte/src/vk_chats.cpp | 10 +- protocols/VKontakte/src/vk_feed.cpp | 4 +- protocols/VKontakte/src/vk_files.cpp | 12 +- protocols/VKontakte/src/vk_history.cpp | 2 +- protocols/VKontakte/src/vk_messages.cpp | 6 +- protocols/VKontakte/src/vk_pollserver.cpp | 8 +- protocols/VKontakte/src/vk_proto.cpp | 2 +- protocols/VKontakte/src/vk_proto.h | 60 ++++---- protocols/VKontakte/src/vk_queue.cpp | 10 +- protocols/VKontakte/src/vk_search.cpp | 4 +- protocols/VKontakte/src/vk_status.cpp | 4 +- protocols/VKontakte/src/vk_struct.cpp | 4 +- protocols/VKontakte/src/vk_struct.h | 2 +- protocols/VKontakte/src/vk_thread.cpp | 26 ++-- protocols/Weather/src/weather_http.cpp | 37 ++--- protocols/WebView/src/webview_getdata.cpp | 22 +-- protocols/WhatsApp/src/avatars.cpp | 10 +- protocols/WhatsApp/src/server.cpp | 9 +- protocols/WhatsApp/src/utils.cpp | 15 +- 92 files changed, 673 insertions(+), 1121 deletions(-) (limited to 'protocols') diff --git a/protocols/CloudFile/src/Services/dropbox_service.cpp b/protocols/CloudFile/src/Services/dropbox_service.cpp index 805a7f2800..92b00fc493 100644 --- a/protocols/CloudFile/src/Services/dropbox_service.cpp +++ b/protocols/CloudFile/src/Services/dropbox_service.cpp @@ -85,7 +85,7 @@ void CDropboxService::RequestAccessTokenThread(void *param) return; } - JSONNode root = JSONNode::parse(response->pData); + JSONNode root = JSONNode::parse(response->body); if (root.empty()) { Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), HttpStatusToError(response->resultCode)); ShowNotification(TranslateT("Server does not respond"), MB_ICONERROR); @@ -206,7 +206,7 @@ auto CDropboxService::CreateSharedLink(const std::string &path) if (!response || response->resultCode != HTTP_CODE_CONFLICT) HttpResponseToError(response); - JSONNode root = JSONNode::parse(response->pData); + JSONNode root = JSONNode::parse(response->body); if (root.isnull()) throw Exception(HttpStatusToError()); diff --git a/protocols/CloudFile/src/Services/google_service.cpp b/protocols/CloudFile/src/Services/google_service.cpp index 58b32eaa7f..fea2c3b2b4 100644 --- a/protocols/CloudFile/src/Services/google_service.cpp +++ b/protocols/CloudFile/src/Services/google_service.cpp @@ -100,8 +100,8 @@ void CGDriveService::RequestAccessTokenThread(void *param) NLHR_PTR response(request.Send(m_hConnection)); if (response == nullptr || response->resultCode != HTTP_CODE_OK) { - const char *error = response && response->dataLength - ? response->pData + const char *error = response && response->body.GetLength() + ? response->body : HttpStatusToError(response ? response->resultCode : 0); Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), error); @@ -110,7 +110,7 @@ void CGDriveService::RequestAccessTokenThread(void *param) return; } - JSONNode root = JSONNode::parse(response->pData); + JSONNode root = JSONNode::parse(response->body); if (root.empty()) { Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), HttpStatusToError(response->resultCode)); ShowNotification(TranslateT("Server does not respond"), MB_ICONERROR); @@ -180,7 +180,7 @@ auto CGDriveService::CreateUploadSession(const std::string &parentId, const std: HandleHttpError(response); if (HTTP_CODE_SUCCESS(response->resultCode)) - if (auto *pszHdr = Netlib_GetHeader(response, "Location")) + if (auto *pszHdr = response->FindHeader("Location")) return std::string(pszHdr); HttpResponseToError(response); diff --git a/protocols/CloudFile/src/Services/microsoft_service.cpp b/protocols/CloudFile/src/Services/microsoft_service.cpp index 8d63f4a23f..17115235cd 100644 --- a/protocols/CloudFile/src/Services/microsoft_service.cpp +++ b/protocols/CloudFile/src/Services/microsoft_service.cpp @@ -100,8 +100,8 @@ void COneDriveService::RequestAccessTokenThread(void *param) NLHR_PTR response(request.Send(m_hConnection)); if (response == nullptr || response->resultCode != HTTP_CODE_OK) { - const char *error = response->dataLength - ? response->pData + const char *error = response->body.GetLength() + ? response->body : HttpStatusToError(response->resultCode); Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), error); @@ -110,7 +110,7 @@ void COneDriveService::RequestAccessTokenThread(void *param) return; } - JSONNode root = JSONNode::parse(response->pData); + JSONNode root = JSONNode::parse(response->body); if (root.empty()) { Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), HttpStatusToError(response->resultCode)); ShowNotification(TranslateT("Server does not respond"), MB_ICONERROR); diff --git a/protocols/CloudFile/src/Services/yandex_service.cpp b/protocols/CloudFile/src/Services/yandex_service.cpp index f21e9ef2e0..892821e0c4 100644 --- a/protocols/CloudFile/src/Services/yandex_service.cpp +++ b/protocols/CloudFile/src/Services/yandex_service.cpp @@ -102,8 +102,8 @@ void CYandexService::RequestAccessTokenThread(void *param) NLHR_PTR response(request.Send(m_hConnection)); if (response == nullptr || response->resultCode != HTTP_CODE_OK) { - const char *error = response->dataLength - ? response->pData + const char *error = response->body.GetLength() + ? response->body : HttpStatusToError(response->resultCode); Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), error); @@ -112,7 +112,7 @@ void CYandexService::RequestAccessTokenThread(void *param) return; } - JSONNode root = JSONNode::parse(response->pData); + JSONNode root = JSONNode::parse(response->body); if (root.empty()) { Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), HttpStatusToError(response->resultCode)); ShowNotification(TranslateT("Server does not respond"), MB_ICONERROR); diff --git a/protocols/CloudFile/src/cloud_file.cpp b/protocols/CloudFile/src/cloud_file.cpp index 9f66caaf82..772b42ae36 100644 --- a/protocols/CloudFile/src/cloud_file.cpp +++ b/protocols/CloudFile/src/cloud_file.cpp @@ -129,16 +129,16 @@ char* CCloudService::HttpStatusToError(int status) return "Unknown error"; } -void CCloudService::HttpResponseToError(NETLIBHTTPREQUEST *response) +void CCloudService::HttpResponseToError(MHttpResponse *response) { if (response == nullptr) throw Exception(HttpStatusToError()); - if (response->dataLength) - throw Exception(response->pData); + if (response->body.GetLength()) + throw Exception(response->body.c_str()); throw Exception(HttpStatusToError(response->resultCode)); } -void CCloudService::HandleHttpError(NETLIBHTTPREQUEST *response) +void CCloudService::HandleHttpError(MHttpResponse *response) { if (response == nullptr) throw Exception(HttpStatusToError()); @@ -152,11 +152,11 @@ void CCloudService::HandleHttpError(NETLIBHTTPREQUEST *response) HttpResponseToError(response); } -JSONNode CCloudService::GetJsonResponse(NETLIBHTTPREQUEST *response) +JSONNode CCloudService::GetJsonResponse(MHttpResponse *response) { HandleHttpError(response); - JSONNode root = JSONNode::parse(response->pData); + JSONNode root = JSONNode::parse(response->body); if (root.isnull()) throw Exception(HttpStatusToError()); diff --git a/protocols/CloudFile/src/cloud_file.h b/protocols/CloudFile/src/cloud_file.h index de053af431..4a5b91da72 100644 --- a/protocols/CloudFile/src/cloud_file.h +++ b/protocols/CloudFile/src/cloud_file.h @@ -18,15 +18,15 @@ protected: std::string PreparePath(const std::string &path) const; virtual char* HttpStatusToError(int status = 0); - virtual void HttpResponseToError(NETLIBHTTPREQUEST *response); - virtual void HandleHttpError(NETLIBHTTPREQUEST *response); + virtual void HttpResponseToError(MHttpResponse *response); + virtual void HandleHttpError(MHttpResponse *response); virtual void HandleJsonError(JSONNode &node) = 0; // events void OnModulesLoaded() override; MWindow OnCreateAccMgrUI(MWindow) override; - JSONNode GetJsonResponse(NETLIBHTTPREQUEST *response); + JSONNode GetJsonResponse(MHttpResponse *response); virtual void Upload(FileTransferParam *ftp) = 0; diff --git a/protocols/CloudFile/src/http_request.h b/protocols/CloudFile/src/http_request.h index 68be404921..214ecea761 100644 --- a/protocols/CloudFile/src/http_request.h +++ b/protocols/CloudFile/src/http_request.h @@ -17,7 +17,7 @@ public: } }; -class HttpRequest : protected NETLIBHTTPREQUEST +class HttpRequest : public MHttpRequest { private: CMStringA m_szUrl; @@ -26,28 +26,11 @@ private: { requestType = type; flags = NLHRF_HTTP11 | NLHRF_SSL | NLHRF_NODUMP; - szUrl = NULL; - headers = NULL; - headersCount = 0; - pData = NULL; - dataLength = 0; - resultCode = 0; - szResultDescr = NULL; nlc = NULL; timeout = 0; } protected: - enum HttpRequestUrlFormat { FORMAT }; - - void AddHeader(LPCSTR szName, LPCSTR szValue) - { - headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER) * (headersCount + 1)); - headers[headersCount].szName = mir_strdup(szName); - headers[headersCount].szValue = mir_strdup(szValue); - headersCount++; - } - void AddBasicAuthHeader(LPCSTR szLogin, LPCSTR szPassword) { size_t length = mir_strlen(szLogin) + mir_strlen(szPassword) + 1; @@ -60,47 +43,17 @@ protected: szPassword); ptrA ePair(mir_base64_encode(cPair, length)); - - length = mir_strlen(ePair) + 7; - char *value = (char*)mir_calloc(length + 1); - mir_snprintf(value, length, "Basic %s", ePair.get()); - - headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount + 1)); - headers[headersCount].szName = mir_strdup("Authorization"); - headers[headersCount].szValue = value; - headersCount++; + AddHeader("Authorization", CMStringA(FORMAT, "Basic %s", ePair.get())); } void AddBearerAuthHeader(LPCSTR szValue) { - size_t length = mir_strlen(szValue) + 8; - char *value = (char*)mir_calloc(length + 1); - mir_snprintf( - value, - length, - "Bearer %s", - szValue); - - headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount + 1)); - headers[headersCount].szName = mir_strdup("Authorization"); - headers[headersCount].szValue = value; - headersCount++; + AddHeader("Authorization", CMStringA(FORMAT, "Bearer %s", szValue)); } void AddOAuthHeader(LPCSTR szValue) { - size_t length = mir_strlen(szValue) + 7; - char *value = (char*)mir_calloc(length + 1); - mir_snprintf( - value, - length, - "OAuth %s", - szValue); - - headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount + 1)); - headers[headersCount].szName = mir_strdup("Authorization"); - headers[headersCount].szValue = value; - headersCount++; + AddHeader("Authorization", CMStringA(FORMAT, "OAuth %s", szValue)); } void AddUrlParameter(const char *urlFormat, ...) @@ -124,16 +77,6 @@ protected: va_end(valueArgs); } - void SetData(const char *data, size_t size) - { - if (pData != NULL) - mir_free(pData); - - dataLength = (int)size; - pData = (char*)mir_alloc(size); - memcpy(pData, data, size); - } - public: HttpRequest(int type, LPCSTR url) { @@ -142,7 +85,7 @@ public: m_szUrl = url; } - HttpRequest(int type, HttpRequestUrlFormat, LPCSTR urlFormat, ...) + HttpRequest(int type, CMStringDataFormat, LPCSTR urlFormat, ...) { Init(type); @@ -152,22 +95,9 @@ public: va_end(formatArgs); } - ~HttpRequest() - { - for (int i = 0; i < headersCount; i++) - { - mir_free(headers[i].szName); - mir_free(headers[i].szValue); - } - mir_free(headers); - if (pData) - mir_free(pData); - } - - NETLIBHTTPREQUEST* Send(HNETLIBUSER hConnection) + MHttpResponse* Send(HNETLIBUSER hConnection) { m_szUrl.Replace('\\', '/'); - szUrl = m_szUrl.GetBuffer(); return Netlib_HttpTransaction(hConnection, this); } }; diff --git a/protocols/CurrencyRates/src/HTTPSession.cpp b/protocols/CurrencyRates/src/HTTPSession.cpp index beee03b47c..a00a71d635 100644 --- a/protocols/CurrencyRates/src/HTTPSession.cpp +++ b/protocols/CurrencyRates/src/HTTPSession.cpp @@ -9,15 +9,6 @@ void CALLBACK waitStub() show_popup(g_pCurrentProvider, 0, -1, TranslateW(ERROR_MSG)); } -static int find_header(const NETLIBHTTPREQUEST* pRequest, const char* hdr) -{ - for (int i = 0; i < pRequest->headersCount; ++i) - if (0 == _stricmp(pRequest->headers[i].szName, hdr)) - return i; - - return -1; -} - bool CHTTPSession::OpenURL(const CMStringW &rsURL) { m_szUrl = rsURL; @@ -29,20 +20,14 @@ bool CHTTPSession::ReadResponce(CMStringW &rsResponce) if (m_szUrl.IsEmpty()) return false; - NETLIBHTTPHEADER headers[] = - { - { "User-Agent", NETLIB_USER_AGENT }, - { "Connection", "close" }, - { "Cache-Control", "no-cache" }, - { "Pragma", "no-cache" } - }; - - NETLIBHTTPREQUEST nlhr = {}; + MHttpRequest nlhr; nlhr.requestType = REQUEST_GET; nlhr.flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11 | NLHRF_REDIRECT; - nlhr.szUrl = m_szUrl.GetBuffer(); - nlhr.headersCount = _countof(headers); - nlhr.headers = headers; + nlhr.m_szUrl = m_szUrl; + nlhr.AddHeader("User-Agent", NETLIB_USER_AGENT); + nlhr.AddHeader("Connection", "close"); + nlhr.AddHeader("Cache-Control", "no-cache"); + nlhr.AddHeader("Pragma", "no-cache"); bool bResult = false; NLHR_PTR pReply(0); @@ -52,13 +37,12 @@ bool CHTTPSession::ReadResponce(CMStringW &rsResponce) } if (pReply) { - if ((200 == pReply->resultCode) && (pReply->dataLength > 0)) { - CMStringA buf(pReply->pData, pReply->dataLength); - int nIndex = find_header(pReply, "Content-Type"); - if ((-1 != nIndex) && (nullptr != strstr(_strlwr(pReply->headers[nIndex].szValue), "utf-8"))) - rsResponce = ptrW(mir_utf8decodeW(buf)); + if ((200 == pReply->resultCode) && !pReply->body.IsEmpty()) { + auto *pEncoding = pReply->FindHeader("Content-Type"); + if (pEncoding && strstr(pEncoding, "utf-8")) + rsResponce = ptrW(mir_utf8decodeW(pReply->body)); else - rsResponce = _A2T(buf); + rsResponce = _A2T(pReply->body); bResult = true; } diff --git a/protocols/Discord/src/avatars.cpp b/protocols/Discord/src/avatars.cpp index a131b23856..fd2d57181c 100644 --- a/protocols/Discord/src/avatars.cpp +++ b/protocols/Discord/src/avatars.cpp @@ -52,7 +52,7 @@ INT_PTR CDiscordProto::GetAvatarCaps(WPARAM wParam, LPARAM lParam) ///////////////////////////////////////////////////////////////////////////////////////// -void CDiscordProto::OnReceiveAvatar(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CDiscordProto::OnReceiveAvatar(MHttpResponse *reply, AsyncHttpRequest *pReq) { PROTO_AVATAR_INFORMATION ai = { 0 }; ai.format = PA_FORMAT_UNKNOWN; @@ -64,7 +64,7 @@ LBL_Error: return; } - if (auto *pszHdr = Netlib_GetHeader(reply, "Content-Type")) + if (auto *pszHdr = reply->FindHeader("Content-Type")) ai.format = ProtoGetAvatarFormatByMimeType(pszHdr); if (ai.format == PA_FORMAT_UNKNOWN) { @@ -81,7 +81,7 @@ LBL_Error: goto LBL_Error; } - fwrite(reply->pData, 1, reply->dataLength, out); + fwrite(reply->body, 1, reply->body.GetLength(), out); fclose(out); if (ai.hContact) diff --git a/protocols/Discord/src/connection.cpp b/protocols/Discord/src/connection.cpp index d98d6e4ec8..00c9214b2c 100644 --- a/protocols/Discord/src/connection.cpp +++ b/protocols/Discord/src/connection.cpp @@ -21,15 +21,10 @@ void CDiscordProto::ExecuteRequest(AsyncHttpRequest *pReq) { CMStringA str; - 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->pData = mir_strdup(pReq->m_szParam); - pReq->dataLength = pReq->m_szParam.GetLength(); + pReq->m_szUrl = str.GetBuffer(); } } @@ -40,7 +35,7 @@ void CDiscordProto::ExecuteRequest(AsyncHttpRequest *pReq) } bool bRetryable = pReq->nlc != nullptr; - debugLogA("Executing request #%d:\n%s", pReq->m_iReqNum, pReq->szUrl); + debugLogA("Executing request #%d:\n%s", pReq->m_iReqNum, pReq->m_szUrl.c_str()); LBL_Retry: NLHR_PTR reply(Netlib_HttpTransaction(m_hNetlibUser, pReq)); diff --git a/protocols/Discord/src/gateway.cpp b/protocols/Discord/src/gateway.cpp index e76de2848a..759afed573 100644 --- a/protocols/Discord/src/gateway.cpp +++ b/protocols/Discord/src/gateway.cpp @@ -43,19 +43,15 @@ void CDiscordProto::GatewayThread(void*) bool CDiscordProto::GatewayThreadWorker() { - NETLIBHTTPHEADER hdrs[] = - { - { "Origin", "https://discord.com" }, - { 0, 0 }, - { 0, 0 }, - }; - + bool bHasCookie = false; + MHttpHeaders hdrs; + hdrs.AddHeader("Origin", "https://discord.com"); if (!m_szWSCookie.IsEmpty()) { - hdrs[1].szName = "Cookie"; - hdrs[1].szValue = m_szWSCookie.GetBuffer(); + bHasCookie = true; + hdrs.AddHeader("Cookie", m_szWSCookie); } - NLHR_PTR pReply(WebSocket_Connect(m_hGatewayNetlibUser, m_szGateway + "/?encoding=json&v=8", hdrs)); + NLHR_PTR pReply(WebSocket_Connect(m_hGatewayNetlibUser, m_szGateway + "/?encoding=json&v=8", &hdrs)); if (pReply == nullptr) { debugLogA("Gateway connection failed, exiting"); return false; @@ -66,7 +62,7 @@ bool CDiscordProto::GatewayThreadWorker() if (pReply->resultCode != 101) { // if there's no cookie & Miranda is bounced with error 404, simply apply the cookie and try again if (pReply->resultCode == 404) { - if (hdrs[1].szName == nullptr) + if (!bHasCookie) return true; m_szWSCookie.Empty(); // don't use the same cookie twice diff --git a/protocols/Discord/src/http.cpp b/protocols/Discord/src/http.cpp index f65451f9ce..82e8d495d0 100644 --- a/protocols/Discord/src/http.cpp +++ b/protocols/Discord/src/http.cpp @@ -59,8 +59,7 @@ AsyncHttpRequest::AsyncHttpRequest(CDiscordProto *ppro, int iRequestType, LPCSTR if (pRoot != nullptr) { ptrW text(json_write(pRoot)); - pData = mir_utf8encodeW(text); - dataLength = (int)mir_strlen(pData); + m_szParam = ptrA(mir_utf8encodeW(text)); AddHeader("Content-Type", "application/json"); } @@ -71,7 +70,7 @@ AsyncHttpRequest::AsyncHttpRequest(CDiscordProto *ppro, int iRequestType, LPCSTR m_iReqNum = ::InterlockedIncrement(&g_reqNum); } -JsonReply::JsonReply(NETLIBHTTPREQUEST *pReply) +JsonReply::JsonReply(MHttpResponse *pReply) { if (pReply == nullptr) { m_errorCode = 500; @@ -80,7 +79,7 @@ JsonReply::JsonReply(NETLIBHTTPREQUEST *pReply) m_errorCode = pReply->resultCode; - m_root = json_parse(pReply->pData); + m_root = json_parse(pReply->body); if (m_root == nullptr) m_errorCode = 500; } diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index 1e76ead10d..210d649d00 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -329,7 +329,7 @@ HANDLE CDiscordProto::SearchAdvanced(HWND hwndDlg) ///////////////////////////////////////////////////////////////////////////////////////// // Basic search - by SnowFlake -void CDiscordProto::OnReceiveUserinfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +void CDiscordProto::OnReceiveUserinfo(MHttpResponse *pReply, AsyncHttpRequest*) { JsonReply root(pReply); if (!root) { @@ -459,7 +459,7 @@ MCONTACT CDiscordProto::AddToListByEvent(int flags, int, MEVENT hDbEvent) //////////////////////////////////////////////////////////////////////////////////////// // SendMsg -void CDiscordProto::OnSendMsg(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CDiscordProto::OnSendMsg(MHttpResponse *pReply, AsyncHttpRequest *pReq) { JsonReply root(pReply); if (!root) { @@ -574,7 +574,7 @@ int CDiscordProto::UserIsTyping(MCONTACT hContact, int type) ///////////////////////////////////////////////////////////////////////////////////////// -void CDiscordProto::OnReceiveMarkRead(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *) +void CDiscordProto::OnReceiveMarkRead(MHttpResponse *pReply, AsyncHttpRequest *) { JsonReply root(pReply); if (root) @@ -712,10 +712,10 @@ void CDiscordProto::SendFileThread(void *param) szBoundary.Insert(0, "\r\n"); szBoundary.Append("--\r\n"); - pReq->dataLength = int(szBody.GetLength() + szBoundary.GetLength() + cbBytes); - pReq->pData = (char*)mir_alloc(pReq->dataLength+1); - memcpy(pReq->pData, szBody.c_str(), szBody.GetLength()); - size_t cbRead = fread(pReq->pData + szBody.GetLength(), 1, cbBytes, in); + pReq->m_szParam.Truncate(int(szBody.GetLength() + szBoundary.GetLength() + cbBytes)); + + memcpy(pReq->m_szParam.GetBuffer(), szBody.c_str(), szBody.GetLength()); + size_t cbRead = fread(pReq->m_szParam.GetBuffer() + szBody.GetLength(), 1, cbBytes, in); fclose(in); if (cbBytes != cbRead) { debugLogA("cannot read file %S: %d bytes read instead of %d", p->wszFileName.c_str(), cbRead, cbBytes); @@ -723,14 +723,14 @@ void CDiscordProto::SendFileThread(void *param) goto LBL_Error; } - memcpy(pReq->pData + szBody.GetLength() + cbBytes, szBoundary, szBoundary.GetLength()); + memcpy(pReq->m_szParam.GetBuffer() + szBody.GetLength() + cbBytes, szBoundary, szBoundary.GetLength()); pReq->pUserInfo = p; Push(pReq); ProtoBroadcastAck(p->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, param); } -void CDiscordProto::OnReceiveFile(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CDiscordProto::OnReceiveFile(MHttpResponse *pReply, AsyncHttpRequest *pReq) { SendFileThreadParam *p = (SendFileThreadParam*)pReq->pUserInfo; if (pReply->resultCode != 200) { diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index e34f6cbfa9..fdb5e6a33b 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -80,7 +80,7 @@ class JsonReply int m_errorCode = 0; public: - JsonReply(NETLIBHTTPREQUEST *); + JsonReply(MHttpResponse *); ~JsonReply(); __forceinline int error() const { return m_errorCode; } @@ -495,25 +495,25 @@ public: void OnLoggedIn(); void OnLoggedOut(); - void OnReceiveCreateChannel(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveFile(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveGateway(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveLogout(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveMarkRead(NETLIBHTTPREQUEST*, AsyncHttpRequest *); - void OnReceiveMessageAck(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveToken(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveUserinfo(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveCreateChannel(MHttpResponse*, AsyncHttpRequest*); + void OnReceiveFile(MHttpResponse*, AsyncHttpRequest*); + void OnReceiveGateway(MHttpResponse*, AsyncHttpRequest*); + void OnReceiveLogout(MHttpResponse*, AsyncHttpRequest*); + void OnReceiveMarkRead(MHttpResponse*, AsyncHttpRequest *); + void OnReceiveMessageAck(MHttpResponse*, AsyncHttpRequest*); + void OnReceiveToken(MHttpResponse*, AsyncHttpRequest*); + void OnReceiveUserinfo(MHttpResponse*, AsyncHttpRequest*); void RetrieveMyInfo(); - void OnReceiveMyInfo(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveMyInfo(MHttpResponse*, AsyncHttpRequest*); void RetrieveHistory(CDiscordUser *pUser, CDiscordHistoryOp iOp = MSG_NOFILTER, SnowFlake msgid = 0, int iLimit = 50); - void OnReceiveHistory(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveHistory(MHttpResponse*, AsyncHttpRequest*); bool RetrieveAvatar(MCONTACT hContact); - void OnReceiveAvatar(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveAvatar(MHttpResponse*, AsyncHttpRequest*); - void OnSendMsg(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnSendMsg(MHttpResponse*, AsyncHttpRequest*); ////////////////////////////////////////////////////////////////////////////////////// // Misc diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp index 1ef3517822..104d45435d 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -54,7 +54,7 @@ static int compareMsgHistory(const JSONNode *p1, const JSONNode *p2) return wcscmp((*p1)["id"].as_mstring(), (*p2)["id"].as_mstring()); } -void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CDiscordProto::OnReceiveHistory(MHttpResponse *pReply, AsyncHttpRequest *pReq) { CDiscordUser *pUser = (CDiscordUser*)pReq->pUserInfo; @@ -147,7 +147,7 @@ void CDiscordProto::RetrieveMyInfo() Push(new AsyncHttpRequest(this, REQUEST_GET, "/users/@me", &CDiscordProto::OnReceiveMyInfo)); } -void CDiscordProto::OnReceiveMyInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +void CDiscordProto::OnReceiveMyInfo(MHttpResponse *pReply, AsyncHttpRequest*) { JsonReply root(pReply); if (!root) { @@ -179,7 +179,7 @@ void CDiscordProto::OnReceiveMyInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest* ///////////////////////////////////////////////////////////////////////////////////////// // finds a gateway address -void CDiscordProto::OnReceiveGateway(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +void CDiscordProto::OnReceiveGateway(MHttpResponse *pReply, AsyncHttpRequest*) { JsonReply root(pReply); if (!root) { @@ -195,7 +195,7 @@ void CDiscordProto::OnReceiveGateway(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest ///////////////////////////////////////////////////////////////////////////////////////// // logs a session out -void CDiscordProto::OnReceiveLogout(NETLIBHTTPREQUEST *, AsyncHttpRequest *) +void CDiscordProto::OnReceiveLogout(MHttpResponse *, AsyncHttpRequest *) { delSetting("AccessToken"); } @@ -213,7 +213,7 @@ void CDiscordProto::SetServerStatus(int iStatus) ///////////////////////////////////////////////////////////////////////////////////////// // channels -void CDiscordProto::OnReceiveCreateChannel(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +void CDiscordProto::OnReceiveCreateChannel(MHttpResponse *pReply, AsyncHttpRequest*) { JsonReply root(pReply); if (root) @@ -222,7 +222,7 @@ void CDiscordProto::OnReceiveCreateChannel(NETLIBHTTPREQUEST *pReply, AsyncHttpR ///////////////////////////////////////////////////////////////////////////////////////// -void CDiscordProto::OnReceiveMessageAck(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +void CDiscordProto::OnReceiveMessageAck(MHttpResponse *pReply, AsyncHttpRequest*) { JsonReply root(pReply); if (!root) @@ -243,10 +243,10 @@ void CDiscordProto::OnReceiveMessageAck(NETLIBHTTPREQUEST *pReply, AsyncHttpRequ #define RECAPTCHA_API_KEY "6Lef5iQTAAAAAKeIvIY-DeexoO3gj7ryl9rLMEnn" #define RECAPTCHA_SITE_URL "https://discord.com" -void CDiscordProto::OnReceiveToken(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +void CDiscordProto::OnReceiveToken(MHttpResponse *pReply, AsyncHttpRequest*) { if (pReply->resultCode != 200) { - JSONNode root = JSONNode::parse(pReply->pData); + JSONNode root = JSONNode::parse(pReply->body); if (root) { const JSONNode &captcha = root["captcha_key"].as_array(); if (captcha) { 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); } } diff --git a/protocols/Gadu-Gadu/src/avatar.cpp b/protocols/Gadu-Gadu/src/avatar.cpp index 3ab82cd50f..c9887a1da4 100644 --- a/protocols/Gadu-Gadu/src/avatar.cpp +++ b/protocols/Gadu-Gadu/src/avatar.cpp @@ -51,9 +51,9 @@ bool GaduProto::getAvatarFileInfo(uin_t uin, char **avatarurl, char **avatarts) char szUrl[128]; mir_snprintf(szUrl, "http://api.gadu-gadu.pl/avatars/%d/0.xml", uin); - NETLIBHTTPREQUEST req = { sizeof(req) }; + MHttpRequest req; req.requestType = REQUEST_GET; - req.szUrl = szUrl; + req.m_szUrl = szUrl; req.flags = NLHRF_NODUMP | NLHRF_HTTP11 | NLHRF_REDIRECT; NLHR_PTR resp(Netlib_HttpTransaction(m_hNetlibUser, &req)); @@ -62,15 +62,15 @@ bool GaduProto::getAvatarFileInfo(uin_t uin, char **avatarurl, char **avatarts) return false; } - if (resp->resultCode != 200 || !resp->dataLength || !resp->pData) { + if (resp->resultCode != 200 || resp->body.IsEmpty()) { debugLogA("getAvatarFileInfo(): Invalid response code from HTTP request"); return false; } - if ((strncmp(resp->pData, "", 8) == 0) || (strncmp(resp->pData, "body, "", 8) == 0) || (strncmp(resp->body, "pData) == 0) { + if (doc.Parse(resp->body) == 0) { auto *node = TiXmlConst(doc.FirstChildElement("result"))["users"]["user"]["avatars"]["avatar"].ToElement(); const char *blank = (node != nullptr) ? node->Attribute("blank") : nullptr; if (mir_strcmp(blank, "1")) { @@ -84,9 +84,9 @@ bool GaduProto::getAvatarFileInfo(uin_t uin, char **avatarurl, char **avatarts) } } } - else if (strncmp(resp->pData, "{\"result\":", 10) == 0) { + else if (strncmp(resp->body, "{\"result\":", 10) == 0) { // if this url returns json data (11.2013 gg convention) - JSONNode root = JSONNode::parse(resp->pData); + JSONNode root = JSONNode::parse(resp->body); if (root) { const JSONNode &respJSONavatars = root["result"].at("users").at("user").at("avatars"); if (respJSONavatars) { @@ -225,26 +225,26 @@ void __cdecl GaduProto::avatarrequestthread(void*) ai.hContact = data->hContact; ai.format = getByte(ai.hContact, GG_KEY_AVATARTYPE, GG_KEYDEF_AVATARTYPE); - NETLIBHTTPREQUEST req = { sizeof(req) }; + MHttpRequest req; req.requestType = REQUEST_GET; - req.szUrl = data->szAvatarURL; + req.m_szUrl = data->szAvatarURL; req.flags = NLHRF_NODUMP | NLHRF_HTTP11 | NLHRF_REDIRECT; NLHR_PTR resp(Netlib_HttpTransaction(m_hNetlibUser, &req)); if (resp) { - if (resp->resultCode == 200 && resp->dataLength > 0 && resp->pData) { + if (resp->resultCode == 200 && !resp->body.IsEmpty()) { int file_fd; int avatarType = PA_FORMAT_UNKNOWN; - if (strncmp(resp->pData, "\xFF\xD8", 2) == 0) avatarType = PA_FORMAT_JPEG; - if (strncmp(resp->pData, "\x47\x49\x46\x38", 4) == 0) avatarType = PA_FORMAT_GIF; - if (strncmp(resp->pData, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8) == 0) avatarType = PA_FORMAT_PNG; + if (strncmp(resp->body, "\xFF\xD8", 2) == 0) avatarType = PA_FORMAT_JPEG; + if (strncmp(resp->body, "\x47\x49\x46\x38", 4) == 0) avatarType = PA_FORMAT_GIF; + if (strncmp(resp->body, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8) == 0) avatarType = PA_FORMAT_PNG; setByte(data->hContact, GG_KEY_AVATARTYPE, (uint8_t)avatarType); getAvatarFilename(ai.hContact, ai.filename, _countof(ai.filename)); file_fd = _wopen(ai.filename, _O_WRONLY | _O_TRUNC | _O_BINARY | _O_CREAT, _S_IREAD | _S_IWRITE); if (file_fd != -1) { - _write(file_fd, resp->pData, resp->dataLength); + _write(file_fd, resp->body, resp->body.GetLength()); _close(file_fd); result = 1; debugLogW(L"avatarrequestthread() new avatar_transfers item. Saved data to file=%s.", ai.filename); @@ -367,49 +367,26 @@ void __cdecl GaduProto::setavatarthread(void *param) CMStringA avatarFileB64Enc(mir_urlEncode(avatarFileB64)); - char dataPrefix[64]; - mir_snprintf(dataPrefix, "uin=%d&photo=", getDword(GG_KEY_UIN, 0)); - size_t dataPrefixLen = mir_strlen(dataPrefix); - - size_t dataLen = dataPrefixLen + avatarFileB64Enc.GetLength(); - char* data = (char*)mir_alloc(dataLen); - memcpy(data, dataPrefix, dataPrefixLen); - memcpy(data + dataPrefixLen, avatarFileB64Enc, avatarFileB64Enc.GetLength()); - - //check if we have token, if no - receive new one + // check if we have token, if no - receive new one oauth_checktoken(0); char* token = getStringA(GG_KEY_TOKEN); - //construct request - NETLIBHTTPREQUEST req = {}; + // construct request + MHttpRequest req = {}; req.requestType = REQUEST_POST; - req.szUrl = "http://avatars.nowe.gg/upload"; + req.m_szUrl = "http://avatars.nowe.gg/upload"; req.flags = NLHRF_NODUMP | NLHRF_HTTP11; - req.headersCount = 10; - NETLIBHTTPHEADER httpHeaders[10]; - httpHeaders[0].szName = "X-Request"; - httpHeaders[0].szValue = "JSON"; - httpHeaders[1].szName = "Authorization"; - httpHeaders[1].szValue = token; - httpHeaders[2].szName = "X-Requested-With"; - httpHeaders[2].szValue = "XMLHttpRequest"; - httpHeaders[3].szName = "From"; - httpHeaders[3].szValue = "avatars to avatars"; - httpHeaders[4].szName = "X-IM-Web-App-Version"; - httpHeaders[4].szValue = "10,5,2,13164"; - httpHeaders[5].szName = "User-Agent"; - httpHeaders[5].szValue = "avatars to avatars"; - httpHeaders[6].szName = "From"; - httpHeaders[6].szValue = NETLIB_USER_AGENT; - httpHeaders[7].szName = "Content-type"; - httpHeaders[7].szValue = "application/x-www-form-urlencoded; charset=utf-8"; - httpHeaders[8].szName = "Accept"; - httpHeaders[8].szValue = "application/json"; - httpHeaders[9].szName = "Referer"; - httpHeaders[9].szValue = "http://avatars.nowe.gg/.static/index_new_22.0.2_595nwh.html"; - req.headers = httpHeaders; - req.pData = data; - req.dataLength = int(dataLen); + req.AddHeader("X-Request", "JSON"); + req.AddHeader("Authorization", token); + req.AddHeader("X-Requested-With", "XMLHttpRequest"); + req.AddHeader("From", "avatars to avatars"); + req.AddHeader("X-IM-Web-App-Version", "10,5,2,13164"); + req.AddHeader("User-Agent", "avatars to avatars"); + req.AddHeader("From", NETLIB_USER_AGENT); + req.AddHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8"); + req.AddHeader("Accept", "application/json"); + req.AddHeader("Referer", "http://avatars.nowe.gg/.static/index_new_22.0.2_595nwh.html"); + req << INT_PARAM("uin", getDword(GG_KEY_UIN, 0)) << CHAR_PARAM("photo", avatarFileB64Enc); // send request int resultSuccess = 0; @@ -417,8 +394,8 @@ void __cdecl GaduProto::setavatarthread(void *param) { NLHR_PTR resp(Netlib_HttpTransaction(m_hNetlibUser, &req)); if (resp) { - if (resp->resultCode == 200 && resp->dataLength > 0 && resp->pData) { - debugLogA("setavatarthread(): 1 resp.data= %s", resp->pData); + if (resp->resultCode == 200 && !resp->body.IsEmpty()) { + debugLogA("setavatarthread(): 1 resp.data= %s", resp->body.c_str()); resultSuccess = 1; } else { @@ -439,22 +416,14 @@ void __cdecl GaduProto::setavatarthread(void *param) oauth_checktoken(1); mir_free(token); token = getStringA(GG_KEY_TOKEN); - httpHeaders[1].szValue = token; - - //construct 2nd request - memset(&req, 0, sizeof(req)); - req.requestType = REQUEST_POST; - req.szUrl = "http://avatars.nowe.gg/upload"; - req.flags = NLHRF_NODUMP | NLHRF_HTTP11; - req.headersCount = 10; - req.headers = httpHeaders; - req.pData = data; - req.dataLength = int(dataLen); + + // construct 2nd request + req.AddHeader("Authorization", token); NLHR_PTR resp(Netlib_HttpTransaction(m_hNetlibUser, &req)); if (resp) { - if (resp->resultCode == 200 && resp->dataLength > 0 && resp->pData) { - debugLogA("setavatarthread(): 2 resp.data= %s", resp->pData); + if (resp->resultCode == 200 && !resp->body.IsEmpty()) { + debugLogA("setavatarthread(): 2 resp.data= %s", resp->body.c_str()); resultSuccess = 1; } else debugLogA("setavatarthread(): Invalid response code from HTTP request [%d]", resp->resultCode); @@ -464,7 +433,6 @@ void __cdecl GaduProto::setavatarthread(void *param) //clean and end thread mir_free(token); - mir_free(data); if (resultSuccess) { debugLogA("setavatarthread(): User avatar set successfully."); diff --git a/protocols/Gadu-Gadu/src/oauth.cpp b/protocols/Gadu-Gadu/src/oauth.cpp index 932c9c1553..084b4df1bf 100644 --- a/protocols/Gadu-Gadu/src/oauth.cpp +++ b/protocols/Gadu-Gadu/src/oauth.cpp @@ -271,7 +271,7 @@ char *oauth_auth_header(const char *httpmethod, const char *url, OAUTHSIGNMETHOD int GaduProto::oauth_receivetoken() { - char szUrl[256], uin[32], *token = nullptr, *token_secret = nullptr; + char uin[32], *token = nullptr, *token_secret = nullptr; int res = 0; HNETLIBCONN nlc = nullptr; @@ -280,109 +280,90 @@ int GaduProto::oauth_receivetoken() // 1. Obtaining an Unauthorized Request Token debugLogA("oauth_receivetoken(): Obtaining an Unauthorized Request Token..."); - mir_strcpy(szUrl, "http://api.gadu-gadu.pl/request_token"); - char *str = oauth_auth_header("POST", szUrl, HMACSHA1, uin, password, nullptr, nullptr); - - NETLIBHTTPHEADER httpHeaders[3]; - httpHeaders[0].szName = "User-Agent"; - httpHeaders[0].szValue = GG8_VERSION; - httpHeaders[1].szName = "Authorization"; - httpHeaders[1].szValue = str; - httpHeaders[2].szName = "Accept"; - httpHeaders[2].szValue = "*/*"; - - NETLIBHTTPREQUEST req = { sizeof(req) }; - req.requestType = REQUEST_POST; - req.szUrl = szUrl; - req.flags = NLHRF_NODUMP | NLHRF_HTTP11 | NLHRF_PERSISTENT; - req.headersCount = 3; - req.headers = httpHeaders; - - NLHR_PTR resp(Netlib_HttpTransaction(m_hNetlibUser, &req)); - if (resp) { - nlc = resp->nlc; - if (resp->resultCode == 200 && resp->dataLength > 0 && resp->pData) { - TiXmlDocument doc; - if (0 == doc.Parse(resp->pData)) { - TiXmlConst hXml(doc.FirstChildElement("result")); - if (auto *p = hXml["oauth_token"].ToElement()) - token = mir_strdup(p->GetText()); - - if (auto *p = hXml["oauth_token_secret"].ToElement()) - token_secret = mir_strdup(p->GetText()); + + NLHR_PTR resp(0); + { + MHttpRequest req; + req.requestType = REQUEST_POST; + req.m_szUrl = "http://api.gadu-gadu.pl/request_token"; + req.flags = NLHRF_NODUMP | NLHRF_HTTP11 | NLHRF_PERSISTENT; + req.AddHeader("User-Agent", GG8_VERSION); + req.AddHeader("Authorization", ptrA(oauth_auth_header("POST", req.m_szUrl, HMACSHA1, uin, password, nullptr, nullptr))); + req.AddHeader("Accept", "*/*"); + + resp = Netlib_HttpTransaction(m_hNetlibUser, &req); + if (resp) { + nlc = resp->nlc; + if (resp->resultCode == 200 && !resp->body.IsEmpty()) { + TiXmlDocument doc; + if (0 == doc.Parse(resp->body)) { + TiXmlConst hXml(doc.FirstChildElement("result")); + if (auto *p = hXml["oauth_token"].ToElement()) + token = mir_strdup(p->GetText()); + + if (auto *p = hXml["oauth_token_secret"].ToElement()) + token_secret = mir_strdup(p->GetText()); + } } + else debugLogA("oauth_receivetoken(): Invalid response code from HTTP request"); } - else debugLogA("oauth_receivetoken(): Invalid response code from HTTP request"); + else debugLogA("oauth_receivetoken(): No response from HTTP request"); } - else debugLogA("oauth_receivetoken(): No response from HTTP request"); // 2. Obtaining User Authorization debugLogA("oauth_receivetoken(): Obtaining User Authorization..."); - mir_free(str); - str = oauth_uri_escape("http://www.mojageneracja.pl"); - - mir_snprintf(szUrl, "callback_url=%s&request_token=%s&uin=%s&password=%s", - str, token, uin, password); - mir_free(str); - str = mir_strdup(szUrl); - - memset(&req, 0, sizeof(req)); - req.requestType = REQUEST_POST; - req.szUrl = szUrl; - req.flags = NLHRF_NODUMP | NLHRF_HTTP11; - req.headersCount = 3; - req.headers = httpHeaders; - mir_strcpy(szUrl, "https://login.gadu-gadu.pl/authorize"); - httpHeaders[1].szName = "Content-Type"; - httpHeaders[1].szValue = "application/x-www-form-urlencoded"; - req.pData = str; - req.dataLength = (int)mir_strlen(str); - - resp = Netlib_HttpTransaction(m_hNetlibUser, &req); - if (!resp) - debugLogA("oauth_receivetoken(): No response from HTTP request"); + { + MHttpRequest req; + req.requestType = REQUEST_POST; + req.m_szUrl = "https://login.gadu-gadu.pl/authorize"; + req.flags = NLHRF_NODUMP | NLHRF_HTTP11; + req.m_szParam.Format("callback_url=%s&request_token=%s&uin=%s&password=%s", ptrA(oauth_uri_escape("http://www.mojageneracja.pl")), token, uin, password); + req.AddHeader("User-Agent", GG8_VERSION); + req.AddHeader("Content-Type", "application/x-www-form-urlencoded"); + req.AddHeader("Accept", "*/*"); + + resp = Netlib_HttpTransaction(m_hNetlibUser, &req); + if (!resp) + debugLogA("oauth_receivetoken(): No response from HTTP request"); + } // 3. Obtaining an Access Token debugLogA("oauth_receivetoken(): Obtaining an Access Token..."); - mir_strcpy(szUrl, "http://api.gadu-gadu.pl/access_token"); - mir_free(str); - str = oauth_auth_header("POST", szUrl, HMACSHA1, uin, password, token, token_secret); + mir_free(token); mir_free(token_secret); token = nullptr; token_secret = nullptr; - - memset(&req, 0, sizeof(req)); - req.requestType = REQUEST_POST; - req.szUrl = szUrl; - req.flags = NLHRF_NODUMP | NLHRF_HTTP11 | NLHRF_PERSISTENT; - req.nlc = nlc; - req.headersCount = 3; - req.headers = httpHeaders; - httpHeaders[1].szName = "Authorization"; - httpHeaders[1].szValue = str; - - resp = Netlib_HttpTransaction(m_hNetlibUser, &req); - if (resp) { - if (resp->resultCode == 200 && resp->dataLength > 0 && resp->pData) { - TiXmlDocument doc; - if (0 == doc.Parse(resp->pData)) { - TiXmlConst hXml(doc.FirstChildElement("result")); - if (auto *p = hXml["oauth_token"].ToElement()) - token = mir_strdup(p->GetText()); - - if (auto *p = hXml["oauth_token_secret"].ToElement()) - token_secret = mir_strdup(p->GetText()); + { + MHttpRequest req; + req.requestType = REQUEST_POST; + req.m_szUrl = "http://api.gadu-gadu.pl/access_token"; + req.flags = NLHRF_NODUMP | NLHRF_HTTP11 | NLHRF_PERSISTENT; + req.nlc = nlc; + req.AddHeader("User-Agent", GG8_VERSION); + req.AddHeader("Authorization", ptrA(oauth_auth_header("POST", req.m_szUrl, HMACSHA1, uin, password, token, token_secret))); + req.AddHeader("Accept", "*/*"); + + resp = Netlib_HttpTransaction(m_hNetlibUser, &req); + if (resp) { + if (resp->resultCode == 200 && !resp->body.IsEmpty()) { + TiXmlDocument doc; + if (0 == doc.Parse(resp->body)) { + TiXmlConst hXml(doc.FirstChildElement("result")); + if (auto *p = hXml["oauth_token"].ToElement()) + token = mir_strdup(p->GetText()); + + if (auto *p = hXml["oauth_token_secret"].ToElement()) + token_secret = mir_strdup(p->GetText()); + } } - } - else debugLogA("oauth_receivetoken(): Invalid response code from HTTP request"); + else debugLogA("oauth_receivetoken(): Invalid response code from HTTP request"); - Netlib_CloseHandle(resp->nlc); + Netlib_CloseHandle(resp->nlc); + } + else debugLogA("oauth_receivetoken(): No response from HTTP request"); } - else debugLogA("oauth_receivetoken(): No response from HTTP request"); - mir_free(password); - mir_free(str); if (token != nullptr && token_secret != nullptr) { setString(GG_KEY_TOKEN, token); diff --git a/protocols/GmailNotifier/src/check.cpp b/protocols/GmailNotifier/src/check.cpp index b5fa2d2dee..df86f3fa0d 100644 --- a/protocols/GmailNotifier/src/check.cpp +++ b/protocols/GmailNotifier/src/check.cpp @@ -67,19 +67,13 @@ void CheckMailInbox(Account *curAcc) szBody.Append("&password="); szBody.Append(curAcc->pass); - NETLIBHTTPHEADER headers[1] = { - { "Content-Type", "application/x-www-form-urlencoded" } - }; - - NETLIBHTTPREQUEST nlr = {}; - nlr.szUrl = szUrl.GetBuffer(); - nlr.requestType = REQUEST_POST; - nlr.headersCount = _countof(headers); - nlr.headers = headers; - nlr.dataLength = szBody.GetLength(); - nlr.pData = szBody.GetBuffer(); - - NLHR_PTR nlu(Netlib_HttpTransaction(hNetlibUser, &nlr)); + MHttpRequest nlhr; + nlhr.m_szUrl = szUrl.GetBuffer(); + nlhr.m_szParam = szBody; + nlhr.requestType = REQUEST_POST; + nlhr.AddHeader("Content-Type", "application/x-www-form-urlencoded"); + + NLHR_PTR nlu(Netlib_HttpTransaction(hNetlibUser, &nlhr)); if (nlu == nullptr || nlu->resultCode != 200) { mir_strcpy(curAcc->results.content, Translate("Can't send account data!")); @@ -99,25 +93,18 @@ void CheckMailInbox(Account *curAcc) else szUrl.Append("/mail/feed/atom"); - NETLIBHTTPHEADER headers[1] = { - { "Authorization", szAuth.GetBuffer() } - }; - - NETLIBHTTPREQUEST nlr = {}; - nlr.szUrl = szUrl.GetBuffer(); - nlr.requestType = REQUEST_GET; - nlr.headers = headers; - nlr.headersCount = _countof(headers); + MHttpRequest nlhr; + nlhr.m_szUrl = szUrl.GetBuffer(); + nlhr.requestType = REQUEST_GET; + nlhr.AddHeader("Authorization", szAuth.GetBuffer()); - NLHR_PTR nlu(Netlib_HttpTransaction(hNetlibUser, &nlr)); + NLHR_PTR nlu(Netlib_HttpTransaction(hNetlibUser, &nlhr)); if (nlu == nullptr) { - mir_snprintf(curAcc->results.content, "%s [%s]", szNick.get(), - (nlr.resultCode == 401) ? Translate("Wrong name or password!") : Translate("Can't get RSS feed!")); - + mir_snprintf(curAcc->results.content, "%s [%s]", szNick.get(), Translate("Wrong name or password!")); curAcc->results_num = -1; } else { - curAcc->results_num = ParsePage(nlu->pData, &curAcc->results); + curAcc->results_num = ParsePage(nlu->body.GetBuffer(), &curAcc->results); mir_snprintf(curAcc->results.content, "%s [%d]", szNick.get(), curAcc->results_num); } diff --git a/protocols/ICQ-WIM/src/avatars.cpp b/protocols/ICQ-WIM/src/avatars.cpp index 452a9da444..73766e7ec8 100644 --- a/protocols/ICQ-WIM/src/avatars.cpp +++ b/protocols/ICQ-WIM/src/avatars.cpp @@ -106,23 +106,13 @@ INT_PTR __cdecl CIcqProto::SetAvatar(WPARAM, LPARAM lParam) } unsigned dwSize = (unsigned)_filelengthi64(fileId); - char *pData = (char *)mir_alloc(dwSize); - if (pData == nullptr) { - _close(fileId); - delete pReq; - return 2; - } - - _read(fileId, pData, dwSize); + pReq->m_szParam.Truncate(dwSize); + _read(fileId, pReq->m_szParam.GetBuffer(), dwSize); _close(fileId); - pReq->pData = pData; - pReq->dataLength = dwSize; - - int iAvatarType = ProtoGetBufferFormat(pData); + int iAvatarType = ProtoGetBufferFormat(pReq->m_szParam); if (iAvatarType == PA_FORMAT_UNKNOWN) { delete pReq; - delete pData; return 3; } diff --git a/protocols/ICQ-WIM/src/file.cpp b/protocols/ICQ-WIM/src/file.cpp index 1dc58ba06b..eb20c4c18b 100644 --- a/protocols/ICQ-WIM/src/file.cpp +++ b/protocols/ICQ-WIM/src/file.cpp @@ -51,10 +51,9 @@ void IcqFileTransfer::FillHeaders(AsyncHttpRequest *pReq) pReq->AddHeader("Content-Range", CMStringA(FORMAT, "bytes %lld-%lld/%lld", pfts.currentFileProgress, pfts.currentFileProgress + dwPortion - 1, pfts.currentFileSize)); pReq->AddHeader("Content-Length", CMStringA(FORMAT, "%d", dwPortion)); - pReq->dataLength = dwPortion; - pReq->pData = (char *)mir_alloc(dwPortion); + pReq->m_szParam.Truncate(dwPortion); _lseek(m_fileId, pfts.currentFileProgress, SEEK_SET); - _read(m_fileId, pReq->pData, dwPortion); + _read(m_fileId, pReq->m_szParam.GetBuffer(), dwPortion); pfts.currentFileProgress += dwPortion; pfts.totalProgress += dwPortion; diff --git a/protocols/ICQ-WIM/src/groupchats.cpp b/protocols/ICQ-WIM/src/groupchats.cpp index 968182e63b..a765426045 100644 --- a/protocols/ICQ-WIM/src/groupchats.cpp +++ b/protocols/ICQ-WIM/src/groupchats.cpp @@ -60,7 +60,7 @@ INT_PTR CIcqProto::SvcLeaveChat(WPARAM hContact, LPARAM) ///////////////////////////////////////////////////////////////////////////////////////// -void CIcqProto::OnGetChatInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnGetChatInfo(MHttpResponse *pReply, AsyncHttpRequest *pReq) { RobustReply root(pReply); if (root.error() != 20000) @@ -191,7 +191,7 @@ void CIcqProto::InviteUserToChat(SESSION_INFO *si) dlg.DoModal(); } -void CIcqProto::OnLeaveChat(NETLIBHTTPREQUEST*, AsyncHttpRequest *pReq) +void CIcqProto::OnLeaveChat(MHttpResponse*, AsyncHttpRequest *pReq) { db_delete_contact(INT_PTR(pReq->pUserInfo)); } diff --git a/protocols/ICQ-WIM/src/http.cpp b/protocols/ICQ-WIM/src/http.cpp index b422c0d57b..97fc58875d 100644 --- a/protocols/ICQ-WIM/src/http.cpp +++ b/protocols/ICQ-WIM/src/http.cpp @@ -147,12 +147,8 @@ AsyncHttpRequest::AsyncHttpRequest(IcqConnection conn, int iType, const char *sz strncpy_s(m_reqId, (char*)szId, _TRUNCATE); RpcStringFreeA(&szId); - if (iType == REQUEST_POST) { + if (iType == REQUEST_POST) AddHeader("Content-Type", "application/x-www-form-urlencoded"); - - dataLength = m_szParam.GetLength(); - pData = m_szParam.Detach(); - } } void AsyncHttpRequest::ReplaceJsonParam(const JSONNode &n) @@ -166,29 +162,15 @@ void AsyncHttpRequest::ReplaceJsonParam(const JSONNode &n) else root.push_back(n); m_szParam = root.write().c_str(); - - replaceStr(pData, nullptr); - dataLength = 0; } bool CIcqProto::ExecuteRequest(AsyncHttpRequest *pReq) { - CMStringA str; - - 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()); // replace credentials inside JSON body for pure RAPI requests - if (pReq->m_conn == CONN_RAPI && !mir_strcmp(pReq->szUrl, ICQ_ROBUST_SERVER) && !getByte(DB_KEY_PHONEREG)) { + if (pReq->m_conn == CONN_RAPI && !mir_strcmp(pReq->m_szUrl, ICQ_ROBUST_SERVER) && !getByte(DB_KEY_PHONEREG)) { CMStringA szAgent(FORMAT, "%S Mail.ru Windows ICQ (version 10.0.1999)", (wchar_t*)m_szOwnId); pReq->AddHeader("User-Agent", szAgent); pReq->AddHeader("Content-Type", "application/json"); @@ -203,11 +185,9 @@ bool CIcqProto::ExecuteRequest(AsyncHttpRequest *pReq) if (m_iRClientId) pReq->ReplaceJsonParam(JSONNode("clientId", m_iRClientId)); pReq->ReplaceJsonParam(JSONNode("authToken", m_szRToken)); - pReq->dataLength = pReq->m_szParam.GetLength(); - pReq->pData = mir_strdup(pReq->m_szParam); } - debugLogA("Executing request %s:\n%s", pReq->m_reqId, pReq->szUrl); + debugLogA("Executing request %s:\n%s", pReq->m_reqId, pReq->m_szUrl.c_str()); if (pReq->m_conn != CONN_NONE) { pReq->flags |= NLHRF_PERSISTENT; @@ -222,14 +202,14 @@ bool CIcqProto::ExecuteRequest(AsyncHttpRequest *pReq) auto &conn = m_ConnPool[pReq->m_conn]; conn.s = reply->nlc; conn.timeout = 0; - if (auto *pszHdr = Netlib_GetHeader(reply, "Keep-Alive")) { + if (auto *pszHdr = reply->FindHeader("Keep-Alive")) { int timeout; if (1 == sscanf(pszHdr, "timeout=%d", &timeout)) conn.timeout = timeout; } } - if (pReq->m_conn == CONN_RAPI && reply->pData && strstr(reply->pData, "\"code\": 40201")) { + if (pReq->m_conn == CONN_RAPI && reply->body.Find("\"code\": 40201") != -1) { RobustReply r(reply); if (r.error() == 40201) { // robust token expired m_szRToken.Empty(); @@ -305,7 +285,7 @@ MHttpRequest* operator<<(MHttpRequest *pReq, const GROUP_PARAM ¶m) ///////////////////////////////////////////////////////////////////////////////////////// -JsonReply::JsonReply(NETLIBHTTPREQUEST *pReply) +JsonReply::JsonReply(MHttpResponse *pReply) { if (pReply == nullptr) { m_errorCode = 500; @@ -316,7 +296,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; @@ -336,7 +316,7 @@ JsonReply::~JsonReply() ///////////////////////////////////////////////////////////////////////////////////////// -FileReply::FileReply(NETLIBHTTPREQUEST *pReply) +FileReply::FileReply(MHttpResponse *pReply) { if (pReply == nullptr) { m_errorCode = 500; @@ -347,7 +327,7 @@ FileReply::FileReply(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; @@ -364,7 +344,7 @@ FileReply::~FileReply() ///////////////////////////////////////////////////////////////////////////////////////// -RobustReply::RobustReply(NETLIBHTTPREQUEST *pReply) +RobustReply::RobustReply(MHttpResponse *pReply) { if (pReply == nullptr) { m_errorCode = 500; @@ -375,7 +355,7 @@ RobustReply::RobustReply(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; diff --git a/protocols/ICQ-WIM/src/http.h b/protocols/ICQ-WIM/src/http.h index 5bbccd0be7..1b201d097a 100644 --- a/protocols/ICQ-WIM/src/http.h +++ b/protocols/ICQ-WIM/src/http.h @@ -49,7 +49,7 @@ class JsonReply CMStringA m_requestId; public: - JsonReply(NETLIBHTTPREQUEST*); + JsonReply(MHttpResponse*); ~JsonReply(); __forceinline const CMStringA& requestId() const { return m_requestId; } @@ -65,7 +65,7 @@ class FileReply JSONNode* m_data = nullptr; public: - FileReply(NETLIBHTTPREQUEST*); + FileReply(MHttpResponse*); ~FileReply(); __forceinline JSONNode& data() const { return *m_data; } @@ -80,7 +80,7 @@ class RobustReply JSONNode *m_results = nullptr; public: - RobustReply(NETLIBHTTPREQUEST*); + RobustReply(MHttpResponse*); ~RobustReply(); __forceinline JSONNode &result() const { return *m_result; } diff --git a/protocols/ICQ-WIM/src/ignore.cpp b/protocols/ICQ-WIM/src/ignore.cpp index 6dd86c51fb..a07b5271c7 100644 --- a/protocols/ICQ-WIM/src/ignore.cpp +++ b/protocols/ICQ-WIM/src/ignore.cpp @@ -26,7 +26,7 @@ void CIcqProto::GetPermitDeny() Push(new AsyncHttpRequest(CONN_MAIN, REQUEST_GET, "/preference/getPermitDeny", &CIcqProto::OnGetPermitDeny) << AIMSID(this)); } -void CIcqProto::OnGetPermitDeny(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +void CIcqProto::OnGetPermitDeny(MHttpResponse *pReply, AsyncHttpRequest*) { JsonReply root(pReply); if (root.error() == 200) diff --git a/protocols/ICQ-WIM/src/mra.cpp b/protocols/ICQ-WIM/src/mra.cpp index b65ae238dc..751734e43b 100644 --- a/protocols/ICQ-WIM/src/mra.cpp +++ b/protocols/ICQ-WIM/src/mra.cpp @@ -17,7 +17,7 @@ along with this program. If not, see . #include "stdafx.h" -void CIcqProto::SendMrimLogin(NETLIBHTTPREQUEST *pReply) +void CIcqProto::SendMrimLogin(MHttpResponse *pReply) { m_szMraCookie = pReply->GetCookies(); @@ -33,7 +33,7 @@ void CIcqProto::SendMrimLogin(NETLIBHTTPREQUEST *pReply) Push(pReq); } -void CIcqProto::OnCheckMrimLogin(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *) +void CIcqProto::OnCheckMrimLogin(MHttpResponse *pReply, AsyncHttpRequest *) { JsonReply root(pReply); switch (root.error()) { @@ -98,7 +98,7 @@ void CIcqProto::OnCheckMrimLogin(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *) StartSession(); } -void CIcqProto::OnCheckMraAuth(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *) +void CIcqProto::OnCheckMraAuth(MHttpResponse *pReply, AsyncHttpRequest *) { JsonReply root(pReply); switch (root.error()) { @@ -113,7 +113,7 @@ void CIcqProto::OnCheckMraAuth(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *) } } -void CIcqProto::OnCheckMraAuthFinal(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *) +void CIcqProto::OnCheckMraAuthFinal(MHttpResponse *pReply, AsyncHttpRequest *) { switch (pReply->resultCode) { case 200: diff --git a/protocols/ICQ-WIM/src/options.cpp b/protocols/ICQ-WIM/src/options.cpp index b06ee210a1..9036ed95c9 100644 --- a/protocols/ICQ-WIM/src/options.cpp +++ b/protocols/ICQ-WIM/src/options.cpp @@ -133,7 +133,7 @@ struct CIcqRegistrationDlg : public CIcqDlgBase } }; -void CIcqProto::OnCheckPhone(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnCheckPhone(MHttpResponse *pReply, AsyncHttpRequest *pReq) { if (pReply == nullptr || pReply->resultCode != 200) return; @@ -142,7 +142,7 @@ void CIcqProto::OnCheckPhone(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) pDlg->btnSendSms.Disable(); pDlg->edtCode.Disable(); - JSONROOT root(pReply->pData); + JSONROOT root(pReply->body); CMStringW wszStatus((*root)["status"].as_mstring()); if (wszStatus != L"OK") { pDlg->edtCode.SetText((*root)["printable"].as_mstring()); @@ -159,7 +159,7 @@ void CIcqProto::OnCheckPhone(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) Push(pNew); } -void CIcqProto::OnNormalizePhone(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnNormalizePhone(MHttpResponse *pReply, AsyncHttpRequest *pReq) { CIcqRegistrationDlg *pDlg = (CIcqRegistrationDlg*)pReq->pUserInfo; @@ -173,7 +173,7 @@ void CIcqProto::OnNormalizePhone(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pR pDlg->btnSendSms.Enable(); } -void CIcqProto::OnValidateSms(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnValidateSms(MHttpResponse *pReply, AsyncHttpRequest *pReq) { JsonReply root(pReply); if (root.error() != 200) @@ -187,7 +187,7 @@ void CIcqProto::OnValidateSms(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) pDlg->edtCode.SetText(L""); } -void CIcqProto::OnLoginViaPhone(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnLoginViaPhone(MHttpResponse *pReply, AsyncHttpRequest *pReq) { CIcqRegistrationDlg *pDlg = (CIcqRegistrationDlg*)pReq->pUserInfo; diff --git a/protocols/ICQ-WIM/src/poll.cpp b/protocols/ICQ-WIM/src/poll.cpp index 2f2d8fdf32..1be03cc30d 100644 --- a/protocols/ICQ-WIM/src/poll.cpp +++ b/protocols/ICQ-WIM/src/poll.cpp @@ -362,7 +362,7 @@ void CIcqProto::ProcessTyping(const JSONNode &ev) } } -void CIcqProto::OnFetchEvents(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +void CIcqProto::OnFetchEvents(MHttpResponse *pReply, AsyncHttpRequest*) { JsonReply root(pReply); if (root.error() != 200) { diff --git a/protocols/ICQ-WIM/src/proto.cpp b/protocols/ICQ-WIM/src/proto.cpp index 68567975b1..ac913a8afe 100644 --- a/protocols/ICQ-WIM/src/proto.cpp +++ b/protocols/ICQ-WIM/src/proto.cpp @@ -242,7 +242,7 @@ void CIcqProto::OnEventDeleted(MCONTACT hContact, MEVENT hEvent) ///////////////////////////////////////////////////////////////////////////////////////// -void CIcqProto::OnFileRecv(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnFileRecv(MHttpResponse *pReply, AsyncHttpRequest *pReq) { if (pReply->resultCode != 200) return; @@ -255,10 +255,10 @@ void CIcqProto::OnFileRecv(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) return; } - int cbWritten = _write(fileId, pReply->pData, pReply->dataLength); + int cbWritten = _write(fileId, pReply->body, pReply->body.GetLength()); _close(fileId); - if (cbWritten != pReply->dataLength) { - debugLogW(L"Error writing data into [%s]: %d instead of %d", ofd->wszPath.c_str(), cbWritten, pReply->dataLength); + if (cbWritten != pReply->body.GetLength()) { + debugLogW(L"Error writing data into [%s]: %d instead of %d", ofd->wszPath.c_str(), cbWritten, pReply->body.GetLength()); return; } diff --git a/protocols/ICQ-WIM/src/proto.h b/protocols/ICQ-WIM/src/proto.h index 44def39b16..3ca731e905 100644 --- a/protocols/ICQ-WIM/src/proto.h +++ b/protocols/ICQ-WIM/src/proto.h @@ -229,7 +229,7 @@ class CIcqProto : public PROTO void RetrieveUserCaps(IcqUser *pUser); void RetrieveUserHistory(MCONTACT, __int64 startMsgId, bool bCreateRead); void RetrieveUserInfo(MCONTACT hContact); - void SendMrimLogin(NETLIBHTTPREQUEST *pReply); + void SendMrimLogin(MHttpResponse *pReply); void SendMessageParts(MCONTACT hContact, const JSONNode &parts, IcqOwnMessage *pOwn = nullptr); void SetOwnId(const CMStringW &wszId); void SetServerStatus(int iNewStatus); @@ -263,36 +263,36 @@ class CIcqProto : public PROTO __int64 getId(MCONTACT hContact, const char *szSetting); void setId(MCONTACT hContact, const char *szSetting, __int64 iValue); - void OnAddBuddy(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnAddClient(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnCheckMraAuth(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnCheckMraAuthFinal(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnCheckMrimLogin(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnCheckPassword(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnCheckPhone(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnFetchEvents(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnFileContinue(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnFileInit(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnFileInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnFileRecv(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnGenToken(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnGetChatInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnGetPatches(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnGetPermitDeny(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnGePresence(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnGetSticker(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnGetUserCaps(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnGetUserHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnGetUserInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnLeaveChat(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnLoginViaPhone(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnNormalizePhone(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnReceiveAvatar(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnSearchResults(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnSendMessage(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnSessionEnd(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnStartSession(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); - void OnValidateSms(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq); + void OnAddBuddy(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnAddClient(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnCheckMraAuth(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnCheckMraAuthFinal(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnCheckMrimLogin(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnCheckPassword(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnCheckPhone(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnFetchEvents(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnFileContinue(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnFileInit(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnFileInfo(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnFileRecv(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnGenToken(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnGetChatInfo(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnGetPatches(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnGetPermitDeny(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnGePresence(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnGetSticker(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnGetUserCaps(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnGetUserHistory(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnGetUserInfo(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnLeaveChat(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnLoginViaPhone(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnNormalizePhone(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnReceiveAvatar(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnSearchResults(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnSendMessage(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnSessionEnd(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnStartSession(MHttpResponse *pReply, AsyncHttpRequest *pReq); + void OnValidateSms(MHttpResponse *pReply, AsyncHttpRequest *pReq); void ProcessBuddyList(const JSONNode &pRoot); void ProcessDiff(const JSONNode &pRoot); diff --git a/protocols/ICQ-WIM/src/server.cpp b/protocols/ICQ-WIM/src/server.cpp index 36887220fa..e62a609cc4 100644 --- a/protocols/ICQ-WIM/src/server.cpp +++ b/protocols/ICQ-WIM/src/server.cpp @@ -133,7 +133,7 @@ void CIcqProto::CheckPassword() ///////////////////////////////////////////////////////////////////////////////////////// -void CIcqProto::OnFileInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnFileInfo(MHttpResponse *pReply, AsyncHttpRequest *pReq) { IcqFileInfo **res = (IcqFileInfo **)pReq->pUserInfo; *res = nullptr; @@ -691,7 +691,7 @@ LBL_Error: ///////////////////////////////////////////////////////////////////////////////////////// -void CIcqProto::OnGetUserCaps(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnGetUserCaps(MHttpResponse *pReply, AsyncHttpRequest *pReq) { JsonReply root(pReply); if (root.error() != 200) @@ -718,7 +718,7 @@ void CIcqProto::RetrieveUserCaps(IcqUser *pUser) ///////////////////////////////////////////////////////////////////////////////////////// -void CIcqProto::OnGePresence(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnGePresence(MHttpResponse *pReply, AsyncHttpRequest *pReq) { JsonReply root(pReply); if (root.error() != 200) @@ -744,7 +744,7 @@ void CIcqProto::RetrievePresence(MCONTACT hContact) ///////////////////////////////////////////////////////////////////////////////////////// -void CIcqProto::OnGetUserInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnGetUserInfo(MHttpResponse *pReply, AsyncHttpRequest *pReq) { RobustReply root(pReply); if (root.error() != 20000) { @@ -774,7 +774,7 @@ void CIcqProto::RetrieveUserInfo(MCONTACT hContact) ///////////////////////////////////////////////////////////////////////////////////////// -void CIcqProto::OnGetPatches(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnGetPatches(MHttpResponse *pReply, AsyncHttpRequest *pReq) { RobustReply root(pReply); if (root.error() != 20000) @@ -828,7 +828,7 @@ void CIcqProto::ProcessPatchVersion(MCONTACT hContact, __int64 currPatch) ///////////////////////////////////////////////////////////////////////////////////////// -void CIcqProto::OnGetUserHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnGetUserHistory(MHttpResponse *pReply, AsyncHttpRequest *pReq) { RobustReply root(pReply); if (root.error() != 20000) @@ -998,7 +998,7 @@ void CIcqProto::StartSession() ///////////////////////////////////////////////////////////////////////////////////////// -void CIcqProto::OnAddBuddy(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnAddBuddy(MHttpResponse *pReply, AsyncHttpRequest *pReq) { JsonReply root(pReply); if (root.error() != 200) @@ -1032,7 +1032,7 @@ void CIcqProto::OnAddBuddy(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) } } -void CIcqProto::OnAddClient(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnAddClient(MHttpResponse *pReply, AsyncHttpRequest *pReq) { bool *pRet = (bool*)pReq->pUserInfo; @@ -1048,7 +1048,7 @@ void CIcqProto::OnAddClient(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) *pRet = true; } -void CIcqProto::OnCheckPassword(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +void CIcqProto::OnCheckPassword(MHttpResponse *pReply, AsyncHttpRequest*) { JsonReply root(pReply); switch (root.error()) { @@ -1087,7 +1087,7 @@ void CIcqProto::OnCheckPassword(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) StartSession(); } -void CIcqProto::OnFileContinue(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pOld) +void CIcqProto::OnFileContinue(MHttpResponse *pReply, AsyncHttpRequest *pOld) { IcqFileTransfer *pTransfer = (IcqFileTransfer*)pOld->pUserInfo; if (pTransfer->m_bCanceled) { @@ -1155,7 +1155,7 @@ LBL_Error: ProtoBroadcastAck(pTransfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, pTransfer, (LPARAM)&pTransfer->pfts); } -void CIcqProto::OnFileInit(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pOld) +void CIcqProto::OnFileInit(MHttpResponse *pReply, AsyncHttpRequest *pOld) { IcqFileTransfer *pTransfer = (IcqFileTransfer*)pOld->pUserInfo; if (pTransfer->m_bCanceled) { @@ -1192,7 +1192,7 @@ LBL_Error: ///////////////////////////////////////////////////////////////////////////////////////// // Support for stickers -void CIcqProto::OnGetSticker(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnGetSticker(MHttpResponse *pReply, AsyncHttpRequest *pReq) { if (pReply->resultCode != 200) { debugLogA("Error getting sticker: %d", pReply->resultCode); @@ -1204,7 +1204,7 @@ void CIcqProto::OnGetSticker(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) CMStringW wszFileName(FORMAT, L"%s\\STK{%s}.png", wszPath.c_str(), (wchar_t*)pReq->pUserInfo); FILE *out = _wfopen(wszFileName, L"wb"); - fwrite(pReply->pData, 1, pReply->dataLength, out); + fwrite(pReply->body, 1, pReply->body.GetLength(), out); fclose(out); SmileyAdd_LoadContactSmileys(SMADD_FILE, m_szModuleName, wszFileName); @@ -1212,7 +1212,7 @@ void CIcqProto::OnGetSticker(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) ///////////////////////////////////////////////////////////////////////////////////////// -void CIcqProto::OnGenToken(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +void CIcqProto::OnGenToken(MHttpResponse *pReply, AsyncHttpRequest*) { RobustReply root(pReply); if (root.error() != 20000) @@ -1222,7 +1222,7 @@ void CIcqProto::OnGenToken(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) m_szRToken = results["authToken"].as_mstring(); } -void CIcqProto::OnStartSession(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *) +void CIcqProto::OnStartSession(MHttpResponse *pReply, AsyncHttpRequest *) { JsonReply root(pReply); switch (root.error()) { @@ -1279,18 +1279,18 @@ void CIcqProto::OnStartSession(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *) ForkThread(&CIcqProto::PollThread); } -void CIcqProto::OnReceiveAvatar(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnReceiveAvatar(MHttpResponse *pReply, AsyncHttpRequest *pReq) { PROTO_AVATAR_INFORMATION ai = {}; ai.hContact = pReq->hContact; - if (pReply->resultCode != 200 || pReply->pData == nullptr) { + if (pReply->resultCode != 200 || pReply->body.IsEmpty()) { LBL_Error: ProtoBroadcastAck(pReq->hContact, ACKTYPE_AVATAR, ACKRESULT_FAILED, &ai); return; } - const char *szContentType = Netlib_GetHeader(pReply, "Content-Type"); + const char *szContentType = pReply->FindHeader("Content-Type"); if (szContentType == nullptr) szContentType = "image/jpeg"; @@ -1302,7 +1302,7 @@ LBL_Error: if (out == nullptr) goto LBL_Error; - fwrite(pReply->pData, pReply->dataLength, 1, out); + fwrite(pReply->body, pReply->body.GetLength(), 1, out); fclose(out); if (pReq->hContact != 0) { @@ -1312,7 +1312,7 @@ LBL_Error: else ReportSelfAvatarChanged(); } -void CIcqProto::OnSearchResults(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnSearchResults(MHttpResponse *pReply, AsyncHttpRequest *pReq) { RobustReply root(pReply); if (root.error() != 20000) { @@ -1347,7 +1347,7 @@ void CIcqProto::OnSearchResults(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pRe ///////////////////////////////////////////////////////////////////////////////////////// // Send message -void CIcqProto::OnSendMessage(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +void CIcqProto::OnSendMessage(MHttpResponse *pReply, AsyncHttpRequest *pReq) { IcqOwnMessage *ownMsg = (IcqOwnMessage *)pReq->pUserInfo; @@ -1394,7 +1394,7 @@ void CIcqProto::SendMessageParts(MCONTACT hContact, const JSONNode &parts, IcqOw ///////////////////////////////////////////////////////////////////////////////////////// -void CIcqProto::OnSessionEnd(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *) +void CIcqProto::OnSessionEnd(MHttpResponse *pReply, AsyncHttpRequest *) { JsonReply root(pReply); if (root.error() == 200) { diff --git a/protocols/JabberG/src/jabber_file.cpp b/protocols/JabberG/src/jabber_file.cpp index ebd33b3d93..c35069a294 100644 --- a/protocols/JabberG/src/jabber_file.cpp +++ b/protocols/JabberG/src/jabber_file.cpp @@ -49,10 +49,10 @@ void __cdecl CJabberProto::OfflineFileThread(OFDTHREAD *ofd) } // initialize the netlib request - NETLIBHTTPREQUEST nlhr = {}; + MHttpRequest nlhr; nlhr.requestType = REQUEST_GET; nlhr.flags = NLHRF_HTTP11 | NLHRF_DUMPASTEXT | NLHRF_REDIRECT; - nlhr.szUrl = (char *)url; + nlhr.m_szUrl = url; // download the page NLHR_PTR nlhrReply(Netlib_HttpTransaction(m_hNetlibUser, &nlhr)); @@ -61,18 +61,18 @@ void __cdecl CJabberProto::OfflineFileThread(OFDTHREAD *ofd) size_t written = 0; if (f) { if (encrypted) { - int payload_len = nlhrReply->dataLength - 16; + int payload_len = nlhrReply->body.GetLength() - 16; if (payload_len > 0) { uint8_t ivkey[44]; hex2bin(hexkey, ivkey, 44); EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL); EVP_DecryptInit(ctx, EVP_aes_256_gcm(), ivkey + 12, ivkey); - EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, nlhrReply->pData + payload_len); + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, (uint8_t *)nlhrReply->body.c_str() + payload_len); int outl = 0, round_len = 0; uint8_t *out = (uint8_t *)mir_alloc(payload_len); - EVP_DecryptUpdate(ctx, out, &outl, (uint8_t *)nlhrReply->pData, (int)payload_len); + EVP_DecryptUpdate(ctx, out, &outl, (uint8_t *)nlhrReply->body.c_str(), (int)payload_len); int dec_success = EVP_DecryptFinal(ctx, out + outl, &round_len); outl += round_len; EVP_CIPHER_CTX_free(ctx); @@ -82,8 +82,8 @@ void __cdecl CJabberProto::OfflineFileThread(OFDTHREAD *ofd) mir_free(out); } } - else if (fwrite(nlhrReply->pData, 1, nlhrReply->dataLength, f) == size_t(nlhrReply->dataLength)) - written = nlhrReply->dataLength; + else if (fwrite(nlhrReply->body, 1, nlhrReply->body.GetLength(), f) == size_t(nlhrReply->body.GetLength())) + written = nlhrReply->body.GetLength(); fclose(f); } @@ -127,16 +127,16 @@ void CJabberProto::OnReceiveOfflineFile(DB::FILE_BLOB &blob, void *pHandle) void __cdecl CJabberProto::FileReceiveHttpThread(filetransfer *ft) { - NETLIBHTTPREQUEST req = {}; + MHttpRequest req; req.requestType = REQUEST_GET; - req.szUrl = ft->httpPath; + req.m_szUrl = ft->httpPath; NLHR_PTR pResp(Netlib_HttpTransaction(m_hNetlibUser, &req)); if (pResp && pResp->resultCode == 200) { - ft->std.currentFileSize = pResp->dataLength; + ft->std.currentFileSize = pResp->body.GetLength(); ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, ft); - FtReceive(ft, pResp->pData, pResp->dataLength); + FtReceive(ft, pResp->body.GetBuffer(), pResp->body.GetLength()); ft->complete(); } @@ -154,9 +154,9 @@ void CJabberProto::FileProcessHttpDownload(MCONTACT hContact, const char *jid, c szName.AppendChar(*b++); auto *pszName = szName.c_str(); - NETLIBHTTPREQUEST req = {}; + MHttpRequest req; req.requestType = REQUEST_HEAD; - req.szUrl = (char*)pszUrl; + req.m_szUrl = pszUrl; filetransfer *ft = new filetransfer(this, 0); ft->jid = mir_strdup(jid); diff --git a/protocols/JabberG/src/jabber_ft.cpp b/protocols/JabberG/src/jabber_ft.cpp index 9ca292ca71..a489aa696c 100644 --- a/protocols/JabberG/src/jabber_ft.cpp +++ b/protocols/JabberG/src/jabber_ft.cpp @@ -691,22 +691,16 @@ LBL_Fail: if (!szUrl) goto LBL_Fail; - NETLIBHTTPHEADER hdr[10]; - - NETLIBHTTPREQUEST nlhr = {}; + MHttpRequest nlhr; nlhr.requestType = REQUEST_PUT; nlhr.flags = NLHRF_NODUMPSEND | NLHRF_SSL | NLHRF_REDIRECT; - nlhr.szUrl = (char *)szUrl; + nlhr.m_szUrl = szUrl; for (auto *it : TiXmlFilter(putNode, "header")) { auto *szName = it->Attribute("name"); auto *szValue = it->GetText(); - if (szName && szValue && nlhr.headersCount < _countof(hdr)) { - nlhr.headers = hdr; - hdr[nlhr.headersCount].szName = (char *)szName; - hdr[nlhr.headersCount].szValue = (char *)szValue; - nlhr.headersCount++; - } + if (szName && szValue) + nlhr.AddHeader(szName, szValue); } const wchar_t *pwszFileName = ft->std.pszFiles.w[ft->std.currentFileNumber]; @@ -728,8 +722,8 @@ LBL_Fail: EVP_EncryptInit(ctx, EVP_aes_256_gcm(), key, iv); int tmp_len = 0, outl; - //EVP_EncryptUpdate(ctx, nullptr, &outl, aad, _countof(aad)); - unsigned char *out = (unsigned char *)mir_alloc(_filelength(fileId) + _countof(key) - 1 + _countof(tag)); + nlhr.m_szParam.Truncate(_filelength(fileId) + _countof(key) - 1 + _countof(tag)); + unsigned char *out = (unsigned char *)nlhr.m_szParam.GetBuffer(); unsigned char *in = (unsigned char *)mir_alloc(128 * 1024); for (;;) { int inl = _read(fileId, in, 128 * 1024); @@ -746,13 +740,11 @@ LBL_Fail: EVP_CIPHER_CTX_free(ctx); memcpy(out + tmp_len, tag, _countof(tag)); - nlhr.dataLength = tmp_len + _countof(tag); - nlhr.pData = (char *)out; } else { - nlhr.dataLength = _filelength(fileId); - nlhr.pData = new char[nlhr.dataLength]; - _read(fileId, nlhr.pData, nlhr.dataLength); + int iLength = _filelength(fileId); + nlhr.m_szParam.Truncate(iLength); + _read(fileId, nlhr.m_szParam.GetBuffer(), iLength); } _close(fileId); diff --git a/protocols/JabberG/src/jabber_util.cpp b/protocols/JabberG/src/jabber_util.cpp index e018c39b40..441b5de49c 100644 --- a/protocols/JabberG/src/jabber_util.cpp +++ b/protocols/JabberG/src/jabber_util.cpp @@ -881,17 +881,17 @@ void __cdecl CJabberProto::LoadHttpAvatars(void* param) OBJLIST &avs = *(OBJLIST*)param; HNETLIBCONN hHttpCon = nullptr; for (auto &it : avs) { - NETLIBHTTPREQUEST nlhr = { 0 }; + MHttpRequest nlhr; nlhr.requestType = REQUEST_GET; nlhr.flags = NLHRF_HTTP11 | NLHRF_REDIRECT | NLHRF_PERSISTENT; - nlhr.szUrl = it->Url; + nlhr.m_szUrl = it->Url; nlhr.nlc = hHttpCon; NLHR_PTR res(Netlib_HttpTransaction(m_hNetlibUser, &nlhr)); if (res) { hHttpCon = res->nlc; - if (res->resultCode == 200 && res->dataLength) { - int pictureType = ProtoGetBufferFormat(res->pData); + if (res->resultCode == 200 && !res->body.IsEmpty()) { + int pictureType = ProtoGetBufferFormat(res->body); if (pictureType != PA_FORMAT_UNKNOWN) { PROTO_AVATAR_INFORMATION ai; ai.format = pictureType; @@ -909,7 +909,7 @@ void __cdecl CJabberProto::LoadHttpAvatars(void* param) uint8_t digest[MIR_SHA1_HASH_SIZE]; mir_sha1_ctx sha; mir_sha1_init(&sha); - mir_sha1_append(&sha, (uint8_t*)res->pData, res->dataLength); + mir_sha1_append(&sha, (uint8_t*)res->body.c_str(), res->body.GetLength()); mir_sha1_finish(&sha, digest); bin2hex(digest, sizeof(digest), buffer); @@ -920,7 +920,7 @@ void __cdecl CJabberProto::LoadHttpAvatars(void* param) wcsncpy_s(ai.filename, tszFileName, _TRUNCATE); FILE* out = _wfopen(tszFileName, L"wb"); if (out != nullptr) { - fwrite(res->pData, res->dataLength, 1, out); + fwrite(res->body, res->body.GetLength(), 1, out); fclose(out); setString(ai.hContact, "AvatarHash", buffer); ProtoBroadcastAck(ai.hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, &ai); diff --git a/protocols/MinecraftDynmap/src/communication.cpp b/protocols/MinecraftDynmap/src/communication.cpp index 47e6ec4480..e9e035404d 100644 --- a/protocols/MinecraftDynmap/src/communication.cpp +++ b/protocols/MinecraftDynmap/src/communication.cpp @@ -21,18 +21,16 @@ along with this program. If not, see . #include "stdafx.h" -http::response MinecraftDynmapProto::sendRequest(const int request_type, std::string *post_data, std::string *get_data) +MHttpResponse* MinecraftDynmapProto::sendRequest(const int request_type, std::string *post_data, std::string *get_data) { - http::response resp; - // Prepare the request - NETLIBHTTPREQUEST nlhr = { sizeof(NETLIBHTTPREQUEST) }; + MHttpRequest nlhr; // FIXME: get server // Set request URL std::string url = m_server + chooseAction(request_type, get_data); - nlhr.szUrl = (char*)url.c_str(); + nlhr.m_szUrl = url.c_str(); // Set timeout (bigger for channel request) nlhr.timeout = 1000 * ((request_type == MINECRAFTDYNMAP_REQUEST_EVENTS) ? 65 : 20); @@ -40,15 +38,18 @@ http::response MinecraftDynmapProto::sendRequest(const int request_type, std::st // Set request type (GET/POST) and eventually also POST data if (post_data != nullptr) { nlhr.requestType = REQUEST_POST; - nlhr.pData = (char*)(*post_data).c_str(); - nlhr.dataLength = (int)post_data->length(); - } - else { - nlhr.requestType = REQUEST_GET; + nlhr.SetData(post_data->c_str(), post_data->length()); } + else nlhr.requestType = REQUEST_GET; // Set headers - it depends on requestType so it must be after setting that - nlhr.headers = get_request_headers(nlhr.requestType, &nlhr.headersCount); + if (request_type == REQUEST_POST) + nlhr.AddHeader("Content-Type", "application/json; charset=utf-8"); + + nlhr.AddHeader("Cookie", m_cookie.c_str()); + nlhr.AddHeader("User-Agent", g_strUserAgent.c_str()); + nlhr.AddHeader("Accept", "*/*"); + nlhr.AddHeader("Accept-Language", "en,en-US;q=0.9"); // Set flags nlhr.flags = NLHRF_HTTP11; @@ -78,12 +79,10 @@ http::response MinecraftDynmapProto::sendRequest(const int request_type, std::st break; } - debugLogA("@@@@@ Sending request to '%s'", nlhr.szUrl); + debugLogA("@@@@@ Sending request to '%s'", nlhr.m_szUrl.c_str()); // Send the request - NLHR_PTR pnlhr(Netlib_HttpTransaction(m_hNetlibUser, &nlhr)); - - mir_free(nlhr.headers); + auto *pnlhr = Netlib_HttpTransaction(m_hNetlibUser, &nlhr); // Remember the persistent connection handle (or not) switch (request_type) { @@ -100,22 +99,7 @@ http::response MinecraftDynmapProto::sendRequest(const int request_type, std::st break; } - // Check and copy response data - if (pnlhr != nullptr) - { - debugLogA("@@@@@ Got response with code %d", pnlhr->resultCode); - store_headers(&resp, pnlhr->headers, pnlhr->headersCount); - resp.code = pnlhr->resultCode; - resp.data = pnlhr->pData ? pnlhr->pData : ""; - - // debugLogA("&&&&& Got response: %s", resp.data.c_str()); - } else { - debugLogA("!!!!! No response from server (time-out)"); - resp.code = HTTP_CODE_FAKE_DISCONNECTED; - // Better to have something set explicitely as this value is compaired in all communication requests - } - - return resp; + return pnlhr; } ////////////////////////////////////////////////////////////////////////////// @@ -138,56 +122,17 @@ std::string MinecraftDynmapProto::chooseAction(int request_type, std::string *ge } } - -NETLIBHTTPHEADER* MinecraftDynmapProto::get_request_headers(int request_type, int* headers_count) -{ - if (request_type == REQUEST_POST) - *headers_count = 5; - else - *headers_count = 4; - - NETLIBHTTPHEADER *headers = (NETLIBHTTPHEADER*)mir_calloc(sizeof(NETLIBHTTPHEADER)*(*headers_count)); - - if (request_type == REQUEST_POST) { - headers[4].szName = "Content-Type"; - headers[4].szValue = "application/json; charset=utf-8"; - } - - headers[3].szName = "Cookie"; - headers[3].szValue = (char *)m_cookie.c_str(); - headers[2].szName = "User-Agent"; - headers[2].szValue = (char *)g_strUserAgent.c_str(); - headers[1].szName = "Accept"; - headers[1].szValue = "*/*"; - headers[0].szName = "Accept-Language"; - headers[0].szValue = "en,en-US;q=0.9"; - - return headers; -} - -void MinecraftDynmapProto::store_headers(http::response* resp, NETLIBHTTPHEADER* headers, int headersCount) -{ - for (size_t i = 0; i < (size_t)headersCount; i++) { - std::string header_name = headers[i].szName; - std::string header_value = headers[i].szValue; - - resp->headers[header_name] = header_value; - } -} - ////////////////////////////////////////////////////////////////////////////// bool MinecraftDynmapProto::doSignOn() { handleEntry(__FUNCTION__); - http::response resp = sendRequest(MINECRAFTDYNMAP_REQUEST_CONFIGURATION); - - if (resp.code != HTTP_CODE_OK) { + NLHR_PTR resp(sendRequest(MINECRAFTDYNMAP_REQUEST_CONFIGURATION)); + if (!resp || resp->resultCode != HTTP_CODE_OK) return handleError(__FUNCTION__, "Can't load configuration", true); - } - JSONNode root = JSONNode::parse(resp.data.c_str()); + JSONNode root = JSONNode::parse(resp->body); if (!root) return false; @@ -212,16 +157,14 @@ bool MinecraftDynmapProto::doSignOn() m_updateRate = rate_.as_int(); m_cookie.clear(); - if (resp.headers.find("Set-Cookie") != resp.headers.end()) { - // Load Session identifier - std::string cookies = resp.headers["Set-Cookie"]; + if (auto *pszCookie = resp->FindHeader("Set-Cookie")) { + m_cookie = pszCookie; + // Load Session identifier const char *findStr = "JSESSIONID="; - std::string::size_type start = cookies.find(findStr); - - if (start != std::string::npos) { - m_cookie = cookies.substr(start, cookies.find(";") - start); - } + std::string::size_type start = m_cookie.find(findStr); + if (start != std::string::npos) + m_cookie = m_cookie.substr(start, m_cookie.find(";") - start); } if (m_cookie.empty()) { @@ -236,12 +179,11 @@ bool MinecraftDynmapProto::doEvents() handleEntry(__FUNCTION__); // Get update - http::response resp = sendRequest(MINECRAFTDYNMAP_REQUEST_EVENTS); - - if (resp.code != HTTP_CODE_OK) + NLHR_PTR resp(sendRequest(MINECRAFTDYNMAP_REQUEST_EVENTS)); + if (!resp || resp->resultCode != HTTP_CODE_OK) return handleError(__FUNCTION__, "Response is not code 200"); - JSONNode root = JSONNode::parse(resp.data.c_str()); + JSONNode root = JSONNode::parse(resp->body); if (!root) return handleError(__FUNCTION__, "Invalid JSON response"); @@ -290,10 +232,9 @@ bool MinecraftDynmapProto::doSendMessage(const std::string &message_text) json.push_back(JSONNode("message", message_text.c_str())); std::string data = json.write(); - http::response resp = sendRequest(MINECRAFTDYNMAP_REQUEST_MESSAGE, &data); - - if (resp.code == HTTP_CODE_OK) { - JSONNode root = JSONNode::parse(resp.data.c_str()); + NLHR_PTR resp(sendRequest(MINECRAFTDYNMAP_REQUEST_MESSAGE, &data)); + if (resp && resp->resultCode == HTTP_CODE_OK) { + JSONNode root = JSONNode::parse(resp->body); if (root) { const JSONNode &error_ = root["error"]; if (error_) { @@ -315,15 +256,14 @@ std::string MinecraftDynmapProto::doGetPage(const int request_type) { handleEntry(__FUNCTION__); - http::response resp = sendRequest(request_type); - - if (resp.code == HTTP_CODE_OK) { + NLHR_PTR resp(sendRequest(request_type)); + if (resp && resp->resultCode == HTTP_CODE_OK) { handleSuccess(__FUNCTION__); - } else { - handleError(__FUNCTION__); + return resp->body.c_str(); } - return resp.data; + handleError(__FUNCTION__); + return ""; } void MinecraftDynmapProto::SignOnWorker(void*) diff --git a/protocols/MinecraftDynmap/src/proto.h b/protocols/MinecraftDynmap/src/proto.h index fb8046d79b..9cebc8c128 100644 --- a/protocols/MinecraftDynmap/src/proto.h +++ b/protocols/MinecraftDynmap/src/proto.h @@ -86,8 +86,6 @@ public: HANDLE chatHandle_; // Data storage - void store_headers(http::response *resp, NETLIBHTTPHEADER *headers, int headers_count); - std::string get_server(bool not_last = false); std::string get_language(); @@ -104,9 +102,8 @@ public: void __inline reset_error() { error_count_ = 0; } // HTTP communication - http::response sendRequest(const int request_type, std::string *post_data = nullptr, std::string *get_data = nullptr); + MHttpResponse* sendRequest(const int request_type, std::string *post_data = nullptr, std::string *get_data = nullptr); std::string chooseAction(int, std::string *get_data = nullptr); - NETLIBHTTPHEADER *get_request_headers(int request_type, int *headers_count); // Requests and processing bool doSignOn(); diff --git a/protocols/MinecraftDynmap/src/utils.h b/protocols/MinecraftDynmap/src/utils.h index c42fb86d9e..135faf0d11 100644 --- a/protocols/MinecraftDynmap/src/utils.h +++ b/protocols/MinecraftDynmap/src/utils.h @@ -30,17 +30,6 @@ along with this program. If not, see . #define HTTP_CODE_FAKE_DISCONNECTED 0 #define HTTP_CODE_FAKE_ERROR 1 -namespace http -{ - struct response - { - response() : code(0) {} - int code; - std::map< std::string, std::string > headers; - std::string data; - }; -} - class ScopedLock { public: diff --git a/protocols/NewsAggregator/Src/Utils.cpp b/protocols/NewsAggregator/Src/Utils.cpp index 328c63bc8e..c5f1df5b95 100644 --- a/protocols/NewsAggregator/Src/Utils.cpp +++ b/protocols/NewsAggregator/Src/Utils.cpp @@ -47,48 +47,35 @@ void NetlibUnInit() void GetNewsData(wchar_t *tszUrl, char **szData, MCONTACT hContact, CFeedEditor *pEditDlg) { Netlib_LogfW(hNetlibUser, L"Getting feed data %s.", tszUrl); - NETLIBHTTPREQUEST nlhr = { 0 }; + MHttpRequest nlhr; // initialize the netlib request nlhr.requestType = REQUEST_GET; nlhr.flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11 | NLHRF_REDIRECT; if (wcsstr(tszUrl, L"https://") != nullptr) nlhr.flags |= NLHRF_SSL; - char *szUrl = mir_u2a(tszUrl); - nlhr.szUrl = szUrl; + nlhr.m_szUrl = _T2A(tszUrl); nlhr.nlc = hNetlibHttp; // change the header so the plugin is pretended to be IE 6 + WinXP - NETLIBHTTPHEADER headers[5]; - nlhr.headersCount = 4; - nlhr.headers = headers; - nlhr.headers[0].szName = "User-Agent"; - nlhr.headers[0].szValue = NETLIB_USER_AGENT; - nlhr.headers[1].szName = "Cache-Control"; - nlhr.headers[1].szValue = "no-cache"; - nlhr.headers[2].szName = "Pragma"; - nlhr.headers[2].szValue = "no-cache"; - nlhr.headers[3].szName = "Connection"; - nlhr.headers[3].szValue = "close"; - char auth[256]; - if (g_plugin.getByte(hContact, "UseAuth", 0) || (pEditDlg && pEditDlg->m_useauth.IsChecked()) /*IsDlgButtonChecked(hwndDlg, IDC_USEAUTH)*/) { - nlhr.headersCount++; - nlhr.headers[4].szName = "Authorization"; + nlhr.AddHeader("User-Agent", NETLIB_USER_AGENT); + nlhr.AddHeader("Cache-Control", "no-cache"); + nlhr.AddHeader("Pragma", "no-cache"); + nlhr.AddHeader("Connection", "close"); + if (g_plugin.getByte(hContact, "UseAuth", 0) || (pEditDlg && pEditDlg->m_useauth.IsChecked())) { + char auth[256]; CreateAuthString(auth, hContact, pEditDlg); - nlhr.headers[4].szValue = auth; + nlhr.AddHeader("Authorization", auth); } // download the page NLHR_PTR nlhrReply(Netlib_HttpTransaction(hNetlibUser, &nlhr)); if (nlhrReply) { // if the recieved code is 200 OK - if (nlhrReply->resultCode == 200 && nlhrReply->dataLength > 0) { + if (nlhrReply->resultCode == 200 && !nlhrReply->body.IsEmpty()) { Netlib_LogfW(hNetlibUser, L"Code 200: Succeeded getting feed data %s.", tszUrl); - // allocate memory and save the retrieved data - *szData = (char *)mir_alloc((size_t)nlhrReply->dataLength + 2); - memcpy(*szData, nlhrReply->pData, (size_t)nlhrReply->dataLength); - (*szData)[nlhrReply->dataLength] = 0; + *szData = nlhrReply->body.Detach(); } else if (nlhrReply->resultCode == 401) { Netlib_LogfW(hNetlibUser, L"Code 401: feed %s needs auth data.", tszUrl); @@ -99,8 +86,6 @@ void GetNewsData(wchar_t *tszUrl, char **szData, MCONTACT hContact, CFeedEditor else Netlib_LogfW(hNetlibUser, L"Code %d: Failed getting feed data %s.", nlhrReply->resultCode, tszUrl); } else Netlib_LogfW(hNetlibUser, L"Failed getting feed data %s, no response.", tszUrl); - - mir_free(szUrl); } time_t DateToUnixTime(const char *stamp, bool FeedType) @@ -220,81 +205,42 @@ time_t DateToUnixTime(const char *stamp, bool FeedType) bool DownloadFile(LPCTSTR tszURL, LPCTSTR tszLocal) { - NETLIBHTTPREQUEST nlhr = { 0 }; + MHttpRequest nlhr; nlhr.requestType = REQUEST_GET; nlhr.flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11; - char *szUrl = mir_u2a(tszURL); - nlhr.szUrl = szUrl; - NETLIBHTTPHEADER headers[4]; - nlhr.headersCount = 4; - nlhr.headers = headers; - nlhr.headers[0].szName = "User-Agent"; - nlhr.headers[0].szValue = NETLIB_USER_AGENT; - nlhr.headers[1].szName = "Connection"; - nlhr.headers[1].szValue = "close"; - nlhr.headers[2].szName = "Cache-Control"; - nlhr.headers[2].szValue = "no-cache"; - nlhr.headers[3].szName = "Pragma"; - nlhr.headers[3].szValue = "no-cache"; - - bool ret = false; + nlhr.m_szUrl = _T2A(tszURL); + nlhr.AddHeader("User-Agent", NETLIB_USER_AGENT); + nlhr.AddHeader("Connection", "close"); + nlhr.AddHeader("Cache-Control", "no-cache"); + nlhr.AddHeader("Pragma", "no-cache"); + NLHR_PTR pReply(Netlib_HttpTransaction(hNetlibUser, &nlhr)); if (pReply) { - if ((200 == pReply->resultCode) && (pReply->dataLength > 0)) { - char *date = nullptr, *size = nullptr; - for (int i = 0; i < pReply->headersCount; i++) { - if (!mir_strcmpi(pReply->headers[i].szName, "Last-Modified")) { - date = pReply->headers[i].szValue; - continue; - } - else if (!mir_strcmpi(pReply->headers[i].szName, "Content-Length")) { - size = pReply->headers[i].szValue; - continue; - } - } + if (200 == pReply->resultCode && !pReply->body.IsEmpty()) { + const char *date = pReply->FindHeader("Last-Modified"); + const char *size = pReply->FindHeader("Content-Length"); if (date != nullptr && size != nullptr) { - wchar_t *tsize = mir_a2u(size); struct _stat buf; - int fh = _wopen(tszLocal, _O_RDONLY); if (fh != -1) { _fstat(fh, &buf); + _close(fh); + time_t modtime = DateToUnixTime(date, 0); time_t filemodtime = mktime(localtime(&buf.st_atime)); - if (modtime > filemodtime && buf.st_size != _wtoi(tsize)) { - DWORD dwBytes; - HANDLE hFile = CreateFile(tszLocal, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); - WriteFile(hFile, pReply->pData, (uint32_t)pReply->dataLength, &dwBytes, nullptr); - ret = true; - if (hFile) - CloseHandle(hFile); - } - _close(fh); + if (modtime <= filemodtime || buf.st_size == atoi(size)) + return false; } - else { - DWORD dwBytes; - HANDLE hFile = CreateFile(tszLocal, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); - WriteFile(hFile, pReply->pData, (uint32_t)pReply->dataLength, &dwBytes, nullptr); - ret = true; - if (hFile) - CloseHandle(hFile); - } - mir_free(tsize); - } - else { - DWORD dwBytes; - HANDLE hFile = CreateFile(tszLocal, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); - WriteFile(hFile, pReply->pData, (uint32_t)pReply->dataLength, &dwBytes, nullptr); - ret = true; - if (hFile) - CloseHandle(hFile); } + + DWORD dwBytes; + HANDLE hFile = CreateFile(tszLocal, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + WriteFile(hFile, pReply->body, pReply->body.GetLength(), &dwBytes, nullptr); + if (hFile) + CloseHandle(hFile); } } - - mir_free(szUrl); - - return ret; + return true; } typedef HRESULT(MarkupCallback)(IHTMLDocument3 *, BSTR &message); diff --git a/protocols/Non-IM Contact/src/http.cpp b/protocols/Non-IM Contact/src/http.cpp index 70804521b4..5aba5bbd8d 100644 --- a/protocols/Non-IM Contact/src/http.cpp +++ b/protocols/Non-IM Contact/src/http.cpp @@ -35,16 +35,13 @@ HNETLIBUSER hNetlibUser; int InternetDownloadFile(char *szUrl) { // initialize the netlib request - NETLIBHTTPREQUEST nlhr = {}; + MHttpRequest nlhr; nlhr.requestType = REQUEST_GET; nlhr.flags = NLHRF_DUMPASTEXT; - nlhr.szUrl = szUrl; + nlhr.m_szUrl = szUrl; // change the header so the plugin is pretended to be IE 6 + WinXP - nlhr.headersCount++; - nlhr.headers = (NETLIBHTTPHEADER*)malloc(sizeof(NETLIBHTTPHEADER)*nlhr.headersCount); - nlhr.headers[nlhr.headersCount - 1].szName = "User-Agent"; - nlhr.headers[nlhr.headersCount - 1].szValue = NETLIB_USER_AGENT; + nlhr.AddHeader("User-Agent", NETLIB_USER_AGENT); // download the page NLHR_PTR nlhrReply(Netlib_HttpTransaction(hNetlibUser, &nlhr)); @@ -57,14 +54,14 @@ int InternetDownloadFile(char *szUrl) // if the recieved code is 200 OK else if (nlhrReply->resultCode == 200) { // allocate memory and save the retrieved data - szData = (char *)malloc(mir_strlen(nlhrReply->pData) + 2); - mir_strncpy(szData, nlhrReply->pData, mir_strlen(nlhrReply->pData)); + szData = (char *)malloc(nlhrReply->body.GetLength() + 2); + mir_strncpy(szData, nlhrReply->body, nlhrReply->body.GetLength()); } // if the recieved code is 302 Moved, Found, etc else if (nlhrReply->resultCode == 302) { // page moved // get the url for the new location and save it to szInfo // look for the reply header "Location" - if (auto *pszHdr = Netlib_GetHeader(nlhrReply, "Location")) { + if (auto *pszHdr = nlhrReply->FindHeader("Location")) { szData = (char *)malloc(512); // add "Moved/Location:" in front of the new URL for identification mir_snprintf(szData, 512, "Moved/Location: %s\n", pszHdr); diff --git a/protocols/SkypeWeb/src/request_queue.cpp b/protocols/SkypeWeb/src/request_queue.cpp index d6240e7cee..9dd5ecd71e 100644 --- a/protocols/SkypeWeb/src/request_queue.cpp +++ b/protocols/SkypeWeb/src/request_queue.cpp @@ -73,7 +73,7 @@ void CSkypeProto::PushRequest(AsyncHttpRequest *request) ///////////////////////////////////////////////////////////////////////////////////////// -NETLIBHTTPREQUEST* CSkypeProto::DoSend(AsyncHttpRequest *pReq) +MHttpResponse* CSkypeProto::DoSend(AsyncHttpRequest *pReq) { if (pReq->m_host != HOST_OTHER) pReq->m_szUrl.Insert(0, ((pReq->flags & NLHRF_SSL) ? "https://" : "http://")); @@ -88,17 +88,12 @@ NETLIBHTTPREQUEST* CSkypeProto::DoSend(AsyncHttpRequest *pReq) case REQUEST_PUT: case REQUEST_POST: - if (Netlib_GetHeader(pReq, "Content-Type") == nullptr) { + if (!pReq->FindHeader("Content-Type")) { if (pReq->m_szParam[0] == '[' || pReq->m_szParam[0] == '{') pReq->AddHeader("Content-Type", "application/json; charset=UTF-8"); else pReq->AddHeader("Content-Type", "application/x-www-form-urlencoded"); } - __fallthrough; - - default: - pReq->pData = pReq->m_szParam.Detach(); - pReq->dataLength = (int)mir_strlen(pReq->pData); } } @@ -126,8 +121,8 @@ NETLIBHTTPREQUEST* CSkypeProto::DoSend(AsyncHttpRequest *pReq) break; } - pReq->szUrl = pReq->m_szUrl.GetBuffer(); - debugLogA("Send request to %s", pReq->szUrl); + pReq->m_szUrl = pReq->m_szUrl.GetBuffer(); + debugLogA("Send request to %s", pReq->m_szUrl.c_str()); return Netlib_HttpTransaction(m_hNetlibUser, pReq); } diff --git a/protocols/SkypeWeb/src/requests/avatars.h b/protocols/SkypeWeb/src/requests/avatars.h index cdf5ac7b6e..aeffbf8ab0 100644 --- a/protocols/SkypeWeb/src/requests/avatars.h +++ b/protocols/SkypeWeb/src/requests/avatars.h @@ -30,21 +30,15 @@ struct GetAvatarRequest : public AsyncHttpRequest struct SetAvatarRequest : public AsyncHttpRequest { - SetAvatarRequest(const uint8_t *data, size_t dataSize, const char *szMime, CSkypeProto *ppro) : + SetAvatarRequest(const uint8_t *data, int dataSize, const char *szMime, CSkypeProto *ppro) : AsyncHttpRequest(REQUEST_PUT, HOST_API, 0, &CSkypeProto::OnSentAvatar) { m_szUrl.AppendFormat("/users/%s/profile/avatar", ppro->m_szSkypename.MakeLower().c_str()); AddHeader("Content-Type", szMime); - pData = (char *)mir_alloc(dataSize); - memcpy(pData, data, dataSize); - dataLength = (int)dataSize; - } - - ~SetAvatarRequest() - { - mir_free(pData); + m_szParam.Truncate(dataSize); + memcpy(m_szParam.GetBuffer(), data, dataSize); } }; diff --git a/protocols/SkypeWeb/src/requests/files.h b/protocols/SkypeWeb/src/requests/files.h index 72c47b4788..d47b3637f3 100644 --- a/protocols/SkypeWeb/src/requests/files.h +++ b/protocols/SkypeWeb/src/requests/files.h @@ -28,7 +28,7 @@ struct ASMObjectCreateRequest : public AsyncHttpRequest struct ASMObjectUploadRequest : public AsyncHttpRequest { - ASMObjectUploadRequest(CSkypeProto *ppro, const char *szObject, const uint8_t *data, const size_t size, CFileUploadParam *fup) : + ASMObjectUploadRequest(CSkypeProto *ppro, const char *szObject, const uint8_t *data, int size, CFileUploadParam *fup) : AsyncHttpRequest(REQUEST_PUT, HOST_OTHER, 0, &CSkypeProto::OnASMObjectUploaded) { m_szUrl.AppendFormat("https://api.asm.skype.com/v1/objects/%s/content/original", szObject); @@ -37,13 +37,7 @@ struct ASMObjectUploadRequest : public AsyncHttpRequest AddHeader("Authorization", CMStringA(FORMAT, "skype_token %s", ppro->m_szApiToken.get())); AddHeader("Content-Type", "application/octet-stream"); - pData = (char*)mir_alloc(size); - memcpy(pData, data, size); - dataLength = (int)size; - } - - ~ASMObjectUploadRequest() - { - mir_free(pData); + m_szParam.Truncate(size); + memcpy(m_szParam.GetBuffer(), data, size); } }; diff --git a/protocols/SkypeWeb/src/skype_avatars.cpp b/protocols/SkypeWeb/src/skype_avatars.cpp index 94d097c852..50c1207803 100644 --- a/protocols/SkypeWeb/src/skype_avatars.cpp +++ b/protocols/SkypeWeb/src/skype_avatars.cpp @@ -52,9 +52,9 @@ void CSkypeProto::ReloadAvatarInfo(MCONTACT hContact) SvcGetAvatarInfo(0, (LPARAM)&ai); } -void CSkypeProto::OnReceiveAvatar(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest) +void CSkypeProto::OnReceiveAvatar(MHttpResponse *response, AsyncHttpRequest *pRequest) { - if (response == nullptr || response->pData == nullptr) + if (response == nullptr || response->body.IsEmpty()) return; MCONTACT hContact = (DWORD_PTR)pRequest->pUserInfo; @@ -62,7 +62,7 @@ void CSkypeProto::OnReceiveAvatar(NETLIBHTTPREQUEST *response, AsyncHttpRequest return; PROTO_AVATAR_INFORMATION ai = { 0 }; - ai.format = ProtoGetBufferFormat(response->pData); + ai.format = ProtoGetBufferFormat(response->body); setByte(hContact, "AvatarType", ai.format); GetAvatarFileName(hContact, ai.filename, _countof(ai.filename)); @@ -72,13 +72,13 @@ void CSkypeProto::OnReceiveAvatar(NETLIBHTTPREQUEST *response, AsyncHttpRequest return; } - fwrite(response->pData, 1, response->dataLength, out); + fwrite(response->body, 1, response->body.GetLength(), out); fclose(out); setByte(hContact, "NeedNewAvatar", 0); ProtoBroadcastAck(hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, &ai, 0); } -void CSkypeProto::OnSentAvatar(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnSentAvatar(MHttpResponse *response, AsyncHttpRequest*) { JsonReply root(response); if (root.error()) @@ -175,7 +175,7 @@ INT_PTR CSkypeProto::SvcSetMyAvatar(WPARAM, LPARAM lParam) if (data != NULL && fread(data, sizeof(uint8_t), length, hFile) == length) { const char *szMime = FreeImage_GetFIFMimeType(FreeImage_GetFIFFromFilenameU(path)); - PushRequest(new SetAvatarRequest(data, length, szMime, this)); + PushRequest(new SetAvatarRequest(data, (int)length, szMime, this)); fclose(hFile); return 0; } diff --git a/protocols/SkypeWeb/src/skype_chatrooms.cpp b/protocols/SkypeWeb/src/skype_chatrooms.cpp index 2d1fc6c3b8..243257353e 100644 --- a/protocols/SkypeWeb/src/skype_chatrooms.cpp +++ b/protocols/SkypeWeb/src/skype_chatrooms.cpp @@ -51,7 +51,7 @@ SESSION_INFO* CSkypeProto::StartChatRoom(const wchar_t *tid, const wchar_t *tnam return si; } -void CSkypeProto::OnLoadChats(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnLoadChats(MHttpResponse *response, AsyncHttpRequest*) { JsonReply reply(response); if (reply.error()) @@ -380,7 +380,7 @@ void CSkypeProto::AddMessageToChat(SESSION_INFO *si, const wchar_t *from, const Chat_Event(&gce); } -void CSkypeProto::OnGetChatInfo(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnGetChatInfo(MHttpResponse *response, AsyncHttpRequest*) { JsonReply reply(response); if (reply.error()) diff --git a/protocols/SkypeWeb/src/skype_contacts.cpp b/protocols/SkypeWeb/src/skype_contacts.cpp index c249c6e281..f5f83e6fa9 100644 --- a/protocols/SkypeWeb/src/skype_contacts.cpp +++ b/protocols/SkypeWeb/src/skype_contacts.cpp @@ -100,7 +100,7 @@ MCONTACT CSkypeProto::AddContact(const char *skypeId, const char *nick, bool isT return hContact; } -void CSkypeProto::LoadContactsAuth(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::LoadContactsAuth(MHttpResponse *response, AsyncHttpRequest*) { JsonReply reply(response); if (reply.error()) @@ -142,7 +142,7 @@ void CSkypeProto::LoadContactsAuth(NETLIBHTTPREQUEST *response, AsyncHttpRequest //[{"skypeId":"echo123", "authorized" : true, "blocked" : false, ...},...] // other properties is exists but empty -void CSkypeProto::LoadContactList(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::LoadContactList(MHttpResponse *response, AsyncHttpRequest*) { JsonReply reply(response); if (reply.error()) @@ -255,7 +255,7 @@ INT_PTR CSkypeProto::BlockContact(WPARAM hContact, LPARAM) return 0; } -void CSkypeProto::OnBlockContact(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest) +void CSkypeProto::OnBlockContact(MHttpResponse *response, AsyncHttpRequest *pRequest) { MCONTACT hContact = (DWORD_PTR)pRequest->pUserInfo; if (response != nullptr) @@ -268,7 +268,7 @@ INT_PTR CSkypeProto::UnblockContact(WPARAM hContact, LPARAM) return 0; } -void CSkypeProto::OnUnblockContact(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest) +void CSkypeProto::OnUnblockContact(MHttpResponse *response, AsyncHttpRequest *pRequest) { if (response == nullptr) return; diff --git a/protocols/SkypeWeb/src/skype_files.cpp b/protocols/SkypeWeb/src/skype_files.cpp index 358a337e90..9eaef8b665 100644 --- a/protocols/SkypeWeb/src/skype_files.cpp +++ b/protocols/SkypeWeb/src/skype_files.cpp @@ -28,10 +28,10 @@ void CSkypeProto::SendFileThread(void *p) PushRequest(new ASMObjectCreateRequest(this, fup)); } -void CSkypeProto::OnASMObjectCreated(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest) +void CSkypeProto::OnASMObjectCreated(MHttpResponse *response, AsyncHttpRequest *pRequest) { auto *fup = (CFileUploadParam*)pRequest->pUserInfo; - if (response == nullptr || response->pData == nullptr) { + if (response == nullptr || response->body.IsEmpty()) { LBL_Error: FILETRANSFER_FAILED(fup); return; @@ -42,7 +42,7 @@ LBL_Error: goto LBL_Error; } - JSONNode node = JSONNode::parse((char*)response->pData); + JSONNode node = JSONNode::parse(response->body); std::string strObjectId = node["id"].as_string(); if (strObjectId.empty()) { debugLogA("Invalid server response (empty object id)"); @@ -74,7 +74,7 @@ LBL_Error: fclose(pFile); } -void CSkypeProto::OnASMObjectUploaded(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest) +void CSkypeProto::OnASMObjectUploaded(MHttpResponse *response, AsyncHttpRequest *pRequest) { auto *fup = (CFileUploadParam*)pRequest->pUserInfo; if (response == nullptr) { diff --git a/protocols/SkypeWeb/src/skype_history_sync.cpp b/protocols/SkypeWeb/src/skype_history_sync.cpp index b10897b0d3..7497d8f7a1 100644 --- a/protocols/SkypeWeb/src/skype_history_sync.cpp +++ b/protocols/SkypeWeb/src/skype_history_sync.cpp @@ -19,7 +19,7 @@ along with this program. If not, see . /* HISTORY SYNC */ -void CSkypeProto::OnGetServerHistory(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest) +void CSkypeProto::OnGetServerHistory(MHttpResponse *response, AsyncHttpRequest *pRequest) { JsonReply reply(response); if (reply.error()) @@ -140,7 +140,7 @@ INT_PTR CSkypeProto::GetContactHistory(WPARAM hContact, LPARAM) return 0; } -void CSkypeProto::OnSyncHistory(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnSyncHistory(MHttpResponse *response, AsyncHttpRequest*) { JsonReply reply(response); if (reply.error()) diff --git a/protocols/SkypeWeb/src/skype_login.cpp b/protocols/SkypeWeb/src/skype_login.cpp index 76977ab6cd..f9f2ecab57 100644 --- a/protocols/SkypeWeb/src/skype_login.cpp +++ b/protocols/SkypeWeb/src/skype_login.cpp @@ -66,18 +66,18 @@ void CSkypeProto::Login() PushRequest(new OAuthRequest()); } -void CSkypeProto::OnLoginOAuth(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnLoginOAuth(MHttpResponse *response, AsyncHttpRequest*) { if (!IsStatusConnecting(m_iStatus)) return; - if (response == nullptr || response->pData == nullptr) { + if (response == nullptr || response->body.IsEmpty()) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; } - JSONNode json = JSONNode::parse(response->pData); + JSONNode json = JSONNode::parse(response->body); if (!json) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); @@ -148,7 +148,7 @@ void CSkypeProto::OnLoginSuccess() PushRequest(new CreateEndpointRequest(this)); } -void CSkypeProto::OnEndpointCreated(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnEndpointCreated(MHttpResponse *response, AsyncHttpRequest*) { if (IsStatusConnecting(m_iStatus)) m_iStatus++; @@ -167,7 +167,7 @@ void CSkypeProto::OnEndpointCreated(NETLIBHTTPREQUEST *response, AsyncHttpReques case 301: case 302: // redirect to the closest data center - if (auto *hdr = Netlib_GetHeader(response, "Location")) { + if (auto *hdr = response->FindHeader("Location")) { CMStringA szUrl(hdr+8); int iEnd = szUrl.Find('/'); g_plugin.szDefaultServer = (iEnd != -1) ? szUrl.Left(iEnd) : szUrl; @@ -176,7 +176,7 @@ void CSkypeProto::OnEndpointCreated(NETLIBHTTPREQUEST *response, AsyncHttpReques return; case 401: // unauthorized - if (auto *szStatus = Netlib_GetHeader(response, "StatusText")) + if (auto *szStatus = response->FindHeader("StatusText")) if (strstr(szStatus, "SkypeTokenExpired")) delSetting("TokenSecret"); delSetting("TokenExpiresIn"); @@ -191,7 +191,7 @@ void CSkypeProto::OnEndpointCreated(NETLIBHTTPREQUEST *response, AsyncHttpReques } // Succeeded, decode the answer - if (auto *hdr = Netlib_GetHeader(response, "Set-RegistrationToken")) { + if (auto *hdr = response->FindHeader("Set-RegistrationToken")) { CMStringA szValue = hdr; int iStart = 0; while (true) { @@ -215,13 +215,13 @@ void CSkypeProto::OnEndpointCreated(NETLIBHTTPREQUEST *response, AsyncHttpReques PushRequest(new CreateSubscriptionsRequest()); } -void CSkypeProto::OnEndpointDeleted(NETLIBHTTPREQUEST *, AsyncHttpRequest *) +void CSkypeProto::OnEndpointDeleted(MHttpResponse *, AsyncHttpRequest *) { m_szId = nullptr; m_szToken = nullptr; } -void CSkypeProto::OnSubscriptionsCreated(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnSubscriptionsCreated(MHttpResponse *response, AsyncHttpRequest*) { if (response == nullptr) { debugLogA(__FUNCTION__ ": failed to create subscription"); @@ -249,9 +249,9 @@ void CSkypeProto::SendPresence() PushRequest(new SendCapabilitiesRequest(epname, this)); } -void CSkypeProto::OnCapabilitiesSended(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnCapabilitiesSended(MHttpResponse *response, AsyncHttpRequest*) { - if (response == nullptr || response->pData == nullptr) { + if (response == nullptr || response->body.IsEmpty()) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; @@ -277,23 +277,23 @@ void CSkypeProto::OnCapabilitiesSended(NETLIBHTTPREQUEST *response, AsyncHttpReq if (bAutoHistorySync) PushRequest(new SyncHistoryFirstRequest(100)); - JSONNode root = JSONNode::parse(response->pData); + JSONNode root = JSONNode::parse(response->body); if (root) setString("SelfEndpointName", UrlToSkypeId(root["selfLink"].as_string().c_str())); PushRequest(new GetProfileRequest(this, 0)); } -void CSkypeProto::OnStatusChanged(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnStatusChanged(MHttpResponse *response, AsyncHttpRequest*) { - if (response == nullptr || response->pData == nullptr) { + if (response == nullptr || response->body.IsEmpty()) { debugLogA(__FUNCTION__ ": failed to change status"); ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; } - JSONNode json = JSONNode::parse(response->pData); + JSONNode json = JSONNode::parse(response->body); if (!json) { debugLogA(__FUNCTION__ ": failed to change status"); ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); diff --git a/protocols/SkypeWeb/src/skype_messages.cpp b/protocols/SkypeWeb/src/skype_messages.cpp index 16d9b2cb03..6504224c21 100644 --- a/protocols/SkypeWeb/src/skype_messages.cpp +++ b/protocols/SkypeWeb/src/skype_messages.cpp @@ -45,7 +45,7 @@ int CSkypeProto::SendMsg(MCONTACT hContact, MEVENT, const char *szMessage) return param->hMessage; } -void CSkypeProto::OnMessageSent(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest) +void CSkypeProto::OnMessageSent(MHttpResponse *response, AsyncHttpRequest *pRequest) { auto *param = (SendMessageParam*)pRequest->pUserInfo; MCONTACT hContact = param->hContact; @@ -56,8 +56,8 @@ void CSkypeProto::OnMessageSent(NETLIBHTTPREQUEST *response, AsyncHttpRequest *p if (response->resultCode != 201) { std::string strError = Translate("Unknown error!"); - if (response->pData != nullptr) { - JSONNode jRoot = JSONNode::parse(response->pData); + if (!response->body.IsEmpty()) { + JSONNode jRoot = JSONNode::parse(response->body); const JSONNode &jErr = jRoot["errorCode"]; if (jErr) strError = jErr.as_string(); diff --git a/protocols/SkypeWeb/src/skype_oauth.cpp b/protocols/SkypeWeb/src/skype_oauth.cpp index 0a1f8908d3..d767496efa 100644 --- a/protocols/SkypeWeb/src/skype_oauth.cpp +++ b/protocols/SkypeWeb/src/skype_oauth.cpp @@ -28,9 +28,9 @@ static std::string sub(const std::string &str, const char *start, const char *en return (i2 == -1) ? "" : str.substr(i1, i2 - i1); } -void CSkypeProto::OnOAuthStart(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnOAuthStart(MHttpResponse *response, AsyncHttpRequest*) { - if (response == nullptr || response->pData == nullptr) { + if (response == nullptr || response->body.IsEmpty()) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; @@ -38,7 +38,7 @@ void CSkypeProto::OnOAuthStart(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) std::regex regex; std::smatch match; - std::string content = response->pData; + std::string content = response->body.c_str(); regex = ""; @@ -52,11 +52,11 @@ void CSkypeProto::OnOAuthStart(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) std::map scookies; regex = "^(.+?)=(.*?);"; - for (int i = 0; i < response->headersCount; i++) { - if (mir_strcmpi(response->headers[i].szName, "Set-Cookie")) + for (auto &it : *response) { + if (mir_strcmpi(it->szName, "Set-Cookie")) continue; - content = response->headers[i].szValue; + content = it->szValue; if (std::regex_search(content, match, regex)) scookies[match[1]] = match[2]; } @@ -83,18 +83,18 @@ bool CSkypeProto::CheckOauth(const char *szResponse) return true; } -void CSkypeProto::OnOAuthConfirm(NETLIBHTTPREQUEST *response, AsyncHttpRequest *) +void CSkypeProto::OnOAuthConfirm(MHttpResponse *response, AsyncHttpRequest *) { - if (response == nullptr || response->pData == nullptr) { + if (response == nullptr || response->body.IsEmpty()) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; } - if (CheckOauth(response->pData)) + if (CheckOauth(response->body)) return; - std::string content = response->pData; + std::string content = response->body.c_str(); std::string PPFT = sub(content, "sFT:'", "'"); std::string opid = sub(content, "opid=", "&"); if (PPFT.empty() || opid.empty()) { @@ -107,11 +107,11 @@ void CSkypeProto::OnOAuthConfirm(NETLIBHTTPREQUEST *response, AsyncHttpRequest * std::smatch match; CMStringA mscookies; - for (int i = 0; i < response->headersCount; i++) { - if (mir_strcmpi(response->headers[i].szName, "Set-Cookie")) + for (auto &it : *response) { + if (mir_strcmpi(it->szName, "Set-Cookie")) continue; - content = response->headers[i].szValue; + content = it->szValue; if (std::regex_search(content, match, regex)) mscookies.Append(match[1].str().c_str()); } @@ -119,23 +119,23 @@ void CSkypeProto::OnOAuthConfirm(NETLIBHTTPREQUEST *response, AsyncHttpRequest * PushRequest(new OAuthRequest(mscookies.c_str(), PPFT.c_str(), opid.c_str())); } -void CSkypeProto::OnOAuthAuthorize(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnOAuthAuthorize(MHttpResponse *response, AsyncHttpRequest*) { - if (response == nullptr || response->pData == nullptr) { + if (response == nullptr || response->body.IsEmpty()) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; } - if (!CheckOauth(response->pData)) { + if (!CheckOauth(response->body)) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); } } -void CSkypeProto::OnOAuthEnd(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnOAuthEnd(MHttpResponse *response, AsyncHttpRequest*) { - if (response == nullptr || response->pData == nullptr) { + if (response == nullptr || response->body.IsEmpty()) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; @@ -143,7 +143,7 @@ void CSkypeProto::OnOAuthEnd(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) std::regex regex; std::smatch match; - std::string content = response->pData; + std::string content = response->body; regex = ""; if (!std::regex_search(content, match, regex)) { diff --git a/protocols/SkypeWeb/src/skype_polling.cpp b/protocols/SkypeWeb/src/skype_polling.cpp index 0000de3b20..b850cffb23 100644 --- a/protocols/SkypeWeb/src/skype_polling.cpp +++ b/protocols/SkypeWeb/src/skype_polling.cpp @@ -39,14 +39,14 @@ void CSkypeProto::PollingThread(void *) if (response->resultCode == 200) { nErrors = 0; - if (response->pData) - ParsePollData(response->pData); + if (!response->body.IsEmpty()) + ParsePollData(response->body); } else { nErrors++; - if (response->pData) { - JSONNode root = JSONNode::parse(response->pData); + if (!response->body.IsEmpty()) { + JSONNode root = JSONNode::parse(response->body); const JSONNode &error = root["errorCode"]; if (error && error.as_int() == 729) break; diff --git a/protocols/SkypeWeb/src/skype_profile.cpp b/protocols/SkypeWeb/src/skype_profile.cpp index f8fbaa2fbe..16fda7b0c8 100644 --- a/protocols/SkypeWeb/src/skype_profile.cpp +++ b/protocols/SkypeWeb/src/skype_profile.cpp @@ -397,7 +397,7 @@ void CSkypeProto::UpdateProfileAvatar(const JSONNode &root, MCONTACT hContact) } //{"firstname":"Echo \/ Sound Test Service", "lastname" : null, "birthday" : null, "gender" : null, "country" : null, "city" : null, "language" : null, "homepage" : null, "about" : null, "province" : null, "jobtitle" : null, "emails" : [], "phoneMobile" : null, "phoneHome" : null, "phoneOffice" : null, "mood" : null, "richMood" : null, "avatarUrl" : null, "username" : "echo123"} -void CSkypeProto::LoadProfile(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest) +void CSkypeProto::LoadProfile(MHttpResponse *response, AsyncHttpRequest *pRequest) { MCONTACT hContact = (DWORD_PTR)pRequest->pUserInfo; diff --git a/protocols/SkypeWeb/src/skype_proto.cpp b/protocols/SkypeWeb/src/skype_proto.cpp index 59ad914aaa..536744f36f 100644 --- a/protocols/SkypeWeb/src/skype_proto.cpp +++ b/protocols/SkypeWeb/src/skype_proto.cpp @@ -125,7 +125,7 @@ int CSkypeProto::SetAwayMsg(int, const wchar_t *msg) ///////////////////////////////////////////////////////////////////////////////////////// -void CSkypeProto::OnReceiveAwayMsg(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest) +void CSkypeProto::OnReceiveAwayMsg(MHttpResponse *response, AsyncHttpRequest *pRequest) { JsonReply reply(response); if (reply.error()) diff --git a/protocols/SkypeWeb/src/skype_proto.h b/protocols/SkypeWeb/src/skype_proto.h index 5686aad69c..113e22b228 100644 --- a/protocols/SkypeWeb/src/skype_proto.h +++ b/protocols/SkypeWeb/src/skype_proto.h @@ -120,48 +120,48 @@ public: return getMStringA(hContact, SKYPE_SETTINGS_ID); } - void OnReceiveAvatar(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnSentAvatar(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnSearch(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); + void OnReceiveAvatar(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnSentAvatar(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnSearch(MHttpResponse *response, AsyncHttpRequest *pRequest); // login - void OnLoginOAuth(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnSubscriptionsCreated(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnCapabilitiesSended(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnReceiveStatus(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnStatusChanged(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); + void OnLoginOAuth(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnSubscriptionsCreated(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnCapabilitiesSended(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnReceiveStatus(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnStatusChanged(MHttpResponse *response, AsyncHttpRequest *pRequest); - void OnEndpointCreated(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnEndpointDeleted(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); + void OnEndpointCreated(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnEndpointDeleted(MHttpResponse *response, AsyncHttpRequest *pRequest); // oauth - void OnOAuthStart(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnOAuthConfirm(NETLIBHTTPREQUEST* response, AsyncHttpRequest* pRequest); - void OnOAuthAuthorize(NETLIBHTTPREQUEST* response, AsyncHttpRequest* pRequest); - void OnOAuthEnd(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); + void OnOAuthStart(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnOAuthConfirm(MHttpResponse* response, AsyncHttpRequest* pRequest); + void OnOAuthAuthorize(MHttpResponse* response, AsyncHttpRequest* pRequest); + void OnOAuthEnd(MHttpResponse *response, AsyncHttpRequest *pRequest); - void OnASMObjectCreated(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnASMObjectUploaded(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); + void OnASMObjectCreated(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnASMObjectUploaded(MHttpResponse *response, AsyncHttpRequest *pRequest); - void LoadContactsAuth(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void LoadContactList(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); + void LoadContactsAuth(MHttpResponse *response, AsyncHttpRequest *pRequest); + void LoadContactList(MHttpResponse *response, AsyncHttpRequest *pRequest); - void OnBlockContact(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnUnblockContact(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); + void OnBlockContact(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnUnblockContact(MHttpResponse *response, AsyncHttpRequest *pRequest); - void OnMessageSent(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); + void OnMessageSent(MHttpResponse *response, AsyncHttpRequest *pRequest); - void OnGetServerHistory(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnSyncHistory(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); + void OnGetServerHistory(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnSyncHistory(MHttpResponse *response, AsyncHttpRequest *pRequest); - void OnLoadChats(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnGetChatInfo(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); - void OnReceiveAwayMsg(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); + void OnLoadChats(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnGetChatInfo(MHttpResponse *response, AsyncHttpRequest *pRequest); + void OnReceiveAwayMsg(MHttpResponse *response, AsyncHttpRequest *pRequest); void CheckConvert(void); bool CheckOauth(const char *szResponse); - void LoadProfile(NETLIBHTTPREQUEST *response, AsyncHttpRequest *pRequest); + void LoadProfile(MHttpResponse *response, AsyncHttpRequest *pRequest); static INT_PTR __cdecl GlobalParseSkypeUriService(WPARAM, LPARAM lParam); @@ -206,7 +206,7 @@ private: void StartQueue(); void StopQueue(); - NETLIBHTTPREQUEST* DoSend(AsyncHttpRequest *request); + MHttpResponse* DoSend(AsyncHttpRequest *request); void Execute(AsyncHttpRequest *request); void PushRequest(AsyncHttpRequest *request); diff --git a/protocols/SkypeWeb/src/skype_search.cpp b/protocols/SkypeWeb/src/skype_search.cpp index 51a4952021..8dc34dc62c 100644 --- a/protocols/SkypeWeb/src/skype_search.cpp +++ b/protocols/SkypeWeb/src/skype_search.cpp @@ -30,7 +30,7 @@ void CSkypeProto::SearchBasicThread(void *id) PushRequest(new GetSearchRequest(T2Utf((wchar_t *)id))); } -void CSkypeProto::OnSearch(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnSearch(MHttpResponse *response, AsyncHttpRequest*) { debugLogA(__FUNCTION__); diff --git a/protocols/SkypeWeb/src/skype_trouter.cpp b/protocols/SkypeWeb/src/skype_trouter.cpp index 65e38d4d41..567e449da1 100644 --- a/protocols/SkypeWeb/src/skype_trouter.cpp +++ b/protocols/SkypeWeb/src/skype_trouter.cpp @@ -26,7 +26,7 @@ void CSkypeProto::ProcessTimer() SendPresence(); } -void CSkypeProto::OnReceiveStatus(NETLIBHTTPREQUEST *response, AsyncHttpRequest*) +void CSkypeProto::OnReceiveStatus(MHttpResponse *response, AsyncHttpRequest*) { JsonReply reply(response); if (reply.error()) diff --git a/protocols/SkypeWeb/src/skype_utils.cpp b/protocols/SkypeWeb/src/skype_utils.cpp index acd02c6892..e2a751568c 100644 --- a/protocols/SkypeWeb/src/skype_utils.cpp +++ b/protocols/SkypeWeb/src/skype_utils.cpp @@ -653,7 +653,7 @@ INT_PTR CSkypeProto::GlobalParseSkypeUriService(WPARAM wParam, LPARAM lParam) ///////////////////////////////////////////////////////////////////////////////////////// -JsonReply::JsonReply(NETLIBHTTPREQUEST *pReply) +JsonReply::JsonReply(MHttpResponse *pReply) { if (pReply == nullptr) { m_errorCode = 500; @@ -664,7 +664,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; diff --git a/protocols/SkypeWeb/src/skype_utils.h b/protocols/SkypeWeb/src/skype_utils.h index e8a1f05836..4162a8ec35 100644 --- a/protocols/SkypeWeb/src/skype_utils.h +++ b/protocols/SkypeWeb/src/skype_utils.h @@ -59,7 +59,7 @@ class JsonReply int m_errorCode = 0; public: - JsonReply(NETLIBHTTPREQUEST *response); + JsonReply(MHttpResponse *response); ~JsonReply(); __forceinline JSONNode &data() const { return *m_root; } diff --git a/protocols/Steam/src/http_request.h b/protocols/Steam/src/http_request.h index 4c7b83509d..843ed94cda 100644 --- a/protocols/Steam/src/http_request.h +++ b/protocols/Steam/src/http_request.h @@ -5,10 +5,10 @@ class HttpResponse { - NETLIBHTTPREQUEST *m_response; + MHttpResponse *m_response; public: - HttpResponse(NETLIBHTTPREQUEST *response) : + HttpResponse(MHttpResponse *response) : m_response(response) { } @@ -20,12 +20,12 @@ public: bool operator!() const { - return !m_response || !m_response->pData; + return !m_response || m_response->body.IsEmpty(); } operator bool() const { - return m_response && m_response->pData; + return m_response && !m_response->body.IsEmpty(); } bool IsSuccess() const @@ -37,16 +37,14 @@ public: char* data() const { - return (m_response) ? m_response->pData : nullptr; + return (m_response) ? m_response->body.GetBuffer() : nullptr; } unsigned length() const { - return (m_response) ? m_response->dataLength : 0; + return (m_response) ? m_response->body.GetLength() : 0; } - LIST Headers() const; - int GetStatusCode() const { if (m_response) diff --git a/protocols/Steam/src/steam_proto.h b/protocols/Steam/src/steam_proto.h index 234ae44e88..ed7978e890 100644 --- a/protocols/Steam/src/steam_proto.h +++ b/protocols/Steam/src/steam_proto.h @@ -45,7 +45,7 @@ struct HttpRequest : public MTHttpRequest { HttpRequest(int iRequestType, const char *pszUrl); - NETLIBHTTPREQUEST* Get(); + MHttpRequest* Get(); }; struct ProtoRequest diff --git a/protocols/Steam/src/steam_request.cpp b/protocols/Steam/src/steam_request.cpp index b91283198a..918e01ca16 100644 --- a/protocols/Steam/src/steam_request.cpp +++ b/protocols/Steam/src/steam_request.cpp @@ -2,7 +2,7 @@ bool CSteamProto::SendRequest(HttpRequest *request) { - NETLIBHTTPREQUEST *pResp = Netlib_HttpTransaction(m_hNetlibUser, request->Get()); + auto *pResp = Netlib_HttpTransaction(m_hNetlibUser, request->Get()); HttpResponse response(pResp); delete request; return response.IsSuccess(); @@ -10,7 +10,7 @@ bool CSteamProto::SendRequest(HttpRequest *request) bool CSteamProto::SendRequest(HttpRequest *request, HttpCallback callback, void *param) { - NETLIBHTTPREQUEST *pResp = Netlib_HttpTransaction(m_hNetlibUser, request->Get()); + auto *pResp = Netlib_HttpTransaction(m_hNetlibUser, request->Get()); HttpResponse response(pResp); if (callback) (this->*callback)(response, param); @@ -20,7 +20,7 @@ bool CSteamProto::SendRequest(HttpRequest *request, HttpCallback callback, void bool CSteamProto::SendRequest(HttpRequest *request, JsonCallback callback, void *param) { - NETLIBHTTPREQUEST *pResp = Netlib_HttpTransaction(m_hNetlibUser, request->Get()); + auto *pResp = Netlib_HttpTransaction(m_hNetlibUser, request->Get()); HttpResponse response(pResp); if (callback) { JSONNode root = JSONNode::parse(response.data()); @@ -42,26 +42,11 @@ HttpRequest::HttpRequest(int iRequestType, const char *szUrl) m_szUrl = szUrl; } -NETLIBHTTPREQUEST* HttpRequest::Get() +MHttpRequest* HttpRequest::Get() { if (m_szUrl[0]== '/') { m_szUrl.Insert(0, STEAM_API_URL); AddHeader("User-Agent", STEAM_USER_AGENT); } - szUrl = m_szUrl.GetBuffer(); return this; } - -///////////////////////////////////////////////////////////////////////////////////////// -// HttpResponse - -LIST HttpResponse::Headers() const -{ - LIST ret(10); - - if (m_response) - for (int i = 0; i < m_response->headersCount; i++) - ret.insert(&m_response->headers[i]); - - return ret; -} diff --git a/protocols/Tox/src/http_request.h b/protocols/Tox/src/http_request.h index bba9f0591c..1e140b2ef0 100644 --- a/protocols/Tox/src/http_request.h +++ b/protocols/Tox/src/http_request.h @@ -17,22 +17,12 @@ public: } }; -class HttpRequest : protected NETLIBHTTPREQUEST +class HttpRequest : public MHttpRequest { -private: - CMStringA m_szUrl; - void Init(int type) { requestType = type; flags = NLHRF_HTTP11 | NLHRF_SSL | NLHRF_NODUMPSEND | NLHRF_DUMPASTEXT; - szUrl = nullptr; - headers = nullptr; - headersCount = 0; - pData = nullptr; - dataLength = 0; - resultCode = 0; - szResultDescr = nullptr; nlc = nullptr; timeout = 0; } @@ -60,22 +50,6 @@ public: ~HttpRequest() { - for (int i = 0; i < headersCount; i++) - { - mir_free(headers[i].szName); - mir_free(headers[i].szValue); - } - mir_free(headers); - if (pData) - mir_free(pData); - } - - void AddHeader(LPCSTR szName, LPCSTR szValue) - { - headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER) * (headersCount + 1)); - headers[headersCount].szName = mir_strdup(szName); - headers[headersCount].szValue = mir_strdup(szValue); - headersCount++; } void AddUrlParameter(const char *urlFormat, ...) @@ -87,20 +61,9 @@ public: va_end(urlArgs); } - void SetData(const char *data, size_t size) - { - if (pData != nullptr) - mir_free(pData); - - dataLength = (int)size; - pData = (char*)mir_alloc(size); - memcpy(pData, data, size); - } - - NETLIBHTTPREQUEST* Send(HNETLIBUSER hNetlibConnection) + MHttpResponse* Send(HNETLIBUSER hNetlibConnection) { m_szUrl.Replace('\\', '/'); - szUrl = m_szUrl.GetBuffer(); return Netlib_HttpTransaction(hNetlibConnection, this); } }; diff --git a/protocols/Tox/src/tox_bootstrap.cpp b/protocols/Tox/src/tox_bootstrap.cpp index 663e46a8ab..6e8a5d774e 100644 --- a/protocols/Tox/src/tox_bootstrap.cpp +++ b/protocols/Tox/src/tox_bootstrap.cpp @@ -121,12 +121,12 @@ void CToxProto::UpdateNodes() debugLogA(__FUNCTION__": updating nodes"); HttpRequest request(REQUEST_GET, "https://nodes.tox.chat/json"); NLHR_PTR response(request.Send(m_hNetlibUser)); - if (!response || response->resultCode != HTTP_CODE_OK || !response->pData) { + if (!response || response->resultCode != HTTP_CODE_OK || response->body.IsEmpty()) { debugLogA(__FUNCTION__": failed to dowload tox.json"); return; } - JSONNode root = JSONNode::parse(response->pData); + JSONNode root = JSONNode::parse(response->body); if (root.empty()) { debugLogA(__FUNCTION__": failed to dowload tox.json"); return; @@ -142,7 +142,7 @@ void CToxProto::UpdateNodes() return; } - if (fwrite(response->pData, sizeof(char), response->dataLength, hFile) != (size_t)response->dataLength) + if (fwrite(response->body, sizeof(char), response->body.GetLength(), hFile) != (size_t)response->body.GetLength()) debugLogA(__FUNCTION__": failed to write tox.json"); fclose(hFile); diff --git a/protocols/Twitter/src/http.cpp b/protocols/Twitter/src/http.cpp index 888afdfa84..71aa265954 100644 --- a/protocols/Twitter/src/http.cpp +++ b/protocols/Twitter/src/http.cpp @@ -39,9 +39,6 @@ void CTwitterProto::Execute(AsyncHttpRequest *pReq) } else pReq->AddHeader("Content-Type", "application/x-www-form-urlencoded"); pReq->AddHeader("Cache-Control", "no-cache"); - - pReq->dataLength = (int)pReq->m_szParam.GetLength(); - pReq->pData = pReq->m_szParam.Detach(); } else { if (pReq->requestType == REQUEST_PATCH) @@ -60,7 +57,6 @@ void CTwitterProto::Execute(AsyncHttpRequest *pReq) // auth = OAuthWebRequestSubmit(pReq->m_szUrl, "POST", (bIsJson) ? "" : pReq->pData); // pReq->AddHeader("Authorization", auth); - pReq->szUrl = pReq->m_szUrl.GetBuffer(); pReq->flags = NLHRF_HTTP11 | NLHRF_PERSISTENT | NLHRF_REDIRECT; pReq->nlc = m_hConnHttp; diff --git a/protocols/Twitter/src/oauth.cpp b/protocols/Twitter/src/oauth.cpp index 9ef2c9bf7c..6e919d3b47 100644 --- a/protocols/Twitter/src/oauth.cpp +++ b/protocols/Twitter/src/oauth.cpp @@ -163,7 +163,7 @@ CMStringA CTwitterProto::OAuthCreateSignature(const CMStringA &signatureBase, co ///////////////////////////////////////////////////////////////////////////////////////// -void CTwitterProto::Oauth2RequestToken(NETLIBHTTPREQUEST *pResp, AsyncHttpRequest *) +void CTwitterProto::Oauth2RequestToken(MHttpResponse *pResp, AsyncHttpRequest *) { if (pResp->resultCode != 200) { OnLoggedFail(); @@ -196,7 +196,7 @@ void CTwitterProto::RequestOauthToken(const char *szPin) ///////////////////////////////////////////////////////////////////////////////////////// -void CTwitterProto::Oauth2RequestAuth(NETLIBHTTPREQUEST *pResp, AsyncHttpRequest *) +void CTwitterProto::Oauth2RequestAuth(MHttpResponse *pResp, AsyncHttpRequest *) { if (pResp->resultCode != 200) { OnLoggedFail(); diff --git a/protocols/Twitter/src/proto.h b/protocols/Twitter/src/proto.h index e3e5650b4d..fd09b49147 100644 --- a/protocols/Twitter/src/proto.h +++ b/protocols/Twitter/src/proto.h @@ -119,8 +119,8 @@ class CTwitterProto : public PROTO void RequestOauthToken(const char *szPin = nullptr); void ResetOauthKeys(); - void Oauth2RequestAuth(NETLIBHTTPREQUEST *, AsyncHttpRequest *); - void Oauth2RequestToken(NETLIBHTTPREQUEST *, AsyncHttpRequest *); + void Oauth2RequestAuth(MHttpResponse *, AsyncHttpRequest *); + void Oauth2RequestToken(MHttpResponse *, AsyncHttpRequest *); CMStringA OAuthWebRequestSubmit(const CMStringA &url, const char *httpMethod, const char *postData); diff --git a/protocols/Twitter/src/utility.cpp b/protocols/Twitter/src/utility.cpp index d9031b51ce..737278bf0a 100644 --- a/protocols/Twitter/src/utility.cpp +++ b/protocols/Twitter/src/utility.cpp @@ -71,10 +71,10 @@ void CTwitterProto::ShowPopup(const char *text, int Error) bool save_url(HNETLIBUSER hNetlib, const CMStringA &url, const CMStringW &filename) { - NETLIBHTTPREQUEST req = { sizeof(req) }; + MHttpRequest req; req.requestType = REQUEST_GET; req.flags = NLHRF_HTTP11 | NLHRF_REDIRECT; - req.szUrl = const_cast(url.c_str()); + req.m_szUrl = const_cast(url.c_str()); NLHR_PTR resp(Netlib_HttpTransaction(hNetlib, &req)); if (!resp) @@ -89,7 +89,7 @@ bool save_url(HNETLIBUSER hNetlib, const CMStringA &url, const CMStringW &filena // Write to file if (FILE *f = _wfopen(filename, L"wb")) { - fwrite(resp->pData, 1, resp->dataLength, f); + fwrite(resp->body, 1, resp->body.GetLength(), f); fclose(f); } else return false; diff --git a/protocols/VKontakte/src/misc.cpp b/protocols/VKontakte/src/misc.cpp index 4378651b6f..01e6046a43 100644 --- a/protocols/VKontakte/src/misc.cpp +++ b/protocols/VKontakte/src/misc.cpp @@ -315,14 +315,14 @@ bool CVkProto::IsGroupUser(MCONTACT hContact) ///////////////////////////////////////////////////////////////////////////////////////// -JSONNode& CVkProto::CheckJsonResponse(AsyncHttpRequest *pReq, NETLIBHTTPREQUEST *reply, JSONNode &root) +JSONNode& CVkProto::CheckJsonResponse(AsyncHttpRequest *pReq, MHttpResponse *reply, JSONNode &root) { debugLogA("CVkProto::CheckJsonResponse"); - if (!reply || !reply->pData) + if (!reply || reply->body.IsEmpty()) return nullNode; - root = JSONNode::parse(reply->pData); + root = JSONNode::parse(reply->body); if (!CheckJsonResult(pReq, root)) return nullNode; @@ -442,7 +442,7 @@ bool CVkProto::CheckJsonResult(AsyncHttpRequest *pReq, const JSONNode &jnNode) return (iErrorCode == 0); } -void CVkProto::OnReceiveSmth(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveSmth(MHttpResponse *reply, AsyncHttpRequest *pReq) { JSONNode jnRoot; const JSONNode &jnResponse = CheckJsonResponse(pReq, reply, jnRoot); @@ -575,14 +575,15 @@ CMStringW CVkProto::RunRenameNick(LPCWSTR pwszOldName) ///////////////////////////////////////////////////////////////////////////////////////// -void CVkProto::GrabCookies(NETLIBHTTPREQUEST *nhr, CMStringA szDefDomain) +void CVkProto::GrabCookies(MHttpResponse *nhr, CMStringA szDefDomain) { debugLogA("CVkProto::GrabCookies"); - for (int i = 0; i < nhr->headersCount; i++) { - if (_stricmp(nhr->headers[i].szName, "Set-cookie")) + + for (auto &hdr : *nhr) { + if (_stricmp(hdr->szName, "Set-cookie")) continue; - CMStringA szValue = nhr->headers[i].szValue, szCookieName, szCookieVal, szDomain; + CMStringA szValue = hdr->szValue, szCookieName, szCookieVal, szDomain; int iStart = 0; while (true) { bool bFirstToken = (iStart == 0); @@ -1468,16 +1469,16 @@ CMStringW CVkProto::GetAttachmentDescr(const JSONNode &jnAttachments, BBCSupport if (GetFileAttributesW(wszFileName) == INVALID_FILE_ATTRIBUTES) { T2Utf szUrl(wszUrl); - NETLIBHTTPREQUEST req = {}; + MHttpRequest req; req.flags = NLHRF_NODUMP | NLHRF_SSL | NLHRF_HTTP11 | NLHRF_REDIRECT; req.requestType = REQUEST_GET; - req.szUrl = szUrl; + req.m_szUrl = szUrl.get(); - 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); } } diff --git a/protocols/VKontakte/src/vk_avatars.cpp b/protocols/VKontakte/src/vk_avatars.cpp index 29288a2a3a..53c33f8250 100644 --- a/protocols/VKontakte/src/vk_avatars.cpp +++ b/protocols/VKontakte/src/vk_avatars.cpp @@ -17,7 +17,7 @@ along with this program. If not, see . #include "stdafx.h" -void CVkProto::OnReceiveAvatar(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveAvatar(MHttpResponse *reply, AsyncHttpRequest *pReq) { if (reply->resultCode != 200 || !pReq->pUserInfo) return; @@ -25,7 +25,7 @@ void CVkProto::OnReceiveAvatar(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) PROTO_AVATAR_INFORMATION ai = { 0 }; CVkSendMsgParam *param = (CVkSendMsgParam *)pReq->pUserInfo; GetAvatarFileName(param->hContact, ai.filename, _countof(ai.filename)); - ai.format = ProtoGetBufferFormat(reply->pData); + ai.format = ProtoGetBufferFormat(reply->body); FILE *out = _wfopen(ai.filename, L"wb"); if (out == nullptr) { @@ -35,7 +35,7 @@ void CVkProto::OnReceiveAvatar(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) return; } - fwrite(reply->pData, 1, reply->dataLength, out); + fwrite(reply->body, 1, reply->body.GetLength(), out); fclose(out); setByte(param->hContact, "NeedNewAvatar", 0); ProtoBroadcastAck(param->hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, &ai); diff --git a/protocols/VKontakte/src/vk_captcha.cpp b/protocols/VKontakte/src/vk_captcha.cpp index e82d1a3400..06e1d451e0 100644 --- a/protocols/VKontakte/src/vk_captcha.cpp +++ b/protocols/VKontakte/src/vk_captcha.cpp @@ -33,12 +33,12 @@ bool CVkProto::RunCaptchaForm(LPCSTR szUrl, CMStringA &result) Utils_OpenUrl(szCaptchaAssistant); } else { - NETLIBHTTPREQUEST req = { sizeof(req) }; + MHttpRequest req; req.requestType = REQUEST_GET; - req.szUrl = (LPSTR)szUrl; + req.m_szUrl = (LPSTR)szUrl; req.flags = VK_NODUMPHEADERS; - NETLIBHTTPREQUEST *reply = Netlib_HttpTransaction(m_hNetlibUser, &req); + auto *reply = Netlib_HttpTransaction(m_hNetlibUser, &req); if (reply == nullptr) return false; @@ -47,7 +47,7 @@ bool CVkProto::RunCaptchaForm(LPCSTR szUrl, CMStringA &result) return false; } - param.bmp = Image_LoadFromMem(reply->pData, reply->dataLength, FIF_UNKNOWN); + param.bmp = Image_LoadFromMem(reply->body, reply->body.GetLength(), FIF_UNKNOWN); BITMAP bmp = { 0 }; GetObject(param.bmp, sizeof(bmp), &bmp); diff --git a/protocols/VKontakte/src/vk_chats.cpp b/protocols/VKontakte/src/vk_chats.cpp index 1fa6d8b774..0264ffad89 100644 --- a/protocols/VKontakte/src/vk_chats.cpp +++ b/protocols/VKontakte/src/vk_chats.cpp @@ -195,7 +195,7 @@ void CVkProto::RetrieveChatInfo(CVkChatInfo *cc) )->pUserInfo = cc; } -void CVkProto::OnReceiveChatInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveChatInfo(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveChatInfo %d", reply->resultCode); if (reply->resultCode != 200) @@ -644,7 +644,7 @@ int CVkProto::OnChatEvent(WPARAM, LPARAM lParam) return 1; } -void CVkProto::OnSendChatMsg(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnSendChatMsg(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnSendChatMsg %d", reply->resultCode); int iResult = ACKRESULT_FAILED; @@ -784,7 +784,7 @@ void CVkProto::KickFromChat(VKUserID_t iChatId, VKUserID_t iUserId, const JSONNo LeaveChat(iChatId); } -void CVkProto::OnChatLeave(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnChatLeave(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnChatLeave %d", reply->resultCode); if (reply->resultCode != 200) @@ -795,7 +795,7 @@ void CVkProto::OnChatLeave(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) } -void CVkProto::OnChatDestroy(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnChatDestroy(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnChatDestroy %d", reply->resultCode); if (reply->resultCode != 200) @@ -1031,7 +1031,7 @@ void CVkProto::CreateNewChat(LPCSTR uids, LPCWSTR pwszTitle) << CHAR_PARAM("user_ids", uids)); } -void CVkProto::OnCreateNewChat(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnCreateNewChat(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnCreateNewChat %d", reply->resultCode); if (reply->resultCode != 200) diff --git a/protocols/VKontakte/src/vk_feed.cpp b/protocols/VKontakte/src/vk_feed.cpp index c5e6701ef1..53f5755057 100644 --- a/protocols/VKontakte/src/vk_feed.cpp +++ b/protocols/VKontakte/src/vk_feed.cpp @@ -642,7 +642,7 @@ static int sttCompareVKNotificationItems(const CVKNewsItem *p1, const CVKNewsIte return compareType ? compareDate : (compareId ? (int)compareDate : 0); } -void CVkProto::OnReceiveUnreadNews(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveUnreadNews(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveUnreadNews %d", reply->resultCode); db_unset(0, m_szModuleName, "LastNewsReqTime"); @@ -747,7 +747,7 @@ void CVkProto::NotificationMarkAsViewed() Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/notifications.markAsViewed.json", true, &CVkProto::OnReceiveSmth)); } -void CVkProto::OnReceiveUnreadNotifications(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveUnreadNotifications(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveUnreadNotifications %d", reply->resultCode); db_unset(0, m_szModuleName, "LastNotificationsReqTime"); diff --git a/protocols/VKontakte/src/vk_files.cpp b/protocols/VKontakte/src/vk_files.cpp index 5c83372d51..34bdfd5504 100644 --- a/protocols/VKontakte/src/vk_files.cpp +++ b/protocols/VKontakte/src/vk_files.cpp @@ -127,7 +127,7 @@ void CVkProto::SendFileFiled(CVkFileUploadParam *fup, int ErrorCode) } -void CVkProto::OnReciveUploadServer(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReciveUploadServer(MHttpResponse *reply, AsyncHttpRequest *pReq) { CVkFileUploadParam *fup = (CVkFileUploadParam *)pReq->pUserInfo; if (!IsOnline()) { @@ -201,9 +201,9 @@ void CVkProto::OnReciveUploadServer(NETLIBHTTPREQUEST *reply, AsyncHttpRequest * // Body size long dataLength = iFileLen + DataBegin.GetLength() + DataEnd.GetLength(); // Body { - char *pData = (char *)mir_alloc(dataLength); + pUploadReq->m_szParam.Truncate(dataLength); + char *pData = pUploadReq->m_szParam.GetBuffer(); memcpy(pData, (void *)DataBegin.GetBuffer(), DataBegin.GetLength()); - pUploadReq->pData = pData; pData += DataBegin.GetLength(); long lBytes = (long)fread(pData, 1, iFileLen, pFile); @@ -211,7 +211,6 @@ void CVkProto::OnReciveUploadServer(NETLIBHTTPREQUEST *reply, AsyncHttpRequest * if (lBytes != iFileLen) { SendFileFiled(fup, VKERR_ERR_READ_FILE); - mir_free(pUploadReq->pData); delete pUploadReq; return; } @@ -220,12 +219,11 @@ void CVkProto::OnReciveUploadServer(NETLIBHTTPREQUEST *reply, AsyncHttpRequest * memcpy(pData, (void *)DataEnd.GetBuffer(), DataEnd.GetLength()); // } Body - pUploadReq->dataLength = (int)dataLength; pUploadReq->pUserInfo = pReq->pUserInfo; Push(pUploadReq); } -void CVkProto::OnReciveUpload(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReciveUpload(MHttpResponse *reply, AsyncHttpRequest *pReq) { CVkFileUploadParam *fup = (CVkFileUploadParam *)pReq->pUserInfo; if (!IsOnline()) { @@ -303,7 +301,7 @@ void CVkProto::OnReciveUpload(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) Push(pUploadReq); } -void CVkProto::OnReciveUploadFile(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReciveUploadFile(MHttpResponse *reply, AsyncHttpRequest *pReq) { CVkFileUploadParam *fup = (CVkFileUploadParam *)pReq->pUserInfo; if (!IsOnline()) { diff --git a/protocols/VKontakte/src/vk_history.cpp b/protocols/VKontakte/src/vk_history.cpp index 4e967f319e..64ea57b002 100644 --- a/protocols/VKontakte/src/vk_history.cpp +++ b/protocols/VKontakte/src/vk_history.cpp @@ -166,7 +166,7 @@ void CVkProto::GetHistoryDlg(MCONTACT hContact, VKMessageID_t iLastMsg) } } -void CVkProto::OnReceiveHistoryMessages(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveHistoryMessages(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveHistoryMessages %d", reply->resultCode); if (reply->resultCode != 200 || !pReq->pUserInfo) { diff --git a/protocols/VKontakte/src/vk_messages.cpp b/protocols/VKontakte/src/vk_messages.cpp index 3549a77a8b..3030df3b9f 100644 --- a/protocols/VKontakte/src/vk_messages.cpp +++ b/protocols/VKontakte/src/vk_messages.cpp @@ -186,7 +186,7 @@ int CVkProto::SendMsg(MCONTACT hContact, MEVENT hReplyEvent, const char *szMsg) return uMsgId; } -void CVkProto::OnSendMessage(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnSendMessage(MHttpResponse *reply, AsyncHttpRequest *pReq) { int iResult = ACKRESULT_FAILED; if (pReq->pUserInfo == nullptr) { @@ -299,7 +299,7 @@ void CVkProto::RetrieveUnreadMessages() Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/execute.RetrieveUnreadConversations", true, &CVkProto::OnReceiveDlgs, AsyncHttpRequest::rpHigh)); } -void CVkProto::OnReceiveMessages(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveMessages(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveMessages %d", reply->resultCode); if (reply->resultCode != 200) @@ -487,7 +487,7 @@ void CVkProto::OnReceiveMessages(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pRe } } -void CVkProto::OnReceiveDlgs(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveDlgs(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveDlgs %d", reply->resultCode); if (reply->resultCode != 200) diff --git a/protocols/VKontakte/src/vk_pollserver.cpp b/protocols/VKontakte/src/vk_pollserver.cpp index 1597b439ca..3030816b1c 100644 --- a/protocols/VKontakte/src/vk_pollserver.cpp +++ b/protocols/VKontakte/src/vk_pollserver.cpp @@ -28,7 +28,7 @@ void CVkProto::RetrievePollingInfo() ); } -void CVkProto::OnReceivePollingInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceivePollingInfo(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceivePollingInfo %d", reply->resultCode); if (reply->resultCode != 200) @@ -253,9 +253,9 @@ int CVkProto::PollServer() CMStringA szReqUrl(FORMAT, "https://%s?act=a_check&key=%s&ts=%s&wait=25&access_token=%s&mode=%d&version=%d", m_szPollingServer, m_szPollingKey, m_szPollingTs, m_szAccessToken, 106, 2); // see mode parametr description on https://vk.com/dev/using_longpoll (Russian version) - NETLIBHTTPREQUEST req = {}; + MHttpRequest req; req.requestType = REQUEST_GET; - req.szUrl = szReqUrl.GetBuffer(); + req.m_szUrl = szReqUrl.GetBuffer(); req.flags = VK_NODUMPHEADERS | NLHRF_PERSISTENT | NLHRF_HTTP11 | NLHRF_SSL; req.timeout = 30000; req.nlc = m_hPollingConn; @@ -290,7 +290,7 @@ int CVkProto::PollServer() int retVal = 0; if (reply->resultCode == 200) { - JSONNode jnRoot = JSONNode::parse(reply->pData); + JSONNode jnRoot = JSONNode::parse(reply->body); const JSONNode &jnFailed = jnRoot["failed"]; if (jnFailed && jnFailed.as_int() > 1) { RetrievePollingInfo(); diff --git a/protocols/VKontakte/src/vk_proto.cpp b/protocols/VKontakte/src/vk_proto.cpp index 372e8690a8..285ee996ae 100644 --- a/protocols/VKontakte/src/vk_proto.cpp +++ b/protocols/VKontakte/src/vk_proto.cpp @@ -651,7 +651,7 @@ int CVkProto::AuthRequest(MCONTACT hContact, const wchar_t *message) return 0; } -void CVkProto::OnReceiveAuthRequest(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveAuthRequest(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveAuthRequest %d", reply->resultCode); CVkSendMsgParam *param = (CVkSendMsgParam*)pReq->pUserInfo; diff --git a/protocols/VKontakte/src/vk_proto.h b/protocols/VKontakte/src/vk_proto.h index bc07b1dbc2..4d19cb7d29 100644 --- a/protocols/VKontakte/src/vk_proto.h +++ b/protocols/VKontakte/src/vk_proto.h @@ -82,8 +82,8 @@ struct CVkProto : public PROTO //==== Events ======================================================================== int __cdecl OnOptionsInit(WPARAM, LPARAM); - void OnOAuthAuthorize(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveAvatar(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnOAuthAuthorize(MHttpResponse*, AsyncHttpRequest*); + void OnReceiveAvatar(MHttpResponse*, AsyncHttpRequest*); //==== Services ====================================================================== @@ -284,15 +284,15 @@ private: //==== Search ======================================================================== void FreeProtoShearchStruct(PROTOSEARCHBYNAME *pParam); - void OnSearch(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnSearchByMail(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnSearch(MHttpResponse*, AsyncHttpRequest*); + void OnSearchByMail(MHttpResponse*, AsyncHttpRequest*); //==== Files Upload ================================================================== void SendFileFiled(CVkFileUploadParam *fup, int ErrorCode); - void OnReciveUploadServer(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReciveUpload(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReciveUploadFile(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReciveUploadServer(MHttpResponse*, AsyncHttpRequest*); + void OnReciveUpload(MHttpResponse*, AsyncHttpRequest*); + void OnReciveUploadFile(MHttpResponse*, AsyncHttpRequest*); //==== Feed ========================================================================== @@ -308,11 +308,11 @@ private: CMStringW GetVkFeedback(const JSONNode &jnFeedback, VKObjType vkFeedbackType, OBJLIST &vkUsers, CVkUserInfo *vkUser); CVKNewsItem* GetVkParent(const JSONNode &jnParent, VKObjType vkParentType, LPCWSTR pwszReplyText = nullptr, LPCWSTR pwszReplyLink = nullptr); void RetrieveUnreadNews(time_t tLastNewsTime); - void OnReceiveUnreadNews(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveUnreadNews(MHttpResponse*, AsyncHttpRequest*); void RetrieveUnreadNotifications(time_t tLastNotificationsTime); bool FilterNotification(CVKNewsItem *vkNotificationItem, bool& bIsCommented); void NotificationMarkAsViewed(); - void OnReceiveUnreadNotifications(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveUnreadNotifications(MHttpResponse*, AsyncHttpRequest*); void RetrieveUnreadEvents(); void NewsClearHistory(); INT_PTR __cdecl SvcLoadVKNews(WPARAM, LPARAM); @@ -323,50 +323,50 @@ private: void RetrieveUsersInfo(bool bFlag = false, bool bRepeat = false); void RetrieveStatusMsg(const CMStringW &StatusMsg); void RetrieveStatusMusic(const CMStringW &StatusMsg); - void OnReceiveStatus(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveStatusMsg(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveStatus(MHttpResponse*, AsyncHttpRequest*); + void OnReceiveStatusMsg(MHttpResponse*, AsyncHttpRequest*); MCONTACT SetContactInfo(const JSONNode &jnItem, bool bFlag = false, VKContactType vkContactType = VKContactType::vkContactNormal); void TrackVisitor(); void RetrieveMyInfo(void); - void OnReceiveMyInfo(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveMyInfo(MHttpResponse*, AsyncHttpRequest*); void RetrieveUserInfo(VKUserID_t iUserId); void RetrieveGroupInfo(VKUserID_t iGroupID); void RetrieveGroupInfo(CMStringA & groupIDs); - void OnReceiveUserInfo(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveGroupInfo(NETLIBHTTPREQUEST * reply, AsyncHttpRequest * pReq); + void OnReceiveUserInfo(MHttpResponse*, AsyncHttpRequest*); + void OnReceiveGroupInfo(MHttpResponse * reply, AsyncHttpRequest * pReq); void RetrieveFriends(bool bCleanNonFriendContacts = false); - void OnReceiveFriends(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveFriends(MHttpResponse*, AsyncHttpRequest*); void MarkMessagesRead(const MCONTACT hContact); void RetrieveMessagesByIds(const CMStringA &szMids); void RetrieveUnreadMessages(); - void OnReceiveMessages(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveDlgs(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnSendMessage(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveMessages(MHttpResponse*, AsyncHttpRequest*); + void OnReceiveDlgs(MHttpResponse*, AsyncHttpRequest*); + void OnSendMessage(MHttpResponse*, AsyncHttpRequest*); void WallPost(MCONTACT hContact, wchar_t *pwszMsg, wchar_t *pwszUrl, bool bFriendsOnly); void GetServerHistoryLastNDay(MCONTACT hContact, int iNDay); void GetServerHistory(MCONTACT hContact, int iOffset, int iCount, time_t tTime, VKMessageID_t iLastMsgId, bool bOnce = false); - void OnReceiveHistoryMessages(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq); + void OnReceiveHistoryMessages(MHttpResponse *reply, AsyncHttpRequest *pReq); void GetHistoryDlg(MCONTACT hContact, VKMessageID_t iLastMsg); void RetrievePollingInfo(); - void OnReceivePollingInfo(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceivePollingInfo(MHttpResponse*, AsyncHttpRequest*); void __cdecl PollingThread(void*); int PollServer(); void PollUpdates(const JSONNode&); - void OnReceiveAuthRequest(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveDeleteFriend(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveAuthRequest(MHttpResponse*, AsyncHttpRequest*); + void OnReceiveDeleteFriend(MHttpResponse*, AsyncHttpRequest*); //==== Misc ========================================================================== void SetAllContactStatuses(int iStatus); MCONTACT FindUser(VKUserID_t iUserId, bool bCreate = false); MCONTACT FindChat(VKUserID_t iUserId); - JSONNode& CheckJsonResponse(AsyncHttpRequest *pReq, NETLIBHTTPREQUEST *reply, JSONNode &root); + JSONNode& CheckJsonResponse(AsyncHttpRequest *pReq, MHttpResponse *reply, JSONNode &root); bool CheckJsonResult(AsyncHttpRequest *pReq, const JSONNode &Node); - void OnReceiveSmth(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveSmth(MHttpResponse*, AsyncHttpRequest*); bool AutoFillForm(char*, CMStringA&, CMStringA&); CMStringW RunConfirmationCode(LPCWSTR pwszTitle); CMStringW RunRenameNick(LPCWSTR pwszOldName); - void GrabCookies(NETLIBHTTPREQUEST *nhr, CMStringA szDefDomain = ""); + void GrabCookies(MHttpResponse *nhr, CMStringA szDefDomain = ""); void ApplyCookies(AsyncHttpRequest*); bool IsAuthContactLater(MCONTACT hContact); bool AddAuthContactLater(MCONTACT hContact); @@ -417,16 +417,16 @@ private: void StopChatContactTyping(VKUserID_t iChatId, VKUserID_t iUserId); - void OnCreateNewChat(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnCreateNewChat(MHttpResponse*, AsyncHttpRequest*); CVkChatInfo* AppendConversationChat(VKUserID_t iChatId, const JSONNode& jnItem); void SetChatTitle(CVkChatInfo *cc, LPCWSTR wszTopic); void AppendChatConversationMessage(VKUserID_t iChatId, const JSONNode& jnMsg, const JSONNode& jnFUsers, bool bIsHistory); void AppendChatMessage(CVkChatInfo* vkChatInfo, VKMessageID_t iMessageId, VKUserID_t iUserId, time_t tMsgTime, LPCWSTR pwszBody, bool bIsHistory, bool bIsAction = false); void RetrieveChatInfo(CVkChatInfo*); - void OnReceiveChatInfo(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnSendChatMsg(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnChatLeave(NETLIBHTTPREQUEST*, AsyncHttpRequest *); - void OnChatDestroy(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveChatInfo(MHttpResponse*, AsyncHttpRequest*); + void OnSendChatMsg(MHttpResponse*, AsyncHttpRequest*); + void OnChatLeave(MHttpResponse*, AsyncHttpRequest *); + void OnChatDestroy(MHttpResponse*, AsyncHttpRequest*); int __cdecl OnChatEvent(WPARAM, LPARAM); int __cdecl OnGcMenuHook(WPARAM, LPARAM); void KickFromChat(VKUserID_t iChatId, VKUserID_t iUserId, const JSONNode &jnMsg, const JSONNode &jnFUsers); diff --git a/protocols/VKontakte/src/vk_queue.cpp b/protocols/VKontakte/src/vk_queue.cpp index 94e85b826d..e42bd1db42 100644 --- a/protocols/VKontakte/src/vk_queue.cpp +++ b/protocols/VKontakte/src/vk_queue.cpp @@ -38,15 +38,11 @@ bool CVkProto::ExecuteRequest(AsyncHttpRequest *pReq) do { pReq->bNeedsRestart = false; pReq->m_iErrorCode = 0; - pReq->szUrl = pReq->m_szUrl.GetBuffer(); + pReq->m_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->pData = mir_strdup(pReq->m_szParam); - pReq->dataLength = pReq->m_szParam.GetLength(); + pReq->m_szUrl = str.GetBuffer(); } } @@ -64,7 +60,7 @@ bool CVkProto::ExecuteRequest(AsyncHttpRequest *pReq) tLocalWorkThreadTimer = m_tWorkThreadTimer = time(0); } - debugLogA("CVkProto::ExecuteRequest \n====\n%s\n====\n", pReq->szUrl); + debugLogA("CVkProto::ExecuteRequest \n====\n%s\n====\n", pReq->m_szUrl.c_str()); NLHR_PTR reply(Netlib_HttpTransaction(m_hNetlibUser, pReq)); { mir_cslock lck(m_csWorkThreadTimer); diff --git a/protocols/VKontakte/src/vk_search.cpp b/protocols/VKontakte/src/vk_search.cpp index f5da64438c..399e58945d 100644 --- a/protocols/VKontakte/src/vk_search.cpp +++ b/protocols/VKontakte/src/vk_search.cpp @@ -69,7 +69,7 @@ void CVkProto::FreeProtoShearchStruct(PROTOSEARCHBYNAME *pParam) delete pParam; } -void CVkProto::OnSearch(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnSearch(MHttpResponse *reply, AsyncHttpRequest *pReq) { PROTOSEARCHBYNAME *pParam = (PROTOSEARCHBYNAME *)pReq->pUserInfo; debugLogA("CVkProto::OnSearch %d", reply->resultCode); @@ -124,7 +124,7 @@ void CVkProto::OnSearch(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) FreeProtoShearchStruct(pParam); } -void CVkProto::OnSearchByMail(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnSearchByMail(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnSearch %d", reply->resultCode); if (reply->resultCode != 200) { diff --git a/protocols/VKontakte/src/vk_status.cpp b/protocols/VKontakte/src/vk_status.cpp index 4fb2405aa2..10e55835b9 100644 --- a/protocols/VKontakte/src/vk_status.cpp +++ b/protocols/VKontakte/src/vk_status.cpp @@ -124,7 +124,7 @@ INT_PTR __cdecl CVkProto::SvcSetStatusMsg(WPARAM, LPARAM) return 0; } -void CVkProto::OnReceiveStatusMsg(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveStatusMsg(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveStatusMsg %d", reply->resultCode); if (reply->resultCode != 200) @@ -153,7 +153,7 @@ void CVkProto::OnReceiveStatusMsg(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pR setWString("OldStatusMsg", pwszOldStatusMsg); } -void CVkProto::OnReceiveStatus(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveStatus(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveStatus %d", reply->resultCode); if (reply->resultCode != 200) diff --git a/protocols/VKontakte/src/vk_struct.cpp b/protocols/VKontakte/src/vk_struct.cpp index 2b5eb9e121..153aac408d 100644 --- a/protocols/VKontakte/src/vk_struct.cpp +++ b/protocols/VKontakte/src/vk_struct.cpp @@ -68,9 +68,9 @@ AsyncHttpRequest::AsyncHttpRequest(CVkProto *ppro, int iRequestType, LPCSTR _url m_priority = rpPriority; } -void AsyncHttpRequest::Redirect(NETLIBHTTPREQUEST *nhr) +void AsyncHttpRequest::Redirect(MHttpResponse *nhr) { - if (auto *pszHdr = Netlib_GetHeader(nhr, "Location")) + if (auto *pszHdr = nhr->FindHeader("Location")) m_szUrl = pszHdr; } diff --git a/protocols/VKontakte/src/vk_struct.h b/protocols/VKontakte/src/vk_struct.h index f25072f89f..73683a652a 100644 --- a/protocols/VKontakte/src/vk_struct.h +++ b/protocols/VKontakte/src/vk_struct.h @@ -25,7 +25,7 @@ struct AsyncHttpRequest : public MTHttpRequest AsyncHttpRequest(); AsyncHttpRequest(CVkProto*, int iRequestType, LPCSTR szUrl, bool bSecure, MTHttpRequestHandler pFunc, RequestPriority rpPriority = rpMedium); - void Redirect(NETLIBHTTPREQUEST*); + void Redirect(MHttpResponse*); int m_iRetry; int m_iErrorCode; diff --git a/protocols/VKontakte/src/vk_thread.cpp b/protocols/VKontakte/src/vk_thread.cpp index 393a215079..6f7d0b7791 100644 --- a/protocols/VKontakte/src/vk_thread.cpp +++ b/protocols/VKontakte/src/vk_thread.cpp @@ -200,7 +200,7 @@ void CVkProto::OnLoggedOut() ///////////////////////////////////////////////////////////////////////////////////////// -void CVkProto::OnOAuthAuthorize(NETLIBHTTPREQUEST *reply, AsyncHttpRequest*) +void CVkProto::OnOAuthAuthorize(MHttpResponse *reply, AsyncHttpRequest*) { debugLogA("CVkProto::OnOAuthAuthorize %d", reply->resultCode); GrabCookies(reply, szVKCookieDomain); @@ -225,7 +225,7 @@ void CVkProto::OnOAuthAuthorize(NETLIBHTTPREQUEST *reply, AsyncHttpRequest*) } if (reply->resultCode == 302) { // manual redirect - LPCSTR pszLocation = Netlib_GetHeader(reply, "Location"); + LPCSTR pszLocation = reply->FindHeader("Location"); if (pszLocation) { if (!_strnicmp(pszLocation, szBlankUrl, sizeof(szBlankUrl) - 1)) { m_szAccessToken = nullptr; @@ -283,12 +283,12 @@ void CVkProto::OnOAuthAuthorize(NETLIBHTTPREQUEST *reply, AsyncHttpRequest*) return; } - if (reply->resultCode != 200 || !reply->pData || (!(strstr(reply->pData, "method=\"post\"") || strstr(reply->pData, "method=\"POST\"")) && !strstr(reply->pData, "meta http-equiv=\"refresh\""))) { // something went wrong + if (reply->resultCode != 200 || reply->body.IsEmpty() || (!(strstr(reply->body, "method=\"post\"") || strstr(reply->body, "method=\"POST\"")) && !strstr(reply->body, "meta http-equiv=\"refresh\""))) { // something went wrong ConnectionFailed(LOGINERR_NOSERVER); return; } - LPCSTR pBlankUrl = strstr(reply->pData, szBlankUrl); + LPCSTR pBlankUrl = strstr(reply->body, szBlankUrl); if (pBlankUrl) { debugLogA("CVkProto::OnOAuthAuthorize blank ulr found"); m_szAccessToken = nullptr; @@ -311,10 +311,10 @@ void CVkProto::OnOAuthAuthorize(NETLIBHTTPREQUEST *reply, AsyncHttpRequest*) return; } - char* pMsgWarning = strstr(reply->pData, "service_msg_warning"); + auto *pMsgWarning = strstr(reply->body, "service_msg_warning"); if (pMsgWarning) { - char *p1 = strchr(pMsgWarning, '>'); - char *p2 = strchr(pMsgWarning, '<'); + auto *p1 = strchr(pMsgWarning, '>'); + auto *p2 = strchr(pMsgWarning, '<'); if (p1 && p2 && (p1 + 1 < p2)) { CMStringA szMsg(p1 + 1, (int)(p2 - p1 - 1)); MsgPopup(ptrW(mir_utf8decodeW(szMsg)), TranslateT("Service message"), true); @@ -325,7 +325,7 @@ void CVkProto::OnOAuthAuthorize(NETLIBHTTPREQUEST *reply, AsyncHttpRequest*) } CMStringA szAction, szBody; - bool bSuccess = AutoFillForm(reply->pData, szAction, szBody); + bool bSuccess = AutoFillForm(reply->body.GetBuffer(), szAction, szBody); if (!bSuccess || szAction.IsEmpty() || szBody.IsEmpty()) { if (m_bPrevError) { ConnectionFailed(LOGINERR_NOSERVER); @@ -381,7 +381,7 @@ void CVkProto::RetrieveMyInfo() Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/users.get.json", true, &CVkProto::OnReceiveMyInfo, AsyncHttpRequest::rpHigh)); } -void CVkProto::OnReceiveMyInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveMyInfo(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveMyInfo %d", reply->resultCode); if (reply->resultCode != 200) { @@ -723,7 +723,7 @@ void CVkProto::RetrieveUsersInfo(bool bFreeOffline, bool bRepeat) } -void CVkProto::OnReceiveUserInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveUserInfo(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveUserInfo %d", reply->resultCode); @@ -801,7 +801,7 @@ void CVkProto::OnReceiveUserInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pRe } } -void CVkProto::OnReceiveGroupInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveGroupInfo(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveUserInfo %d", reply->resultCode); @@ -887,7 +887,7 @@ void CVkProto::RetrieveFriends(bool bCleanNonFriendContacts) << CHAR_PARAM("fields", szFieldsName))->pUserInfo = new CVkSendMsgParam(0, bCleanNonFriendContacts ? 1 : 0); } -void CVkProto::OnReceiveFriends(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveFriends(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveFriends %d", reply->resultCode); if (reply->resultCode != 200 || !IsOnline()) @@ -983,7 +983,7 @@ INT_PTR __cdecl CVkProto::SvcDeleteFriend(WPARAM hContact, LPARAM flag) return 0; } -void CVkProto::OnReceiveDeleteFriend(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +void CVkProto::OnReceiveDeleteFriend(MHttpResponse *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnReceiveDeleteFriend %d", reply->resultCode); CVkSendMsgParam *param = (CVkSendMsgParam*)pReq->pUserInfo; diff --git a/protocols/Weather/src/weather_http.cpp b/protocols/Weather/src/weather_http.cpp index a306315011..99c262ec6a 100644 --- a/protocols/Weather/src/weather_http.cpp +++ b/protocols/Weather/src/weather_http.cpp @@ -39,28 +39,17 @@ int InternetDownloadFile(char *szUrl, char *cookie, char *userAgent, wchar_t **s if (userAgent == nullptr || userAgent[0] == 0) userAgent = NETLIB_USER_AGENT; - NETLIBHTTPHEADER headers[5]; - headers[0].szName = "User-Agent"; - headers[0].szValue = userAgent; - headers[1].szName = "Cache-Control"; - headers[1].szValue = "no-cache"; - headers[2].szName = "Pragma"; - headers[2].szValue = "no-cache"; - headers[3].szName = "Connection"; - headers[3].szValue = "close"; - headers[4].szName = "Cookie"; - headers[4].szValue = cookie; - // initialize the netlib request - NETLIBHTTPREQUEST nlhr = { sizeof(nlhr) }; + MHttpRequest nlhr; nlhr.requestType = REQUEST_GET; nlhr.flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11 | NLHRF_REDIRECT; - nlhr.szUrl = szUrl; - nlhr.headers = headers; - nlhr.headersCount = _countof(headers); - - if (cookie == nullptr || cookie[0] == 0) - --nlhr.headersCount; + nlhr.m_szUrl = szUrl; + nlhr.AddHeader("User-Agent", userAgent); + nlhr.AddHeader("Cache-Control", "no-cache"); + nlhr.AddHeader("Pragma", "no-cache"); + nlhr.AddHeader("Connection", "close"); + if (mir_strlen(cookie) > 0) + nlhr.AddHeader("Cookie", cookie); // download the page NLHR_PTR nlhrReply(Netlib_HttpTransaction(hNetlibUser, &nlhr)); @@ -75,17 +64,17 @@ int InternetDownloadFile(char *szUrl, char *cookie, char *userAgent, wchar_t **s // if the recieved code is 200 OK int result; if (nlhrReply->resultCode == 200) { - if (nlhrReply->dataLength) { + if (!nlhrReply->body.IsEmpty()) { bool bIsUtf = false; result = 0; // allocate memory and save the retrieved data - auto *pszHdr = Netlib_GetHeader(nlhrReply, "Content-Type"); + auto *pszHdr = nlhrReply->FindHeader("Content-Type"); // look for Content-Type=utf-8 in header if (pszHdr && strstr(_strlwr(pszHdr), "utf-8")) bIsUtf = true; else { - char *end = nlhrReply->pData; + char *end = nlhrReply->body.GetBuffer(); while (end) { // look for // @@ -112,9 +101,9 @@ int InternetDownloadFile(char *szUrl, char *cookie, char *userAgent, wchar_t **s wchar_t *retVal = nullptr; if (bIsUtf) - retVal = mir_utf8decodeW(nlhrReply->pData); + retVal = mir_utf8decodeW(nlhrReply->body); if (retVal == nullptr) - retVal = mir_a2u(nlhrReply->pData); + retVal = mir_a2u(nlhrReply->body); *szData = retVal; } else result = DATA_EMPTY; diff --git a/protocols/WebView/src/webview_getdata.cpp b/protocols/WebView/src/webview_getdata.cpp index ae29e134ef..babfe3cca8 100644 --- a/protocols/WebView/src/webview_getdata.cpp +++ b/protocols/WebView/src/webview_getdata.cpp @@ -104,20 +104,12 @@ void GetData(void *param) if (mir_strlen(url) < 3) WErrorPopup(hContact, TranslateT("URL not supplied")); - NETLIBHTTPREQUEST nlhr = { sizeof(nlhr) }; + MHttpRequest nlhr; nlhr.requestType = REQUEST_GET; nlhr.flags = NLHRF_DUMPASTEXT; - nlhr.szUrl = url; - nlhr.headersCount = 2; - - NETLIBHTTPHEADER headers[2]; - headers[0].szName = "User-Agent"; - headers[0].szValue = NETLIB_USER_AGENT; - - headers[1].szName = "Content-Length"; - headers[1].szValue = nullptr; - - nlhr.headers = headers; + nlhr.m_szUrl = url; + nlhr.AddHeader("User-Agent", NETLIB_USER_AGENT); + nlhr.AddHeader("Content-Length", nullptr); if ( g_plugin.getByte(NO_PROTECT_KEY, 0)) // disable AlreadyDownloading = 0; @@ -144,10 +136,10 @@ void GetData(void *param) WErrorPopup(hContact, statusText); db_set_ws(hContact, "CList", "StatusMsg", statusText); } - if (nlhrReply->dataLength) { + if (nlhrReply->body.GetLength()) { trunccount = 0; - downloadsize = (ULONG)mir_strlen(nlhrReply->pData); - strncpy_s(truncated2, nlhrReply->pData, _TRUNCATE); + downloadsize = nlhrReply->body.GetLength(); + strncpy_s(truncated2, nlhrReply->body, _TRUNCATE); AlreadyDownloading = 1; } // END DATELENGTH } // END REPLY diff --git a/protocols/WhatsApp/src/avatars.cpp b/protocols/WhatsApp/src/avatars.cpp index 9bb7e926aa..077414e8e0 100644 --- a/protocols/WhatsApp/src/avatars.cpp +++ b/protocols/WhatsApp/src/avatars.cpp @@ -130,10 +130,10 @@ void WhatsAppProto::ServerFetchAvatar(const char *jid) bool CMPlugin::SaveFile(const char *pszUrl, PROTO_AVATAR_INFORMATION &ai) { - NETLIBHTTPREQUEST req = {}; + MHttpRequest req; req.flags = NLHRF_NODUMP | NLHRF_PERSISTENT | NLHRF_SSL | NLHRF_HTTP11 | NLHRF_REDIRECT; req.requestType = REQUEST_GET; - req.szUrl = (char*)pszUrl; + req.m_szUrl = pszUrl; req.nlc = hAvatarConn; NLHR_PTR pReply(Netlib_HttpTransaction(hAvatarUser, &req)); @@ -146,14 +146,14 @@ bool CMPlugin::SaveFile(const char *pszUrl, PROTO_AVATAR_INFORMATION &ai) hAvatarConn = pReply->nlc; 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; } diff --git a/protocols/WhatsApp/src/server.cpp b/protocols/WhatsApp/src/server.cpp index 857666b2e4..e653ac95ba 100644 --- a/protocols/WhatsApp/src/server.cpp +++ b/protocols/WhatsApp/src/server.cpp @@ -24,13 +24,10 @@ void WhatsAppProto::ServerThread(void *) void WhatsAppProto::ServerThreadWorker() { // connect websocket - NETLIBHTTPHEADER hdrs[] = - { - { "Origin", "https://web.whatsapp.com" }, - { 0, 0 } - }; + MHttpHeaders hdrs; + hdrs.AddHeader("Origin", "https://web.whatsapp.com"); - NLHR_PTR pReply(WebSocket_Connect(m_hNetlibUser, "web.whatsapp.com/ws/chat", hdrs)); + NLHR_PTR pReply(WebSocket_Connect(m_hNetlibUser, "web.whatsapp.com/ws/chat", &hdrs)); if (pReply == nullptr) { debugLogA("Server connection failed, exiting"); return; diff --git a/protocols/WhatsApp/src/utils.cpp b/protocols/WhatsApp/src/utils.cpp index 3d3650c043..8e9a19b936 100644 --- a/protocols/WhatsApp/src/utils.cpp +++ b/protocols/WhatsApp/src/utils.cpp @@ -260,7 +260,7 @@ MBinBuffer decodeBufStr(const std::string &buf) uint32_t decodeBigEndian(const uint8_t *buf, size_t len) { uint32_t ret = 0; - for (int i = 0; i < len; i++) { + for (size_t i = 0; i < len; i++) { ret <<= 8; ret += buf[i]; } @@ -271,7 +271,7 @@ uint32_t decodeBigEndian(const uint8_t *buf, size_t len) std::string encodeBigEndian(uint32_t num, size_t len) { std::string res; - for (int i = 0; i < len; i++) { + for (size_t i = 0; i < len; i++) { char c = num & 0xFF; res = c + res; num >>= 8; @@ -519,20 +519,17 @@ WAMediaKeys::WAMediaKeys(const uint8_t *pKey, size_t keyLen, const char *pszMedi MBinBuffer WhatsAppProto::DownloadEncryptedFile(const char *url, const ProtobufCBinaryData &mediaKeys, const char *pszMediaType) { - NETLIBHTTPHEADER headers[1] = {{"Origin", "https://web.whatsapp.com"}}; - - NETLIBHTTPREQUEST req = {}; + MHttpRequest req; req.requestType = REQUEST_GET; - req.szUrl = (char*)url; - req.headersCount = _countof(headers); - req.headers = headers; + req.m_szUrl = url; + req.AddHeader("Origin", "https://web.whatsapp.com"); MBinBuffer ret; auto *pResp = Netlib_HttpTransaction(m_hNetlibUser, &req); if (pResp) { if (pResp->resultCode == 200) { WAMediaKeys out(mediaKeys.data, mediaKeys.len, pszMediaType); - ret = aesDecrypt(EVP_aes_256_cbc(), out.cipherKey, out.iv, pResp->pData, pResp->dataLength); + ret = aesDecrypt(EVP_aes_256_cbc(), out.cipherKey, out.iv, pResp->body, pResp->body.GetLength()); } } -- cgit v1.2.3