Source

ginsfsm / ginsfsm / protocols / sockjs / server / transports / xhrstreaming.py

The default branch has multiple heads

# -*- coding: utf-8 -*-
"""
    ginsfsm.protocols.sockjs.server.transports.xhrstreaming
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Xhr-Streaming transport implementation
"""
from pyramid.view import view_config
from pyramid.security import authenticated_userid

from ginsfsm.gobj import GObj
from ginsfsm.protocols.sockjs.server.transports import streamingbase

#----------------------------------------------------------------#
#                   GClass
#----------------------------------------------------------------#
GXHRSTREAMING_GCONFIG = {
    'sockjs_server': [None, None, 0, None, ""],
}


class GXhrStreaming(GObj):
    """  GStreamingBase GObj.
    """
    def __init__(self):
        GObj.__init__(self, {}, GXHRSTREAMING_GCONFIG)

    def start_up(self):
        """ Initialization zone.
        """
        # views
        """
        self.sockjs_server.pyramid_config.add_view(
            context=GXhrStreaming,
            name='',
            view=XhrStreamingTransport,
            attr='post',
            path_info=self.resource_path(),
            request_method='POST',
            permission=self.sockjs_server.permission,
        )
        """


#----------------------------------------------------------------#
#                   Info views
#----------------------------------------------------------------#
@view_config(
    context=GXhrStreaming,
    name='',
    attr='post',
    request_method='POST',
)
@view_config(
    context=GXhrStreaming,
    name='',
    attr='options',
    request_method='OPTIONS',
)
class XhrStreamingTransport(streamingbase.StreamingTransportBase):
    name = 'xhr_streaming'

    def post(self):
        response = self.set_response(self)
        self.headers = ()  # important trick: clean default webob headers.

        self.sid = self.context.real_name  # session_id
        if self.context.sockjs_server.per_user:
            self.sid = (authenticated_userid(self.request), self.sid)

        #try:
        #    session = manager.get(sid, create, request=request)
        #except KeyError:
        #    return HTTPNotFound(headers=(session_cookie(request),))
        #request.environ['wsgi.sockjs_session'] = session

        # Handle cookie
        self.preflight()
        self.handle_session_cookie()
        self.disable_cache()
        response.content_type = 'application/javascript'
        response.charset = 'UTF-8'
        return self  # Webob will execute __call__

    def __call__(self, environ, start_response):
        print ">>>>>>>>>> gins", self._abs_headerlist(environ)
        write = self.write = start_response(
            self.status, self._abs_headerlist(environ))
        write('h' * 2048 + '\n')
        self.flush()

        if not self._attach_session(self.sid, False):
            self.finish()
            return ()

        if self.session:
            self.session.flush()
        return []

    def send_pack(self, message, binary=False):
        if binary:
            raise Exception('binary not supported for XhrStreamingTransport')

        self.active = False

        try:
            self.notify_sent(len(message))

            self.write(message + '\n')
            self.flush(callback=self.send_complete)
        except IOError:
            # If connection dropped, make sure we close offending session instead
            # of propagating error all way up.
            self.session.delayed_close()
            self._detach()