From 72b9c5a23c8a5e767860269a13672e133a3d1021 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 28 Sep 2013 16:46:38 +0000 Subject: custom redirect handler removed git-svn-id: http://svn.miranda-ng.org/main/trunk@6258 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Weather/src/weather_http.cpp | 188 +++++++++++++-------------------- plugins/Weather/src/weather_update.cpp | 57 +++++----- 2 files changed, 100 insertions(+), 145 deletions(-) (limited to 'plugins/Weather/src') diff --git a/plugins/Weather/src/weather_http.cpp b/plugins/Weather/src/weather_http.cpp index 7cf39bfd54..ddb5cd973c 100644 --- a/plugins/Weather/src/weather_http.cpp +++ b/plugins/Weather/src/weather_http.cpp @@ -47,137 +47,95 @@ int findHeader(NETLIBHTTPREQUEST *nlhrReply, char *hdr) // szUrl = URL of the webpage to be retrieved // return value = 0 for success, 1 or HTTP error code for failure // global var used: szData, szInfo = containing the retrieved data -int InternetDownloadFile (char *szUrl, char* cookie, TCHAR** szData) + +int InternetDownloadFile (char *szUrl, char *cookie, TCHAR **szData) { - int result = 0xBADBAD; - char* szRedirUrl = NULL; - NETLIBHTTPREQUEST nlhr = {0}; - NETLIBHTTPHEADER headers[6]; + NETLIBHTTPHEADER headers[5]; + headers[0].szName = "User-Agent"; + headers[0].szValue = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; + 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 - nlhr.cbSize = sizeof(nlhr); + NETLIBHTTPREQUEST nlhr = { sizeof(nlhr) }; nlhr.requestType = REQUEST_GET; nlhr.flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11 | NLHRF_PERSISTENT | NLHRF_REDIRECT; nlhr.szUrl = szUrl; nlhr.nlc = hNetlibHttp; - - // change the header so the plugin is pretended to be IE 6 + WinXP - nlhr.headersCount = 5; nlhr.headers = headers; - nlhr.headers[0].szName = "User-Agent"; - nlhr.headers[0].szValue = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; - 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"; - nlhr.headers[4].szName = "Cookie"; - nlhr.headers[4].szValue = cookie; -// nlhr.headers[5].szName = "If-Modified-Since"; -// nlhr.headers[5].szValue = "Tue, 24 Feb 2009 03:44:23 GMT"; - - if (cookie == NULL || cookie[0] == 0) --nlhr.headersCount; - - while (result == 0xBADBAD) { - // download the page - NETLIBHTTPREQUEST *nlhrReply = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, - (WPARAM)hNetlibUser, (LPARAM)&nlhr); - - if (nlhrReply) { - // if the recieved code is 200 OK - if (nlhrReply->resultCode == 200) { - if (nlhrReply->dataLength) { - result = 0; - bool bIsUtf = false; - - // allocate memory and save the retrieved data - int i = findHeader(nlhrReply, "Content-Type"); - if (i != -1 && strstr(_strlwr((char*)nlhrReply->headers[i].szValue), "utf-8")) - bIsUtf = true; - else { - char* end = nlhrReply->pData; - for (;;) { - char* beg = strstr(end, "'); - tmp = *end; *end = 0; - - method = strstr(beg, "http-equiv=\""); - if (method && _strnicmp(method+12, "Content-Type", 12) == 0 && strstr(method, "utf-8")) { - bIsUtf = true; - break; - } - else *end = tmp; - } } } - - if ( bIsUtf ) - *szData = mir_utf8decodeT( nlhrReply->pData ); - else - *szData = mir_a2t( nlhrReply->pData ); + nlhr.headersCount = SIZEOF(headers); + + if (cookie == NULL || cookie[0] == 0) + --nlhr.headersCount; + + // download the page + NETLIBHTTPREQUEST *nlhrReply = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)hNetlibUser, (LPARAM)&nlhr); + if (nlhrReply == 0) { + // if the data does not downloaded successfully (ie. disconnected), then return 1000 as error code + *szData = (TCHAR*)mir_alloc(512); + // store the error code in szData + _tcscpy(*szData, _T("NetLib error occurred!!")); + hNetlibHttp = NULL; + return NLHRF_REDIRECT; + } - } - else result = DATA_EMPTY; - } - // if the recieved code is 302 Moved, Found, etc - // workaround for url forwarding - else if (nlhrReply->resultCode == 302 || nlhrReply->resultCode == 301 || nlhrReply->resultCode == 303 ) // page moved - { - // get the url for the new location and save it to szInfo - // look for the reply header "Location" - int i = findHeader(nlhrReply, "Location"); - - if (i != -1) - { - size_t rlen = 0; - if (nlhrReply->headers[i].szValue[0] == '/') - { - char* szPath; - char* szPref = strstr(szUrl, "://"); - szPref = szPref ? szPref + 3 : szUrl; - szPath = strchr(szPref, '/'); - rlen = szPath != NULL ? szPath - szUrl : strlen(szUrl); + // if the recieved code is 200 OK + int result; + if (nlhrReply->resultCode == 200) { + if (nlhrReply->dataLength) { + bool bIsUtf = false; + result = 0; + + // allocate memory and save the retrieved data + int i = findHeader(nlhrReply, "Content-Type"); + if (i != -1 && strstr(_strlwr((char*)nlhrReply->headers[i].szValue), "utf-8")) + bIsUtf = true; + else { + char* end = nlhrReply->pData; + for (;;) { + char* beg = strstr(end, "'); + tmp = *end; *end = 0; + + method = strstr(beg, "http-equiv=\""); + if (method && _strnicmp(method+12, "Content-Type", 12) == 0 && strstr(method, "utf-8")) { + bIsUtf = true; + break; + } + else *end = tmp; } - - szRedirUrl = (char*)mir_realloc(szRedirUrl, - rlen + strlen(nlhrReply->headers[i].szValue)*3 + 1); - - strncpy(szRedirUrl, szUrl, rlen); - strcpy(szRedirUrl+rlen, nlhrReply->headers[i].szValue); - - GetSearchStr(szRedirUrl); - - nlhr.szUrl = szRedirUrl; } } - // return error code if the recieved code is neither 200 OK nor 302 Moved - else { - *szData = ( TCHAR* )mir_alloc(512); - // store the error code in szData - mir_sntprintf(*szData, 512, _T("Error occured! HTTP Error: %i\n"), nlhrReply->resultCode); - result = (int)nlhrReply->resultCode; - } - hNetlibHttp = nlhrReply->nlc; - // make a copy of the retrieved data, then free the memory of the http reply - CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT,0, (LPARAM)nlhrReply); + TCHAR *retVal = NULL; + if (bIsUtf) + retVal = mir_utf8decodeT( nlhrReply->pData ); + if (retVal == NULL) + retVal = mir_a2t(nlhrReply->pData); + *szData = retVal; } - // if the data does not downloaded successfully (ie. disconnected), then return 1000 as error code - else - { - *szData = ( TCHAR* )mir_alloc(512); - // store the error code in szData - _tcscpy(*szData, _T("NetLib error occurred!!")); - result = NETLIB_ERROR; - hNetlibHttp = NULL; - } - + else result = DATA_EMPTY; + } + // return error code if the recieved code is neither 200 OK nor 302 Moved + else { + *szData = (TCHAR*)mir_alloc(512); + // store the error code in szData + mir_sntprintf(*szData, 512, _T("Error occured! HTTP Error: %i\n"), nlhrReply->resultCode); + result = (int)nlhrReply->resultCode; } - mir_free(szRedirUrl); - + hNetlibHttp = nlhrReply->nlc; + // make a copy of the retrieved data, then free the memory of the http reply + CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT,0, (LPARAM)nlhrReply); return result; } diff --git a/plugins/Weather/src/weather_update.cpp b/plugins/Weather/src/weather_update.cpp index 72eeb2cbf7..c013902ea4 100644 --- a/plugins/Weather/src/weather_update.cpp +++ b/plugins/Weather/src/weather_update.cpp @@ -40,7 +40,8 @@ int UpdateWeather(HANDLE hContact) DBVARIANT dbv; BOOL Ch = FALSE; - if (hContact == NULL) return 1; // some error prevention + if (hContact == NULL) // some error prevention + return 1; dbv.pszVal = ""; @@ -53,11 +54,10 @@ int UpdateWeather(HANDLE hContact) // download the info and parse it // result are stored in database int code = GetWeatherData(hContact); - if (code != 0) - { + if (code != 0) { // error occurs if the return value is not equals to 0 - if (opt.ShowWarnings) - { // show warnings by popup + if (opt.ShowWarnings) { + // show warnings by popup mir_sntprintf(str, SIZEOF(str) - 105, TranslateT("Unable to retrieve weather information for %s"), dbv.ptszVal); _tcscat(str, _T("\n")); @@ -67,10 +67,10 @@ int UpdateWeather(HANDLE hContact) // log to netlib Netlib_LogfT(hNetlibUser, _T("Error! Update cannot continue... Start to free memory")); Netlib_LogfT(hNetlibUser, _T("<-- Error occurs while updating station: %s -->"), dbv.ptszVal); - if ( !dbres) db_free(&dbv); + if (!dbres) db_free(&dbv); return 1; } - if ( !dbres) db_free(&dbv); + if (!dbres) db_free(&dbv); // initialize, load new weather Data WEATHERINFO winfo = LoadWeatherInfo(hContact); @@ -170,6 +170,7 @@ int UpdateWeather(HANDLE hContact) // for the option for overwriting the file, delete old file first if (db_get_b(hContact,WEATHERPROTONAME, "Overwrite",0)) DeleteFile(dbv.ptszVal); + // open the file and set point to the end of file FILE *file = _tfopen( dbv.ptszVal, _T("a")); db_free(&dbv); @@ -178,7 +179,9 @@ int UpdateWeather(HANDLE hContact) GetDisplay(&winfo, opt.eText, str2); _fputts(str2, file); fclose(file); - } } } + } + } + } if (db_get_b(hContact, WEATHERPROTONAME, "History", 0)) { // internal log using history @@ -238,15 +241,15 @@ HANDLE UpdateGetFirst() WaitForSingleObject(hUpdateMutex, INFINITE); - if (UpdateListHead != NULL) - { + if (UpdateListHead != NULL) { UPDATELIST* Item = UpdateListHead; hContact = Item->hContact; UpdateListHead = Item->next; mir_free(Item); - if (UpdateListHead == NULL) UpdateListTail = NULL; + if (UpdateListHead == NULL) + UpdateListTail = NULL; } ReleaseMutex(hUpdateMutex); @@ -258,11 +261,9 @@ void DestroyUpdateList(void) { WaitForSingleObject(hUpdateMutex, INFINITE); - UPDATELIST *temp = UpdateListHead; - // free the list one by one - while (temp != NULL) - { + UPDATELIST *temp = UpdateListHead; + while (temp != NULL) { UpdateListHead = temp->next; mir_free(temp); temp = UpdateListHead; @@ -273,7 +274,6 @@ void DestroyUpdateList(void) ReleaseMutex(hUpdateMutex); } - //============ UPDATE WEATHER ============ // update all weather station @@ -282,12 +282,11 @@ void DestroyUpdateList(void) void UpdateAll(BOOL AutoUpdate, BOOL RemoveData) { // add all weather contact to the update queue list - for (HANDLE hContact = db_find_first(WEATHERPROTONAME); hContact; hContact = db_find_next(hContact, WEATHERPROTONAME)) { + for (HANDLE hContact = db_find_first(WEATHERPROTONAME); hContact; hContact = db_find_next(hContact, WEATHERPROTONAME)) if ( !db_get_b(hContact,WEATHERPROTONAME, "AutoUpdate",FALSE) || !AutoUpdate) { if (RemoveData) DBDataManage((HANDLE)hContact, WDBM_REMOVE, 0, 0); UpdateListAdd(hContact); } - } // if it is not updating, then start the update thread process // if it is updating, the stations just added to the queue will get updated by the already-running process @@ -299,8 +298,7 @@ void UpdateAll(BOOL AutoUpdate, BOOL RemoveData) // wParam = handle for the weather station that is going to be updated INT_PTR UpdateSingleStation(WPARAM wParam, LPARAM lParam) { - if (IsMyContact((HANDLE)wParam)) - { + if (IsMyContact((HANDLE)wParam)) { // add the station to the end of the update queue UpdateListAdd((HANDLE)wParam); @@ -318,8 +316,7 @@ INT_PTR UpdateSingleStation(WPARAM wParam, LPARAM lParam) // wParam = handle for the weather station that is going to be updated INT_PTR UpdateSingleRemove(WPARAM wParam, LPARAM lParam) { - if (IsMyContact((HANDLE)wParam)) - { + if (IsMyContact((HANDLE)wParam)) { // add the station to the end of the update queue, and also remove old data DBDataManage((HANDLE)wParam, WDBM_REMOVE, 0, 0); UpdateListAdd((HANDLE)wParam); @@ -338,8 +335,7 @@ INT_PTR UpdateSingleRemove(WPARAM wParam, LPARAM lParam) void UpdateThreadProc(LPVOID hWnd) { WaitForSingleObject(hUpdateMutex, INFINITE); - if (ThreadRunning) - { + if (ThreadRunning) { ReleaseMutex(hUpdateMutex); return; } @@ -359,13 +355,15 @@ void UpdateThreadProc(LPVOID hWnd) // the "Update All" menu item in main menu INT_PTR UpdateAllInfo(WPARAM wParam,LPARAM lParam) { - if ( !ThreadRunning) UpdateAll(FALSE, FALSE); + if (!ThreadRunning) + UpdateAll(FALSE, FALSE); return 0; } // the "Update All" menu item in main menu and remove the old data INT_PTR UpdateAllRemove(WPARAM wParam,LPARAM lParam) { - if ( !ThreadRunning) UpdateAll(FALSE, TRUE); + if (!ThreadRunning) + UpdateAll(FALSE, TRUE); return 0; } @@ -399,10 +397,9 @@ int GetWeatherData(HANDLE hContact) if (Data == NULL) return SVC_NOT_FOUND; // the ini for the station cannot be found - WIDATAITEMLIST* Item; WORD cond = NA; char loc[256]; - for (int i=0; i<4; ++i) { + for (int i=0; i < 4; ++i) { // generate update URL switch(i) { case 0: @@ -425,7 +422,7 @@ int GetWeatherData(HANDLE hContact) continue; } - if ( loc[0] == 0 ) + if (loc[0] == 0) continue; // download the html file from the internet @@ -441,7 +438,7 @@ int GetWeatherData(HANDLE hContact) } szInfo = szData; - Item = Data->UpdateData; + WIDATAITEMLIST *Item = Data->UpdateData; // begin parsing item by item while (Item != NULL) { -- cgit v1.2.3