def data_frame_parser(data, flags): R = [] # PADDED if (flags & 0x8): # tmp_data=[PadLength, Data+Padding] tmp_data = (parser.bytes_parse(data, [1])) R = parser.bytes_parse(tmp_data[1], [len(tmp_data[1]) - tmp_data[0]]) else: R.append(data) return R
def settings_frame_parser(data, flagss): R = [] tmp_data = parser.bytes_parse(data, [2, 4]) length = int(len(data) / 6) if (1 < length): for _ in range(length): R.append([tmp_data[0], tmp_data[1]]) tmp_data = parser.bytes_parse(tmp_data[-1], [2, 4]) else: R = tmp_data return R
def push_promise_frame_parser(data, flags): R = [] tmp_data = [] # R will... [R+PromisedStreamId, HeaderBlockFragment] if (flags & 0x8): # tmp_data=[PadLength, Data+Padding] tmp_data = (parser.bytes_parse(data, [1])) tmp_data = parser.bytes_parse(tmp_data[1], [len(tmp_data[1]) - tmp_data[0]])[0] R = parser.bytes_parse(tmp_data, [4]) else: R = parser.bytes_parse(data, [4]) return R
def frame_parser(data): R = [] tmp_data = [] while True: tmp_data = parser.bytes_parse(data, [3, 1, 1, 4]) payload_and_data = parser.bytes_parse( tmp_data[-1], [int.from_bytes(tmp_data[0], "big")]) tmp_data = tmp_data[:-1] if (tmp_data[1] == b"\x09"): print(tmp_data) R[0] += tmp_data[-1] else: tmp_data.append(frame_payload_parsers[tmp_data[1]]( payload_and_data[0], int.from_bytes(tmp_data[2], "big"))) R.append(tmp_data) if (len(payload_and_data) == 1): break data = payload_and_data[1] return R
def headers_frame_parser(data, flags): #print(data.hex()) R = [] tmp_data = [] # R will... [E+StreamDependency, Weight, HeaderBlockFragment] # PADDED tmp_data = data if (flags & 0x8): # tmp_data=[PadLength, Data+Padding] tmp_data = (parser.bytes_parse(data, [1])) tmp_data = parser.bytes_parse(tmp_data[1], [len(tmp_data[1]) - tmp_data[0]])[0] #R = [b"\xff\xff\xff\xff", b"\xff",parser.bytes_parse(tmp_data[1], [len(tmp_data[1])-tmp_data[0]])[0]] R = [ parser.bytes_parse(tmp_data[1], [len(tmp_data[1]) - tmp_data[0]])[0] ] if (flags & 0x20): R = parser.bytes_parse(tmp_data, [4, 1]) return R
def priority_frame_parser(data, flags): R = [] R = parser.bytes_parse(data, [4, 1]) return R
def goaway_frame_parser(data, flags): R = [] R = parser.bytes_parse(data, [4, 4]) return R
def client_handler(fileno): client_socket, client_address, tls_obj, tls_in_buff, tls_out_buff = connections_info[ fileno] recv_data = recv_datas[fileno] while True: tmp_data = parser.bytes_parse(recv_data, [3, 1, 1, 4]) payload_and_data = parser.bytes_parse( tmp_data[-1], [int.from_bytes(tmp_data[0], "big")]) tmp_data = tmp_data[:-1] tmp_data.append(http2_parser.frame_payload_parsers[tmp_data[1]]( payload_and_data[0], int.from_bytes(tmp_data[2], "big"))) if (len(payload_and_data) == 2): recv_data = payload_and_data[1] else: recv_data = b"" # tmp_data = [Length, Types, Flags, (R&S), [FramePayload]] #//////////////////////////////////////////////////////////////////// frame = tmp_data print("type:", http2_frame.bytes_frame_name[frame[1]]) if (frame[1] == http2_frame.frame_type[4]): # SETTINGS if (int.from_bytes(frame[2], "big") == http2_frame.frame_flag["NONE"]): hpack_encoders[fileno].header_table_size = int.from_bytes( frame[-1][0][1], "big") stream_identifire = int.from_bytes(frame[2], "big") res_settings_frame = http2_frame.Http2Frame() res_settings_frame.SetFrameType( http2_frame.frame_type_name["SETTINGS"]) res_settings_frame.SetStreamIdentifire(stream_identifire) res_settings_frame.SetFlags([http2_frame.frame_flag["NONE"]]) res_settings_frame.SetFramePayload(b"") SEND(fileno, res_settings_frame.Gen()) if (frame[1] == http2_frame.frame_type[1]): # HEADERS stream_identifire = int.from_bytes(frame[3], "big") request_header = hpack_decoders[fileno].decode(frame[-1][-1]) params = {} data, m_type, status = b"", "", 200 for p in request_header: params[p[0]] = p[1] #print(params) if (params[":method"] == "GET"): if (params[":path"].find("?") != -1): pfg = params[":path"].split("?") data, m_type, status = gept.gept(pfg[0], pfg[1]) data = data.encode("utf-8") else: data, m_type, status = get.get( params[":path"], root_dir=root_directory, message_dir=message_directory) print("GET--->:", params[":path"]) elif (params[":method"] == "POST"): post_data = [] while True: tmp_data = parser.bytes_parse(recv_data, [3, 1, 1, 4]) payload_and_data = parser.bytes_parse( tmp_data[-1], [int.from_bytes(tmp_data[0], "big")]) tmp_data = tmp_data[:-1] tmp_data.append( http2_parser.frame_payload_parsers[tmp_data[1]]( payload_and_data[0], int.from_bytes(tmp_data[2], "big"))) if (len(payload_and_data) == 2): recv_data = payload_and_data[1] else: recv_data = b"" post_data += tmp_data if (int.from_bytes(tmp_data[2], "big") & 0x01): break # tmp_data = [Length, Types, Flags, (R&S), [FramePayload]] print("POST--->:", params[":path"]) print(post_data) data, m_type, status = post.post( params[":path"], (post_data[-1][0]).decode("utf-8")) data = data.encode("utf-8") else: data, m_type, status = get.get( "/400.html", root_dir="./sl_contents/example.com/http/messages") status = 400 rh = [(":status", str(status)), ("server", http2_defines.server_name), ("content-type", m_type), ("content-length", str(len(data)))] res_header_frame = http2_frame.Http2Frame() res_header_frame.SetFrameType( http2_frame.frame_type_name["HEADERS"]) res_header_frame.SetStreamIdentifire(stream_identifire) res_header_frame.SetFramePayload(hpack_encoders[fileno].encode(rh)) res_header_frame.SetFlags([http2_frame.frame_flag["END_HEADERS"]]) SEND(fileno, res_header_frame.Gen()) res_data = parser.bytes_parseq( data, http2_defines.default_max_frame_size) res_header_frame.SetFlags([http2_frame.frame_flag["NONE"]]) res_data_frame = res_header_frame res_data_frame.SetFrameType(http2_frame.frame_type_name["DATA"]) if (len(res_data) == 1): res_data_frame.SetFramePayload(res_data[0]) res_data_frame.SetFlags([http2_frame.frame_flag["END_STREAM"]]) SEND(fileno, res_data_frame.Gen()) for d in res_data[:-1]: res_data_frame.SetFramePayload(d) SEND(fileno, res_data_frame.Gen()) res_data_frame.SetFramePayload(res_data[-1]) res_data_frame.SetFlags([http2_frame.frame_flag["END_STREAM"]]) SEND(fileno, res_data_frame.Gen()) if (len(recv_data) == 0): break