From cc86dda802d9eba80234bfef61a5fff92c877818 Mon Sep 17 00:00:00 2001 From: echel0n Date: Tue, 17 Jun 2014 11:16:04 -0700 Subject: [PATCH] Fix for NoneType is not iterable issue --- sickbeard/browser.py | 6 ++--- sickbeard/webserve.py | 61 ++++++++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/sickbeard/browser.py b/sickbeard/browser.py index 1c790ff3..aec2923e 100644 --- a/sickbeard/browser.py +++ b/sickbeard/browser.py @@ -101,11 +101,11 @@ def foldersAtPath(path, includeParent=False): class WebFileBrowser(RequestHandler): - def index(self, path=''): + def index(self, path='', *args, **kwargs): self.set_header("Content-Type", "application/json") - return self.finish(json.dumps(foldersAtPath(path, True))) + return json.dumps(foldersAtPath(path, True)) def complete(self, term): self.set_header("Content-Type", "application/json") paths = [entry['path'] for entry in foldersAtPath(os.path.dirname(term)) if 'path' in entry] - return self.finish(json.dumps(paths)) + return json.dumps(paths) diff --git a/sickbeard/webserve.py b/sickbeard/webserve.py index 0a094a05..6358ac05 100644 --- a/sickbeard/webserve.py +++ b/sickbeard/webserve.py @@ -92,35 +92,41 @@ from tornado.ioloop import IOLoop req_headers = None -def require_basic_auth(handler_class): +def basicauth(handler_class): def wrap_execute(handler_execute): - def require_basic_auth(handler, kwargs): - def get_auth(): + def basicauth(handler, transforms, *args, **kwargs): + def _request_basic_auth(handler): handler.set_status(401) handler.set_header('WWW-Authenticate', 'Basic realm=Restricted') - handler._transforms = [] handler.finish() return False - if not sickbeard.WEB_USERNAME and not sickbeard.WEB_PASSWORD: - if not handler.get_secure_cookie("user"): - handler.set_secure_cookie("user", str(time.time())) - return True + try: + auth_hdr = handler.request.headers.get('Authorization') - auth_header = handler.request.headers.get('Authorization') - if auth_header and auth_header.startswith('Basic '): - auth_decoded = base64.decodestring(auth_header[6:]) - basicauth_user, basicauth_pass = auth_decoded.split(':', 2) - if basicauth_user == sickbeard.WEB_USERNAME and basicauth_pass == sickbeard.WEB_PASSWORD: + if auth_hdr == None: + return _request_basic_auth(handler) + if not auth_hdr.startswith('Basic '): + return _request_basic_auth(handler) + + auth_decoded = base64.decodestring(auth_hdr[6:]) + username, password = auth_decoded.split(':', 2) + + if username == sickbeard.WEB_USERNAME and password == sickbeard.WEB_PASSWORD: + #logger.log('authenticated user successfully', logger.DEBUG) if not handler.get_secure_cookie("user"): handler.set_secure_cookie("user", str(time.time())) - return True - - handler.clear_cookie("user") - get_auth() + else: + if handler.get_secure_cookie("user"): + handler.clear_cookie("user") + return _request_basic_auth(handler) + except Exception, e: + handler.clear_cookie("user") + return _request_basic_auth(handler) + return True def _execute(self, transforms, *args, **kwargs): - if not require_basic_auth(self, kwargs): + if not basicauth(self, transforms, *args, **kwargs): return False return handler_execute(self, transforms, *args, **kwargs) @@ -129,12 +135,12 @@ def require_basic_auth(handler_class): handler_class._execute = wrap_execute(handler_class._execute) return handler_class -@require_basic_auth class RedirectHandler(RequestHandler): def get(self, path, **kwargs): self.redirect(path, permanent=True) +@basicauth class IndexHandler(RedirectHandler): def __init__(self, application, request, **kwargs): super(IndexHandler, self).__init__(application, request, **kwargs) @@ -155,7 +161,7 @@ class IndexHandler(RedirectHandler): args[arg] = value[0] return args - def _dispatch(self): + def _dispatch(self, callback): args = None path = self.request.uri.split('?')[0] @@ -194,21 +200,22 @@ class IndexHandler(RedirectHandler): if func: if args: - return func(**args) + callback(func(**args)) else: - return func() + callback(func()) - if self.request.uri != ('/'): - raise HTTPError(404) + callback(HTTPError(404)) def get_current_user(self): return self.get_secure_cookie("user") @authenticated @asynchronous + @gen.engine def get(self, *args, **kwargs): try: - self.finish(self._dispatch()) + result = yield gen.Task(self._dispatch) + self.finish(result) except Exception as e: logger.log(ex(e), logger.ERROR) logger.log(u"Traceback: " + traceback.format_exc(), logger.DEBUG) @@ -216,8 +223,8 @@ class IndexHandler(RedirectHandler): def post(self, *args, **kwargs): try: - resp = self._dispatch() - self.finish(resp) + result = yield gen.Task(self._dispatch) + self.finish(result) except Exception as e: logger.log(ex(e), logger.ERROR) logger.log(u"Traceback: " + traceback.format_exc(), logger.DEBUG)