Class: Gem::Net::HTTPGenericRequest

Inherits:
Object
  • Object
show all
Includes:
HTTPHeader
Defined in:
lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb

Overview

HTTPGenericRequest is the parent of the Gem::Net::HTTPRequest class.

Do not use this directly; instead, use a subclass of Gem::Net::HTTPRequest.

About the Examples

:include: doc/net-http/examples.rdoc

Direct Known Subclasses

HTTPRequest

Defined Under Namespace

Classes: Chunker

Constant Summary

Constants included from HTTPHeader

Gem::Net::HTTPHeader::MAX_FIELD_LENGTH, Gem::Net::HTTPHeader::MAX_KEY_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTTPHeader

#[], #add_field, #basic_auth, #chunked?, #connection_close?, #connection_keep_alive?, #content_length, #content_length=, #content_range, #content_type, #delete, #each_capitalized, #each_capitalized_name, #each_header, #each_name, #each_value, #fetch, #get_fields, #initialize_http_header, #key?, #main_type, #proxy_basic_auth, #range, #range_length, #set_content_type, #set_form, #set_form_data, #set_range, #size, #sub_type, #to_hash, #type_params

Constructor Details

#initialize(m, reqbody, resbody, uri_or_path, initheader = nil) ⇒ HTTPGenericRequest

:nodoc:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 15

def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc:
  @method = m
  @request_has_body = reqbody
  @response_has_body = resbody

  if Gem::URI === uri_or_path then
    raise ArgumentError, "not an HTTP Gem::URI" unless Gem::URI::HTTP === uri_or_path
    hostname = uri_or_path.hostname
    raise ArgumentError, "no host component for Gem::URI" unless (hostname && hostname.length > 0)
    @uri = uri_or_path.dup
    host = @uri.hostname.dup
    host << ":" << @uri.port.to_s if @uri.port != @uri.default_port
    @path = uri_or_path.request_uri
    raise ArgumentError, "no HTTP request path given" unless @path
  else
    @uri = nil
    host = nil
    raise ArgumentError, "no HTTP request path given" unless uri_or_path
    raise ArgumentError, "HTTP request path is empty" if uri_or_path.empty?
    @path = uri_or_path.dup
  end

  @decode_content = false

  if Gem::Net::HTTP::HAVE_ZLIB then
    if !initheader ||
       !initheader.keys.any? { |k|
         %w[accept-encoding range].include? k.downcase
       } then
      @decode_content = true if @response_has_body
      initheader = initheader ? initheader.dup : {}
      initheader["accept-encoding"] =
        "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
    end
  end

  initialize_http_header initheader
  self['Accept'] ||= '*/*'
  self['User-Agent'] ||= 'Ruby'
  self['Host'] ||= host if host
  @body = nil
  @body_stream = nil
  @body_data = nil
end

Instance Attribute Details

#bodyObject

Returns the string body for the request, or nil if there is none:

req = Gem::Net::HTTP::Post.new(uri)
req.body # => nil
req.body = '{"title": "foo","body": "bar","userId": 1}'
req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"


145
146
147
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 145

def body
  @body
end

#body_streamObject

Returns the body stream object for the request, or nil if there is none:

req = Gem::Net::HTTP::Post.new(uri)          # => #<Gem::Net::HTTP::Post POST>
req.body_stream                         # => nil
require 'stringio'
req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
req.body_stream                         # => #<StringIO:0x0000027d1e5affa8>


169
170
171
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 169

def body_stream
  @body_stream
end

#decode_contentObject (readonly)

Returns false if the request’s header 'Accept-Encoding' has been set manually or deleted (indicating that the user intends to handle encoding in the response), true otherwise:

req = Gem::Net::HTTP::Get.new(uri) # => #<Gem::Net::HTTP::Get GET>
req['Accept-Encoding']        # => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
req.decode_content            # => true
req['Accept-Encoding'] = 'foo'
req.decode_content            # => false
req.delete('Accept-Encoding')
req.decode_content            # => false


95
96
97
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 95

def decode_content
  @decode_content
end

#methodObject (readonly)

Returns the string method name for the request:

Gem::Net::HTTP::Get.new(uri).method  # => "GET"
Gem::Net::HTTP::Post.new(uri).method # => "POST"


65
66
67
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 65

def method
  @method
end

#pathObject (readonly)

Returns the string path for the request:

Gem::Net::HTTP::Get.new(uri).path # => "/"
Gem::Net::HTTP::Post.new('example.com').path # => "example.com"


72
73
74
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 72

def path
  @path
end

#uriObject (readonly)

Returns the Gem::URI object for the request, or nil if none:

Gem::Net::HTTP::Get.new(uri).uri
# => #<Gem::URI::HTTPS https://jsonplaceholder.typicode.com/>
Gem::Net::HTTP::Get.new('example.com').uri # => nil


80
81
82
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 80

def uri
  @uri
end

Instance Method Details

#[]=(key, val) ⇒ Object

Don’t automatically decode response content-encoding if the user indicates they want to handle it.



109
110
111
112
113
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 109

def []=(key, val) # :nodoc:
  @decode_content = false if key.downcase == 'accept-encoding'

  super key, val
end

#body_exist?Boolean

:nodoc:

Returns:

  • (Boolean)


133
134
135
136
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 133

def body_exist? # :nodoc:
  warn "Gem::Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?", uplevel: 1 if $VERBOSE
  response_body_permitted?
end

#exec(sock, ver, path) ⇒ Object

write



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 198

def exec(sock, ver, path)   #:nodoc: internal use only
  if @body
    send_request_with_body sock, ver, path, @body
  elsif @body_stream
    send_request_with_body_stream sock, ver, path, @body_stream
  elsif @body_data
    send_request_with_body_data sock, ver, path, @body_data
  else
    write_header sock, ver, path
  end
end

#inspectObject

Returns a string representation of the request:

Gem::Net::HTTP::Post.new(uri).inspect # => "#<Gem::Net::HTTP::Post POST>"


101
102
103
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 101

def inspect
  "\#<#{self.class} #{@method}>"
end

#request_body_permitted?Boolean

Returns whether the request may have a body:

Gem::Net::HTTP::Post.new(uri).request_body_permitted? # => true
Gem::Net::HTTP::Get.new(uri).request_body_permitted?  # => false

Returns:

  • (Boolean)


120
121
122
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 120

def request_body_permitted?
  @request_has_body
end

#response_body_permitted?Boolean

Returns whether the response may have a body:

Gem::Net::HTTP::Post.new(uri).response_body_permitted? # => true
Gem::Net::HTTP::Head.new(uri).response_body_permitted? # => false

Returns:

  • (Boolean)


129
130
131
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 129

def response_body_permitted?
  @response_has_body
end

#set_body_internal(str) ⇒ Object

:nodoc: internal use only

Raises:

  • (ArgumentError)


186
187
188
189
190
191
192
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 186

def set_body_internal(str)   #:nodoc: internal use only
  raise ArgumentError, "both of body argument and HTTPRequest#body set" if str and (@body or @body_stream)
  self.body = str if str
  if @body.nil? && @body_stream.nil? && @body_data.nil? && request_body_permitted?
    self.body = ''
  end
end

#update_uri(addr, port, ssl) ⇒ Object

:nodoc: internal use only



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/rubygems/vendor/net-http/lib/net/http/generic_request.rb', line 210

def update_uri(addr, port, ssl) # :nodoc: internal use only
  # reflect the connection and @path to @uri
  return unless @uri

  if ssl
    scheme = 'https'
    klass = Gem::URI::HTTPS
  else
    scheme = 'http'
    klass = Gem::URI::HTTP
  end

  if host = self['host']
    host.sub!(/:.*/m, '')
  elsif host = @uri.host
  else
   host = addr
  end
  # convert the class of the Gem::URI
  if @uri.is_a?(klass)
    @uri.host = host
    @uri.port = port
  else
    @uri = klass.new(
      scheme, @uri.userinfo,
      host, port, nil,
      @uri.path, nil, @uri.query, nil)
  end
end