Class: HTTPX::Request

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/httpx/request.rb

Direct Known Subclasses

Plugins::Proxy::HTTP::ConnectRequest

Defined Under Namespace

Classes: Body, ProcIO

Constant Summary collapse

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}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, uri, options = {}) ⇒ Request

Returns a new instance of Request.

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/httpx/request.rb', line 44

def initialize(verb, uri, options = {})
  @verb    = verb.to_s.downcase.to_sym
  @uri     = URI(URI.escape(uri.to_s))
  @options = Options.new(options)

  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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



34
35
36
# File 'lib/httpx/request.rb', line 34

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



34
35
36
# File 'lib/httpx/request.rb', line 34

def headers
  @headers
end

#responseObject

Returns the value of attribute response.



36
37
38
# File 'lib/httpx/request.rb', line 36

def response
  @response
end

#stateObject (readonly)

Returns the value of attribute state.



34
35
36
# File 'lib/httpx/request.rb', line 34

def state
  @state
end

#uriObject (readonly)

Returns the value of attribute uri.



34
35
36
# File 'lib/httpx/request.rb', line 34

def uri
  @uri
end

#verbObject (readonly)

Returns the value of attribute verb.



34
35
36
# File 'lib/httpx/request.rb', line 34

def verb
  @verb
end

Instance Method Details

#authorityObject



74
75
76
77
78
# File 'lib/httpx/request.rb', line 74

def authority
  host = @uri.host
  port_string = @uri.port == @uri.default_port ? nil : ":#{@uri.port}"
  "#{host}#{port_string}"
end

#drain_bodyObject



90
91
92
93
94
95
96
97
# File 'lib/httpx/request.rb', line 90

def drain_body
  return nil if @body.nil?
  @drainer ||= @body.each
  chunk = @drainer.next
  chunk.dup
rescue StopIteration
  nil
end

#expects?Boolean

Returns:

  • (Boolean)


206
207
208
209
# File 'lib/httpx/request.rb', line 206

def expects?
  @headers["expect"] == "100-continue" &&
    @response && @response.status == 100
end

#inspectObject



99
100
101
# File 'lib/httpx/request.rb', line 99

def inspect
  "#<Request #{@verb.to_s.upcase} #{path} @headers=#{@headers.to_hash} @body=#{@body}>"
end

#merge_headers(h) ⇒ Object



59
60
61
# File 'lib/httpx/request.rb', line 59

def merge_headers(h)
  @headers = @headers.merge(h)
end

#pathObject



67
68
69
70
71
72
# File 'lib/httpx/request.rb', line 67

def path
  path = uri.path.dup
  path << "/" if path.empty?
  path << "?#{query}" unless query.empty?
  path
end

#queryObject



80
81
82
83
84
85
86
87
88
# File 'lib/httpx/request.rb', line 80

def query
  return @query if defined?(@query)
  query = []
  if (q = @options.params)
    query << URI.encode_www_form(q)
  end
  query << @uri.query if @uri.query
  @query = query.join("&")
end

#schemeObject



63
64
65
# File 'lib/httpx/request.rb', line 63

def scheme
  @uri.scheme
end

#transition(nextstate) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/httpx/request.rb', line 174

def transition(nextstate)
  case nextstate
  when :idle
    @response = nil
  when :headers
    return unless @state == :idle
  when :body
    return unless @state == :headers ||
                  @state == :expect

    if @headers.key?("expect")
      unless @response
        @state = :expect
        return
      end

      case @response.status
      when 100
        # deallocate
        @response = nil
      when 417
        @response = @response
        return
      end
    end
  when :done
    return if @state == :expect
  end
  @state = nextstate
  nil
end