<s> """<STR_LIT>""" <EOL> import time <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from urllib import unquote <EOL> from tornado . web import RequestHandler , HTTPError , asynchronous <EOL> from tornadio import pollingsession <EOL> class TornadioPollingHandlerBase ( RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , router , session_id ) : <EOL> """<STR_LIT>""" <EOL> self . router = router <EOL> self . session_id = session_id <EOL> self . session = None <EOL> super ( TornadioPollingHandlerBase , self ) . __init__ ( router . application , <EOL> router . request ) <EOL> def _execute ( self , transforms , * args , ** kwargs ) : <EOL> if not self . session_id : <EOL> session_expiry = self . router . settings [ '<STR_LIT>' ] <EOL> self . session = self . router . sessions . create ( <EOL> pollingsession . PollingSession , <EOL> session_expiry , <EOL> router = self . router , <EOL> args = args , <EOL> kwargs = kwargs ) <EOL> else : <EOL> self . session = self . router . sessions . get ( self . session_id ) <EOL> if self . session is None or self . session . is_closed : <EOL> raise HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> super ( TornadioPollingHandlerBase , self ) . _execute ( transforms , <EOL> * args , ** kwargs ) <EOL> @ asynchronous <EOL> def get ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ asynchronous <EOL> def post ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def data_available ( self , raw_data ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ asynchronous <EOL> def options ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . preflight ( ) <EOL> self . finish ( ) <EOL> def preflight ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . request . headers . has_key ( '<STR_LIT>' ) : <EOL> if self . verify_origin ( ) : <EOL> self . set_header ( '<STR_LIT>' , <EOL> self . request . headers [ '<STR_LIT>' ] ) <EOL> self . set_header ( '<STR_LIT>' , '<STR_LIT:true>' ) <EOL> return True <EOL> else : <EOL> return False <EOL> else : <EOL> return True <EOL> def verify_origin ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> class TornadioXHRPollingSocketHandler ( TornadioPollingHandlerBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , router , session_id ) : <EOL> self . _timeout = None <EOL> self . _timeout_interval = router . settings [ '<STR_LIT>' ] <EOL> super ( TornadioXHRPollingSocketHandler , self ) . __init__ ( router , <EOL> session_id ) <EOL> @ asynchronous <EOL> def get ( self , * args , ** kwargs ) : <EOL> if not self . session . set_handler ( self ) : <EOL> raise HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> if not self . session . send_queue : <EOL> self . _timeout = self . router . io_loop . add_timeout ( <EOL> time . time ( ) + self . _timeout_interval , <EOL> self . _polling_timeout ) <EOL> else : <EOL> self . session . flush ( ) <EOL> def _polling_timeout ( self ) : <EOL> if self . session : <EOL> self . data_available ( '<STR_LIT>' ) <EOL> @ asynchronous <EOL> def post ( self , * args , ** kwargs ) : <EOL> if not self . preflight ( ) : <EOL> raise HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> ctype = self . request . headers . get ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) . split ( "<STR_LIT:;>" ) [ <NUM_LIT:0> ] <EOL> if ctype == '<STR_LIT>' : <EOL> data = None <EOL> body = self . request . body <EOL> if body . startswith ( '<STR_LIT>' ) : <EOL> data = unquote ( body [ <NUM_LIT:5> : ] ) <EOL> else : <EOL> data = self . get_argument ( '<STR_LIT:data>' , None ) <EOL> self . async_callback ( self . session . raw_message ) ( data ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> self . write ( '<STR_LIT>' ) <EOL> self . finish ( ) <EOL> def _detach ( self ) : <EOL> if self . session : <EOL> self . session . remove_handler ( self ) <EOL> self . session = None <EOL> def on_connection_close ( self ) : <EOL> self . _detach ( ) <EOL> def data_available ( self , raw_data ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> self . set_header ( '<STR_LIT>' , len ( raw_data ) ) <EOL> self . write ( raw_data ) <EOL> self . finish ( ) <EOL> self . _detach ( ) <EOL> class TornadioXHRMultipartSocketHandler ( TornadioPollingHandlerBase ) : <EOL> """<STR_LIT>""" <EOL> @ asynchronous <EOL> def get ( self , * args , ** kwargs ) : <EOL> if not self . session . set_handler ( self ) : <EOL> raise HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , <EOL> '<STR_LIT>' ) <EOL> self . set_header ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . write ( '<STR_LIT>' ) <EOL> self . session . flush ( ) <EOL> self . session . reset_heartbeat ( ) <EOL> @ asynchronous <EOL> def post ( self , * args , ** kwargs ) : <EOL> if not self . preflight ( ) : <EOL> raise HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> data = self . get_argument ( '<STR_LIT:data>' ) <EOL> self . async_callback ( self . session . raw_message ) ( data ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> self . write ( '<STR_LIT>' ) <EOL> self . finish ( ) <EOL> def on_connection_close ( self ) : <EOL> if self . session : <EOL> self . session . stop_heartbeat ( ) <EOL> self . session . remove_handler ( self ) <EOL> def data_available ( self , raw_data ) : <EOL> self . preflight ( ) <EOL> self . write ( "<STR_LIT>" ) <EOL> self . write ( raw_data + '<STR_LIT:\n>' ) <EOL> self . write ( '<STR_LIT>' ) <EOL> self . flush ( ) <EOL> self . session . delay_heartbeat ( ) <EOL> class TornadioHtmlFileSocketHandler ( TornadioPollingHandlerBase ) : <EOL> """<STR_LIT>""" <EOL> @ asynchronous <EOL> def get ( self , * args , ** kwargs ) : <EOL> if not self . session . set_handler ( self ) : <EOL> raise HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> self . set_header ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . set_header ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . write ( '<STR_LIT>' % ( '<STR_LIT:U+0020>' * <NUM_LIT> ) ) <EOL> self . session . flush ( ) <EOL> self . session . reset_heartbeat ( ) <EOL> @ asynchronous <EOL> def post ( self , * args , ** kwargs ) : <EOL> if not self . preflight ( ) : <EOL> raise HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> data = self . get_argument ( '<STR_LIT:data>' ) <EOL> self . async_callback ( self . session . raw_message ) ( data ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> self . write ( '<STR_LIT>' ) <EOL> self . finish ( ) <EOL> def on_connection_close ( self ) : <EOL> if self . session : <EOL> self . session . stop_heartbeat ( ) <EOL> self . session . remove_handler ( self ) <EOL> def data_available ( self , raw_data ) : <EOL> self . write ( <EOL> '<STR_LIT>' % json . dumps ( raw_data ) <EOL> ) <EOL> self . flush ( ) <EOL> self . session . delay_heartbeat ( ) <EOL> class TornadioJSONPSocketHandler ( TornadioXHRPollingSocketHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , router , session_id ) : <EOL> self . _index = None <EOL> super ( TornadioJSONPSocketHandler , self ) . __init__ ( router , session_id ) <EOL> @ asynchronous <EOL> def get ( self , * args , ** kwargs ) : <EOL> self . _index = kwargs . get ( '<STR_LIT>' , None ) <EOL> super ( TornadioJSONPSocketHandler , self ) . get ( * args , ** kwargs ) <EOL> @ asynchronous <EOL> def post ( self , * args , ** kwargs ) : <EOL> self . _index = kwargs . get ( '<STR_LIT>' , None ) <EOL> super ( TornadioJSONPSocketHandler , self ) . post ( * args , ** kwargs ) <EOL> def data_available ( self , raw_data ) : <EOL> if not self . _index : <EOL> raise HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> message = '<STR_LIT>' % ( <EOL> self . _index , <EOL> json . dumps ( raw_data ) <EOL> ) <EOL> self . preflight ( ) <EOL> self . set_header ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> self . set_header ( "<STR_LIT>" , len ( message ) ) <EOL> self . write ( message ) <EOL> self . finish ( ) <EOL> self . _detach ( ) </s>
