/usr/lib/python3/dist-packages/tornadio2/persistent.py is in python3-tornadio2 0.0.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | # -*- coding: utf-8 -*-
#
# Copyright: (c) 2011 by the Serge S. Koval, see AUTHORS for more details.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
tornadio2.persistent
~~~~~~~~~~~~~~~~~~~~
Persistent transport implementations.
"""
import logging
import time
import traceback
import tornado
from tornado.web import HTTPError
from tornado import stack_context
from tornado.websocket import WebSocketHandler
from tornadio2 import proto
class TornadioWebSocketHandler(WebSocketHandler):
"""Websocket protocol handler"""
# Transport name
name = 'websocket'
def initialize(self, server):
self.server = server
self.session = None
self._is_active = not self.server.settings['websocket_check']
self._global_heartbeats = self.server.settings['global_heartbeats']
logging.debug('Initializing %s handler.' % self.name)
# Additional verification of the websocket handshake
# For now it will stay here, till https://github.com/facebook/tornado/pull/415
# is merged.
def _execute(self, transforms, *args, **kwargs):
with stack_context.ExceptionStackContext(self._handle_websocket_exception):
# Websocket only supports GET method
if self.request.method != 'GET':
self.stream.write(tornado.escape.utf8(
"HTTP/1.1 405 Method Not Allowed\r\n\r\n"
))
self.stream.close()
return
# Upgrade header should be present and should be equal to WebSocket
if self.request.headers.get("Upgrade", "").lower() != 'websocket':
self.stream.write(tornado.escape.utf8(
"HTTP/1.1 400 Bad Request\r\n\r\n"
"Can \"Upgrade\" only to \"WebSocket\"."
))
self.stream.close()
return
# Connection header should be upgrade. Some proxy servers/load balancers
# might mess with it.
if self.request.headers.get("Connection", "").lower().find('upgrade') == -1:
self.stream.write(tornado.escape.utf8(
"HTTP/1.1 400 Bad Request\r\n\r\n"
"\"Connection\" must be \"Upgrade\"."
))
self.stream.close()
return
super(TornadioWebSocketHandler, self)._execute(transforms, *args, **kwargs)
def open(self, session_id):
"""WebSocket open handler"""
self.session = self.server.get_session(session_id)
if self.session is None:
raise HTTPError(401, "Invalid Session")
if not self._is_active:
# Need to check if websocket connection was really established by sending hearbeat packet
# and waiting for response
self.write_message(proto.heartbeat())
self.server.io_loop.add_timeout(time.time() + self.server.settings['client_timeout'],
self._connection_check)
else:
# Associate session handler
self.session.set_handler(self)
self.session.reset_heartbeat()
# Flush messages, if any
self.session.flush()
def _connection_check(self):
if not self._is_active:
self._detach()
try:
# Might throw exception if connection was closed already
self.close()
except:
pass
def _detach(self):
if self.session is not None:
if self._is_active:
self.session.stop_heartbeat()
self.session.remove_handler(self)
self.session = None
def on_message(self, message):
# Tracking
self.server.stats.on_packet_recv(1)
# Fix for late messages (after connection was closed)
if not self.session:
return
# Mark that connection is active and flush any pending messages
if not self._is_active:
# Associate session handler and flush queued messages
self.session.set_handler(self)
self.session.reset_heartbeat()
self.session.flush()
self._is_active = True
if not self._global_heartbeats:
self.session.delay_heartbeat()
try:
self.session.raw_message(message)
except Exception as ex:
logging.error('Failed to handle message: ' + traceback.format_exc(ex))
# Close session on exception
if self.session is not None:
self.session.close()
def on_close(self):
self._detach()
def send_messages(self, messages):
# Tracking
self.server.stats.on_packet_sent(len(messages))
try:
for m in messages:
self.write_message(m)
except IOError:
if self.ws_connection and self.ws_connection.client_terminated:
logging.debug('Dropping active websocket connection due to IOError.')
self._detach()
def session_closed(self):
try:
self.close()
except Exception:
logging.debug('Exception', exc_info=True)
finally:
self._detach()
def _handle_websocket_exception(self, type, value, traceback):
if type is IOError:
self.server.io_loop.add_callback(self.on_connection_close)
# raise (type, value, traceback)
logging.debug('Exception', exc_info=(type, value, traceback))
return True
# Websocket overrides
def allow_draft76(self):
return True
class TornadioFlashSocketHandler(TornadioWebSocketHandler):
# Transport name
name = 'flashsocket'
|