From: epage Date: Fri, 16 Oct 2009 00:38:01 +0000 (+0000) Subject: Applying a hack for redirect issues X-Git-Url: https://vcs.maemo.org/git/?a=commitdiff_plain;h=0f33d720e11cb73b7da2e25b456e97b28e2f4b24;p=gc-dialer Applying a hack for redirect issues git-svn-id: file:///svnroot/gc-dialer/trunk@530 c39d3808-3fe2-4d86-a59f-b7f623ee9f21 --- diff --git a/src/browser_emu.py b/src/browser_emu.py index 32ffbca..e3446ac 100644 --- a/src/browser_emu.py +++ b/src/browser_emu.py @@ -47,43 +47,8 @@ class MozillaEmulator(object): self.debug = False self.trycount = trycount - def build_opener(self, url, postdata = None, extraheaders = None, forbid_redirect = False): - if extraheaders is None: - extraheaders = {} - - txheaders = { - 'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png', - 'Accept-Language': 'en,en-us;q=0.5', - 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', - } - for key, value in extraheaders.iteritems(): - txheaders[key] = value - req = urllib2.Request(url, postdata, txheaders) - self.cookies.add_cookie_header(req) - if forbid_redirect: - redirector = HTTPNoRedirector() - else: - redirector = urllib2.HTTPRedirectHandler() - - http_handler = urllib2.HTTPHandler(debuglevel=self.debug) - https_handler = urllib2.HTTPSHandler(debuglevel=self.debug) - - u = urllib2.build_opener( - http_handler, - https_handler, - urllib2.HTTPCookieProcessor(self.cookies), - redirector - ) - u.addheaders = [( - 'User-Agent', - 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4' - )] - if not postdata is None: - req.add_data(postdata) - return (req, u) - def download(self, url, - postdata = None, extraheaders = None, forbid_redirect = False, + postdata = None, extraheaders = None, forbidRedirect = False, trycount = None, only_head = False, ): """Download an URL with GET or POST methods. @@ -91,7 +56,7 @@ class MozillaEmulator(object): @param postdata: It can be a string that will be POST-ed to the URL. When None is given, the method will be GET instead. @param extraheaders: You can add/modify HTTP headers with a dict here. - @param forbid_redirect: Set this flag if you do not want to handle + @param forbidRedirect: Set this flag if you do not want to handle HTTP 301 and 302 redirects. @param trycount: Specify the maximum number of retries here. 0 means no retry on error. Using -1 means infinite retring. @@ -111,7 +76,7 @@ class MozillaEmulator(object): while True: try: - req, u = self.build_opener(url, postdata, extraheaders, forbid_redirect) + req, u = self._build_opener(url, postdata, extraheaders, forbidRedirect) openerdirector = u.open(req) if self.debug: _moduleLogger.info("%r - %r" % (req.get_method(), url)) @@ -130,6 +95,43 @@ class MozillaEmulator(object): # Retry :-) _moduleLogger.info("MozillaEmulator: urllib2.URLError, retryting %d" % cnt) + def _build_opener(self, url, postdata = None, extraheaders = None, forbidRedirect = False): + if extraheaders is None: + extraheaders = {} + + txheaders = { + 'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png', + 'Accept-Language': 'en,en-us;q=0.5', + 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', + } + for key, value in extraheaders.iteritems(): + txheaders[key] = value + req = urllib2.Request(url, postdata, txheaders) + self.cookies.add_cookie_header(req) + if forbidRedirect: + redirector = HTTPNoRedirector() + _moduleLogger.info("Redirection disabled") + else: + redirector = urllib2.HTTPRedirectHandler() + _moduleLogger.info("Redirection enabled") + + http_handler = urllib2.HTTPHandler(debuglevel=self.debug) + https_handler = urllib2.HTTPSHandler(debuglevel=self.debug) + + u = urllib2.build_opener( + http_handler, + https_handler, + urllib2.HTTPCookieProcessor(self.cookies), + redirector + ) + u.addheaders = [( + 'User-Agent', + 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4' + )] + if not postdata is None: + req.add_data(postdata) + return (req, u) + def _read(self, openerdirector, trycount): chunks = [] @@ -163,4 +165,5 @@ class HTTPNoRedirector(urllib2.HTTPRedirectHandler): elif 'uri' in headers: newurl = headers.getheaders('uri')[0] e.newurl = newurl + _moduleLogger.info("New url: %s" % e.newurl) raise e diff --git a/src/constants.py b/src/constants.py index 9db52b1..9a4c573 100644 --- a/src/constants.py +++ b/src/constants.py @@ -3,7 +3,7 @@ import os __pretty_app_name__ = "DialCentral" __app_name__ = "dialcentral" __version__ = "1.0.7" -__build__ = 11 +__build__ = 12 __app_magic__ = 0xdeadbeef _data_path_ = os.path.join(os.path.expanduser("~"), ".dialcentral") _user_settings_ = "%s/settings.ini" % _data_path_ diff --git a/src/dc_glade.py b/src/dc_glade.py index 789b485..3729e7a 100755 --- a/src/dc_glade.py +++ b/src/dc_glade.py @@ -413,7 +413,15 @@ class Dialcentral(object): @note Thread agnostic """ username, password = self._credentials - loggedIn = self._phoneBackends[self._defaultBackendId].login(username, password) + try: + loggedIn = self._phoneBackends[self._defaultBackendId].login(username, password) + except Exception: + # Retry in case the redirect failed + # luckily is_authed does everything we need for a retry + loggedIn = self._phoneBackends[self._defaultBackendId].is_authed(True) + if not loggedIn: + raise + _moduleLogger.info("Redirection failed on initial login attempt, auto-corrected for this") if loggedIn: self._credentials = username, password _moduleLogger.info("Logged into %r through settings" % self._phoneBackends[self._defaultBackendId]) @@ -438,7 +446,15 @@ class Dialcentral(object): banner = hildonize.show_busy_banner_start(self._window, "Logging In...") try: username, password = credentials - loggedIn = self._phoneBackends[tmpServiceId].login(username, password) + try: + loggedIn = self._phoneBackends[tmpServiceId].login(username, password) + except Exception: + # Retry in case the redirect failed + # luckily is_authed does everything we need for a retry + loggedIn = self._phoneBackends[self._defaultBackendId].is_authed(True) + if not loggedIn: + raise + _moduleLogger.info("Redirection failed on initial login attempt, auto-corrected for this") finally: with gtk_toolbox.gtk_lock(): hildonize.show_busy_banner_end(banner) diff --git a/src/gv_backend.py b/src/gv_backend.py index 2ecbb58..35da434 100644 --- a/src/gv_backend.py +++ b/src/gv_backend.py @@ -747,8 +747,14 @@ def grab_debug_info(username, password): with open("loggingin.txt", "w") as f: print "\tWriting to file" f.write(loginSuccessOrFailurePage) - backend._grab_account_info(loginSuccessOrFailurePage) - assert backend.is_authed() + try: + backend._grab_account_info(loginSuccessOrFailurePage) + except Exception: + # Retry in case the redirect failed + # luckily is_authed does everything we need for a retry + loggedIn = backend.is_authed(True) + if not loggedIn: + raise # Get Pages print "Grabbing post-login pages" diff --git a/support/builddeb.py b/support/builddeb.py index 0bcc219..97d84aa 100755 --- a/support/builddeb.py +++ b/support/builddeb.py @@ -42,6 +42,7 @@ __changelog__ = """ * Bug Fix: Once PyMaemo supports thumb button sizes, setting that for all of the random buttons * Bug Fix: When debugging hildonization, dialogs weren't closing * Bug Fix: Not properly hildonizing some code +* Bug Fix: Attempting a hack to fix redirect issues for people * Debugging: Improved logging output * Debugging: Printing page when can't get a callback number * Debugging: Included stuff to create all the debug files