Class: Puppet::HTTP::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/http/response.rb

Overview

Represents the response returned from the server from an HTTP request.

Direct Known Subclasses

ResponseNetHTTP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, code, reason) ⇒ Response

Create a response associated with the URL.

Parameters:

  • url (URI)
  • HTTP (Integer)

    status

  • HTTP (String)

    reason



15
16
17
18
19
# File 'lib/puppet/http/response.rb', line 15

def initialize(url, code, reason)
  @url = url
  @code = code
  @reason = reason
end

Instance Attribute Details

#urlURI (readonly)

Returns the response url.

Returns:

  • (URI)

    the response url



8
9
10
# File 'lib/puppet/http/response.rb', line 8

def url
  @url
end

Instance Method Details

#[](name) ⇒ String

Get a header case-insensitively.

Parameters:

  • name (String)

    The header name

Returns:

  • (String)

    The header value

Raises:

  • (NotImplementedError)


78
79
80
# File 'lib/puppet/http/response.rb', line 78

def [](name)
  raise NotImplementedError
end

#bodyString

Returns the entire response body. Can be used instead of

`Puppet::HTTP::Response.read_body`, but both methods cannot be used for the
same response.

Returns:

  • (String)

    Response body for the request

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/puppet/http/response.rb', line 46

def body
  raise NotImplementedError
end

#codeInteger

Return the response code.

Returns:

  • (Integer)

    Response code for the request



26
27
28
# File 'lib/puppet/http/response.rb', line 26

def code
  @code
end

#drainObject

Ensure the response body is fully read so that the server is not blocked waiting for us to read data from the socket. Also if the caller streamed the response, but didn’t read the data, we need a way to drain the socket before adding the connection back to the connection pool, otherwise the unread response data would “leak” into the next HTTP request/response.



99
100
101
102
# File 'lib/puppet/http/response.rb', line 99

def drain
  body
  true
end

#each_header {|header, header| ... } ⇒ Object

Yield each header name and value. Returns an enumerator if no block is given.

Yield Parameters:

  • header (String)

    name

  • header (String)

    value

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/puppet/http/response.rb', line 88

def each_header(&block)
  raise NotImplementedError
end

#read_body {|String| ... } ⇒ Object

Streams the response body to the caller in chunks. Can be used instead of

`Puppet::HTTP::Response.body`, but both methods cannot be used for the same
response.

Yields:

  • (String)

    Streams the response body in chunks

Raises:

  • (ArgumentError)

    raise if a block is not given



59
60
61
# File 'lib/puppet/http/response.rb', line 59

def read_body(&block)
  raise NotImplementedError
end

#reasonString

Return the response message.

Returns:

  • (String)

    Response message for the request



35
36
37
# File 'lib/puppet/http/response.rb', line 35

def reason
  @reason
end

#success?Boolean

Check if the request received a response of success (HTTP 2xx).

Returns:

  • (Boolean)

    Returns true if the response indicates success



68
69
70
# File 'lib/puppet/http/response.rb', line 68

def success?
  200 <= @code && @code < 300
end