lib/httpx/request.rb in httpx-0.13.2 vs lib/httpx/request.rb in httpx-0.14.0
- old
+ new
@@ -33,29 +33,45 @@
USER_AGENT = "httpx.rb/#{VERSION}"
attr_reader :verb, :uri, :headers, :body, :state, :options, :response
+ # Exception raised during enumerable body writes
+ attr_reader :drain_error
+
def_delegator :@body, :empty?
def_delegator :@body, :chunk!
def initialize(verb, uri, options = {})
@verb = verb.to_s.downcase.to_sym
- @uri = Utils.uri(uri)
@options = Options.new(options)
+ @uri = Utils.uri(uri)
+ if @uri.relative?
+ raise(Error, "invalid URI: #{@uri}") unless @options.origin
+ @uri = @options.origin.merge(@uri)
+ end
+
raise(Error, "unknown method: #{verb}") unless METHODS.include?(@verb)
@headers = @options.headers_class.new(@options.headers)
@headers["user-agent"] ||= USER_AGENT
@headers["accept"] ||= "*/*"
@body = @options.request_body_class.new(@headers, @options)
@state = :idle
end
+ def trailers?
+ defined?(@trailers)
+ end
+
+ def trailers
+ @trailers ||= @options.headers_class.new
+ end
+
def interests
return :r if @state == :done || @state == :expect
:w
end
@@ -122,10 +138,13 @@
@drainer ||= @body.each
chunk = @drainer.next
chunk.dup
rescue StopIteration
nil
+ rescue StandardError => e
+ @drain_error = e
+ nil
end
# :nocov:
def inspect
"#<HTTPX::Request:#{object_id} " \
@@ -198,11 +217,13 @@
encoded = Transcoder.registry("chunker").encode(body) if chunked?
encoded
end
def unbounded_body?
- chunked? || @body.bytesize == Float::INFINITY
+ return @unbounded_body if defined?(@unbounded_body)
+
+ @unbounded_body = (chunked? || @body.bytesize == Float::INFINITY)
end
def chunked?
@headers["transfer-encoding"] == "chunked"
end
@@ -251,9 +272,11 @@
return if @state == :expect # do not re-set it
nextstate = :expect
end
end
+ when :trailers
+ return unless @state == :body
when :done
return if @state == :expect
end
@state = nextstate
emit(@state, self)