Class: Async::HTTP::AcceptEncoding

Inherits:
Middleware show all
Defined in:
lib/async/http/accept_encoding.rb

Overview

Set a valid accept-encoding header and decode the response.

Constant Summary collapse

DEFAULT_WRAPPERS =
{
	'gzip' => Body::Inflate.method(:for)
}

Instance Method Summary collapse

Methods inherited from Middleware

build, #close

Constructor Details

#initialize(app, wrappers = DEFAULT_WRAPPERS) ⇒ AcceptEncoding

Returns a new instance of AcceptEncoding.



34
35
36
37
38
39
# File 'lib/async/http/accept_encoding.rb', line 34

def initialize(app, wrappers = DEFAULT_WRAPPERS)
	super(app)
	
	@accept_encoding = wrappers.keys.join(', ')
	@wrappers = wrappers
end

Instance Method Details

#call(request) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/async/http/accept_encoding.rb', line 41

def call(request)
	request.headers['accept-encoding'] = @accept_encoding
	
	response = super
	
	if !response.body.empty? and content_encoding = response.headers['content-encoding']
		body = response.body
		
		# We want to unwrap all encodings
		content_encoding.reverse_each do |name|
			if wrapper = @wrappers[name]
				body = wrapper.call(body)
			end
		end
		
		response.body = body
	end
	
	return response
end