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/Non-IM Contact/src | |
parent | 21f52dbfa251d171b4cc9dc315e8736da2e2be08 (diff) |
Netlib_GetHeader() - handful utility to avoid writing cycles
Diffstat (limited to 'protocols/Non-IM Contact/src')
-rw-r--r-- | protocols/Non-IM Contact/src/http.cpp | 50 |
1 files changed, 23 insertions, 27 deletions
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; |