<commit_before>import struct
from util import *
from settings import *

def getFrame(data):
    header = Http2Header.getFrame(data[:9])

    if header.frame == TYPE.DATA:
        frame = Data.getFrame(header, data[9:])
    elif header.frame == TYPE.HEADERS:
        frame = Headers.getFrame(header, data[9:])
    elif header.frame == TYPE.PRIORITY:
        frame = Priority.getFrame(header, data[9:])
    elif header.frame == TYPE.RST_STREAM:
        frame = RstStream.getFrame(header, data[9:])
    elif header.frame == TYPE.SETTINGS:
        frame = Settings.getFrame(header, data[9:])
    elif header.frame == TYPE.PUSH_PROMISE:
        frame = PushPromise.getFrame(header, data[9:])
    elif header.frame == TYPE.PING:
        frame = Ping.getFrame(header, data[9:])
    elif header.frame == TYPE.GOAWAY:
        frame = Goaway.getFrame(header, data[9:])
    elif header.frame == TYPE.WINDOW_UPDATE:
        frame = WindowUpdate.getFrame(header, data[9:])
    elif header.frame == TYPE.CONTINUATION:
        frame = Continuation.getFrame(header, data[9:])


    return frame

class Http2Header():
    def __init__(self, frame, flags, streamID, parsing):
        self.frame = frame
        self.flags = flags
        self.streamID = streamID
        if not parsing:
            self._makeWire()

    def _makeWire(self):
        self.wire = struct.pack(">I2BI", self.length, self.frame, self.flags, self.sId)[1:]

    def setLength(self, length):
        self.length = length

    def getWire(self):
        return self.wire

    @staticmethod
    def getFrame(data):
        length, frame, flags, streamID = struct.unpack(">I2BI", "\x00"+data)
        return Http2Header(length, frame, flags, streamID, True)


class Data():
    def __init__(self, flags, streamID, data = "", padLen = 0, header = None):
        self.data = data
        self.padLen = padLen
        if header:
            self.header = header
        else:
            self.header = Http2Header(TYPE.DATA, flags, streamID)
            self._makeWire()
            self.header.setLength(len(self.wire))

    def _makeWire(self):
        self.wire = ""
        padding = ""
        if self.header.flags&FLAG.PADDED == FLAG.PADDED:
            self.wire += packHex(self.padLen, 1)
            padding += packHex(0, self.padLen)
        self.wire += self.data + padding

    @staticmethod
    def getFrame(header, data):
        index = 0
        padLen = 0
        if header.flags&FLAG.PADDED == FLAG.PADDED:
            padLen = struct.unpack(">B", data[0])[0]
            index += 1
        content = data[index: len(data) if Flag != FLAG.PADDED else -padLen]
        return Data(None, None, content, padLen, header)



class Headers():
    def __init__(self, flags, streamID, headers, table = None, padLen = 0, E = 0, streamDependency = 0, weight = 0, header = None):
        self.headers = headers
        self.padLen = padLen
        if E:
            self.streamDependency = streamDependency | 0x80000000
        else:
            self.streamDependency = streamDependency
        self.weight = weight
        if header:
            self.header = header
        else:
            self.header = Http2Header(TYPE.HEADERS, flgas, streamID)
            self._makeWire(table)
            self.header.setLength(len(self.wire))

    def _makeWire(self, table):
        padding = ""
        self.wire = encode(headers, False, False, False, table)
        if self.header.flags&FLAG.PADDED == FLAG.PADDED:
            self.wire += packHex(self.padLen, 1)
            padding += packHex(0, self.padLen)
        if self.header.flags&FLAG.PRIORITY == FLAG.PRIORITY:
            self.wire += self.streamDependency
            self.wire += packHex(self.weight, 1)
        if self.header.flags&FLAG.END_HEADERS == FLAG.END_HEADERS:
            #set state half closed local
            pass
        if self.header.flags&FLAG.END_STREAM == FLAG.END_STREAM:
            #set state half closed local
            pass
        self.wire += padding

    @staticmethod
    def getFrame(header, data, table):
        if header.streamID == 0:
            #send error here?
            pass

        index = 0
        if header.flags&FLAG.END_HEADERS = FLAG.END_HEADERS:
            print(decode(data, stable))
            #return DATA
            return
        if header.flags&FLAG.PADDED == FLAG.PADDED:
            padLen = struct,unpack(">B", data[0])[0]
            padding = data[-padLen:]
            index += 1
        if header.flags&FLAG.PRIORITY == FLAG.PRIORITY:
            streamDependency, weight = struct.unpack(">IB", data[index:index+5])
            E = streamDependenct >> 31
            streamDependency &= 0x7fffffff
            index += 5
        if header.flags&FLAG.END_STREAM == FLAG.END_STREAM:
            #set state half closed remote
            pass

        #append wire if not a END_HEADERS flag


class Goaway():
    def __init__(self, lastID, errorNum = ERR_CODE.NO_ERROR, debugString = "", header = None):
        self.lastID = lastID
        self.errorNum = errorNum
        self.debugString = debugString
        if header:
            self.header = header
        else:
            self.header = Http2Header(TYPE.GOAWAY, 0, 0)
            self._makeWire()
            self.header.setLength(len(self.wire))

    def _makeWire(self):
        self.wire = packHex(self.lastID, 4)
        self.wire += packHex(self.errorNum, 4)
        self.wire += self.debugString

    @staticmethod<commit_msg>change argument order and return frame in static method<commit_after>import struct
from util import *
from settings import *

def getFrame(data):
    header = Http2Header.getFrame(data[:9])

    if header.frame == TYPE.DATA:
        frame = Data.getFrame(header, data[9:])
    elif header.frame == TYPE.HEADERS:
        frame = Headers.getFrame(header, data[9:])
    elif header.frame == TYPE.PRIORITY:
        frame = Priority.getFrame(header, data[9:])
    elif header.frame == TYPE.RST_STREAM:
        frame = RstStream.getFrame(header, data[9:])
    elif header.frame == TYPE.SETTINGS:
        frame = Settings.getFrame(header, data[9:])
    elif header.frame == TYPE.PUSH_PROMISE:
        frame = PushPromise.getFrame(header, data[9:])
    elif header.frame == TYPE.PING:
        frame = Ping.getFrame(header, data[9:])
    elif header.frame == TYPE.GOAWAY:
        frame = Goaway.getFrame(header, data[9:])
    elif header.frame == TYPE.WINDOW_UPDATE:
        frame = WindowUpdate.getFrame(header, data[9:])
    elif header.frame == TYPE.CONTINUATION:
        frame = Continuation.getFrame(header, data[9:])


    return frame

class Http2Header():
    def __init__(self, frame, flags, streamID, parsing):
        self.frame = frame
        self.flags = flags
        self.streamID = streamID
        if not parsing:
            self._makeWire()

    def _makeWire(self):
        self.wire = struct.pack(">I2BI", self.length, self.frame, self.flags, self.sId)[1:]

    def setLength(self, length):
        self.length = length

    def getWire(self):
        return self.wire

    @staticmethod
    def getFrame(data):
        length, frame, flags, streamID = struct.unpack(">I2BI", "\x00"+data)
        return Http2Header(length, frame, flags, streamID, True)


class Data():
    def __init__(self, flags, streamID, data = "", padLen = 0, header = None):
        self.data = data
        self.padLen = padLen
        if header:
            self.header = header
        else:
            self.header = Http2Header(TYPE.DATA, flags, streamID)
            self._makeWire()
            self.header.setLength(len(self.wire))

    def _makeWire(self):
        self.wire = ""
        padding = ""
        if self.header.flags&FLAG.PADDED == FLAG.PADDED:
            self.wire += packHex(self.padLen, 1)
            padding += packHex(0, self.padLen)
        self.wire += self.data + padding

    @staticmethod
    def getFrame(header, data):
        index = 0
        padLen = 0
        if header.flags&FLAG.PADDED == FLAG.PADDED:
            padLen = struct.unpack(">B", data[0])[0]
            index += 1
        content = data[index: len(data) if Flag != FLAG.PADDED else -padLen]
        return Data(None, None, content, padLen, header)



class Headers():
    def __init__(self, flags, streamID, headers, padLen = 0, E = 0, streamDependency = 0, weight = 0table = None, header = None):
        self.headers = headers
        self.padLen = padLen
        self.E = E
        self.streamDependency = streamDependency
        if header:
            self.header = header
        else:
            self.header = Http2Header(TYPE.HEADERS, flgas, streamID)
            self._makeWire(table)
            self.header.setLength(len(self.wire))

    def _makeWire(self, table):
        padding = ""
        self.wire = encode(headers, False, False, False, table)
        if self.header.flags&FLAG.PADDED == FLAG.PADDED:
            self.wire += packHex(self.padLen, 1)
            padding += packHex(0, self.padLen)
        if self.header.flags&FLAG.PRIORITY == FLAG.PRIORITY:
            if self.E:
                self.wire += packHex(self.streamDependency | 0x80000000, 4)
            else:
                self.wire += packHex(self.streamDependency, 4)
            self.wire += packHex(self.weight, 1)
        if self.header.flags&FLAG.END_HEADERS == FLAG.END_HEADERS:
            #set state half closed local
            pass
        if self.header.flags&FLAG.END_STREAM == FLAG.END_STREAM:
            #set state half closed local
            pass
        self.wire += padding

    @staticmethod
    def getFrame(header, data, table):
        if header.streamID == 0:
            #send error here?
            pass

        index = 0
        padLen = 0
        E = 0
        streamDependency = 0
        weight = 0
        if header.flags&FLAG.END_HEADERS = FLAG.END_HEADERS:
            headers = decode(data, table)
            #return DATA
        if header.flags&FLAG.PADDED == FLAG.PADDED:
            padLen = struct,unpack(">B", data[0])[0]
            padding = data[-padLen:]
            index += 1
        if header.flags&FLAG.PRIORITY == FLAG.PRIORITY:
            streamDependency, weight = struct.unpack(">IB", data[index:index+5])
            E = streamDependenct >> 31
            streamDependency &= 0x7fffffff
            index += 5
        if header.flags&FLAG.END_STREAM == FLAG.END_STREAM:
            #set state half closed remote
            pass

        #append wire if not a END_HEADERS flag
        return Headers(None, None, headers, padLen, E, streamDependency, weight, None, header)


class Goaway():
    def __init__(self, lastID, errorNum = ERR_CODE.NO_ERROR, debugString = "", header = None):
        self.lastID = lastID
        self.errorNum = errorNum
        self.debugString = debugString
        if header:
            self.header = header
        else:
            self.header = Http2Header(TYPE.GOAWAY, 0, 0)
            self._makeWire()
            self.header.setLength(len(self.wire))

    def _makeWire(self):
        self.wire = packHex(self.lastID, 4)
        self.wire += packHex(self.errorNum, 4)
        self.wire += self.debugString

    @staticmethod
{'fraction_non_alphanumeric': 0.11372584108804581, 'fraction_numerical': 0.01145311381531854, 'mean_word_length': 2.1264335664335663, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6447898', 'n_tokens_mistral': 3470, 'n_tokens_neox': 3311, 'n_words': 800}