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.


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
# 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.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


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

def body
  @body
end

#body_streamObject

Returns the value of attribute body_stream


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

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.


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

def decode_content
  @decode_content
end

#methodObject (readonly)

Returns the value of attribute method


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

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path


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

def path
  @path
end

#uriObject (readonly)

Returns the value of attribute uri


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

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.


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

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

  super key, val
end

#body_exist?Boolean

Returns:

  • (Boolean)

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

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


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/net/http/generic_request.rb', line 118

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


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

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

#request_body_permitted?Boolean

Returns:

  • (Boolean)

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

def request_body_permitted?
  @request_has_body
end

#response_body_permitted?Boolean

Returns:

  • (Boolean)

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

def response_body_permitted?
  @response_has_body
end

#set_body_internal(str) ⇒ Object

:nodoc: internal use only

Raises:

  • (ArgumentError)

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

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


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

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