httpdbg Documentation

repository·main·Indexed 21 days ago

https://github.com/cle-b/httpdbg

A tool for intercepting, recording, and inspecting HTTP(S) client and server requests. It features a web-based interface for viewing captured traffic and provides a Python library for managing HTTP records, grouping requests, and exporting traffic to HTML reports. It can be used via the pyhttpdbg CLI in module, script, or console modes.

Tokens
7.7K
Snippets
31
Records
37
Agent score
75%

What's inside httpdbg

  1. How stack extraction and instruction reconstruction works

    main

    The httpdbg library uses several internal utilities to provide high-fidelity debugging information:

    1. get_current_instruction: Analyzes the traceback.StackSummary to find the specific line of code that initiated a request. It filters out internal httpdbg hooks and asyncio frames to focus on the user's code.
    2. extract_short_stack_from_file: Reads the source file of a stack frame to reconstruct the actual code instruction. It attempts to handle multi-line statements by looking ahead for balanced parentheses.
    3. construct_call_str: Generates a string representation of a function call, including the module, function name, and the values of all passed *args and **kwargs. This allows you to see exactly what parameters were passed to the HTTP client.
  2. Use the pyhttpdbg CLI to intercept HTTP(S) requests

    main

    The pyhttpdbg command is the primary entry point for intercepting and inspecting HTTP(S) requests. It can be used in several modes:

    1. Module Mode: Intercept requests made by a Python module.
    2. Script Mode: Intercept requests made by a specific Python script.
    3. Console Mode: Start an interactive session to record requests.
    4. Export Mode: Record requests and export the results to an HTML file.

    When running in server mode, httpdbg starts a local server (defaulting to localhost) where you can view intercepted requests via a web interface. The tool can be configured to act as a client-only recorder or to run a full server.

    # Example of running pyhttpdbg as a module (usage depends on specific CLI flags defined in httpdbg.args)
    python -m httpdbg my_module
    
    # Example of running pyhttpdbg on a script
    python -m httpdbg my_script.py
    
    # Example of exporting to HTML
    python -m httpdbg --export-html output.html my_script.py
  3. Configure HTTPDBG via environment variables

    main

    You can configure httpdbg behavior using the following environment variables:

    • HTTPDBG_MULTIPROCESS_DIR: Specifies the directory used for multiprocess communication/storage.
    • HTTPDBG_LOG_LEVEL: Sets the logging verbosity level.
    • HTTPDBG_LOG_PATH: Specifies the file path where logs should be written.
    export HTTPDBG_LOG_LEVEL=DEBUG
    export HTTPDBG_LOG_PATH=/var/log/httpdbg.log
    export HTTPDBG_MULTIPROCESS_DIR=/tmp/httpdbg_mp
  4. Initialize HTTPRecords with client/server modes and ignore filters

    main

    The HTTPRecords constructor accepts the following arguments:

    • client (bool): If True, captures client-side HTTP requests. Defaults to True.
    • server (bool): If True, captures server-side HTTP responses. Defaults to False.
    • ignore (tuple[tuple[str, int], ...]): A collection of pairs used to filter out specific traffic. Each pair consists of a string pattern and an integer (typically a status code or port identifier).
    # Example: Capture both client and server traffic, ignoring localhost on port 80
    records = HTTPRecords(
        client=True, 
        server=True, 
        ignore=(("localhost", 80),)
    )
  5. Access HTTP records with httprecord

    main

    The httprecord symbol provides access to individual HTTP records captured by the library. It is available at the top-level httpdbg package.

    from httpdbg import httprecord
    
    # Use httprecord to interact with captured HTTP data
  6. Generate a compatible UUID with get_new_uuid()

    main

    Use get_new_uuid() to generate a 10-character string consisting of random ASCII letters. This is specifically designed to be compatible with method naming rules.

    from httpdbg.utils import get_new_uuid
    
    uuid = get_new_uuid()
    print(uuid)  # Example output: 'aBcDeFgHiJ'
  7. Add initiators and groups to HTTPRecords

    main

    To properly associate captured requests with their origins or logical groupings, use the add_initiator and add_group methods. This sets the current_initiator and current_group context for subsequent record creation.

    • add_initiator(initiator: Initiator): Registers an Initiator object and sets it as the current active initiator.
    • add_group(group: Group): Registers a Group object and sets it as the current active group.
    from httpdbg.records import HTTPRecords
    # Assuming initiator and group objects are already instantiated
    
    records = HTTPRecords()
    records.add_initiator(my_initiator)
    records.add_group(my_group)
  8. Use httpdbg_srv to intercept and record HTTP requests

    main

    The httpdbg_srv context manager starts a background server that intercepts HTTP requests and records them into an HTTPRecords object. It is designed to be used within a with statement.

    When the context block exits, the server is automatically shut down. If an error occurs during server startup, it raises a HttpdbgException.

    from httpdbg.server import httpdbg_srv
    
    # Start the interceptor on localhost at port 8080
    with httpdbg_srv('127.0.0.1', 8080) as records:
        # Perform your HTTP operations here
        # The 'records' object will contain the intercepted data
        print(f"Intercepted {len(records)} requests")
    
    # Server is automatically shut down here
  9. Handle request exceptions in HTTPRecords

    main

    If an HTTP request fails with an exception, use add_new_record_exception to log the failure as an HTTPRecord.

    Arguments:

    • initiator (Initiator): The initiator responsible for the request.
    • group (Group): The group the request belongs to.
    • url (str): The URL of the attempted request.
    • exception (Exception): The exception that occurred.
    try:
        # ... perform request ...
        pass
    except Exception as e:
        records.add_new_record_exception(current_initiator, current_group, target_url, e)
  10. Group requests using the httpdbg_group context manager

    main

    The httpdbg_group context manager allows you to logically group related HTTP requests. This is useful for organizing logs or traces by a specific context (e.g., a specific user session or a specific task).

    • label: A short name for the group.
    • full_label: A detailed description of the group.
    • update: If True, the group's label and full_label can be updated within the context (useful for endpoint-based grouping).
    • updatable: If False, the group's labels cannot be updated.

    If a group is already active in the current records context, the manager will use the existing group instead of creating a new one.

    from httpdbg.initiator import httpdbg_group
    
    # Assuming 'records' is an instance of HTTPRecords
    with httpdbg_group(records, "my_label", "Detailed group description") as group:
        # All HTTP requests made within this block will be associated with this group
        pass
  11. Export HTTP records to HTML with export_html()

    main

    Use export_html to generate an HTML representation of captured HTTP traffic. This function is available at the top-level httpdbg package via lazy loading.

    from httpdbg import export_html
    
    # Example usage (actual arguments depend on the export implementation)
    export_html(records, filename='output.html')
  12. Export an HTML report to a file with export_html()

    main

    Use export_html() to write an HTML report directly to a file on disk.

    Arguments:

    • records (HTTPRecords): The collection of HTTP records to include.
    • filename (Path): The destination file path where the HTML will be written.
    from pathlib import Path
    from httpdbg import HTTPRecords
    from httpdbg.export import export_html
    
    # records: your HTTPRecords instance
    # output_file: the path where you want to save the report
    output_file = Path("report.html")
    export_html(records, output_file)