Methods
B
E
I
P
R
S
V
Included Modules
Constants
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 }
 
Attributes
[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'.

Class Public methods
body_permitted?()

true if the response has a body.

# File ../ruby/lib/net/http.rb, line 2327
def HTTPResponse.body_permitted?
  self::HAS_BODY
end
Instance Public methods
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
}
Also aliased as: entity
# File ../ruby/lib/net/http.rb, line 2735
def body
  read_body()
end
body=(value)

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

# File ../ruby/lib/net/http.rb, line 2741
def body=(value)
  @body = value
end
entity()
Alias for: body
inspect()
# File ../ruby/lib/net/http.rb, line 2620
def inspect
  "#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
end
read_body(dest = nil, &block)

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
value()

Raises an HTTP error if the response is not 2xx (success).

# File ../ruby/lib/net/http.rb, line 2641
def value
  error! unless self.kind_of?(HTTPSuccess)
end
Instance Private methods
each_response_header(sock)
# 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
procdest(dest, block)
# File ../ruby/lib/net/http.rb, line 2792
def procdest(dest, block)
  raise ArgumentError, 'both arg and block given for HTTP method'            if dest and block
  if block
    ReadAdapter.new(block)
  else
    dest || ''
  end
end
read_body_0(dest)
# 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
read_chunked(dest)
# 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
read_status_line(sock)
# File ../ruby/lib/net/http.rb, line 2562
def read_status_line(sock)
  str = sock.readline
  m = /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)\s*(.*)\z/in.match(str) or
    raise HTTPBadResponse, "wrong status line: #{str.dump}"
  m.captures
end
response_class(code)
# File ../ruby/lib/net/http.rb, line 2569
def response_class(code)
  CODE_TO_OBJ[code] or
  CODE_CLASS_TO_OBJ[code[0,1]] or
  HTTPUnknownResponse
end
stream_check()
# File ../ruby/lib/net/http.rb, line 2788
def stream_check
  raise IOError, 'attempt to read body out of block' if @socket.closed?
end