pyscalpel.utils

 1import inspect
 2from typing import TypeVar, Union
 3from pyscalpel.burp_utils import (
 4    urldecode,
 5    urlencode_all,
 6)
 7
 8
 9T = TypeVar("T", str, bytes)
10
11
12def removeprefix(s: T, prefix: Union[str, bytes]) -> T:
13    if isinstance(s, str) and isinstance(prefix, str):
14        if s.startswith(prefix):
15            return s[len(prefix) :]  # type: ignore
16    elif isinstance(s, bytes) and isinstance(prefix, bytes):
17        if s.startswith(prefix):
18            return s[len(prefix) :]  # type: ignore
19    return s
20
21
22def removesuffix(s: T, suffix: Union[str, bytes]) -> T:
23    if isinstance(s, str) and isinstance(suffix, str):
24        if s.endswith(suffix):
25            return s[: -len(suffix)]
26    elif isinstance(s, bytes) and isinstance(suffix, bytes):
27        if s.endswith(suffix):
28            return s[: -len(suffix)]
29    return s
30
31
32def current_function_name() -> str:
33    """Get current function name
34
35    Returns:
36        str: The function name
37    """
38    frame = inspect.currentframe()
39    if frame is None:
40        return ""
41
42    caller_frame = frame.f_back
43    if caller_frame is None:
44        return ""
45
46    return caller_frame.f_code.co_name
47
48
49def get_tab_name() -> str:
50    """Get current editor tab name
51
52    Returns:
53        str: The tab name
54    """
55    frame = inspect.currentframe()
56    prefixes = ("req_edit_in", "req_edit_out")
57
58    # Go to previous frame till the editor name is found
59    while frame is not None:
60        frame_name = frame.f_code.co_name
61        for prefix in prefixes:
62            if frame_name.startswith(prefix):
63                return removeprefix(removeprefix(frame_name, prefix), "_")
64
65        frame = frame.f_back
66
67    raise RuntimeError("get_tab_name() wasn't called from an editor callback.")
68
69
70__all__ = [
71    "urldecode",
72    "urlencode_all",
73    "current_function_name",
74]
def urldecode(data: bytes | str, encoding='latin-1') -> bytes:
46def urldecode(data: bytes | str, encoding="latin-1") -> bytes:
47    """URL Decode all bytes in the given bytes object"""
48    return urllibdecode(always_bytes(data, encoding))

URL Decode all bytes in the given bytes object

def urlencode_all(data: bytes | str, encoding='latin-1') -> bytes:
41def urlencode_all(data: bytes | str, encoding="latin-1") -> bytes:
42    """URL Encode all bytes in the given bytes object"""
43    return "".join(f"%{b:02X}" for b in always_bytes(data, encoding)).encode(encoding)

URL Encode all bytes in the given bytes object

def current_function_name() -> str:
33def current_function_name() -> str:
34    """Get current function name
35
36    Returns:
37        str: The function name
38    """
39    frame = inspect.currentframe()
40    if frame is None:
41        return ""
42
43    caller_frame = frame.f_back
44    if caller_frame is None:
45        return ""
46
47    return caller_frame.f_code.co_name

Get current function name

Returns: str: The function name