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


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

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


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

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)

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

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)

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

def body
  raise NotImplementedError
end

#codeInteger

Return the response code.

Returns:

  • (Integer)

    Response code for the request


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

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.


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

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)

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

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


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

def read_body(&block)
  raise NotImplementedError
end

#reasonString

Return the response message.

Returns:

  • (String)

    Response message for the request


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

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


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

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