diff libervia/server/websockets.py @ 1216:b2d067339de3

python 3 port: /!\ Python 3.6+ is now needed to use libervia /!\ instability may occur and features may not be working anymore, this will improve with time /!\ TxJSONRPC dependency has been removed The same procedure as in backend has been applied (check backend commit ab2696e34d29 logs for details). Removed now deprecated code (Pyjamas compiled browser part, legacy blog, JSON RPC related code). Adapted code to work without `html` and `themes` dirs.
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:12:31 +0200
parents d2e2bf14f2e2
children 987595a254b0
line wrap: on
line diff
--- a/libervia/server/websockets.py	Tue Aug 13 09:39:33 2019 +0200
+++ b/libervia/server/websockets.py	Tue Aug 13 19:12:31 2019 +0200
@@ -46,19 +46,19 @@
         self.ws_protocol = ws_protocol
         self.ws_request = connection_request
         if self.isSecure():
-            cookie_string = "TWISTED_SECURE_SESSION"
+            cookie_name = "TWISTED_SECURE_SESSION"
         else:
-            cookie_string = "TWISTED_SESSION"
-        cookie_value = server_request.getCookie(cookie_string)
+            cookie_name = "TWISTED_SESSION"
+        cookie_value = server_request.getCookie(cookie_name.encode('utf-8'))
         try:
             raw_cookies = ws_protocol.http_headers['cookie']
         except KeyError:
-            raise ValueError(u"missing expected cookie header")
+            raise ValueError("missing expected cookie header")
         self.cookies = {k:v for k,v in (c.split('=') for c in raw_cookies.split(';'))}
-        if self.cookies[cookie_string] != cookie_value:
+        if self.cookies[cookie_name] != cookie_value.decode('utf-8'):
             raise exceptions.PermissionError(
-                u"Bad cookie value, this should never happen.\n"
-                u"headers: {headers}".format(headers=ws_protocol.http_headers))
+                "Bad cookie value, this should never happen.\n"
+                "headers: {headers}".format(headers=ws_protocol.http_headers))
 
         self.template_data = server_request.template_data
         self.data = server_request.data
@@ -94,7 +94,7 @@
     tokens_map = {}
 
     def onConnect(self, request):
-        prefix = LIBERVIA_PROTOCOL + u"_"
+        prefix = LIBERVIA_PROTOCOL + "_"
         for protocol in request.protocols:
             if protocol.startswith(prefix):
                 token = protocol[len(prefix) :].strip()
@@ -102,13 +102,13 @@
                     break
         else:
             raise types.ConnectionDeny(
-                types.ConnectionDeny.NOT_IMPLEMENTED, u"Can't use this subprotocol"
+                types.ConnectionDeny.NOT_IMPLEMENTED, "Can't use this subprotocol"
             )
 
         if token not in self.tokens_map:
-            log.warning(_(u"Can't activate page socket: unknown token"))
+            log.warning(_("Can't activate page socket: unknown token"))
             raise types.ConnectionDeny(
-                types.ConnectionDeny.FORBIDDEN, u"Bad token, please reload page"
+                types.ConnectionDeny.FORBIDDEN, "Bad token, please reload page"
             )
         self.token = token
         token_map = self.tokens_map.pop(token)
@@ -119,7 +119,7 @@
     def onOpen(self):
         log.debug(
             _(
-                u"Websocket opened for {page} (token: {token})".format(
+                "Websocket opened for {page} (token: {token})".format(
                     page=self.page, token=self.token
                 )
             )
@@ -131,7 +131,7 @@
             data_json = json.loads(payload.decode("utf8"))
         except ValueError as e:
             log.warning(
-                _(u"Not valid JSON, ignoring data: {msg}\n{data}").format(
+                _("Not valid JSON, ignoring data: {msg}\n{data}").format(
                     msg=e, data=payload
                 )
             )
@@ -144,7 +144,7 @@
         except AttributeError:
             log.warning(
                 _(
-                    u'No "on_data" method set on dynamic page, ignoring data:\n{data}'
+                    'No "on_data" method set on dynamic page, ignoring data:\n{data}'
                 ).format(data=data_json)
             )
         else:
@@ -155,26 +155,26 @@
             page = self.page
         except AttributeError:
             log.debug(
-                u"page is not available, the socket was probably not opened cleanly.\n"
-                u"reason: {reason}".format(reason=reason))
+                "page is not available, the socket was probably not opened cleanly.\n"
+                "reason: {reason}".format(reason=reason))
             return
         page.onSocketClose(self.request)
 
         log.debug(
             _(
-                u"Websocket closed for {page} (token: {token}). {reason}".format(
+                "Websocket closed for {page} (token: {token}). {reason}".format(
                     page=self.page,
                     token=self.token,
-                    reason=u""
+                    reason=""
                     if wasClean
-                    else _(u"Reason: {reason}").format(reason=reason),
+                    else _("Reason: {reason}").format(reason=reason),
                 )
             )
         )
 
     @classmethod
     def getBaseURL(cls, host, secure):
-        return u"ws{sec}://localhost:{port}".format(
+        return "ws{sec}://localhost:{port}".format(
             sec="s" if secure else "",
             port=cls.host.options["port_https" if secure else "port"],
         )
@@ -190,5 +190,5 @@
     @classmethod
     def registerToken(cls, token, page, request):
         if token in cls.tokens_map:
-            raise exceptions.ConflictError(_(u"This token is already registered"))
+            raise exceptions.ConflictError(_("This token is already registered"))
         cls.tokens_map[token] = {"page": page, "request": request}