- B
- E
- I
- P
- R
- S
- V
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 } |
[R] | code | 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. |
[R] | http_version | The HTTP version supported by the server. |
[R] | message | The HTTP result message sent by the server. For example, 'Not Found'. |
[R] | msg | The HTTP result message sent by the server. For example, 'Not Found'. |
true if the response has a body.
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
}
Because it may be necessary to modify the body, Eg, decompression this method facilitates that.
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
}
# File ../ruby/lib/net/http.rb, line 2703 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
Raises an HTTP error if the response is not 2xx (success).
# File ../ruby/lib/net/http.rb, line 2575 def each_response_header(sock) key = value = nil while true line = sock.readuntil("\n", true).sub(/\s+\z/, '') break if line.empty? if line[0] == ?\s or line[0] == ?\t and value value << ' ' unless value.empty? value << line.strip else yield key, value if key key, value = line.strip.split(/\s*:\s*/, 2) raise HTTPBadResponse, 'wrong header line format' if value.nil? end end yield key, value if key end
# File ../ruby/lib/net/http.rb, line 2749 def read_body_0(dest) if chunked? read_chunked dest return end clen = content_length() if clen @socket.read clen, dest, true # ignore EOF return end clen = range_length() if clen @socket.read clen, dest return end @socket.read_all dest end
# File ../ruby/lib/net/http.rb, line 2767 def read_chunked(dest) len = nil total = 0 while true line = @socket.readline hexlen = line.slice(/[0-9a-fA-F]+/) or raise HTTPBadResponse, "wrong chunk size line: #{line}" len = hexlen.hex break if len == 0 begin @socket.read len, dest ensure total += len @socket.read 2 # \r\n end end until @socket.readline.empty? # none end end