def post(self, session_id): try: # Stats self.server.stats.connection_opened() # Get session self.session = self._get_session(session_id) # Can not send messages to closed session or if preflight() failed if self.session.is_closed or not self.preflight(): raise HTTPError(401) # Socket.io always send data utf-8 encoded. data = self.request.body # IE XDomainRequest support if not data.startswith('d='): logging.error('Malformed JSONP POST request') raise HTTPError(403) # Grab data data = urllib.unquote_plus(data[2:]).decode('utf-8') # If starts with double quote, it is json encoded (socket.io workaround) if data.startswith(u'"'): data = proto.json_load(data) # Process packets one by one packets = proto.decode_frames(data) # Tracking self.server.stats.on_packet_recv(len(packets)) for p in packets: try: self.session.raw_message(p) except Exception: # Close session if something went wrong self.session.close() self.set_header('Content-Type', 'text/plain; charset=UTF-8') self.finish() finally: self.server.stats.connection_closed()
def _parse_qs(self, environ): try: querystring = urllib.unquote_plus(environ['QUERY_STRING']) except KeyError: return {} return parse_qs(querystring)