class HTTPX::Request

  1. lib/httpx/request.rb
Superclass: Object

Included modules

  1. Callbacks

Constants

METHODS = [ # RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1 :options, :get, :head, :post, :put, :delete, :trace, :connect, # RFC 2518: HTTP Extensions for Distributed Authoring -- WEBDAV :propfind, :proppatch, :mkcol, :copy, :move, :lock, :unlock, # RFC 3648: WebDAV Ordered Collections Protocol :orderpatch, # RFC 3744: WebDAV Access Control Protocol :acl, # RFC 6352: vCard Extensions to WebDAV -- CardDAV :report, # RFC 5789: PATCH Method for HTTP :patch, # draft-reschke-webdav-search: WebDAV Search :search ].freeze  
USER_AGENT = "httpx.rb/#{VERSION}"  

Attributes

body [R]
headers [R]
options [R]
response [R]
state [R]
timer [RW]
uri [R]
verb [R]

Public Class methods

new (verb, uri, options = {})
[show source]
   # File lib/httpx/request.rb
48 def initialize(verb, uri, options = {})
49   @verb    = verb.to_s.downcase.to_sym
50   @uri     = URI(URI.escape(uri.to_s))
51   @options = Options.new(options)
52 
53   raise(Error, "unknown method: #{verb}") unless METHODS.include?(@verb)
54 
55   @headers = @options.headers_class.new(@options.headers)
56   @headers["user-agent"] ||= USER_AGENT
57   @headers["accept"]     ||= "*/*"
58 
59   @body = @options.request_body_class.new(@headers, @options)
60   @state = :idle
61 end

Public Instance methods

authority ()
[show source]
   # File lib/httpx/request.rb
86 def authority
87   @uri.authority
88 end
drain_body ()
[show source]
    # File lib/httpx/request.rb
106 def drain_body
107   return nil if @body.nil?
108 
109   @drainer ||= @body.each
110   chunk = @drainer.next
111   chunk.dup
112 rescue StopIteration
113   nil
114 end
expects? ()
[show source]
    # File lib/httpx/request.rb
228 def expects?
229   @headers["expect"] == "100-continue" &&
230     @response && @response.status == 100
231 end
inspect ()
[show source]
    # File lib/httpx/request.rb
116 def inspect
117   "#<Request #{@verb.to_s.upcase} #{path} @headers=#{@headers.to_hash} @body=#{@body}>"
118 end
merge_headers (h)
[show source]
   # File lib/httpx/request.rb
63 def merge_headers(h)
64   @headers = @headers.merge(h)
65 end
origin ()
[show source]
   # File lib/httpx/request.rb
91 def origin
92   @uri.origin
93 end
path ()
[show source]
   # File lib/httpx/request.rb
78 def path
79   path = uri.path.dup
80   path << "/" if path.empty?
81   path << "?#{query}" unless query.empty?
82   path
83 end
query ()
[show source]
    # File lib/httpx/request.rb
 95 def query
 96   return @query if defined?(@query)
 97 
 98   query = []
 99   if (q = @options.params)
100     query << URI.encode_www_form(q)
101   end
102   query << @uri.query if @uri.query
103   @query = query.join("&")
104 end
response= (response)
[show source]
   # File lib/httpx/request.rb
71 def response=(response)
72   return unless response
73 
74   @timer.cancel if @timer
75   @response = response
76 end
scheme ()
[show source]
   # File lib/httpx/request.rb
67 def scheme
68   @uri.scheme
69 end
transition (nextstate)
[show source]
    # File lib/httpx/request.rb
196 def transition(nextstate)
197   case nextstate
198   when :idle
199     @response = nil
200   when :headers
201     return unless @state == :idle
202   when :body
203     return unless @state == :headers ||
204                   @state == :expect
205 
206     if @headers.key?("expect")
207       unless @response
208         @state = :expect
209         return
210       end
211 
212       case @response.status
213       when 100
214         # deallocate
215         @response = nil
216       when 417
217         @response = @response
218         return
219       end
220     end
221   when :done
222     return if @state == :expect
223   end
224   @state = nextstate
225   nil
226 end