Skip to content

Commit 8c72228

Browse files
committed
default to requesting compressed response
This behavior can be disabled via :compressed => false when initializing the request. Closes #270.
1 parent 4b8f38d commit 8c72228

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

lib/em-http/client.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,16 @@ def build_request
155155

156156
# Set the User-Agent if it hasn't been specified
157157
if !head.key?('user-agent')
158-
head['user-agent'] = "EventMachine HttpClient"
158+
head['user-agent'] = 'EventMachine HttpClient'
159159
elsif head['user-agent'].nil?
160160
head.delete('user-agent')
161161
end
162162

163+
# Set the Accept-Encoding header if none is provided
164+
if !head.key?('accept-encoding') && req.compressed
165+
head['accept-encoding'] = 'gzip, compressed'
166+
end
167+
163168
# Set the auth from the URI if given
164169
head['Authorization'] = @req.uri.userinfo.split(/:/, 2) if @req.uri.userinfo
165170

lib/em-http/http_client_options.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class HttpClientOptions
22
attr_reader :uri, :method, :host, :port
33
attr_reader :headers, :file, :body, :query, :path
4-
attr_reader :keepalive, :pass_cookies, :decoding
4+
attr_reader :keepalive, :pass_cookies, :decoding, :compressed
55

66
attr_accessor :followed, :redirects
77

@@ -18,6 +18,7 @@ def initialize(uri, options, method)
1818

1919
@pass_cookies = options.fetch(:pass_cookies, true) # pass cookies between redirects
2020
@decoding = options.fetch(:decoding, true) # auto-decode compressed response
21+
@compressed = options.fetch(:compressed, true) # auto-negotiated compressed response
2122

2223
set_uri(uri, options[:path], options[:query])
2324
end

spec/client_spec.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def failed(http=nil)
386386
}
387387
end
388388

389-
it "should detect gzip encoding" do
389+
it "should auto-detect and decode gzip encoding" do
390390
EventMachine.run {
391391

392392
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/gzip').get :head => {"accept-encoding" => "gzip, compressed"}
@@ -446,6 +446,36 @@ def failed(http=nil)
446446
}
447447
end
448448

449+
it "should default to requesting compressed response" do
450+
EventMachine.run {
451+
452+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_accept_encoding').get
453+
454+
http.errback { failed(http) }
455+
http.callback {
456+
http.response_header.status.should == 200
457+
http.response.should == "gzip, compressed"
458+
459+
EventMachine.stop
460+
}
461+
}
462+
end
463+
464+
it "should default to requesting compressed response" do
465+
EventMachine.run {
466+
467+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_accept_encoding').get :compressed => false
468+
469+
http.errback { failed(http) }
470+
http.callback {
471+
http.response_header.status.should == 200
472+
http.response.should == ""
473+
474+
EventMachine.stop
475+
}
476+
}
477+
end
478+
449479
it "should timeout after 0.1 seconds of inactivity" do
450480
EventMachine.run {
451481
t = Time.now.to_i

spec/stallion.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ def self.call(env)
235235
stable.response.write deflater.finish
236236
stable.response["Content-Encoding"] = "deflate"
237237

238+
elsif stable.request.path_info == '/echo_accept_encoding'
239+
stable.response.status = 200
240+
stable.response.write stable.request.env["HTTP_ACCEPT_ENCODING"]
241+
238242
elsif stable.request.env["HTTP_IF_NONE_MATCH"]
239243
stable.response.status = 304
240244

0 commit comments

Comments
 (0)