def poll(self, handler): """ Spin lock the thread until we have a message on the gevent queue. """ writer = handler.socket.makefile() written = 0 try: while True: messages = self.session.get_messages(timeout=self.TIMING) messages = self.encode(messages) frame = protocol.message_frame(messages) + '\n' chunk = handler.raw_chunk(frame) writer.write(chunk) writer.flush() written += len(chunk) zero_chunk = handler.raw_chunk('') writer.write(zero_chunk) if written > self.CUTOFF: zero_chunk = handler.raw_chunk('') writer.write(zero_chunk) break except socket.error: self.session.expire()
def stream(self, handler): try: callback_param = handler.environ.get("QUERY_STRING").split('=')[1] self.callback = urllib2.unquote(callback_param) except IndexError: handler.do500(message='"callback" parameter required') return # Turn on cookie, turn off caching, set headers handler.enable_cookie() handler.enable_nocache() handler.headers += [ ("Content-Type", "text/html; charset=UTF-8"), ("Transfer-Encoding", "chunked"), ('Connection', 'keep-alive'), ] # Start writing handler.start_response("200 OK", handler.headers) headers = handler.raw_headers() writer = handler.socket.makefile() writer.write(headers) written = 0 # Send down HTMLFile IFRAME html = protocol.HTMLFILE_IFRAME_HTML % self.callback html = pad(html) chunk = handler.raw_chunk(html) writer.write(chunk) writer.flush() written += len(chunk) chunk = '<script>\np("o");\n</script>\r\n' chunk = handler.raw_chunk(chunk) writer.write(chunk) writer.flush() written += len(chunk) try: while written < self.response_limit: messages = self.session.get_messages(timeout=5) messages = self.encode(messages) frame = protocol.message_frame(messages) frame = json.dumps(frame) chunk = '<script>\np(%s);\n</script>\r\n' % frame chunk = handler.raw_chunk(chunk) writer.write(chunk) writer.flush() written += len(chunk) except socket.error: self.session.expire() zero_chunk = handler.raw_chunk('') writer.write(zero_chunk) writer.close()
def encode(self, data): # TODO: Not using protocol.encode because it doesn't escape # things properly here. The other version should be fixed at # some point to avoid duplication. data = json.dumps(data, separators=(',', ':')) if isinstance(data, basestring): # Don't both calling json, since its simple data = '[' + data + ']' elif isinstance(data, (object, dict, list)): data = json.dumps(data, separators=(',',':')) else: raise ValueError("Unable to serialize: %s", str(data)) return protocol.message_frame(data)
def poll(self, socket): """ Spin lock the thread until we have a message on the gevent queue. """ while not self.session.expired: messages = self.session.get_messages() messages = self.encode(messages) socket.send(protocol.message_frame(messages)) close_error = protocol.close_frame(3000, "Go away!", newline=False) socket.send(close_error) # Session expires, so unlock socket.close() self.session.unlock()
def stream(self, handler): writer, written = self.write_prelude(handler) try: open_chunk = handler.raw_chunk('o\n') writer.write(open_chunk) writer.flush() while written < self.response_limit: messages = self.session.get_messages(timeout=self.TIMING) messages = self.encode(messages) frame = protocol.message_frame(messages) + '\n' chunk = handler.raw_chunk(frame) writer.write(chunk) writer.flush() written += len(chunk) except socket.error: self.session.expire() zero_chunk = handler.raw_chunk('') writer.write(zero_chunk) self.session.unlock()
def write_frame(self, data): frame = protocol.json.dumps(protocol.message_frame(data)) return """%s(%s);\r\n""" % ( self.callback, frame)
def write_frame(self, data): return protocol.message_frame(data) + '\n'