Skip to content

ccproxy.api.middleware.headers

ccproxy.api.middleware.headers

Header preservation middleware to maintain proxy response headers.

HeaderPreservationMiddleware

HeaderPreservationMiddleware(app)

Bases: BaseHTTPMiddleware

Middleware to preserve certain headers from proxy responses.

This middleware ensures that headers like 'server' from the upstream API are preserved and not overridden by Uvicorn/Starlette.

Parameters:

Name Type Description Default
app ASGIApp

The ASGI application

required
Source code in ccproxy/api/middleware/headers.py
def __init__(self, app: ASGIApp):
    """Initialize the header preservation middleware.

    Args:
        app: The ASGI application
    """
    super().__init__(app)

dispatch async

dispatch(request, call_next)

Process the request and preserve specific headers.

Parameters:

Name Type Description Default
request Request

The incoming HTTP request

required
call_next RequestResponseEndpoint

The next middleware/handler in the chain

required

Returns:

Type Description
Response

The HTTP response with preserved headers

Source code in ccproxy/api/middleware/headers.py
async def dispatch(
    self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
    """Process the request and preserve specific headers.

    Args:
        request: The incoming HTTP request
        call_next: The next middleware/handler in the chain

    Returns:
        The HTTP response with preserved headers
    """
    # Process the request
    response = await call_next(request)

    # Check if we have a stored server header to preserve
    # This would be set by the proxy service if we want to preserve it
    if hasattr(request.state, "preserve_headers"):
        for header_name, header_value in request.state.preserve_headers.items():
            # Force set the header to override any default values
            response.headers[header_name] = header_value
            # Also try raw header setting for more control
            response.raw_headers.append(
                (header_name.encode(), header_value.encode())
            )

    return response