| Class | Net::HTTPResponse |
| In: |
lib/net/http.rb
|
| Parent: | Object |
HTTP response class. This class wraps response header and entity. Mixes in the HTTPHeader module, which provides access to response header values both via hash-like methods and individual readers. Note that each possible HTTP response code defines its own HTTPResponse subclass. These are listed below. All classes are defined under the Net module. Indentation indicates inheritance.
xxx HTTPResponse
1xx HTTPInformation
100 HTTPContinue
101 HTTPSwitchProtocol
2xx HTTPSuccess
200 HTTPOK
201 HTTPCreated
202 HTTPAccepted
203 HTTPNonAuthoritativeInformation
204 HTTPNoContent
205 HTTPResetContent
206 HTTPPartialContent
3xx HTTPRedirection
300 HTTPMultipleChoice
301 HTTPMovedPermanently
302 HTTPFound
303 HTTPSeeOther
304 HTTPNotModified
305 HTTPUseProxy
307 HTTPTemporaryRedirect
4xx HTTPClientError
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
5xx HTTPServerError
500 HTTPInternalServerError
501 HTTPNotImplemented
502 HTTPBadGateway
503 HTTPServiceUnavailable
504 HTTPGatewayTimeOut
505 HTTPVersionNotSupported
xxx HTTPUnknownResponse
| 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 |
| message | -> | msg |
# File lib/net/http.rb, line 2040
2040: def each_response_header(sock)
2041: while true
2042: line = sock.readuntil("\n", true).sub(/\s+\z/, '')
2043: break if line.empty?
2044: m = /\A([^:]+):\s*/.match(line) or
2045: raise HTTPBadResponse, 'wrong header line format'
2046: yield m[1], m.post_match
2047: end
2048: end
# File lib/net/http.rb, line 2027
2027: def read_status_line(sock)
2028: str = sock.readline
2029: m = /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)\s*(.*)\z/in.match(str) or
2030: raise HTTPBadResponse, "wrong status line: #{str.dump}"
2031: m.captures
2032: end
# File lib/net/http.rb, line 2034
2034: def response_class(code)
2035: CODE_TO_OBJ[code] or
2036: CODE_CLASS_TO_OBJ[code[0,1]] or
2037: HTTPUnknownResponse
2038: end
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
}
# File lib/net/http.rb, line 2205
2205: def body
2206: read_body()
2207: end
# File lib/net/http.rb, line 2078
2078: def inspect
2079: "#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
2080: end
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
}
# File lib/net/http.rb, line 2173
2173: def read_body(dest = nil, &block)
2174: if @read
2175: raise IOError, "#{self.class}\#read_body called twice" if dest or block
2176: return @body
2177: end
2178: to = procdest(dest, block)
2179: stream_check
2180: if @body_exist
2181: read_body_0 to
2182: @body = to
2183: else
2184: @body = nil
2185: end
2186: @read = true
2187:
2188: @body
2189: end
For backward compatibility. To allow Net::HTTP 1.1 style assignment e.g.
response, body = Net::HTTP.get(....)
# File lib/net/http.rb, line 2087
2087: def to_ary
2088: warn "net/http.rb: warning: Net::HTTP v1.1 style assignment found at #{caller(1)[0]}; use `response = http.get(...)' instead." if $VERBOSE
2089: res = self.dup
2090: class << res
2091: undef to_ary
2092: end
2093: [res, res.body]
2094: end
# File lib/net/http.rb, line 2252
2252: def procdest(dest, block)
2253: raise ArgumentError, 'both arg and block given for HTTP method' \
2254: if dest and block
2255: if block
2256: ReadAdapter.new(block)
2257: else
2258: dest || ''
2259: end
2260: end
# File lib/net/http.rb, line 2213
2213: def read_body_0(dest)
2214: if chunked?
2215: read_chunked dest
2216: return
2217: end
2218: clen = content_length()
2219: if clen
2220: @socket.read clen, dest, true # ignore EOF
2221: return
2222: end
2223: clen = range_length()
2224: if clen
2225: @socket.read clen, dest
2226: return
2227: end
2228: @socket.read_all dest
2229: end
# File lib/net/http.rb, line 2231
2231: def read_chunked(dest)
2232: len = nil
2233: total = 0
2234: while true
2235: line = @socket.readline
2236: hexlen = line.slice(/[0-9a-fA-F]+/) or
2237: raise HTTPBadResponse, "wrong chunk size line: #{line}"
2238: len = hexlen.hex
2239: break if len == 0
2240: @socket.read len, dest; total += len
2241: @socket.read 2 # \r\n
2242: end
2243: until @socket.readline.empty?
2244: # none
2245: end
2246: end