mirror of
https://github.com/lostisland/faraday.git
synced 2025-08-21 00:01:31 -04:00
move some env logic back to Env
This commit is contained in:
parent
a18d7a856d
commit
691ff3c8e3
@ -2,8 +2,6 @@ module Faraday
|
||||
# Public: This is a base class for all Faraday adapters. Adapters are
|
||||
# responsible for fulfilling a Faraday request.
|
||||
class Adapter < Middleware
|
||||
CONTENT_LENGTH = 'Content-Length'.freeze
|
||||
|
||||
extend AutoloadHelper
|
||||
extend MiddlewareRegistry
|
||||
|
||||
@ -46,18 +44,13 @@ module Faraday
|
||||
self.supports_parallel = false
|
||||
|
||||
def call(env)
|
||||
if !env[:body] and Connection::METHODS_WITH_BODIES.include? env[:method]
|
||||
# play nice and indicate we're sending an empty body
|
||||
env[:request_headers][CONTENT_LENGTH] = "0"
|
||||
# Typhoeus hangs on PUT requests if body is nil
|
||||
env[:body] = ''
|
||||
end
|
||||
env.clear_body if env.needs_body?
|
||||
end
|
||||
|
||||
def save_response(env, status, body, headers = nil)
|
||||
env[:status] = status
|
||||
env[:body] = body
|
||||
env[:response_headers] = Utils::Headers.new.tap do |response_headers|
|
||||
env.status = status
|
||||
env.body = body
|
||||
env.response_headers = Utils::Headers.new.tap do |response_headers|
|
||||
response_headers.update headers unless headers.nil?
|
||||
yield response_headers if block_given?
|
||||
end
|
||||
|
@ -18,10 +18,6 @@ module Faraday
|
||||
# A Set of allowed HTTP verbs.
|
||||
METHODS = Set.new [:get, :post, :put, :delete, :head, :patch, :options]
|
||||
|
||||
# A Set of HTTP verbs that typically send a body. If no body is set for
|
||||
# these requests, the Content-Length header is set to 0.
|
||||
METHODS_WITH_BODIES = Set.new [:post, :put, :patch, :options]
|
||||
|
||||
# Public: Returns a Hash of URI query unencoded key/value pairs.
|
||||
attr_reader :params
|
||||
|
||||
|
@ -132,9 +132,14 @@ module Faraday
|
||||
class Env < Options.new(:method, :body, :url, :request, :request_headers,
|
||||
:ssl, :parallel_manager, :params, :response, :response_headers, :status)
|
||||
|
||||
ContentLength = 'Content-Length'.freeze
|
||||
StatusesWithoutBody = Set.new [204, 304]
|
||||
SuccessfulStatuses = (200..299)
|
||||
|
||||
# A Set of HTTP verbs that typically send a body. If no body is set for
|
||||
# these requests, the Content-Length header is set to 0.
|
||||
MethodsWithBodies = Set.new [:post, :put, :patch, :options]
|
||||
|
||||
options :request => RequestOptions,
|
||||
:request_headers => Utils::Headers, :response_headers => Utils::Headers
|
||||
|
||||
@ -142,6 +147,15 @@ module Faraday
|
||||
SuccessfulStatuses.include?(status)
|
||||
end
|
||||
|
||||
def needs_body?
|
||||
!body && MethodsWithBodies.include?(method)
|
||||
end
|
||||
|
||||
def clear_body
|
||||
request_headers[ContentLength] = '0'
|
||||
self.body = ''
|
||||
end
|
||||
|
||||
def parse_body?
|
||||
!StatusesWithoutBody.include?(status)
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user