Ejemplo n.º 1
0
    def unwrap_header(self):
        size = self.reader.size()
        rdata = self.reader.read()

        p = rdata.find(b"\r\n\r\n")

        if p > 0 and p < 10: raise HttpErr("wrong http1x response header")
        if p < 0 and size > 8192:
            raise HttpErr("the http1x response header too long")

        if p < 0: return
        p += 4
        header_data = rdata[0:p]
        body_data = rdata[p:]

        sts = header_data.decode("iso-8859-1")

        try:
            resp, fields = httputils.parse_http1x_response_header(sts)
        except httputils.Http1xHeaderErr:
            raise HttpErr("wrong http response header")

        _, status = resp

        self.set_status(int(status[0:3]))
        self.set_headers(fields)

        self.reader._putvalue(body_data)
        self.header_ok = True
Ejemplo n.º 2
0
    def handle_http_response_header(self):
        size = self.writer.size()
        rdata = self.writer._getvalue()

        p = rdata.find(b"\r\n\r\n")
        if p < 0 and size > 4096: return False
        if p < 12: return False

        p += 4
        try:
            resp, kv_pairs = httputils.parse_http1x_response_header(
                rdata[0:p].decode("iso-8859-1"))
        except httputils.Http1xHeaderErr:
            return False

        # 重写头部连接字段,数据传输完毕后就关闭连接
        resp_headers = []
        for k, v in kv_pairs:
            if k.lower() == "connection":
                resp_headers.append((
                    "Connection",
                    "close",
                ))
                continue
            resp_headers.append((
                k,
                v,
            ))
        data = httputils.build_http1x_resp_header(resp[1], resp_headers)

        self.writer.write(data.encode("iso-8859-1"))
        self.writer.write(rdata[p:])
        self.__http_response_header_ok = True

        return True
Ejemplo n.º 3
0
    def unwrap_header(self):
        size = self.reader.size()
        rdata = self.reader.read()

        p = rdata.find(b"\r\n\r\n")

        if p > 0 and p < 10: raise HttpErr("wrong http1x response header")
        if p < 0 and size > 8192: raise HttpErr("the http1x response header too long")

        if p < 0: return
        p += 4
        header_data = rdata[0:p]
        body_data = rdata[p:]

        sts = header_data.decode("iso-8859-1")

        try:
            resp, fields = httputils.parse_http1x_response_header(sts)
        except httputils.Http1xHeaderErr:
            raise HttpErr("wrong http response header")

        _, status = resp

        self.set_status(int(status[0:3]))
        self.set_headers(fields)

        self.reader._putvalue(body_data)
        self.header_ok = True
Ejemplo n.º 4
0
    def recv_handshake(self):
        size = self.reader.size()
        data = self.reader.read()

        p = data.find(b"\r\n\r\n")

        if p < 10 and size > 2048:
            logging.print_general("wrong_http_response_header", self.__server_address)
            self.delete_handler(self.fileno)
            return

        if p < 0:
            self.reader._putvalue(data)
            return
        p += 4

        self.reader._putvalue(data[p:])

        s = data[0:p].decode("iso-8859-1")

        try:
            resp, kv_pairs = httputils.parse_http1x_response_header(s)
        except httputils.Http1xHeaderErr:
            logging.print_general("wrong_http_reponse_header", self.__server_address)
            self.delete_handler(self.fileno)
            return

        version, status = resp

        if status.find("101") != 0:
            logging.print_general("https_handshake_error:%s" % status, self.__server_address)
            self.delete_handler(self.fileno)
            return

        accept_key = self.get_http_kv_pairs("sec-websocket-accept", kv_pairs)
        if wslib.gen_handshake_key(self.__http_handshake_key) != accept_key:
            logging.print_general("https_handshake_error:wrong websocket response key", self.__server_address)
            self.delete_handler(self.fileno)
            return

        auth_id = self.get_http_kv_pairs("x-auth-id", kv_pairs)
        if hashlib.sha256(self.__http_auth_id.encode()).hexdigest() != auth_id:
            logging.print_general("wrong_auth_id", self.__server_address)
            self.delete_handler(self.fileno)
            return

        self.__http_handshake_ok = True
        logging.print_general("http_handshake_ok", self.__server_address)
        # 发送还没有连接的时候堆积的数据包
        if self.__tmp_buf: self.add_evt_write(self.fileno)
        while 1:
            try:
                self.writer.write(self.__tmp_buf.pop(0))
            except IndexError:
                break
            ''''''
        ''''''
Ejemplo n.º 5
0
    def __parse_header(self):
        size = self.__reader.size()
        rdata = self.__reader.read()

        p = rdata.find(b"\r\n\r\n")

        if p < 0 and size > self.__MAX_HEADER_SIZE:
            raise _http_response_error("the response header too long")

        if p < 0: return

        p += 4

        try:
            response, mapv = httputils.parse_http1x_response_header(rdata[0:p].decode("iso-8859-1"))
        except httputils.Http1xHeaderErr:
            raise _http_response_error("wrong response header")

        has_chunked = False
        has_length = False

        self.__resp_code = int(response[1][0:3])

        for k, v in mapv:
            if k.lower() == "content-length":
                has_length = True
                try:
                    self.__resp_length = int(v)
                except ValueError:
                    raise _http_response_error("wrong http content length value")
                continue

            if k.lower() == "transfer-encoding":
                if v.lower() != "chunked":
                    raise _http_response_error("wrong http transfer-encoding")
                has_chunked = True

        if has_chunked and has_length:
            raise _http_response_error("conflict chunked with content-length")

        self.__is_chunked = has_chunked

        if self.__resp_code >= 200:
            self.__is_resp_header = True

        if has_chunked:
            self.__chunked = httpchunked.parser()

        self.__data_list.append(rdata[0:p])
        self.__reader._putvalue(rdata[p:])