def process(self, data): data = self.handle_request_data(data) ret = self.parse_response(self.execute(data)) if not ret: LOG.info("no returned data") return for (session, data) in self.iterate_response_data(ret): session.send(data)
def rawDataReceived(self, rawData): data = self.protocol.received(rawData) if data == b"\x03\xe9": LOG.info("client was closed") self.session.quit() else: self.session.process(data.decode())
def handshake(self): _hash = self.validate_key() _hs = [] _hs.append("HTTP/1.1 101 Switching Protocols") _hs.append("Upgrade: websocket") _hs.append("Connection: Upgrade") _hs.append("Sec-WebSocket-Accept: " + _hash) # _hs.append("Sec-WebSocket-Protocol: %s" % self.header.protocol) response = "\r\n".join(_hs) + "\r\n\r\n" LOG.debug("handshake response : \n%s " % response) return response
def __init__(self, potocol, factory): self.protocol = potocol self.factory = factory self.attr = {} self.header = self.protocol.header self.sendto = None self.send_queue = deque() self.triggered = False if hasattr(self.protocol, "factory"): config = self.protocol.factory.config self.project, self.module = config.get_module(self.header.path) LOG.info("session belong to project " + self.project.name) self.apps = self.project.apps
def lineReceived(self, line): self.state = WebSocketProtocol.STATE_CONNECTING if line: LOG.debug("new line was received : %s " % line) self._header.append(line.decode()) else: self.header = Header("\n".join(self._header)) _p = Protocol13(self.header) if _p: self.session = self.factory.create(self) self.protocol = _p response = _p.handshake() self.transport.write(response.encode()) self.state = WebSocketProtocol.STATE_OPEN self.session.connected() self.setRawMode()
def handle_request_data(self, data): if not data: data = "{}" try: data = json.loads(re.sub("'", "\"", str(data))) except ValueError: pass if "cmd" not in data: LOG.warning("no cmd key in request data %s" % data) if type(data) == dict: data["cmd"] = DEFAULT_CMD if type(data) == str: data = {"cmd": DEFAULT_CMD, "data": data} return data
def get_module(self, module): for url in self.urls: LOG.info("url 0 [ %s ] , module [ %s ]" % (url[0], module)) if url[0] == module: LOG.debug("found it " + url[1]["target"]) module = _import(url[1]["target"]) return module else: LOG.error("not found module named %s in file setting" % module)
def search(self): search_path = self.root + "/*/*_setting.py" _p_path_list = glob.glob(search_path) if not _p_path_list: LOG.error("Not found any project in path " + self.root) return None for path in _p_path_list: LOG.debug("Add project " + path) self.projects.append(Project(path)) LOG.debug("Found %d project in path" % len(self.projects))
def connectionMade(self): now_number = len(self.factory.find()) LOG.debug("new connection has been accepted, now was %d " % now_number) self._header = []
def _init_base_info(self, line): LOG.debug("base info from header : %s" % line) self.scheme, self.uri, self.protocol = line.split() self.p = dict() self.parse_uri()
def _process_frame_header(self, data): # first byte contains fin, rsv and ops b = data[0] #fin: #Indicates that this is the final fragment in a message. The first #fragment MAY also be the final fragment self._frame_fin = (b & 0x80) != 0 #rsv #MUST be 0 unless an extension is negotiated which defines meanings #for non-zero values. If a nonzero value is received and none of #the negotiated extensions defines the meaning of such a nonzero #value, the receiving endpoint MUST _Fail the WebSocket Connection_. self._frame_rsv = (b & 0x70) >> 4 self._frame_ops = b & 0x0f # second byte contains mask and payload length b = data[1] self._frame_mask = (b & 0x80) != 0 frame_payload_len1 = b & 0x7f if (self._frame_mask): mask_len = 4 else: mask_len = 0 # i is frame index. It's at 2 here because we've already processed the # first 2 bytes of the frame. i = 2 if frame_payload_len1 < 126: self._frame_header_len = i + mask_len self._frame_payload_len = frame_payload_len1 elif frame_payload_len1 == 126: self._frame_header_len = i + 2 + mask_len self._frame_payload_len = struct.unpack( "!H", data[i:i+2])[0] i += 2 elif frame_payload_len1 == 127: self._frame_header_len = i + 8 + mask_len self._frame_payload_len = struct.unpack( "!Q", data[i:i+8])[0] i += 8 self._header_index = i LOG.debug("====== received data header ======") LOG.debug("_frame_fin = {}".format(self._frame_fin)) LOG.debug("_frame_rsv = {}".format(self._frame_rsv)) LOG.debug("_frame_ops = {}".format(self._frame_ops)) LOG.debug("_frame_mask = {}".format(self._frame_mask)) LOG.debug("_frame_mask_len = {}".format(mask_len)) LOG.debug("_header_index = {}".format(self._header_index)) LOG.debug("_frame_payload_len = {}".format(self._frame_payload_len)) LOG.debug("=================================")
def _import(_path): LOG.debug("import module with path " + _path) return importlib.import_module(_path)
def add_search_path(_path): LOG.debug("add search path " + _path) sys.path.insert(0, _path)
def add_app(self, key, value): self.apps[key] = value return value def get_app(self, key, default=None): if key not in self.apps and default is not None: self.apps[key] = default return self.apps.get(key, None) def get_conf(self, key): print("get conf %s and val %s" % (key, self.module.__dict__.get(key, None))) return self.module.__dict__.get(key, None) def get_module(self, module): for url in self.urls: LOG.info("url 0 [ %s ] , module [ %s ]" % (url[0], module)) if url[0] == module: LOG.debug("found it " + url[1]["target"]) module = _import(url[1]["target"]) return module else: LOG.error("not found module named %s in file setting" % module) if __name__ == '__main__': LOG.info("cloudbreath startup") reactor.listenTCP(setting.PORT, WebsocketFactory(Config(setting.webroot))) reactor.run()