Class: Net::HTTPResponse

Inherits:
Object show all
Includes:
HTTPHeader
Defined in:
lib/extensions/net-http/net/http.rb,
lib/extensions/net-http/net/http.rb

Overview

reopen

Constant Summary collapse

CODE_CLASS_TO_OBJ =
{
  '1' => HTTPInformation,
  '2' => HTTPSuccess,
  '3' => HTTPRedirection,
  '4' => HTTPClientError,
  '5' => HTTPServerError
}
CODE_TO_OBJ =
{
  '100' => HTTPContinue,
  '101' => HTTPSwitchProtocol,

  '200' => HTTPOK,
  '201' => HTTPCreated,
  '202' => HTTPAccepted,
  '203' => HTTPNonAuthoritativeInformation,
  '204' => HTTPNoContent,
  '205' => HTTPResetContent,
  '206' => HTTPPartialContent,

  '300' => HTTPMultipleChoice,
  '301' => HTTPMovedPermanently,
  '302' => HTTPFound,
  '303' => HTTPSeeOther,
  '304' => HTTPNotModified,
  '305' => HTTPUseProxy,
  '307' => HTTPTemporaryRedirect,

  '400' => HTTPBadRequest,
  '401' => HTTPUnauthorized,
  '402' => HTTPPaymentRequired,
  '403' => HTTPForbidden,
  '404' => HTTPNotFound,
  '405' => HTTPMethodNotAllowed,
  '406' => HTTPNotAcceptable,
  '407' => HTTPProxyAuthenticationRequired,
  '408' => HTTPRequestTimeOut,
  '409' => HTTPConflict,
  '410' => HTTPGone,
  '411' => HTTPLengthRequired,
  '412' => HTTPPreconditionFailed,
  '413' => HTTPRequestEntityTooLarge,
  '414' => HTTPRequestURITooLong,
  '415' => HTTPUnsupportedMediaType,
  '416' => HTTPRequestedRangeNotSatisfiable,
  '417' => HTTPExpectationFailed,

  '500' => HTTPInternalServerError,
  '501' => HTTPNotImplemented,
  '502' => HTTPBadGateway,
  '503' => HTTPServiceUnavailable,
  '504' => HTTPGatewayTimeOut,
  '505' => HTTPVersionNotSupported
}

Instance Attribute Summary collapse

Class Method 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_data, #set_range, #size, #sub_type, #to_hash, #type_params

Constructor Details

#initialize(httpv, code, msg) ⇒ HTTPResponse

:nodoc: internal use only



2276
2277
2278
2279
2280
2281
2282
2283
# File 'lib/extensions/net-http/net/http.rb', line 2276

def initialize(httpv, code, msg)   #:nodoc: internal use only
  @http_version = httpv
  @code         = code
  @message      = msg
  initialize_http_header nil
  @body = nil
  @read = false
end

Instance Attribute Details

#codeObject (readonly)

HTTP result code string. For example, ‘302’. You can also determine the response type by which response subclass the response object is an instance of.



2291
2292
2293
# File 'lib/extensions/net-http/net/http.rb', line 2291

def code
  @code
end

#http_versionObject (readonly)

The HTTP version supported by the server.



2286
2287
2288
# File 'lib/extensions/net-http/net/http.rb', line 2286

def http_version
  @http_version
end

#messageObject (readonly) Also known as: msg

HTTP result message. For example, ‘Not Found’.



2294
2295
2296
# File 'lib/extensions/net-http/net/http.rb', line 2294

def message
  @message
end

Class Method Details

.body_permitted?Boolean

true if the response has body.

Returns:

  • (Boolean)


2004
2005
2006
# File 'lib/extensions/net-http/net/http.rb', line 2004

def HTTPResponse.body_permitted?
  self::HAS_BODY
end

.exception_typeObject

:nodoc: internal use only



2008
2009
2010
# File 'lib/extensions/net-http/net/http.rb', line 2008

def HTTPResponse.exception_type   # :nodoc: internal use only
  self::EXCEPTION_TYPE
end

.read_new(sock) ⇒ Object

:nodoc: internal use only



2228
2229
2230
2231
2232
2233
2234
2235
# File 'lib/extensions/net-http/net/http.rb', line 2228

def read_new(sock)   #:nodoc: internal use only
  httpv, code, msg = read_status_line(sock)
  res = response_class(code).new(httpv, code, msg)
  each_response_header(sock) do |k,v|
    res.add_field k, v
  end
  res
end

Instance Method Details

#bodyObject Also known as: entity

Returns the entity body.

Calling this method a second or subsequent time will return the already read string.

http.request_get('/index.html') {|res|
  puts res.body
}

http.request_get('/index.html') {|res|
  p res.body.object_id   # 538149362
  p res.body.object_id   # 538149362
}


2424
2425
2426
# File 'lib/extensions/net-http/net/http.rb', line 2424

def body
  read_body()
end

#body=(value) ⇒ Object

Because it may be necessary to modify the body, Eg, decompression this method facilitates that.



2430
2431
2432
# File 'lib/extensions/net-http/net/http.rb', line 2430

def body=(value)
  @body = value
end

#code_typeObject

response <-> exception relationship



2319
2320
2321
# File 'lib/extensions/net-http/net/http.rb', line 2319

def code_type   #:nodoc:
  self.class
end

#error!Object

:nodoc:

Raises:



2323
2324
2325
# File 'lib/extensions/net-http/net/http.rb', line 2323

def error!   #:nodoc:
  raise error_type().new(@code + ' ' + @message.dump, self)
end

#error_typeObject

:nodoc:



2327
2328
2329
# File 'lib/extensions/net-http/net/http.rb', line 2327

def error_type   #:nodoc:
  self.class::EXCEPTION_TYPE
end

#headerObject

:nodoc:



2345
2346
2347
2348
# File 'lib/extensions/net-http/net/http.rb', line 2345

def header   #:nodoc:
  warn "#{caller(1)[0]}: warning: HTTPResponse#header is obsolete" if $VERBOSE
  self
end

#inspectObject



2297
2298
2299
# File 'lib/extensions/net-http/net/http.rb', line 2297

def inspect
  "#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
end

#read_body(dest = nil, &block) ⇒ Object

Gets entity body. If the block given, yields it to block. The body is provided in fragments, as it is read in from the socket.

Calling this method a second or subsequent time will return the already read string.

http.request_get('/index.html') {|res|
  puts res.read_body
}

http.request_get('/index.html') {|res|
  p res.read_body.object_id   # 538149362
  p res.read_body.object_id   # 538149362
}

# using iterator
http.request_get('/index.html') {|res|
  res.read_body do |segment|
    print segment
  end
}


2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
# File 'lib/extensions/net-http/net/http.rb', line 2392

def read_body(dest = nil, &block)
  if @read
    raise IOError, "#{self.class}\#read_body called twice" if dest or block
    return @body
  end
  to = procdest(dest, block)
  stream_check
  if @body_exist
    read_body_0 to
    @body = to
  else
    @body = nil
  end
  @read = true

  @body
end

#read_headerObject

:nodoc:



2350
2351
2352
2353
# File 'lib/extensions/net-http/net/http.rb', line 2350

def read_header   #:nodoc:
  warn "#{caller(1)[0]}: warning: HTTPResponse#read_header is obsolete" if $VERBOSE
  self
end

#reading_body(sock, reqmethodallowbody) ⇒ Object

body



2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
# File 'lib/extensions/net-http/net/http.rb', line 2359

def reading_body(sock, reqmethodallowbody)  #:nodoc: internal use only
  @socket = sock
  @body_exist = reqmethodallowbody && self.class.body_permitted?
  begin
    yield
    self.body   # ensure to read body
  ensure
    @socket = nil
  end
end

#responseObject

header (for backward compatibility only; DO NOT USE)



2340
2341
2342
2343
# File 'lib/extensions/net-http/net/http.rb', line 2340

def response   #:nodoc:
  warn "#{caller(1)[0]}: warning: HTTPResponse#response is obsolete" if $VERBOSE
  self
end

#to_aryObject

For backward compatibility. To allow Net::HTTP 1.1 style assignment e.g.

response, body = Net::HTTP.get(....)


2306
2307
2308
2309
2310
2311
2312
2313
# File 'lib/extensions/net-http/net/http.rb', line 2306

def to_ary
  warn "net/http.rb: warning: Net::HTTP v1.1 style assignment found at #{caller(1)[0]}; use `response = http.get(...)' instead." if $VERBOSE
  res = self.dup
  class << res
    undef to_ary
  end
  [res, res.body]
end

#valueObject

Raises HTTP error if the response is not 2xx.



2332
2333
2334
# File 'lib/extensions/net-http/net/http.rb', line 2332

def value
  error! unless self.kind_of?(HTTPSuccess)
end