diff src/server/server.py @ 481:bbdc5357dc00

browser and server sides: refactor HTTP request result values + handle "NoReply" error
author souliane <souliane@mailoo.org>
date Sun, 15 Jun 2014 17:52:08 +0200
parents d6daa00ba564
children 437eefa53a01
line wrap: on
line diff
--- a/src/server/server.py	Sat Jun 14 19:20:27 2014 +0200
+++ b/src/server/server.py	Sun Jun 15 17:52:08 2014 +0200
@@ -180,7 +180,7 @@
         if not profile:
             #user is not identified, we return a jsonrpc fault
             parsed = jsonrpclib.loads(request.content.read())
-            fault = jsonrpclib.Fault(C.ERRNUM_LIBERVIA, "Not allowed")  # FIXME: define some standard error codes for libervia
+            fault = jsonrpclib.Fault(C.ERRNUM_LIBERVIA, C.NOT_ALLOWED)  # FIXME: define some standard error codes for libervia
             return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc'))
         return jsonrpc.JSONRPC.render(self, request)
 
@@ -586,7 +586,7 @@
             profile = ISATSession(_session).profile
             if not profile:
                 #user is not identified, we return a jsonrpc fault
-                fault = jsonrpclib.Fault(C.ERRNUM_LIBERVIA, "Not allowed")  # FIXME: define some standard error codes for libervia
+                fault = jsonrpclib.Fault(C.ERRNUM_LIBERVIA, C.NOT_ALLOWED)  # FIXME: define some standard error codes for libervia
                 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc'))
         self.request = request
         return jsonrpc.JSONRPC.render(self, request)
@@ -596,13 +596,13 @@
 
         @param request: request of the register form
         @return: a constant indicating the state:
-            - BAD REQUEST: something is wrong in the request (bad arguments)
+            - C.BAD_REQUEST: something is wrong in the request (bad arguments)
             - a return value from self._loginAccount or self._registerNewAccount
         """
         try:
             submit_type = request.args['submit_type'][0]
         except KeyError:
-            return "BAD REQUEST"
+            return C.BAD_REQUEST
 
         if submit_type == 'register':
             return self._registerNewAccount(request)
@@ -614,10 +614,10 @@
         """Try to authenticate the user with the request information.
         @param request: request of the register form
         @return: a constant indicating the state:
-            - BAD REQUEST: something is wrong in the request (bad arguments)
-            - PROFILE AUTH ERROR: either the profile (login) or the profile password is wrong
-            - XMPP AUTH ERROR: the profile is authenticated but the XMPP password is wrong
-            - ALREADY WAITING: a request has already been submitted for this profile
+            - C.BAD_REQUEST: something is wrong in the request (bad arguments)
+            - C.PROFILE_AUTH_ERROR: either the profile (login) or the profile password is wrong
+            - C.XMPP_AUTH_ERROR: the profile is authenticated but the XMPP password is wrong
+            - C.ALREADY_WAITING: a request has already been submitted for this profile
             - server.NOT_DONE_YET: the profile is being processed, the return
                 value will be given by self._logged or auth_eb
         """
@@ -625,7 +625,7 @@
             login_ = request.args['login'][0]
             password_ = request.args['login_password'][0]
         except KeyError:
-            return "BAD REQUEST"
+            return C.BAD_REQUEST
 
         if login_.startswith('@'):
             raise Exception('No profile_key allowed')
@@ -633,24 +633,27 @@
         profile_check = self.sat_host.bridge.getProfileName(login_)
         if ((not profile_check or profile_check != login_) or
             (not password_ and profile_check not in self.sat_host.empty_password_allowed_warning_dangerous_list)):
-            return "PROFILE AUTH ERROR"
+            return C.PROFILE_AUTH_ERROR
             # profiles with empty passwords are restricted to local frontends
 
         if login_ in self.profiles_waiting:
-            return "ALREADY WAITING"
+            return C.ALREADY_WAITING
 
         def auth_eb(failure):
             fault = failure.value.faultString
             self.__cleanWaiting(login_)
             if fault == 'PasswordError':
                 log.info("Profile %s doesn't exist or the submitted password is wrong" % login_)
-                request.write("PROFILE AUTH ERROR")
+                request.write(C.PROFILE_AUTH_ERROR)
             elif fault == 'SASLAuthError':
                 log.info("The XMPP password of profile %s is wrong" % login_)
-                request.write("XMPP AUTH ERROR")
+                request.write(C.XMPP_AUTH_ERROR)
+            elif fault == 'NoReply':
+                log.info(_("Did not receive a reply (the timeout expired or the connection is broken)"))
+                request.write(C.NO_REPLY)
             else:
                 log.error('Unmanaged fault string %s in errback for the connection of profile %s' % (fault, login_))
-                request.write('UNMANAGED FAULT STRING: %s' % str(fault))
+                request.write(C.UNKNOWN_ERROR % fault)
             request.finish()
 
         self.profiles_waiting[login_] = request
@@ -663,10 +666,10 @@
         """Create a new account, or return error
         @param request: request of the register form
         @return: a constant indicating the state:
-            - BAD REQUEST: something is wrong in the request (bad arguments)
-            - REGISTRATION: new account has been successfully registered
-            - ALREADY EXISTS: the given profile already exists
-            - INTERNAL or 'Unknown error (...)'
+            - C.BAD_REQUEST: something is wrong in the request (bad arguments)
+            - C.REGISTRATION_SUCCEED: new account has been successfully registered
+            - C.ALREADY_EXISTS: the given profile already exists
+            - C.INTERNAL_ERROR or C.UNKNOWN_ERROR
             - server.NOT_DONE_YET: the profile is being processed, the return
                 value will be given later (one of those previously described)
         """
@@ -675,25 +678,25 @@
             password = request.args['register_password'][0]
             email = request.args['email'][0]
         except KeyError:
-            return "BAD REQUEST"
+            return C.BAD_REQUEST
         if not re.match(r'^[a-z0-9_-]+$', login, re.IGNORECASE) or \
            not re.match(r'^.+@.+\..+', email, re.IGNORECASE) or \
            len(password) < C.PASSWORD_MIN_LENGTH:
-            return "BAD REQUEST"
+            return C.BAD_REQUEST
 
         def registered(result):
-            request.write('REGISTRATION')
+            request.write(C.REGISTRATION_SUCCEED)
             request.finish()
 
         def registeringError(failure):
             reason = failure.value.faultString
             if reason == "ConflictError":
-                request.write('ALREADY EXISTS')
+                request.write(C.ALREADY_EXISTS)
             elif reason == "InternalError":
-                request.write('INTERNAL')
+                request.write(C.INTERNAL_ERROR)
             else:
                 log.error('Unknown registering error: %s' % (reason,))
-                request.write('Unknown error (%s)' % reason)
+                request.write(C.UNKNOWN_ERROR % reason)
             request.finish()
 
         d = self.asyncBridgeCall("registerSatAccount", email, password, profile)
@@ -714,15 +717,15 @@
         @param profile
         @param request
         @return: a constant indicating the state:
-            - LOGGED
-            - SESSION_ACTIVE
+            - C.PROFILE_LOGGED
+            - C.SESSION_ACTIVE
         """
         self.__cleanWaiting(profile)
         _session = request.getSession()
         sat_session = ISATSession(_session)
         if sat_session.profile:
             log.error(('/!\\ Session has already a profile, this should NEVER happen!'))
-            request.write('SESSION_ACTIVE')
+            request.write(C.SESSION_ACTIVE)
             request.finish()
             return
         sat_session.profile = profile
@@ -740,7 +743,7 @@
 
         _session.notifyOnExpire(onExpire)
 
-        request.write('LOGGED')
+        request.write(C.PROFILE_LOGGED)
         request.finish()
 
     def jsonrpc_isConnected(self):
@@ -752,7 +755,7 @@
         _session = self.request.getSession()
         profile = ISATSession(_session).profile
         if profile in self.profiles_waiting:
-            raise jsonrpclib.Fault(1, 'Already waiting')  # FIXME: define some standard error codes for libervia
+            raise jsonrpclib.Fault(1, C.ALREADY_WAITING)  # FIXME: define some standard error codes for libervia
         self.profiles_waiting[profile] = self.request
         self.sat_host.bridge.asyncConnect(profile)
         return server.NOT_DONE_YET
@@ -888,7 +891,7 @@
         profile = ISATSession(_session).profile
         if not profile:
             #user is not identified, we return a jsonrpc fault
-            fault = jsonrpclib.Fault(C.ERRNUM_LIBERVIA, "Not allowed")  # FIXME: define some standard error codes for libervia
+            fault = jsonrpclib.Fault(C.ERRNUM_LIBERVIA, C.NOT_ALLOWED)  # FIXME: define some standard error codes for libervia
             return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc'))
         self.request = request
         return jsonrpc.JSONRPC.render(self, request)
@@ -940,7 +943,7 @@
 
         def finish(d):
             error = isinstance(d, Exception) or isinstance(d, Failure)
-            request.write('KO' if error else 'OK')
+            request.write(C.UPLOAD_KO if error else C.UPLOAD_OK)
             # TODO: would be great to re-use the original Exception class and message
             # but it is lost in the middle of the backtrace and encapsulated within
             # a DBusException instance --> extract the data from the backtrace?