Class: Net::HTTPResponse
- Inherits:
-
Object
- Object
- Net::HTTPResponse
- Includes:
- HTTPHeader
- Defined in:
- lib/net/http.rb,
lib/net/http.rb
Overview
reopen
Direct Known Subclasses
HTTPClientError, HTTPInformation, HTTPRedirection, HTTPServerError, HTTPSuccess, HTTPUnknownResponse
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
-
#code ⇒ Object
readonly
The HTTP result code string.
-
#http_version ⇒ Object
readonly
The HTTP version supported by the server.
-
#message ⇒ Object
(also: #msg)
readonly
The HTTP result message sent by the server.
Class Method Summary collapse
-
.body_permitted? ⇒ Boolean
true if the response has a body.
-
.exception_type ⇒ Object
:nodoc: internal use only.
-
.read_new(sock) ⇒ Object
:nodoc: internal use only.
Instance Method Summary collapse
-
#body ⇒ Object
(also: #entity)
Returns the full entity body.
-
#body=(value) ⇒ Object
Because it may be necessary to modify the body, Eg, decompression this method facilitates that.
-
#code_type ⇒ Object
response <-> exception relationship.
-
#error! ⇒ Object
:nodoc:.
-
#error_type ⇒ Object
:nodoc:.
-
#header ⇒ Object
:nodoc:.
-
#initialize(httpv, code, msg) ⇒ HTTPResponse
constructor
:nodoc: internal use only.
- #inspect ⇒ Object
-
#read_body(dest = nil, &block) ⇒ Object
Gets the entity body returned by the remote HTTP server.
-
#read_header ⇒ Object
:nodoc:.
-
#reading_body(sock, reqmethodallowbody) ⇒ Object
body.
-
#response ⇒ Object
header (for backward compatibility only; DO NOT USE).
-
#value ⇒ Object
Raises an HTTP error if the response is not 2xx (success).
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(httpv, code, msg) ⇒ HTTPResponse
:nodoc: internal use only
2598 2599 2600 2601 2602 2603 2604 2605 |
# File 'lib/net/http.rb', line 2598 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
#code ⇒ Object (readonly)
The HTTP result code string. For example, '302'. You can also determine the response type by examining which response subclass the response object is an instance of.
2613 2614 2615 |
# File 'lib/net/http.rb', line 2613 def code @code end |
#http_version ⇒ Object (readonly)
The HTTP version supported by the server.
2608 2609 2610 |
# File 'lib/net/http.rb', line 2608 def http_version @http_version end |
#message ⇒ Object (readonly) Also known as: msg
The HTTP result message sent by the server. For example, 'Not Found'.
2616 2617 2618 |
# File 'lib/net/http.rb', line 2616 def @message end |
Class Method Details
.body_permitted? ⇒ Boolean
true if the response has a body.
2326 2327 2328 |
# File 'lib/net/http.rb', line 2326 def HTTPResponse.body_permitted? self::HAS_BODY end |
.exception_type ⇒ Object
:nodoc: internal use only
2330 2331 2332 |
# File 'lib/net/http.rb', line 2330 def HTTPResponse.exception_type # :nodoc: internal use only self::EXCEPTION_TYPE end |
.read_new(sock) ⇒ Object
:nodoc: internal use only
2550 2551 2552 2553 2554 2555 2556 2557 |
# File 'lib/net/http.rb', line 2550 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
#body ⇒ Object Also known as: entity
Returns the full entity body.
Calling this method a second or subsequent time will return the string already read.
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
}
2734 2735 2736 |
# File 'lib/net/http.rb', line 2734 def body read_body() end |
#body=(value) ⇒ Object
Because it may be necessary to modify the body, Eg, decompression this method facilitates that.
2740 2741 2742 |
# File 'lib/net/http.rb', line 2740 def body=(value) @body = value end |
#code_type ⇒ Object
response <-> exception relationship
2627 2628 2629 |
# File 'lib/net/http.rb', line 2627 def code_type #:nodoc: self.class end |
#error! ⇒ Object
:nodoc:
2631 2632 2633 |
# File 'lib/net/http.rb', line 2631 def error! #:nodoc: raise error_type().new(@code + ' ' + @message.dump, self) end |
#error_type ⇒ Object
:nodoc:
2635 2636 2637 |
# File 'lib/net/http.rb', line 2635 def error_type #:nodoc: self.class::EXCEPTION_TYPE end |
#header ⇒ Object
:nodoc:
2653 2654 2655 2656 |
# File 'lib/net/http.rb', line 2653 def header #:nodoc: warn "#{caller(1)[0]}: warning: HTTPResponse#header is obsolete" if $VERBOSE self end |
#inspect ⇒ Object
2619 2620 2621 |
# File 'lib/net/http.rb', line 2619 def inspect "#<#{self.class} #{@code} #{@message} readbody=#{@read}>" end |
#read_body(dest = nil, &block) ⇒ Object
Gets the entity body returned by the remote HTTP server.
If a block is given, the body is passed to the block, and the body is provided in fragments, as it is read in from the socket.
Calling this method a second or subsequent time for the same HTTPResponse object will return the value already read.
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
}
2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 |
# File 'lib/net/http.rb', line 2702 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_header ⇒ Object
:nodoc:
2658 2659 2660 2661 |
# File 'lib/net/http.rb', line 2658 def read_header #:nodoc: warn "#{caller(1)[0]}: warning: HTTPResponse#read_header is obsolete" if $VERBOSE self end |
#reading_body(sock, reqmethodallowbody) ⇒ Object
body
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 |
# File 'lib/net/http.rb', line 2667 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 |
#response ⇒ Object
header (for backward compatibility only; DO NOT USE)
2648 2649 2650 2651 |
# File 'lib/net/http.rb', line 2648 def response #:nodoc: warn "#{caller(1)[0]}: warning: HTTPResponse#response is obsolete" if $VERBOSE self end |
#value ⇒ Object
Raises an HTTP error if the response is not 2xx (success).
2640 2641 2642 |
# File 'lib/net/http.rb', line 2640 def value error! unless self.kind_of?(HTTPSuccess) end |