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 2036 2036: def each_response_header(sock) 2037: while true 2038: line = sock.readuntil("\n", true).sub(/\s+\z/, '') 2039: break if line.empty? 2040: m = /\A([^:]+):\s*/.match(line) or 2041: raise HTTPBadResponse, 'wrong header line format' 2042: yield m[1], m.post_match 2043: end 2044: end
# File lib/net/http.rb, line 2023 2023: def read_status_line(sock) 2024: str = sock.readline 2025: m = /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)\s*(.*)\z/in.match(str) or 2026: raise HTTPBadResponse, "wrong status line: #{str.dump}" 2027: m.captures 2028: end
# File lib/net/http.rb, line 2030 2030: def response_class(code) 2031: CODE_TO_OBJ[code] or 2032: CODE_CLASS_TO_OBJ[code[0,1]] or 2033: HTTPUnknownResponse 2034: 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 2201 2201: def body 2202: read_body() 2203: end
# File lib/net/http.rb, line 2074 2074: def inspect 2075: "#<#{self.class} #{@code} #{@message} readbody=#{@read}>" 2076: 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 2169 2169: def read_body(dest = nil, &block) 2170: if @read 2171: raise IOError, "#{self.class}\#read_body called twice" if dest or block 2172: return @body 2173: end 2174: to = procdest(dest, block) 2175: stream_check 2176: if @body_exist 2177: read_body_0 to 2178: @body = to 2179: else 2180: @body = nil 2181: end 2182: @read = true 2183: 2184: @body 2185: 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 2083 2083: def to_ary 2084: warn "net/http.rb: warning: Net::HTTP v1.1 style assignment found at #{caller(1)[0]}; use `response = http.get(...)' instead." if $VERBOSE 2085: res = self.dup 2086: class << res 2087: undef to_ary 2088: end 2089: [res, res.body] 2090: end
# File lib/net/http.rb, line 2248 2248: def procdest(dest, block) 2249: raise ArgumentError, 'both arg and block given for HTTP method' \ 2250: if dest and block 2251: if block 2252: ReadAdapter.new(block) 2253: else 2254: dest || '' 2255: end 2256: end
# File lib/net/http.rb, line 2209 2209: def read_body_0(dest) 2210: if chunked? 2211: read_chunked dest 2212: return 2213: end 2214: clen = content_length() 2215: if clen 2216: @socket.read clen, dest, true # ignore EOF 2217: return 2218: end 2219: clen = range_length() 2220: if clen 2221: @socket.read clen, dest 2222: return 2223: end 2224: @socket.read_all dest 2225: end
# File lib/net/http.rb, line 2227 2227: def read_chunked(dest) 2228: len = nil 2229: total = 0 2230: while true 2231: line = @socket.readline 2232: hexlen = line.slice(/[0-9a-fA-F]+/) or 2233: raise HTTPBadResponse, "wrong chunk size line: #{line}" 2234: len = hexlen.hex 2235: break if len == 0 2236: @socket.read len, dest; total += len 2237: @socket.read 2 # \r\n 2238: end 2239: until @socket.readline.empty? 2240: # none 2241: end 2242: end