def __call__(self, environ, start_response): request = self.request callback = request.GET.get('c', None) if callback is None: self.status = 500 self.body = '"callback" parameter required' return super(HTMLFileTransport, self).__call__( environ, start_response) write = start_response(self.status, self.headerlist) prelude = PRELUDE % callback.encode('UTF-8') prelude += ' ' * 1024 write(prelude) timing = self.timing session = self.session if session.state == STATE_NEW: write('<script>\np("o");\n</script>\r\n') session.open() size = 0 try: while True: try: message = [session.get_transport_message(timeout=timing)] except Empty: message = HEARTBEAT session.heartbeat() else: message = message_frame(message) if session.state == STATE_CLOSING: try: write("<script>\np(%s);\n</script>\r\n" % encode(close_frame('session closed','connection expired'))) except Exception as e: print "Error closing html streaming sockjs protocol" session.closed() break message = "<script>\np(%s);\n</script>\r\n" % encode(message) try: write(message) except: session.closed() raise StopStreaming() size += len(message) if size > self.maxsize: break finally: session.release() return []
def __call__(self, environ, start_response): request = self.request callback = request.GET.get('c', None) if callback is None: self.status = 500 self.body = '"callback" parameter required' return super(HTMLFileTransport, self).__call__(environ, start_response) write = start_response(self.status, self.headerlist) prelude = PRELUDE % callback prelude += ' ' * 1024 write(prelude) timing = self.timing session = self.session if session.state == STATE_NEW: write('<script>\np("o");\n</script>\r\n') session.open() size = 0 try: while True: try: message = [session.get_transport_message(timeout=timing)] except Empty: message = HEARTBEAT session.heartbeat() else: message = message_frame(message) if session.state == STATE_CLOSING: write("<script>\np(%s);\n</script>\r\n" % encode(close_frame('Go away!'))) session.closed() break message = "<script>\np(%s);\n</script>\r\n" % encode(message) try: write(message) except: session.closed() raise StopStreaming() size += len(message) if size > self.maxsize: break finally: session.release() return []
def JSONPolling(session, request): meth = request.method response = request.response response.headers['Content-Type'] = 'application/javascript; charset=UTF-8' response.headerlist.append( ('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')) session_cookie(request) if session.state == STATE_NEW: callback = request.GET.get('c', None) if callback is None: return HTTPServerError('"callback" parameter required') response.text = '%s("o");\r\n' % callback session.open() session.release() elif meth == "GET": callback = request.GET.get('c', None) if callback is None: return HTTPServerError('"callback" parameter required') if session.state in (STATE_CLOSING, STATE_CLOSED): response.text = "%s(%s);\r\n" % ( callback, encode(close_frame(3000, 'Go away!'))) if session.state == STATE_CLOSING: session.closed() else: messages = [] try: messages.append(session.get_transport_message(timeout=timing)) while True: try: messages.append( session.get_transport_message(block=False)) except Empty: break except Empty: messages = HEARTBEAT session.heartbeat() else: messages = message_frame(messages) response.text = "%s(%s);\r\n" % (callback, encode(messages)) session.release() elif meth == "POST": data = request.body_file.read() ctype = request.headers.get('Content-Type', '').lower() if ctype == 'application/x-www-form-urlencoded': if not data.startswith('d='): return HTTPServerError("Payload expected.") data = unquote_plus(data[2:]) if not data: return HTTPServerError("Payload expected.") try: messages = decode(data) except: return HTTPServerError("Broken JSON encoding.") for msg in messages: session.message(msg) response.status = 200 response.headers['Content-Type'] = 'text/plain; charset=UTF-8' response.body = 'ok' else: raise Exception("No support for such method: %s" % meth) return response
def test_encode(self): from pyramid_sockjs import protocol self.assertEqual(protocol.encode({}), json.dumps({})) self.assertEqual(protocol.encode(['test']), json.dumps(['test'])) self.assertEqual(protocol.encode('"test"'), json.dumps('"test"'))
def test_message_frame_endline(self): from pyramid_sockjs import protocol msg = protocol.message_frame(['msg1', 'msg2'], '\n') self.assertEqual(msg, 'a%s\n' % protocol.encode(['msg1', 'msg2']))
def test_message_frame_endline(self): from pyramid_sockjs import protocol msg = protocol.message_frame(['msg1', 'msg2'], '\n') self.assertEqual(msg, 'a%s\n'%protocol.encode(['msg1', 'msg2']))
def JSONPolling(session, request): meth = request.method response = request.response response.headers['Content-Type'] = 'application/javascript; charset=UTF-8' response.headerlist.append( ('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')) session_cookie(request) if session.state == STATE_NEW: callback = request.GET.get('c', None) if callback is None: return HTTPServerError('"callback" parameter required') response.text = '%s("o");\r\n' % callback session.open() session.release() elif meth == "GET": callback = request.GET.get('c', None) if callback is None: return HTTPServerError('"callback" parameter required') if session.state in (STATE_CLOSING, STATE_CLOSED): response.text = "%s(%s);\r\n"%( callback, encode(close_frame(3000, 'Go away!'))) if session.state == STATE_CLOSING: session.closed() else: messages = [] try: messages.append(session.get_transport_message(timeout=timing)) while True: try: messages.append( session.get_transport_message(block=False)) except Empty: break except Empty: messages = HEARTBEAT session.heartbeat() else: messages = message_frame(messages) response.text = "%s(%s);\r\n"%(callback, encode(messages)) session.release() elif meth == "POST": data = request.body_file.read() ctype = request.headers.get('Content-Type', '').lower() if ctype == 'application/x-www-form-urlencoded': if not data.startswith('d='): return HTTPServerError("Payload expected.") data = unquote_plus(data[2:]) if not data: return HTTPServerError("Payload expected.") try: messages = decode(data) except: return HTTPServerError("Broken JSON encoding.") for msg in messages: session.message(msg) response.status = 200 response.headers['Content-Type'] = 'text/plain; charset=UTF-8' response.body = 'ok' else: raise Exception("No support for such method: %s"%meth) return response