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 HTTPRequest class. Do not use this directly; use a subclass of HTTPRequest.

Mixes in the 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.

Raises:

  • (ArgumentError)


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

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
    @uri = uri_or_path.dup
    host = @uri.hostname
    host += ":#{@uri.port}" if @uri.port != @uri.class::DEFAULT_PORT
    path = uri_or_path.request_uri
  else
    @uri = nil
    host = nil
    path = uri_or_path
  end

  raise ArgumentError, "no HTTP request path given" unless path
  raise ArgumentError, "HTTP request path is empty" if path.empty?
  @path = path

  @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
  @body = nil
  @body_stream = nil
  @body_data = nil
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body



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

def body
  @body
end

#body_streamObject

Returns the value of attribute body_stream



98
99
100
# File 'lib/net/http/generic_request.rb', line 98

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.



60
61
62
# File 'lib/net/http/generic_request.rb', line 60

def decode_content
  @decode_content
end

#methodObject (readonly)

Returns the value of attribute method



53
54
55
# File 'lib/net/http/generic_request.rb', line 53

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path



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

def path
  @path
end

#uriObject (readonly)

Returns the value of attribute uri



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

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.



70
71
72
73
74
# File 'lib/net/http/generic_request.rb', line 70

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

  super key, val
end

#body_exist?Boolean

Returns:

  • (Boolean)


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

def body_exist?
  warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?" if $VERBOSE
  response_body_permitted?
end

#exec(sock, ver, path) ⇒ Object

write



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/net/http/generic_request.rb', line 119

def exec(sock, ver, path)   #:nodoc: internal use only
  if @uri
    if @uri.port == @uri.default_port
      # [Bug #7650] Amazon ECS API and GFE/1.3 disallow extra default port number
      self['host'] = @uri.host
    else
      self['host'] = "#{@uri.host}:#{@uri.port}"
    end
  end

  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



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

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

#request_body_permitted?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/net/http/generic_request.rb', line 76

def request_body_permitted?
  @request_has_body
end

#response_body_permitted?Boolean

Returns:

  • (Boolean)


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

def response_body_permitted?
  @response_has_body
end

#set_body_internal(str) ⇒ Object

:nodoc: internal use only

Raises:

  • (ArgumentError)


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

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(host, port, ssl) ⇒ Object

:nodoc: internal use only



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/net/http/generic_request.rb', line 140

def update_uri(host, port, ssl) # :nodoc: internal use only
  return unless @uri

  @uri.host ||= host
  @uri.port = port

  scheme = ssl ? 'https' : 'http'

  # convert the class of the URI
  unless scheme == @uri.scheme then
    new_uri = @uri.to_s.sub(/^https?/, scheme)
    @uri = URI new_uri
  end

  @uri
end