Class: Net::HTTPGenericRequest
- Inherits:
-
Object
- Object
- Net::HTTPGenericRequest
- Includes:
- HTTPHeader
- Defined in:
- lib/net/http/generic_request.rb
Overview
HTTPGenericRequest is the parent of the Net::HTTPRequest class.
Do not use this directly; instead, use a subclass of Net::HTTPRequest.
About the Examples
:include: doc/net-http/examples.rdoc
Direct Known Subclasses
Defined Under Namespace
Classes: Chunker
Constant Summary
Constants included from HTTPHeader
Net::HTTPHeader::MAX_FIELD_LENGTH, Net::HTTPHeader::MAX_KEY_LENGTH
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the string body for the request, or
nilif there is none:. -
#body_stream ⇒ Object
Returns the body stream object for the request, or
nilif there is none:. -
#decode_content ⇒ Object
readonly
Returns
falseif the request’s header'Accept-Encoding'has been set manually or deleted (indicating that the user intends to handle encoding in the response),trueotherwise:. -
#method ⇒ Object
readonly
Returns the string method name for the request:.
-
#path ⇒ Object
readonly
Returns the string path for the request:.
-
#uri ⇒ Object
readonly
Returns the URI object for the request, or
nilif none:.
Instance Method Summary collapse
-
#[]=(key, val) ⇒ Object
Don’t automatically decode response content-encoding if the user indicates they want to handle it.
-
#body_exist? ⇒ Boolean
:nodoc:.
-
#exec(sock, ver, path) ⇒ Object
write.
-
#initialize(m, reqbody, resbody, uri_or_path, initheader = nil) ⇒ HTTPGenericRequest
constructor
:nodoc:.
-
#inspect ⇒ Object
Returns a string representation of the request:.
-
#pretty_print(q) ⇒ Object
Returns a string representation of the request with the details for pp:.
-
#request_body_permitted? ⇒ Boolean
Returns whether the request may have a body:.
-
#response_body_permitted? ⇒ Boolean
Returns whether the response may have a body:.
-
#set_body_internal(str) ⇒ Object
:nodoc: internal use only.
-
#update_uri(addr, port, ssl) ⇒ Object
:nodoc: internal use only.
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 |
# File '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 URI === uri_or_path then raise ArgumentError, "not an HTTP URI" unless URI::HTTP === uri_or_path hostname = uri_or_path.host raise ArgumentError, "no host component for URI" unless (hostname && hostname.length > 0) @uri = uri_or_path.dup @path = uri_or_path.request_uri raise ArgumentError, "no HTTP request path given" unless @path else @uri = 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 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'] ||= @uri. if @uri @body = nil @body_stream = nil @body_data = nil end |
Instance Attribute Details
#body ⇒ Object
167 168 169 |
# File 'lib/net/http/generic_request.rb', line 167 def body @body end |
#body_stream ⇒ Object
Returns the body stream object for the request, or nil if there is none:
req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
req.body_stream # => nil
require 'stringio'
req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
req.body_stream # => #<StringIO:0x0000027d1e5affa8>
191 192 193 |
# File 'lib/net/http/generic_request.rb', line 191 def body_stream @body_stream end |
#decode_content ⇒ Object (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 = Net::HTTP::Get.new(uri) # => #<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
92 93 94 |
# File 'lib/net/http/generic_request.rb', line 92 def decode_content @decode_content end |
#method ⇒ Object (readonly)
62 63 64 |
# File 'lib/net/http/generic_request.rb', line 62 def method @method end |
#path ⇒ Object (readonly)
69 70 71 |
# File 'lib/net/http/generic_request.rb', line 69 def path @path end |
Instance Method Details
#[]=(key, val) ⇒ Object
Don’t automatically decode response content-encoding if the user indicates they want to handle it.
131 132 133 134 135 |
# File 'lib/net/http/generic_request.rb', line 131 def []=(key, val) # :nodoc: @decode_content = false if key.downcase == 'accept-encoding' super key, val end |
#body_exist? ⇒ Boolean
:nodoc:
155 156 157 158 |
# File 'lib/net/http/generic_request.rb', line 155 def body_exist? # :nodoc: warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?", uplevel: 1 if $VERBOSE response_body_permitted? end |
#exec(sock, ver, path) ⇒ Object
write
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/net/http/generic_request.rb', line 220 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 |
#inspect ⇒ Object
98 99 100 |
# File 'lib/net/http/generic_request.rb', line 98 def inspect "\#<#{self.class} #{@method}>" end |
#pretty_print(q) ⇒ Object
Returns a string representation of the request with the details for pp:
require 'pp'
post = Net::HTTP::Post.new(uri)
post.inspect # => "#<Net::HTTP::Post POST>"
post.pretty_inspect
# => #<Net::HTTP::Post
POST
path="/"
headers={"accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
"accept" => ["*/*"],
"user-agent" => ["Ruby"],
"host" => ["www.ruby-lang.org"]}>
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/net/http/generic_request.rb', line 116 def pretty_print(q) q.object_group(self) { q.breakable q.text @method q.breakable q.text "path="; q.pp @path q.breakable q.text "headers="; q.pp to_hash } end |
#request_body_permitted? ⇒ Boolean
142 143 144 |
# File 'lib/net/http/generic_request.rb', line 142 def request_body_permitted? @request_has_body end |
#response_body_permitted? ⇒ Boolean
151 152 153 |
# File 'lib/net/http/generic_request.rb', line 151 def response_body_permitted? @response_has_body end |
#set_body_internal(str) ⇒ Object
:nodoc: internal use only
208 209 210 211 212 213 214 |
# File 'lib/net/http/generic_request.rb', line 208 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
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/net/http/generic_request.rb', line 232 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 = URI::HTTPS else scheme = 'http' klass = URI::HTTP end if host = self['host'] host = URI.parse("//#{host}").host # Remove a port component from the existing Host header elsif host = @uri.host else host = addr end # convert the class of the 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 |