Mercurial > libervia-web
comparison 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 |
comparison
equal
deleted
inserted
replaced
1215:f14ab8a25e8b | 1216:b2d067339de3 |
---|---|
44 @param serveur_request: original request of the page | 44 @param serveur_request: original request of the page |
45 """ | 45 """ |
46 self.ws_protocol = ws_protocol | 46 self.ws_protocol = ws_protocol |
47 self.ws_request = connection_request | 47 self.ws_request = connection_request |
48 if self.isSecure(): | 48 if self.isSecure(): |
49 cookie_string = "TWISTED_SECURE_SESSION" | 49 cookie_name = "TWISTED_SECURE_SESSION" |
50 else: | 50 else: |
51 cookie_string = "TWISTED_SESSION" | 51 cookie_name = "TWISTED_SESSION" |
52 cookie_value = server_request.getCookie(cookie_string) | 52 cookie_value = server_request.getCookie(cookie_name.encode('utf-8')) |
53 try: | 53 try: |
54 raw_cookies = ws_protocol.http_headers['cookie'] | 54 raw_cookies = ws_protocol.http_headers['cookie'] |
55 except KeyError: | 55 except KeyError: |
56 raise ValueError(u"missing expected cookie header") | 56 raise ValueError("missing expected cookie header") |
57 self.cookies = {k:v for k,v in (c.split('=') for c in raw_cookies.split(';'))} | 57 self.cookies = {k:v for k,v in (c.split('=') for c in raw_cookies.split(';'))} |
58 if self.cookies[cookie_string] != cookie_value: | 58 if self.cookies[cookie_name] != cookie_value.decode('utf-8'): |
59 raise exceptions.PermissionError( | 59 raise exceptions.PermissionError( |
60 u"Bad cookie value, this should never happen.\n" | 60 "Bad cookie value, this should never happen.\n" |
61 u"headers: {headers}".format(headers=ws_protocol.http_headers)) | 61 "headers: {headers}".format(headers=ws_protocol.http_headers)) |
62 | 62 |
63 self.template_data = server_request.template_data | 63 self.template_data = server_request.template_data |
64 self.data = server_request.data | 64 self.data = server_request.data |
65 self.session = server_request.getSession() | 65 self.session = server_request.getSession() |
66 self._signals_registered = server_request._signals_registered | 66 self._signals_registered = server_request._signals_registered |
92 class LiberviaPageWSProtocol(websocket.WebSocketServerProtocol): | 92 class LiberviaPageWSProtocol(websocket.WebSocketServerProtocol): |
93 host = None | 93 host = None |
94 tokens_map = {} | 94 tokens_map = {} |
95 | 95 |
96 def onConnect(self, request): | 96 def onConnect(self, request): |
97 prefix = LIBERVIA_PROTOCOL + u"_" | 97 prefix = LIBERVIA_PROTOCOL + "_" |
98 for protocol in request.protocols: | 98 for protocol in request.protocols: |
99 if protocol.startswith(prefix): | 99 if protocol.startswith(prefix): |
100 token = protocol[len(prefix) :].strip() | 100 token = protocol[len(prefix) :].strip() |
101 if token: | 101 if token: |
102 break | 102 break |
103 else: | 103 else: |
104 raise types.ConnectionDeny( | 104 raise types.ConnectionDeny( |
105 types.ConnectionDeny.NOT_IMPLEMENTED, u"Can't use this subprotocol" | 105 types.ConnectionDeny.NOT_IMPLEMENTED, "Can't use this subprotocol" |
106 ) | 106 ) |
107 | 107 |
108 if token not in self.tokens_map: | 108 if token not in self.tokens_map: |
109 log.warning(_(u"Can't activate page socket: unknown token")) | 109 log.warning(_("Can't activate page socket: unknown token")) |
110 raise types.ConnectionDeny( | 110 raise types.ConnectionDeny( |
111 types.ConnectionDeny.FORBIDDEN, u"Bad token, please reload page" | 111 types.ConnectionDeny.FORBIDDEN, "Bad token, please reload page" |
112 ) | 112 ) |
113 self.token = token | 113 self.token = token |
114 token_map = self.tokens_map.pop(token) | 114 token_map = self.tokens_map.pop(token) |
115 self.page = token_map["page"] | 115 self.page = token_map["page"] |
116 self.request = WebsocketRequest(self, request, token_map["request"]) | 116 self.request = WebsocketRequest(self, request, token_map["request"]) |
117 return protocol | 117 return protocol |
118 | 118 |
119 def onOpen(self): | 119 def onOpen(self): |
120 log.debug( | 120 log.debug( |
121 _( | 121 _( |
122 u"Websocket opened for {page} (token: {token})".format( | 122 "Websocket opened for {page} (token: {token})".format( |
123 page=self.page, token=self.token | 123 page=self.page, token=self.token |
124 ) | 124 ) |
125 ) | 125 ) |
126 ) | 126 ) |
127 self.page.onSocketOpen(self.request) | 127 self.page.onSocketOpen(self.request) |
129 def onMessage(self, payload, isBinary): | 129 def onMessage(self, payload, isBinary): |
130 try: | 130 try: |
131 data_json = json.loads(payload.decode("utf8")) | 131 data_json = json.loads(payload.decode("utf8")) |
132 except ValueError as e: | 132 except ValueError as e: |
133 log.warning( | 133 log.warning( |
134 _(u"Not valid JSON, ignoring data: {msg}\n{data}").format( | 134 _("Not valid JSON, ignoring data: {msg}\n{data}").format( |
135 msg=e, data=payload | 135 msg=e, data=payload |
136 ) | 136 ) |
137 ) | 137 ) |
138 return | 138 return |
139 # we request page first, to raise an AttributeError | 139 # we request page first, to raise an AttributeError |
142 try: | 142 try: |
143 cb = page.on_data | 143 cb = page.on_data |
144 except AttributeError: | 144 except AttributeError: |
145 log.warning( | 145 log.warning( |
146 _( | 146 _( |
147 u'No "on_data" method set on dynamic page, ignoring data:\n{data}' | 147 'No "on_data" method set on dynamic page, ignoring data:\n{data}' |
148 ).format(data=data_json) | 148 ).format(data=data_json) |
149 ) | 149 ) |
150 else: | 150 else: |
151 cb(page, self.request, data_json) | 151 cb(page, self.request, data_json) |
152 | 152 |
153 def onClose(self, wasClean, code, reason): | 153 def onClose(self, wasClean, code, reason): |
154 try: | 154 try: |
155 page = self.page | 155 page = self.page |
156 except AttributeError: | 156 except AttributeError: |
157 log.debug( | 157 log.debug( |
158 u"page is not available, the socket was probably not opened cleanly.\n" | 158 "page is not available, the socket was probably not opened cleanly.\n" |
159 u"reason: {reason}".format(reason=reason)) | 159 "reason: {reason}".format(reason=reason)) |
160 return | 160 return |
161 page.onSocketClose(self.request) | 161 page.onSocketClose(self.request) |
162 | 162 |
163 log.debug( | 163 log.debug( |
164 _( | 164 _( |
165 u"Websocket closed for {page} (token: {token}). {reason}".format( | 165 "Websocket closed for {page} (token: {token}). {reason}".format( |
166 page=self.page, | 166 page=self.page, |
167 token=self.token, | 167 token=self.token, |
168 reason=u"" | 168 reason="" |
169 if wasClean | 169 if wasClean |
170 else _(u"Reason: {reason}").format(reason=reason), | 170 else _("Reason: {reason}").format(reason=reason), |
171 ) | 171 ) |
172 ) | 172 ) |
173 ) | 173 ) |
174 | 174 |
175 @classmethod | 175 @classmethod |
176 def getBaseURL(cls, host, secure): | 176 def getBaseURL(cls, host, secure): |
177 return u"ws{sec}://localhost:{port}".format( | 177 return "ws{sec}://localhost:{port}".format( |
178 sec="s" if secure else "", | 178 sec="s" if secure else "", |
179 port=cls.host.options["port_https" if secure else "port"], | 179 port=cls.host.options["port_https" if secure else "port"], |
180 ) | 180 ) |
181 | 181 |
182 @classmethod | 182 @classmethod |
188 return resource.WebSocketResource(factory) | 188 return resource.WebSocketResource(factory) |
189 | 189 |
190 @classmethod | 190 @classmethod |
191 def registerToken(cls, token, page, request): | 191 def registerToken(cls, token, page, request): |
192 if token in cls.tokens_map: | 192 if token in cls.tokens_map: |
193 raise exceptions.ConflictError(_(u"This token is already registered")) | 193 raise exceptions.ConflictError(_("This token is already registered")) |
194 cls.tokens_map[token] = {"page": page, "request": request} | 194 cls.tokens_map[token] = {"page": page, "request": request} |