duck.http.core.response_finalizer

Module containing ResponseFinalizer class focusing on putting on the final touches to the response.

The final touches include:

  • Content compression.

  • Content length calculation and insertion.

  • Content encoding determination and insertion.

  • etc.

Module Contents

Classes

AsyncResponseFinalizer

Asynchronous ResponseFinalizer class focusing on putting on the final touches to the response.

ResponseFinalizer

ResponseFinalizer class focusing on putting on the final touches to the response.

Functions

set_compressable_iter_content

Modifies the response iter_content methods with new functions to compress data as were are iterating.

Data

CUSTOM_TEMPLATES

async_response_finalizer

response_finalizer

API

class duck.http.core.response_finalizer.AsyncResponseFinalizer

Bases: duck.http.core.response_finalizer.ResponseFinalizer

Asynchronous ResponseFinalizer class focusing on putting on the final touches to the response.

async do_content_compression(response, request) None

Compresses the content if the client supports it and if the content is not a streaming response. (if necessary).

async do_set_streaming_range(response, request)

Set streaming range attributes on StreamingRangeHttpResponse. This method parses the ‘Range’ header from the request and sets the start and end positions for partial content streaming.

Parameters:
  • response – The response object to set streaming range on.

  • request – The incoming HTTP request containing the ‘Range’ header.

Raises:

ValueError – If the ‘Range’ header is malformed or invalid.

async finalize_response(response: duck.http.response.HttpResponse, request: duck.http.request.HttpRequest, do_set_streaming_range: bool = True, do_content_compression: bool = True)

Puts the final touches to the response.

duck.http.core.response_finalizer.CUSTOM_TEMPLATES: Dict[int, Callable]

None

class duck.http.core.response_finalizer.ResponseFinalizer

ResponseFinalizer class focusing on putting on the final touches to the response.

do_content_compression(response, request) None

Compresses the content if the client supports it and if the content is not a streaming response. (if necessary).

do_request_response_transformation(response: duck.http.response.HttpResponse, request: duck.http.request.HttpRequest)

Transforms the response object by applying request- and response-based modifications.

This includes, but is not limited to, header changes and body alterations.

Behavior Examples:

  • If the request method is HEAD, the response body is replaced with empty bytes.

  • If a matching template is found in the CUSTOM_TEMPLATES configuration, the entire response may be replaced.

Parameters:
  • response – The original response to be transformed.

  • request – The incoming HTTP request associated with the response.

do_set_connection_mode(response, request) None

Sets the correct response connection mode, i.e. keep-alive or close.

do_set_content_headers(response, request) None

Sets the appropriate content headers like Content-Type, Content-Encoding & Content-Length if not set.

Notes:

  • If response is an instance of StreamingHttpResponse, the Content-Length header is removed as a safe measure. The size of the response content can become unpredictable especially when data is compressed as it is being sent.

do_set_extra_headers(response, request) None

Sets last final extra headers like Date & Cache-Control.

do_set_fixed_headers(response, request) None

Sets fixed headers from settings, i.e. extra headers, cors headers and security headers.

do_set_streaming_range(response, request)

Set streaming range attributes on StreamingRangeHttpResponse. This method parses the ‘Range’ header from the request and sets the start and end positions for partial content streaming.

Parameters:
  • response – The response object to set streaming range on.

  • request – The incoming HTTP request containing the ‘Range’ header.

Raises:

ValueError – If the ‘Range’ header is malformed or invalid.

finalize_response(response: duck.http.response.HttpResponse, request: duck.http.request.HttpRequest, do_set_streaming_range: bool = True, do_content_compression: bool = True)

Puts the final touches to the response.

duck.http.core.response_finalizer.async_response_finalizer

‘AsyncResponseFinalizer(…)’

duck.http.core.response_finalizer.response_finalizer

‘ResponseFinalizer(…)’

duck.http.core.response_finalizer.set_compressable_iter_content(response)

Modifies the response iter_content methods with new functions to compress data as were are iterating.

Note:

  • Only use this function if response data is compressable.

  • This function modifies both sync and async version of iter_content, i.e. iter_content and async_iter_content.