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
# 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
    raise ArgumentError, "no host component for URI" unless uri_or_path.hostname
    @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 @response_has_body and Net::HTTP::HAVE_ZLIB then
    if !initheader ||
       !initheader.keys.any? { |k|
         %w[accept-encoding range].include? k.downcase
       } then
      @decode_content = true
      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.



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

def body
  @body
end

#body_streamObject

Returns the value of attribute body_stream.



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

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.



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

def decode_content
  @decode_content
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#uriObject (readonly)

Returns the value of attribute uri.



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

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.



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

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

  super key, val
end

#body_exist?Boolean

Returns:

  • (Boolean)


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

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



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

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



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

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

#request_body_permitted?Boolean

Returns:

  • (Boolean)


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

def request_body_permitted?
  @request_has_body
end

#response_body_permitted?Boolean

Returns:

  • (Boolean)


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

def response_body_permitted?
  @response_has_body
end

#set_body_internal(str) ⇒ Object

:nodoc: internal use only

Raises:

  • (ArgumentError)


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

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



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

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!(/:.*/s, ''.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