MCPcopy Create free account
hub / github.com/secdev/scapy / HPackStrLenField

Class HPackStrLenField

scapy/contrib/http2.py:1194–1293  ·  view source on GitHub ↗

HPackStrLenField is a StrLenField variant specialized for HTTP/2 HPack This variant uses an internal representation that implements HPackStringsInterface. # noqa: E501

Source from the content-addressed store, hash-verified

1192
1193
1194class HPackStrLenField(fields.Field):
1195 """ HPackStrLenField is a StrLenField variant specialized for HTTP/2 HPack
1196
1197 This variant uses an internal representation that implements HPackStringsInterface. # noqa: E501
1198 """
1199 __slots__ = ['_length_from', '_type_from']
1200
1201 def __init__(self, name, default, length_from, type_from):
1202 # type: (str, HPackStringsInterface, Callable[[packet.Packet], int], str) -> None # noqa: E501
1203 super(HPackStrLenField, self).__init__(name, default)
1204 self._length_from = length_from
1205 self._type_from = type_from
1206
1207 def addfield(self, pkt, s, val):
1208 # type: (Optional[packet.Packet], str, HPackStringsInterface) -> str
1209 return s + self.i2m(pkt, val)
1210
1211 @staticmethod
1212 def _parse(t, s):
1213 # type: (bool, str) -> HPackStringsInterface
1214 """
1215 :param bool t: whether this string is a huffman compressed string.
1216 :param str s: the string to parse.
1217 :return: HPackStringsInterface: either a HPackLiteralString or HPackZString, depending on t. # noqa: E501
1218 :raises: InvalidEncodingException
1219 """
1220 if t:
1221 i, ibl = HPackZString.huffman_conv2bitstring(s)
1222 return HPackZString(HPackZString.huffman_decode(i, ibl))
1223 return HPackLiteralString(s)
1224
1225 def getfield(self, pkt, s):
1226 # type: (packet.Packet, str) -> Tuple[str, HPackStringsInterface]
1227 """
1228 :param packet.Packet pkt: the packet instance containing this field instance. # noqa: E501
1229 :param str s: the string to parse this field from.
1230 :return: (str, HPackStringsInterface): the remaining string after this field was carved out & the extracted # noqa: E501
1231 value.
1232 :raises: KeyError if "type_from" is not a field of pkt or its payloads.
1233 :raises: InvalidEncodingException
1234 """
1235 tmp_len = self._length_from(pkt)
1236 t = pkt.getfieldval(self._type_from) == 1
1237 return s[tmp_len:], self._parse(t, s[:tmp_len])
1238
1239 def i2h(self, pkt, x):
1240 # type: (Optional[packet.Packet], HPackStringsInterface) -> str
1241 fmt = ''
1242 if isinstance(x, HPackLiteralString):
1243 fmt = "HPackLiteralString({})"
1244 elif isinstance(x, HPackZString):
1245 fmt = "HPackZString({})"
1246 return fmt.format(x.origin())
1247
1248 def h2i(self, pkt, x):
1249 # type: (packet.Packet, str) -> HPackStringsInterface
1250 return HPackLiteralString(x)
1251

Callers 1

HPackHdrStringClass · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…