Class: HTTPX::Request

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Callbacks
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}"
URIParser =
URI::DEFAULT_PARSER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#emit, #on, #once, #only

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#drain_errorObject (readonly)

Exception raised during enumerable body writes



40
41
42
# File 'lib/httpx/request.rb', line 40

def drain_error
  @drain_error
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

#verbObject (readonly)

Returns the value of attribute verb.



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

def verb
  @verb
end

Instance Method Details

#authorityObject



115
116
117
# File 'lib/httpx/request.rb', line 115

def authority
  @uri.authority
end

#drain_bodyObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/httpx/request.rb', line 135

def drain_body
  return nil if @body.nil?

  @drainer ||= @body.each
  chunk = @drainer.next
  chunk.dup
rescue StopIteration
  nil
rescue StandardError => e
  @drain_error = e
  nil
end

#expects?Boolean

Returns:

  • (Boolean)


278
279
280
# File 'lib/httpx/request.rb', line 278

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

#initialize_with_escape(verb, uri, options = {}) ⇒ Object Also known as: initialize



82
83
84
# File 'lib/httpx/request.rb', line 82

def initialize_with_escape(verb, uri, options = {})
  initialize_without_escape(verb, URIParser.escape(uri.to_s), options)
end

#inspectObject

:nocov:



149
150
151
152
153
154
155
# File 'lib/httpx/request.rb', line 149

def inspect
  "#<HTTPX::Request:#{object_id} " \
    "#{@verb.to_s.upcase} " \
    "#{uri} " \
    "@headers=#{@headers} " \
    "@body=#{@body}>"
end

#interestsObject



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

def interests
  return :r if @state == :done || @state == :expect

  :w
end

#merge_headers(h) ⇒ Object



89
90
91
# File 'lib/httpx/request.rb', line 89

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

#originObject



120
121
122
# File 'lib/httpx/request.rb', line 120

def origin
  @uri.origin
end

#pathObject



107
108
109
110
111
112
# File 'lib/httpx/request.rb', line 107

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

#queryObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/httpx/request.rb', line 124

def query
  return @query if defined?(@query)

  query = []
  if (q = @options.params)
    query << Transcoder.registry("form").encode(q)
  end
  query << @uri.query if @uri.query
  @query = query.join("&")
end

#schemeObject



93
94
95
# File 'lib/httpx/request.rb', line 93

def scheme
  @uri.scheme
end

#trailersObject



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

def trailers
  @trailers ||= @options.headers_class.new
end

#trailers?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/httpx/request.rb', line 65

def trailers?
  defined?(@trailers)
end

#transition(nextstate) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/httpx/request.rb', line 244

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

    if @headers.key?("expect")
      if @informational_status && @informational_status == 100
        # check for 100 Continue response, and deallocate the var
        # if @informational_status == 100
        #   @response = nil
        # end
      else
        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)
  nil
end