Pure-Python HTTP/2 protocol implementation providing low-level connection and stream management
—
HTTP/2 connection configuration controlling validation, encoding, normalization, and logging behavior. Configuration provides fine-tuned control over protocol handling and allows customization for different use cases.
Main configuration object controlling HTTP/2 connection behavior, header processing, validation rules, and logging.
class H2Configuration:
def __init__(
self,
client_side: bool = True,
header_encoding: str | None = None,
validate_outbound_headers: bool = True,
normalize_outbound_headers: bool = True,
split_outbound_cookies: bool = False,
validate_inbound_headers: bool = True,
normalize_inbound_headers: bool = True,
logger: DummyLogger | OutputLogger | None = None
):
"""
Configure HTTP/2 connection behavior.
Args:
client_side: Whether connection is client-side (True) or server-side (False)
header_encoding: Header encoding ('utf-8', None for bytes, False to disable)
validate_outbound_headers: Validate outbound headers against RFC 7540
normalize_outbound_headers: Normalize outbound headers before sending
split_outbound_cookies: Split cookie headers for better compression
validate_inbound_headers: Validate received headers
normalize_inbound_headers: Normalize received headers
logger: Logger instance for debugging
"""@property
def client_side(self) -> bool:
"""Whether this connection is client-side or server-side."""
@property
def header_encoding(self) -> str | None:
"""
Header encoding configuration.
Returns:
None: Headers returned as bytes
str: Encoding name (e.g., 'utf-8') for automatic string conversion
False: Disable header encoding entirely
"""
@property
def validate_outbound_headers(self) -> bool:
"""Whether to validate outbound headers against RFC 7540 rules."""
@property
def normalize_outbound_headers(self) -> bool:
"""Whether to normalize outbound headers before sending."""
@property
def split_outbound_cookies(self) -> bool:
"""Whether to split outbound cookie headers for better compression."""
@property
def validate_inbound_headers(self) -> bool:
"""Whether to validate inbound headers against RFC 7540 rules."""
@property
def normalize_inbound_headers(self) -> bool:
"""Whether to normalize inbound headers after receiving."""
@property
def logger(self) -> DummyLogger | OutputLogger | None:
"""Logger instance for connection debugging."""Logging infrastructure for HTTP/2 connection debugging and tracing.
No-operation logger that discards all log messages. Used when logging is not needed.
class DummyLogger:
def __init__(self, *args):
"""Initialize dummy logger that performs no logging."""
def debug(self, *args, **kwargs) -> None:
"""No-op debug logging method."""
def trace(self, *args, **kwargs) -> None:
"""No-op trace logging method."""Simple logger that outputs debug and trace messages to a file-like object (default stderr).
class OutputLogger:
def __init__(self, file=None, trace_level: bool = False):
"""
Initialize output logger.
Args:
file: File-like object to write to (default: stderr)
trace_level: Whether to enable trace-level logging
"""
def debug(self, fmtstr: str, *args) -> None:
"""
Log debug message.
Args:
fmtstr: Format string
*args: Format arguments
"""
def trace(self, fmtstr: str, *args) -> None:
"""
Log trace message (if trace_level enabled).
Args:
fmtstr: Format string
*args: Format arguments
"""from h2.config import H2Configuration
# Basic client configuration
config = H2Configuration(client_side=True)
# Client with UTF-8 header encoding
config = H2Configuration(
client_side=True,
header_encoding='utf-8'
)
# Strict client with full validation
config = H2Configuration(
client_side=True,
validate_outbound_headers=True,
validate_inbound_headers=True,
normalize_outbound_headers=True,
normalize_inbound_headers=True
)from h2.config import H2Configuration, OutputLogger
# Basic server configuration
config = H2Configuration(client_side=False)
# Server with logging enabled
logger = OutputLogger(trace_level=True)
config = H2Configuration(
client_side=False,
logger=logger
)
# Server with cookie splitting for better compression
config = H2Configuration(
client_side=False,
split_outbound_cookies=True
)from h2.config import H2Configuration, OutputLogger
import sys
# Configuration for debugging with trace logging
logger = OutputLogger(file=sys.stdout, trace_level=True)
config = H2Configuration(
client_side=True,
header_encoding='utf-8',
logger=logger
)
# Permissive configuration for testing
config = H2Configuration(
client_side=True,
validate_outbound_headers=False,
validate_inbound_headers=False,
normalize_outbound_headers=False,
normalize_inbound_headers=False
)Configuration controls how headers are processed:
; boundaries for HPACK compressionThe client_side setting affects:
Logger configuration enables debugging of:
Install with Tessl CLI
npx tessl i tessl/pypi-h2