Class: Net::HTTPGenericRequest

Inherits:
Object
  • Object
show all
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; use a subclass of Net::HTTPRequest.

Mixes in the Net::HTTPHeader module to provide easier access to HTTP headers.

Direct Known Subclasses

HTTPRequest

Defined Under Namespace

Classes: Chunker

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

Returns a new instance of HTTPGenericRequest.



11
12
13
14
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
# File 'lib/net/http/generic_request.rb', line 11

def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
  @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.hostname
    raise ArgumentError, "no host component for URI" unless (hostname && hostname.length > 0)
    @uri = uri_or_path.dup
    host = @uri.hostname.dup
    host << ":".freeze << @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 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 value of attribute body.



92
93
94
# File 'lib/net/http/generic_request.rb', line 92

def body
  @body
end

#body_streamObject

Returns the value of attribute body_stream.



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

def body_stream
  @body_stream
end

#decode_contentObject (readonly)

Automatically set to false if the user sets the Accept-Encoding header. This indicates they wish to handle Content-encoding in responses themselves.



63
64
65
# File 'lib/net/http/generic_request.rb', line 63

def decode_content
  @decode_content
end

#methodObject (readonly)

Returns the value of attribute method.



56
57
58
# File 'lib/net/http/generic_request.rb', line 56

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



57
58
59
# File 'lib/net/http/generic_request.rb', line 57

def path
  @path
end

#uriObject (readonly)

Returns the value of attribute uri.



58
59
60
# File 'lib/net/http/generic_request.rb', line 58

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.



73
74
75
76
77
# File 'lib/net/http/generic_request.rb', line 73

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

  super key, val
end

#body_exist?Boolean

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/net/http/generic_request.rb', line 87

def body_exist?
  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



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/net/http/generic_request.rb', line 122

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



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

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

#request_body_permitted?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/net/http/generic_request.rb', line 79

def request_body_permitted?
  @request_has_body
end

#response_body_permitted?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/net/http/generic_request.rb', line 83

def response_body_permitted?
  @response_has_body
end

#set_body_internal(str) ⇒ Object

:nodoc: internal use only

Raises:

  • (ArgumentError)


110
111
112
113
114
115
116
# File 'lib/net/http/generic_request.rb', line 110

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/net/http/generic_request.rb', line 134

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

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

  if host = self['host']
    host.sub!(/:.*/m, ''.freeze)
  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