Skip to content

Commit

Permalink
Improved documentation for Protocol::HTTP::ContentEncoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 28, 2024
1 parent 00bf9d9 commit 78ddfc7
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/protocol/http/content_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 78ddfc7

Please sign in to comment.