diff options
author | George Hazan <ghazan@miranda.im> | 2020-01-13 16:48:55 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2020-01-13 16:48:55 +0300 |
commit | 87a2660299edd64cbb6f6c92c33683e91a6d187c (patch) | |
tree | a1b0777ec5b8afc0c49fbb66cf6a122b5ac82c6d /protocols | |
parent | 21f52dbfa251d171b4cc9dc315e8736da2e2be08 (diff) |
Netlib_GetHeader() - handful utility to avoid writing cycles
Diffstat (limited to 'protocols')
-rw-r--r-- | protocols/CloudFile/src/Services/google_service.cpp | 11 | ||||
-rw-r--r-- | protocols/Discord/src/avatars.cpp | 7 | ||||
-rw-r--r-- | protocols/Discord/src/gateway.cpp | 14 | ||||
-rw-r--r-- | protocols/Discord/src/server.cpp | 10 | ||||
-rw-r--r-- | protocols/Facebook/src/avatars.cpp | 7 | ||||
-rw-r--r-- | protocols/ICQ-WIM/src/http.cpp | 11 | ||||
-rw-r--r-- | protocols/ICQ-WIM/src/server.cpp | 17 | ||||
-rw-r--r-- | protocols/MSN/src/msn_auth.cpp | 9 | ||||
-rw-r--r-- | protocols/Non-IM Contact/src/http.cpp | 50 | ||||
-rw-r--r-- | protocols/SkypeWeb/src/skype_mslogin.cpp | 15 | ||||
-rw-r--r-- | protocols/SkypeWeb/src/skype_oauth.cpp | 7 | ||||
-rw-r--r-- | protocols/VKontakte/src/misc.cpp | 16 | ||||
-rw-r--r-- | protocols/VKontakte/src/vk.h | 1 | ||||
-rw-r--r-- | protocols/VKontakte/src/vk_struct.cpp | 7 | ||||
-rw-r--r-- | protocols/VKontakte/src/vk_thread.cpp | 2 | ||||
-rw-r--r-- | protocols/Weather/src/weather_http.cpp | 14 |
16 files changed, 67 insertions, 131 deletions
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; |