From 87a2660299edd64cbb6f6c92c33683e91a6d187c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 13 Jan 2020 16:48:55 +0300 Subject: Netlib_GetHeader() - handful utility to avoid writing cycles --- include/m_netlib.h | 8 ++++ libs/win32/mir_app.lib | Bin 200822 -> 201046 bytes libs/win64/mir_app.lib | Bin 196440 -> 196646 bytes plugins/CrashDumper/src/upload.cpp | 35 +++++++-------- plugins/SmileyAdd/src/download.cpp | 31 ++++++------- .../CloudFile/src/Services/google_service.cpp | 11 ++--- protocols/Discord/src/avatars.cpp | 7 +-- protocols/Discord/src/gateway.cpp | 14 +++--- protocols/Discord/src/server.cpp | 10 ++--- protocols/Facebook/src/avatars.cpp | 7 +-- protocols/ICQ-WIM/src/http.cpp | 11 ++--- protocols/ICQ-WIM/src/server.cpp | 17 ++++--- protocols/MSN/src/msn_auth.cpp | 9 +--- protocols/Non-IM Contact/src/http.cpp | 50 ++++++++++----------- protocols/SkypeWeb/src/skype_mslogin.cpp | 15 ++----- protocols/SkypeWeb/src/skype_oauth.cpp | 7 +-- protocols/VKontakte/src/misc.cpp | 16 +------ protocols/VKontakte/src/vk.h | 1 - protocols/VKontakte/src/vk_struct.cpp | 7 +-- protocols/VKontakte/src/vk_thread.cpp | 2 +- protocols/Weather/src/weather_http.cpp | 14 +----- src/mir_app/src/mir_app.def | 1 + src/mir_app/src/mir_app64.def | 1 + src/mir_app/src/netlib_http.cpp | 15 ++++--- 24 files changed, 116 insertions(+), 173 deletions(-) diff --git a/include/m_netlib.h b/include/m_netlib.h index 3164125e2a..1d5be72fd2 100644 --- a/include/m_netlib.h +++ b/include/m_netlib.h @@ -434,6 +434,10 @@ struct NETLIBHTTPHEADER char *szValue; }; +EXTERN_C MIR_APP_DLL(char*) Netlib_GetHeader(const NETLIBHTTPREQUEST *pRec, const char *pszName); + +///////////////////////////////////////////////////////////////////////////////////////// + #define REQUEST_RESPONSE 0 // used by structure returned by MS_NETLIB_RECVHTTPHEADERS #define REQUEST_GET 1 #define REQUEST_POST 2 @@ -471,6 +475,10 @@ struct NETLIBHTTPREQUEST char *szResultDescr; HNETLIBCONN nlc; int timeout; + + __forceinline const char *operator[](const char *pszName) { + return Netlib_GetHeader(this, pszName); + } }; EXTERN_C MIR_APP_DLL(int) Netlib_SendHttpRequest(HNETLIBCONN hConnection, NETLIBHTTPREQUEST *pRec); diff --git a/libs/win32/mir_app.lib b/libs/win32/mir_app.lib index f3e73b705b..85b6572c5f 100644 Binary files a/libs/win32/mir_app.lib and b/libs/win32/mir_app.lib differ diff --git a/libs/win64/mir_app.lib b/libs/win64/mir_app.lib index 1ce43d250d..7656fe6f57 100644 Binary files a/libs/win64/mir_app.lib and b/libs/win64/mir_app.lib differ diff --git a/plugins/CrashDumper/src/upload.cpp b/plugins/CrashDumper/src/upload.cpp index 09cf4199e0..702bc39e96 100644 --- a/plugins/CrashDumper/src/upload.cpp +++ b/plugins/CrashDumper/src/upload.cpp @@ -154,26 +154,23 @@ bool InternetDownloadFile(const char *szUrl, VerTrnsfr* szReq) case 307: // get the url for the new location and save it to szInfo // look for the reply header "Location" - for (int i = 0; i < nlhrReply->headersCount; i++) { - if (!mir_strcmp(nlhrReply->headers[i].szName, "Location")) { - size_t rlen = 0; - if (nlhrReply->headers[i].szValue[0] == '/') { - const char* szPath; - const char* szPref = strstr(szUrl, "://"); - szPref = szPref ? szPref + 3 : szUrl; - szPath = strchr(szPref, '/'); - rlen = szPath != nullptr ? szPath - szUrl : mir_strlen(szUrl); - } - - szRedirUrl = (char*)mir_realloc(szRedirUrl, - rlen + mir_strlen(nlhrReply->headers[i].szValue) * 3 + 1); - - strncpy(szRedirUrl, szUrl, rlen); - mir_strcpy(szRedirUrl + rlen, nlhrReply->headers[i].szValue); - - nlhr.szUrl = szRedirUrl; - break; + if (auto *pszUrl = Netlib_GetHeader(nlhrReply, "Location")) { + size_t rlen = 0; + if (pszUrl[0] == '/') { + const char* szPath; + const char* szPref = strstr(szUrl, "://"); + szPref = szPref ? szPref + 3 : szUrl; + szPath = strchr(szPref, '/'); + rlen = szPath != nullptr ? szPath - szUrl : mir_strlen(szUrl); } + + szRedirUrl = (char*)mir_realloc(szRedirUrl, + rlen + mir_strlen(pszUrl) * 3 + 1); + + strncpy(szRedirUrl, szUrl, rlen); + mir_strcpy(szRedirUrl + rlen, pszUrl); + + nlhr.szUrl = szRedirUrl; } break; diff --git a/plugins/SmileyAdd/src/download.cpp b/plugins/SmileyAdd/src/download.cpp index ac9cefff92..e66b5ec069 100644 --- a/plugins/SmileyAdd/src/download.cpp +++ b/plugins/SmileyAdd/src/download.cpp @@ -87,24 +87,21 @@ bool InternetDownloadFile(const char *szUrl, char *szDest, HNETLIBCONN &hHttpDwn else if (nlhrReply->resultCode == 302 || nlhrReply->resultCode == 301 || nlhrReply->resultCode == 307) { // page moved // get the url for the new location and save it to szInfo // look for the reply header "Location" - for (int i = 0; i < nlhrReply->headersCount; i++) { - if (!mir_strcmp(nlhrReply->headers[i].szName, "Location")) { - size_t rlen = 0; - if (nlhrReply->headers[i].szValue[0] == '/') { - const char *szPref = strstr(szUrl, "://"); - szPref = szPref ? szPref + 3 : szUrl; - const char *szPath = strchr(szPref, '/'); - rlen = szPath != nullptr ? szPath - szUrl : mir_strlen(szUrl); - } - - szRedirUrl = (char*)mir_realloc(szRedirUrl, rlen + mir_strlen(nlhrReply->headers[i].szValue) * 3 + 1); - - strncpy(szRedirUrl, szUrl, rlen); - mir_strcpy(szRedirUrl + rlen, nlhrReply->headers[i].szValue); - - nlhr.szUrl = szRedirUrl; - break; + if (auto *pszUrl = Netlib_GetHeader(nlhrReply, "Location")) { + size_t rlen = 0; + if (pszUrl[0] == '/') { + const char *szPref = strstr(szUrl, "://"); + szPref = szPref ? szPref + 3 : szUrl; + const char *szPath = strchr(szPref, '/'); + rlen = szPath != nullptr ? szPath - szUrl : mir_strlen(szUrl); } + + szRedirUrl = (char *)mir_realloc(szRedirUrl, rlen + mir_strlen(pszUrl) * 3 + 1); + + strncpy(szRedirUrl, szUrl, rlen); + mir_strcpy(szRedirUrl + rlen, pszUrl); + + nlhr.szUrl = szRedirUrl; } } else result = 1; diff --git a/protocols/CloudFile/src/Services/google_service.cpp b/protocols/CloudFile/src/Services/google_service.cpp index db4cfc1cbd..c7498d936c 100644 --- a/protocols/CloudFile/src/Services/google_service.cpp +++ b/protocols/CloudFile/src/Services/google_service.cpp @@ -179,16 +179,11 @@ auto CGDriveService::CreateUploadSession(const std::string &parentId, const std: HandleHttpError(response); - if (HTTP_CODE_SUCCESS(response->resultCode)) { - for (int i = 0; i < response->headersCount; i++) { - if (mir_strcmpi(response->headers[i].szName, "Location")) - continue; - return std::string(response->headers[i].szValue); - } - } + if (HTTP_CODE_SUCCESS(response->resultCode)) + if (auto *pszHdr = Netlib_GetHeader(response, "Location")) + return std::string(pszHdr); HttpResponseToError(response); - return std::string(); } diff --git a/protocols/Discord/src/avatars.cpp b/protocols/Discord/src/avatars.cpp index c0a5129b8b..d11409dae1 100644 --- a/protocols/Discord/src/avatars.cpp +++ b/protocols/Discord/src/avatars.cpp @@ -69,11 +69,8 @@ LBL_Error: return; } - for (int i = 0; i < reply->headersCount; i++) - if (!mir_strcmp(reply->headers[i].szName, "Content-Type")) { - ai.format = ProtoGetAvatarFormatByMimeType(reply->headers[i].szValue); - break; - } + if (auto *pszHdr = Netlib_GetHeader(reply, "Content-Type")) + ai.format = ProtoGetAvatarFormatByMimeType(pszHdr); if (ai.format == PA_FORMAT_UNKNOWN) { debugLogA("unknown avatar mime type"); diff --git a/protocols/Discord/src/gateway.cpp b/protocols/Discord/src/gateway.cpp index fae8eef7cc..6b5e320904 100644 --- a/protocols/Discord/src/gateway.cpp +++ b/protocols/Discord/src/gateway.cpp @@ -56,15 +56,13 @@ bool CDiscordProto::GatewayThreadWorker() debugLogA("Gateway connection succeeded"); m_hGatewayConnection = pReply->nlc; - for (int i=0; i < pReply->headersCount; i++) - if (!mir_strcmp(pReply->headers[i].szName, "Set-Cookie")) { - m_szCookie = pReply->headers[i].szValue; + if (auto *pszHdr = Netlib_GetHeader(pReply, "Set-Cookie")) { + m_szCookie = pszHdr; - int idx = m_szCookie.Find(';'); - if (idx != -1) - m_szCookie.Truncate(idx); - break; - } + int idx = m_szCookie.Find(';'); + if (idx != -1) + m_szCookie.Truncate(idx); + } Netlib_FreeHttpRequest(pReply); bool bExit = false; diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp index 94f87be530..2ce92b2281 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -173,12 +173,10 @@ void CDiscordProto::OnReceiveMyInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest* m_wszEmail = root["email"].as_mstring(); m_ownId = id; - for (int i = 0; i < pReply->headersCount; i++) { - if (!strcmp(pReply->headers[i].szName, "Set-Cookie")) { - char *p = strchr(pReply->headers[i].szValue, ';'); - if (p) *p = 0; - m_szAccessCookie = mir_strdup(pReply->headers[i].szValue); - } + if (auto *pszHdr = Netlib_GetHeader(pReply, "Set-Cookie")) { + char *p = strchr(pszHdr, ';'); + if (p) *p = 0; + m_szAccessCookie = mir_strdup(pszHdr); } OnLoggedIn(); diff --git a/protocols/Facebook/src/avatars.cpp b/protocols/Facebook/src/avatars.cpp index 1823c8da4d..76754bbefc 100644 --- a/protocols/Facebook/src/avatars.cpp +++ b/protocols/Facebook/src/avatars.cpp @@ -71,11 +71,8 @@ void __cdecl FacebookProto::AvatarsUpdate(void *) bool bSuccess = false; if (pReply->resultCode == 200 && pReply->pData && pReply->dataLength) { - for (int i = 0; i < pReply->headersCount; i++) - if (!mir_strcmp(pReply->headers[i].szName, "Content-Type")) { - ai.format = ProtoGetAvatarFormatByMimeType(pReply->headers[i].szValue); - break; - } + if (auto *pszHdr = Netlib_GetHeader(pReply, "Content-Type")) + ai.format = ProtoGetAvatarFormatByMimeType(pszHdr); if (ai.format != PA_FORMAT_UNKNOWN) { FILE *fout = _wfopen(ai.filename, L"wb"); diff --git a/protocols/ICQ-WIM/src/http.cpp b/protocols/ICQ-WIM/src/http.cpp index 4826a411c6..b41a45404b 100644 --- a/protocols/ICQ-WIM/src/http.cpp +++ b/protocols/ICQ-WIM/src/http.cpp @@ -179,13 +179,10 @@ bool CIcqProto::ExecuteRequest(AsyncHttpRequest *pReq) auto &conn = m_ConnPool[pReq->m_conn]; conn.s = reply->nlc; conn.timeout = 0; - for (int i = 0; i < reply->headersCount; i++) { - if (!mir_strcmp(reply->headers[i].szName, "Keep-Alive")) { - int timeout; - if (1 == sscanf(reply->headers[i].szValue, "timeout=%d", &timeout)) - conn.timeout = timeout; - break; - } + if (auto *pszHdr = Netlib_GetHeader(reply, "Keep-Alive")) { + int timeout; + if (1 == sscanf(pszHdr, "timeout=%d", &timeout)) + conn.timeout = timeout; } } diff --git a/protocols/ICQ-WIM/src/server.cpp b/protocols/ICQ-WIM/src/server.cpp index 4bf8ab7385..0e95605bdd 100644 --- a/protocols/ICQ-WIM/src/server.cpp +++ b/protocols/ICQ-WIM/src/server.cpp @@ -38,17 +38,13 @@ void CIcqProto::CheckAvatarChange(MCONTACT hContact, const JSONNode &ev) } setWString(hContact, "IconId", wszIconId); - } - else delSetting(hContact, "IconId"); - CMStringA szUrl(ev["bigBuddyIcon"].as_mstring()); - if (szUrl.IsEmpty()) - szUrl = ev["buddyIcon"].as_mstring(); - if (!szUrl.IsEmpty()) { - auto *pReq = new AsyncHttpRequest(CONN_MAIN, REQUEST_GET, szUrl, &CIcqProto::OnReceiveAvatar); + auto *pReq = new AsyncHttpRequest(CONN_MAIN, REQUEST_GET, ICQ_API_SERVER "/expressions/get", &CIcqProto::OnReceiveAvatar); + pReq << CHAR_PARAM("f", "native") << WCHAR_PARAM("t", GetUserId(hContact)) << CHAR_PARAM("type", "bigBuddyIcon"); pReq->hContact = hContact; Push(pReq); } + else delSetting(hContact, "IconId"); } void CIcqProto::CheckLastId(MCONTACT hContact, const JSONNode &ev) @@ -889,8 +885,11 @@ LBL_Error: return; } - const wchar_t *pwszExtension; - ai.format = ProtoGetBufferFormat(pReply->pData, &pwszExtension); + const char *szContentType = Netlib_GetHeader(pReply, "Content-Type"); + if (szContentType == nullptr) + szContentType = "image/jpeg"; + + ai.format = ProtoGetAvatarFormatByMimeType(szContentType); setByte(pReq->hContact, "AvatarType", ai.format); GetAvatarFileName(pReq->hContact, ai.filename, _countof(ai.filename)); diff --git a/protocols/MSN/src/msn_auth.cpp b/protocols/MSN/src/msn_auth.cpp index 97b4655b9a..edfd81452f 100644 --- a/protocols/MSN/src/msn_auth.cpp +++ b/protocols/MSN/src/msn_auth.cpp @@ -1055,18 +1055,13 @@ int CMsnProto::MSN_AuthOAuth(void) mHttpsTS = clock(); if (nlhrReply2) { char *pszURL = nullptr, *pAccessToken, *pEnd; + hHttpsConnection = nlhrReply2->nlc; - bPassportAuth = true; if (nlhrReply2->resultCode == 302) { /* Extract access_token from Location can be found */ - for (int i = 0; i < nlhrReply2->headersCount; i++) { - if (!mir_strcmpi(nlhrReply2->headers[i].szName, "Location")) { - pszURL = nlhrReply2->headers[i].szValue; - break; - } - } + pszURL = Netlib_GetHeader(nlhrReply2, "Location"); } else { /* There may be a problem with login, i.e. M$ security measures. Open up browser diff --git a/protocols/Non-IM Contact/src/http.cpp b/protocols/Non-IM Contact/src/http.cpp index 55e648c248..466760d790 100644 --- a/protocols/Non-IM Contact/src/http.cpp +++ b/protocols/Non-IM Contact/src/http.cpp @@ -49,35 +49,31 @@ int InternetDownloadFile(char *szUrl) // download the page NLHR_PTR nlhrReply(Netlib_HttpTransaction(hNetlibUser, &nlhr)); - if (nlhrReply) { - // return error code if the recieved code is neither 200 OK or 302 Moved - if (nlhrReply->resultCode != 200 && nlhrReply->resultCode != 302) - return nlhrReply->resultCode; - // 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)); - } - // if the recieved code is 302 Moved, Found, etc - else if (nlhrReply->resultCode == 302) { // page moved - int i; - // get the url for the new location and save it to szInfo - // look for the reply header "Location" - for (i = 0; i < nlhrReply->headersCount; i++) { - if (!mir_strcmp(nlhrReply->headers[i].szName, "Location")) { - szData = (char *)malloc(512); - // add "Moved/Location:" in front of the new URL for identification - mir_snprintf(szData, 512, "Moved/Location: %s\n", nlhrReply->headers[i].szValue); - break; - } - } - // log the new url into netlib log - Netlib_Log(hNetlibUser, szData); + if (nlhrReply == nullptr) + return 1; // if the data does not downloaded successfully (ie. disconnected), then return 1 as error code + + // return error code if the recieved code is neither 200 OK or 302 Moved + if (nlhrReply->resultCode != 200 && nlhrReply->resultCode != 302) + return nlhrReply->resultCode; + // 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)); + } + // 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")) { + szData = (char *)malloc(512); + // add "Moved/Location:" in front of the new URL for identification + mir_snprintf(szData, 512, "Moved/Location: %s\n", pszHdr); } + + // log the new url into netlib log + Netlib_Log(hNetlibUser, szData); } - // if the data does not downloaded successfully (ie. disconnected), then return 1 as error code - else return 1; // make a copy of the retrieved data, then free the memory of the http reply szInfo = szData; diff --git a/protocols/SkypeWeb/src/skype_mslogin.cpp b/protocols/SkypeWeb/src/skype_mslogin.cpp index 4846ae6243..30272829f7 100644 --- a/protocols/SkypeWeb/src/skype_mslogin.cpp +++ b/protocols/SkypeWeb/src/skype_mslogin.cpp @@ -39,12 +39,9 @@ void CSkypeProto::OnMSLoginFirst(const NETLIBHTTPREQUEST *response) } std::string PPTF = match[1]; - for (int i = 0; i < response->headersCount; i++) { - if (mir_strcmpi(response->headers[i].szName, "Set-Cookie")) - continue; - + if (auto *pszHdr = Netlib_GetHeader(response, "Set-Cookie")) { regex = "^(.+?)=(.+?);"; - content = response->headers[i].szValue; + content = pszHdr; if (std::regex_search(content, match, regex)) scookies[match[1]] = match[2]; } @@ -71,12 +68,9 @@ void CSkypeProto::OnMSLoginSecond(const NETLIBHTTPREQUEST *response) if (std::regex_search(content, match, regex)) { if (match[1] == "i5600") { CMStringA szCookies; - for (int i = 0; i < response->headersCount; i++) { - if (mir_strcmpi(response->headers[i].szName, "Set-Cookie")) - continue; - + if (auto *pszHdr = Netlib_GetHeader(response, "Set-Cookie")) { regex = "^(.+?)=(.+?);"; - content = response->headers[i].szValue; + content = pszHdr; if (std::regex_search(content, match, regex)) if (!std::string(match[2]).empty() && std::string(match[2]) != " ") szCookies.AppendFormat("%s=%s;", std::string(match[1]).c_str(), std::string(match[2]).c_str()); @@ -85,7 +79,6 @@ void CSkypeProto::OnMSLoginSecond(const NETLIBHTTPREQUEST *response) CMStringA url(GetStringChunk(szContent, "urlPost:'", "'")); CMStringA ppft(GetStringChunk(szContent, "sFT:'", "'")); - ptrA code(mir_utf8encodeW(RunConfirmationCode())); SendRequest(new LoginMSRequest(url.c_str(), ptrA(getStringA(SKYPE_SETTINGS_ID)), szCookies.c_str(), ppft.c_str(), code), &CSkypeProto::OnMSLoginEnd); diff --git a/protocols/SkypeWeb/src/skype_oauth.cpp b/protocols/SkypeWeb/src/skype_oauth.cpp index d00fc7fa10..dcb087322b 100644 --- a/protocols/SkypeWeb/src/skype_oauth.cpp +++ b/protocols/SkypeWeb/src/skype_oauth.cpp @@ -39,12 +39,9 @@ void CSkypeProto::OnOAuthStart(const NETLIBHTTPREQUEST *response) } std::string PPTF = match[1]; - for (int i = 0; i < response->headersCount; i++) { - if (mir_strcmpi(response->headers[i].szName, "Set-Cookie")) - continue; - + if (auto *pszHdr = Netlib_GetHeader(response, "Set-Cookie")) { regex = "^(.+?)=(.+?);"; - content = response->headers[i].szValue; + content = pszHdr; if (std::regex_search(content, match, regex)) scookies[match[1]] = match[2]; } diff --git a/protocols/VKontakte/src/misc.cpp b/protocols/VKontakte/src/misc.cpp index a0d97b4ff9..d94361bb96 100644 --- a/protocols/VKontakte/src/misc.cpp +++ b/protocols/VKontakte/src/misc.cpp @@ -37,15 +37,6 @@ bool IsEmpty(LPCSTR str) return (str == nullptr || str[0] == 0); } -LPCSTR findHeader(NETLIBHTTPREQUEST *pReq, LPCSTR szField) -{ - for (int i = 0; i < pReq->headersCount; i++) - if (!_stricmp(pReq->headers[i].szName, szField)) - return pReq->headers[i].szValue; - - return nullptr; -} - bool wlstrstr(wchar_t *_s1, wchar_t *_s2) { wchar_t s1[1024], s2[1024]; @@ -487,11 +478,8 @@ CMStringW CVkProto::RunRenameNick(LPCWSTR pwszOldName) void CVkProto::GrabCookies(NETLIBHTTPREQUEST *nhr) { debugLogA("CVkProto::GrabCookies"); - for (int i = 0; i < nhr->headersCount; i++) { - if (_stricmp(nhr->headers[i].szName, "Set-cookie")) - continue; - - CMStringA szValue = nhr->headers[i].szValue, szCookieName, szCookieVal, szDomain; + if (auto *pszCookie = Netlib_GetHeader(nhr, "Set-cookie")) { + CMStringA szValue = pszCookie, szCookieName, szCookieVal, szDomain; int iStart = 0; while (true) { bool bFirstToken = (iStart == 0); diff --git a/protocols/VKontakte/src/vk.h b/protocols/VKontakte/src/vk.h index 12f9f378d9..1f6b5107c7 100644 --- a/protocols/VKontakte/src/vk.h +++ b/protocols/VKontakte/src/vk.h @@ -104,7 +104,6 @@ struct CVkProto; extern mir_cs csInstances; extern bool g_bMessageState; -LPCSTR findHeader(NETLIBHTTPREQUEST *hdr, LPCSTR szField); bool wlstrstr(wchar_t *_s1, wchar_t *_s2); void InitIcons(void); diff --git a/protocols/VKontakte/src/vk_struct.cpp b/protocols/VKontakte/src/vk_struct.cpp index cbe70b3566..90f3e7cba5 100644 --- a/protocols/VKontakte/src/vk_struct.cpp +++ b/protocols/VKontakte/src/vk_struct.cpp @@ -67,11 +67,8 @@ AsyncHttpRequest::AsyncHttpRequest(CVkProto *ppro, int iRequestType, LPCSTR _url void AsyncHttpRequest::Redirect(NETLIBHTTPREQUEST *nhr) { - for (int i = 0; i < nhr->headersCount; i++) { - LPCSTR szValue = nhr->headers[i].szValue; - if (!_stricmp(nhr->headers[i].szName, "Location")) - m_szUrl = szValue; - } + if (auto *pszHdr = Netlib_GetHeader(nhr, "Location")) + m_szUrl = pszHdr; } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/VKontakte/src/vk_thread.cpp b/protocols/VKontakte/src/vk_thread.cpp index cbc9b269df..2669fdc883 100644 --- a/protocols/VKontakte/src/vk_thread.cpp +++ b/protocols/VKontakte/src/vk_thread.cpp @@ -199,7 +199,7 @@ void CVkProto::OnOAuthAuthorize(NETLIBHTTPREQUEST *reply, AsyncHttpRequest*) GrabCookies(reply); if (reply->resultCode == 302) { // manual redirect - LPCSTR pszLocation = findHeader(reply, "Location"); + LPCSTR pszLocation = Netlib_GetHeader(reply, "Location"); if (pszLocation) { if (!_strnicmp(pszLocation, szBlankUrl, sizeof(szBlankUrl) - 1)) { m_szAccessToken = nullptr; diff --git a/protocols/Weather/src/weather_http.cpp b/protocols/Weather/src/weather_http.cpp index b0672282c1..5ef55cd7e8 100644 --- a/protocols/Weather/src/weather_http.cpp +++ b/protocols/Weather/src/weather_http.cpp @@ -27,16 +27,6 @@ from the web using netlib HNETLIBUSER hNetlibUser; -static int findHeader(const NETLIBHTTPREQUEST *nlhrReply, const char *hdr) -{ - for (int i = 0; i < nlhrReply->headersCount; i++) { - if (_stricmp(nlhrReply->headers[i].szName, hdr) == 0) { - return i; - } - } - return -1; -} - //============ DOWNLOAD NEW WEATHER ============ // // function to download webpage from the internet @@ -90,9 +80,9 @@ int InternetDownloadFile(char *szUrl, char *cookie, char *userAgent, wchar_t **s result = 0; // allocate memory and save the retrieved data - int i = findHeader(nlhrReply, "Content-Type"); + auto *pszHdr = Netlib_GetHeader(nlhrReply, "Content-Type"); // look for Content-Type=utf-8 in header - if (i != -1 && strstr(_strlwr(nlhrReply->headers[i].szValue), "utf-8")) + if (pszHdr && strstr(pszHdr, "utf-8")) bIsUtf = true; else { char *end = nlhrReply->pData; diff --git a/src/mir_app/src/mir_app.def b/src/mir_app/src/mir_app.def index a178beb036..d4d62e5f0a 100644 --- a/src/mir_app/src/mir_app.def +++ b/src/mir_app/src/mir_app.def @@ -256,6 +256,7 @@ Font_RegisterW @326 Options_AddPage @327 Options_Open @328 Options_OpenPage @329 +Netlib_GetHeader @330 Srmm_ModifyButton @331 Srmm_AddButton @332 Srmm_GetButtonState @333 diff --git a/src/mir_app/src/mir_app64.def b/src/mir_app/src/mir_app64.def index d6f40b51b8..bd99493d60 100644 --- a/src/mir_app/src/mir_app64.def +++ b/src/mir_app/src/mir_app64.def @@ -256,6 +256,7 @@ Font_RegisterW @326 Options_AddPage @327 Options_Open @328 Options_OpenPage @329 +Netlib_GetHeader @330 Srmm_ModifyButton @331 Srmm_AddButton @332 Srmm_GetButtonState @333 diff --git a/src/mir_app/src/netlib_http.cpp b/src/mir_app/src/netlib_http.cpp index 6764222929..5bd58996dd 100644 --- a/src/mir_app/src/netlib_http.cpp +++ b/src/mir_app/src/netlib_http.cpp @@ -105,10 +105,13 @@ static int RecvWithTimeoutTime(NetlibConnection *nlc, unsigned dwTimeoutTime, ch return Netlib_Recv(nlc, buf, len, flags); } -static char* NetlibHttpFindHeader(NETLIBHTTPREQUEST *nlhrReply, const char *hdr) +MIR_APP_DLL(char *) Netlib_GetHeader(const NETLIBHTTPREQUEST *nlhr, const char *hdr) { - for (int i=0; i < nlhrReply->headersCount; i++) { - NETLIBHTTPHEADER &p = nlhrReply->headers[i]; + if (nlhr == nullptr || hdr == nullptr) + return nullptr; + + for (int i=0; i < nlhr->headersCount; i++) { + NETLIBHTTPHEADER &p = nlhr->headers[i]; if (_stricmp(p.szName, hdr) == 0) return p.szValue; } @@ -552,7 +555,7 @@ MIR_APP_DLL(int) Netlib_SendHttpRequest(HNETLIBCONN nlc, NETLIBHTTPREQUEST *nlhr nlhrReply = NetlibHttpRecv(nlc, hflags, dflags); if (nlhrReply) { - char* tmpUrl = NetlibHttpFindHeader(nlhrReply, "Location"); + auto *tmpUrl = Netlib_GetHeader(nlhrReply, "Location"); if (tmpUrl) { size_t rlen = 0; if (tmpUrl[0] == '/') { @@ -825,8 +828,8 @@ MIR_APP_DLL(NETLIBHTTPREQUEST*) Netlib_HttpTransaction(HNETLIBUSER nlu, NETLIBHT NETLIBHTTPREQUEST nlhrSend = *nlhr; nlhrSend.flags |= NLHRF_SMARTREMOVEHOST; - bool doneUserAgentHeader = NetlibHttpFindHeader(nlhr, "User-Agent") != nullptr; - bool doneAcceptEncoding = NetlibHttpFindHeader(nlhr, "Accept-Encoding") != nullptr; + bool doneUserAgentHeader = Netlib_GetHeader(nlhr, "User-Agent") != nullptr; + bool doneAcceptEncoding = Netlib_GetHeader(nlhr, "Accept-Encoding") != nullptr; if (!doneUserAgentHeader || !doneAcceptEncoding) { nlhrSend.headers = (NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER) * (nlhrSend.headersCount + 2)); memcpy(nlhrSend.headers, nlhr->headers, sizeof(NETLIBHTTPHEADER) * nlhr->headersCount); -- cgit v1.2.3