lib/http/client.rb in http-2.2.2 vs lib/http/client.rb in http-3.0.0.pre
- old
+ new
@@ -1,6 +1,7 @@
# frozen_string_literal: true
+
require "forwardable"
require "http/form_data"
require "http/options"
require "http/headers"
@@ -21,23 +22,24 @@
@connection = nil
@state = :clean
end
# Make an HTTP request
- def request(verb, uri, opts = {})
+ def request(verb, uri, opts = {}) # rubocop:disable Style/OptionHash
opts = @default_options.merge(opts)
uri = make_request_uri(uri, opts)
headers = make_request_headers(opts)
body = make_request_body(opts, headers)
proxy = opts.proxy
req = HTTP::Request.new(
- :verb => verb,
- :uri => uri,
- :headers => headers,
- :proxy => proxy,
- :body => body
+ :verb => verb,
+ :uri => uri,
+ :headers => headers,
+ :proxy => proxy,
+ :body => body,
+ :auto_deflate => opts.feature(:auto_deflate)
)
res = perform(req, opts)
return res unless opts.follow
@@ -144,32 +146,33 @@
unless cookies.empty?
cookies = opts.headers.get(Headers::COOKIE).concat(cookies).join("; ")
headers[Headers::COOKIE] = cookies
end
+ if (auto_deflate = opts.feature(:auto_deflate))
+ # We need to delete Content-Length header. It will be set automatically
+ # by HTTP::Request::Writer
+ headers.delete(Headers::CONTENT_LENGTH)
+
+ headers[Headers::CONTENT_ENCODING] = auto_deflate.method
+ end
+
headers
end
# Create the request body object to send
def make_request_body(opts, headers)
- request_body =
- case
- when opts.body
- opts.body
- when opts.form
- form = HTTP::FormData.create opts.form
- headers[Headers::CONTENT_TYPE] ||= form.content_type
- headers[Headers::CONTENT_LENGTH] ||= form.content_length
- form.to_s
- when opts.json
- body = MimeType[:json].encode opts.json
- headers[Headers::CONTENT_TYPE] ||= "application/json; charset=#{body.encoding.name}"
- body
- end
- if (auto_deflate = opts.feature(:auto_deflate))
- auto_deflate.deflate(headers, request_body)
- else
- request_body
+ case
+ when opts.body
+ opts.body
+ when opts.form
+ form = HTTP::FormData.create opts.form
+ headers[Headers::CONTENT_TYPE] ||= form.content_type
+ form
+ when opts.json
+ body = MimeType[:json].encode opts.json
+ headers[Headers::CONTENT_TYPE] ||= "application/json; charset=#{body.encoding.name}"
+ body
end
end
end
end