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

USER_AGENT =
"httpx.rb/#{VERSION}"
URIParser =
URI::DEFAULT_PARSER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#callbacks_for?, #emit, #on, #once, #only

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



14
15
16
# File 'lib/httpx/request.rb', line 14

def body
  @body
end

#drain_errorObject (readonly)

Exception raised during enumerable body writes



17
18
19
# File 'lib/httpx/request.rb', line 17

def drain_error
  @drain_error
end

#headersObject (readonly)

Returns the value of attribute headers.



14
15
16
# File 'lib/httpx/request.rb', line 14

def headers
  @headers
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/httpx/request.rb', line 14

def options
  @options
end

#responseObject

Returns the value of attribute response.



14
15
16
# File 'lib/httpx/request.rb', line 14

def response
  @response
end

#stateObject (readonly)

Returns the value of attribute state.



14
15
16
# File 'lib/httpx/request.rb', line 14

def state
  @state
end

#uriObject (readonly)

Returns the value of attribute uri.



14
15
16
# File 'lib/httpx/request.rb', line 14

def uri
  @uri
end

#verbObject (readonly)

Returns the value of attribute verb.



14
15
16
# File 'lib/httpx/request.rb', line 14

def verb
  @verb
end

Instance Method Details

#authorityObject



111
112
113
# File 'lib/httpx/request.rb', line 111

def authority
  @uri.authority
end

#drain_bodyObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/httpx/request.rb', line 131

def drain_body
  return nil if @body.nil?

  @drainer ||= @body.each
  chunk = @drainer.next.dup

  emit(:body_chunk, chunk)
  chunk
rescue StopIteration
  nil
rescue StandardError => e
  @drain_error = e
  nil
end

#expects?Boolean

Returns:

  • (Boolean)


284
285
286
# File 'lib/httpx/request.rb', line 284

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

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



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

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

#inspectObject

:nocov:



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

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

#interestsObject



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

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

  :w
end

#merge_headers(h) ⇒ Object



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

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

#originObject



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

def origin
  @uri.origin
end

#pathObject



102
103
104
105
106
107
108
# File 'lib/httpx/request.rb', line 102

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

#queryObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/httpx/request.rb', line 120

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

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

#read_timeoutObject



46
47
48
# File 'lib/httpx/request.rb', line 46

def read_timeout
  @options.timeout[:read_timeout]
end

#request_timeoutObject



54
55
56
# File 'lib/httpx/request.rb', line 54

def request_timeout
  @options.timeout[:request_timeout]
end

#schemeObject



86
87
88
# File 'lib/httpx/request.rb', line 86

def scheme
  @uri.scheme
end

#trailersObject



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

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

#trailers?Boolean

Returns:

  • (Boolean)


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

def trailers?
  defined?(@trailers)
end

#transition(nextstate) ⇒ Object



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
277
278
279
280
281
282
# File 'lib/httpx/request.rb', line 250

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

#write_timeoutObject



50
51
52
# File 'lib/httpx/request.rb', line 50

def write_timeout
  @options.timeout[:write_timeout]
end