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 --- plugins/CrashDumper/src/upload.cpp | 35 ++++++++++++++++------------------- plugins/SmileyAdd/src/download.cpp | 31 ++++++++++++++----------------- 2 files changed, 30 insertions(+), 36 deletions(-) (limited to 'plugins') 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; -- cgit v1.2.3