/usr/lib/python3/dist-packages/tornadio2/router.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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | # -*- 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.router
~~~~~~~~~~~~~~~~
Transport protocol router and main entry point for all socket.io clients.
"""
from tornado import ioloop, version_info
from tornado.web import HTTPError
from tornadio2 import persistent, polling, sessioncontainer, session, proto, preflight, stats
PROTOCOLS = {
'websocket': persistent.TornadioWebSocketHandler,
'flashsocket': persistent.TornadioFlashSocketHandler,
'xhr-polling': polling.TornadioXHRPollingHandler,
'htmlfile': polling.TornadioHtmlFileHandler,
'jsonp-polling': polling.TornadioJSONPHandler,
}
DEFAULT_SETTINGS = {
# Sessions check interval in seconds
'session_check_interval': 15,
# Session expiration in seconds
'session_expiry': 30,
# Heartbeat time in seconds. Do not change this value unless
# you absolutely sure that new value will work.
'heartbeat_interval': 12,
# Enabled protocols
'enabled_protocols': ['websocket', 'flashsocket', 'xhr-polling',
'jsonp-polling', 'htmlfile'],
# XHR-Polling request timeout, in seconds
'xhr_polling_timeout': 20,
# Some antivirus software messed up with HTTP traffic and, as a result, websockets
# to port 80 stop to work. If you enable this setting, TornadIO will try to send
# ping packet and wait for response. If nothing will happen during 5 seconds,
# TornadIO considers connection not working.
'websocket_check': False,
# Starting from socket.io 0.9.2, client started verifying heartbeats for all transports.
# Disable this if you're on 0.9.1 or lower, as this settings will significantly increase
# your server load for clients with polling transports.
'global_heartbeats': True,
# Client timeout adjustment in seconds. If you see your clients disconnect without a
# reason, increase this value.
'client_timeout': 5
}
class HandshakeHandler(preflight.PreflightHandler):
"""socket.io handshake handler"""
def initialize(self, server):
self.server = server
def get(self, version, *args, **kwargs):
try:
self.server.stats.connection_opened()
# Only version 1 is supported now
if version != '1':
raise HTTPError(503, "Invalid socket.io protocol version")
sess = self.server.create_session(self.request)
settings = self.server.settings
# TODO: Fix heartbeat timeout. For now, it is adding 5 seconds to the client timeout.
data = '%s:%d:%d:%s' % (
sess.session_id.decode('utf-8'),
# TODO: Fix me somehow a well. 0.9.2 will drop connection is no
# heartbeat was sent over
settings['heartbeat_interval'] + settings['client_timeout'],
# TODO: Fix me somehow.
settings['xhr_polling_timeout'] + settings['client_timeout'],
','.join(t for t in self.server.settings.get('enabled_protocols'))
)
if self.server.settings['global_heartbeats']:
sess.reset_heartbeat()
jsonp = self.get_argument('jsonp', None)
if jsonp is not None:
self.set_header('Content-Type', 'application/javascript; charset=UTF-8')
data = 'io.j[%s](%s);' % (jsonp, proto.json_dumps(data))
else:
self.set_header('Content-Type', 'text/plain; charset=UTF-8')
self.preflight()
self.write(data)
self.finish()
finally:
self.server.stats.connection_closed()
class TornadioRouter(object):
"""TornadIO2 router implementation"""
def __init__(self,
connection,
user_settings=dict(),
namespace='socket.io',
io_loop=None):
"""Constructor.
`connection`
SocketConnection class instance
`user_settings`
Settings
`namespace`
Router namespace, defaulted to 'socket.io'
`io_loop`
IOLoop instance, optional.
"""
# TODO: Version check
if version_info[0] < 2:
raise Exception('TornadIO2 requires Tornado 2.0 or higher.')
# Store connection class
self._connection = connection
# Initialize io_loop
self.io_loop = io_loop or ioloop.IOLoop.instance()
# Settings
self.settings = DEFAULT_SETTINGS.copy()
if user_settings:
self.settings.update(user_settings)
# Sessions
self._sessions = sessioncontainer.SessionContainer()
check_interval = self.settings['session_check_interval'] * 1000
self._sessions_cleanup = ioloop.PeriodicCallback(self._sessions.expire,
check_interval,
self.io_loop)
self._sessions_cleanup.start()
# Stats
self.stats = stats.StatsCollector()
self.stats.start(self.io_loop)
# Initialize URLs
self._transport_urls = [
(r'/%s/(?P<version>\d+)/$' % namespace,
HandshakeHandler,
dict(server=self))
]
for t in self.settings.get('enabled_protocols', dict()):
proto = PROTOCOLS.get(t)
if not proto:
# TODO: Error logging
continue
# Only version 1 is supported
self._transport_urls.append(
(r'/%s/1/%s/(?P<session_id>[^/]+)/?' %
(namespace, t),
proto,
dict(server=self))
)
@property
def urls(self):
"""List of the URLs to be added to the Tornado application"""
return self._transport_urls
def apply_routes(self, routes):
"""Feed list of the URLs to the routes list. Returns list"""
routes.extend(self._transport_urls)
return routes
def create_session(self, request):
"""Creates new session object and returns it.
`request`
Request that created the session. Will be used to get query string
parameters and cookies.
"""
# TODO: Possible optimization here for settings.get
s = session.Session(self._connection,
self,
request,
self.settings.get('session_expiry')
)
self._sessions.add(s)
return s
def get_session(self, session_id):
"""Get session by session id
"""
return self._sessions.get(session_id)
|