From 78ddfc75263c037f6151844423f695beceaa7265 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 28 Nov 2024 16:30:59 +1300 Subject: [PATCH] Improved documentation for `Protocol::HTTP::ContentEncoding`. --- lib/protocol/http/content_encoding.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/protocol/http/content_encoding.rb b/lib/protocol/http/content_encoding.rb index 551df2e..c9f6be6 100644 --- a/lib/protocol/http/content_encoding.rb +++ b/lib/protocol/http/content_encoding.rb @@ -12,19 +12,30 @@ module Protocol module HTTP # Encode a response according the the request's acceptable encodings. class ContentEncoding < Middleware + # The default wrappers to use for encoding content. DEFAULT_WRAPPERS = { "gzip" => Body::Deflate.method(:for) } + # The default content types to apply encoding to. DEFAULT_CONTENT_TYPES = %r{^(text/.*?)|(.*?/json)|(.*?/javascript)$} - def initialize(app, content_types = DEFAULT_CONTENT_TYPES, wrappers = DEFAULT_WRAPPERS) - super(app) + # Initialize the content encoding middleware. + # + # @parameter delegate [Middleware] The next middleware in the chain. + # @parameter content_types [Regexp] The content types to apply encoding to. + # @parameter wrappers [Hash] The encoding wrappers to use. + def initialize(delegate, content_types = DEFAULT_CONTENT_TYPES, wrappers = DEFAULT_WRAPPERS) + super(delegate) @content_types = content_types @wrappers = wrappers end + # Encode the response body according to the request's acceptable encodings. + # + # @parameter request [Request] The request. + # @returns [Response] The response. def call(request) response = super @@ -40,7 +51,6 @@ def call(request) # TODO use http-accept and sort by priority if !response.body.empty? and accept_encoding = request.headers["accept-encoding"] - if content_type = response.headers["content-type"] and @content_types =~ content_type body = response.body