Class: HTTP::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Headers::Mixin
Defined in:
lib/http/response.rb,
lib/http/response/body.rb,
lib/http/response/parser.rb,
lib/http/response/status.rb,
lib/http/response/inflater.rb,
lib/http/response/status/reasons.rb

Defined Under Namespace

Classes: Body, Inflater, Parser, Status

Instance Attribute Summary collapse

Attributes included from Headers::Mixin

#headers

Instance Method Summary collapse

Methods included from Headers::Mixin

#[], #[]=

Constructor Details

#initialize(opts) ⇒ Response

Inits a new instance

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :status (Integer)

    Status code

  • :version (String)

    HTTP version

  • :headers (Hash)
  • :proxy_headers (Hash)
  • :connection (HTTP::Connection)
  • :encoding (String)

    Encoding to use when reading body

  • :body (String)
  • request (HTTP::Request)

    The request this is in response to.

  • :uri (String) — default: DEPRECATED

    used to populate a missing request



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/http/response.rb', line 46

def initialize(opts)
  @version       = opts.fetch(:version)
  @request       = init_request(opts)
  @status        = HTTP::Response::Status.new(opts.fetch(:status))
  @headers       = HTTP::Headers.coerce(opts[:headers] || {})
  @proxy_headers = HTTP::Headers.coerce(opts[:proxy_headers] || {})

  if opts.include?(:body)
    @body = opts.fetch(:body)
  else
    connection = opts.fetch(:connection)
    encoding = opts[:encoding] || charset || default_encoding

    @body = Response::Body.new(connection, encoding: encoding)
  end
end

Instance Attribute Details

#bodyBody (readonly)

Returns:



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

def body
  @body
end

#proxy_headersHash (readonly)

Returns:

  • (Hash)


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

def proxy_headers
  @proxy_headers
end

#requestRequest (readonly)

Returns:



30
31
32
# File 'lib/http/response.rb', line 30

def request
  @request
end

#statusStatus (readonly)

Returns:



21
22
23
# File 'lib/http/response.rb', line 21

def status
  @status
end

#versionString (readonly)

Returns:

  • (String)


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

def version
  @version
end

Instance Method Details

#charsetString?

Charset of response (if any)

Returns:

  • (String, nil)


135
# File 'lib/http/response.rb', line 135

def_delegator :content_type, :charset

#chunked?Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
150
# File 'lib/http/response.rb', line 143

def chunked?
  return false unless @headers.include?(Headers::TRANSFER_ENCODING)

  encoding = @headers.get(Headers::TRANSFER_ENCODING)

  # TODO: "chunked" is frozen in the request writer. How about making it accessible?
  encoding.last == "chunked"
end

#codeFixnum

Returns status code.

Returns:

  • (Fixnum)

    status code



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

def_delegator :@status, :code

#connectionHTTP::Connection

The connection object used to make the corresponding request.

Returns:



82
# File 'lib/http/response.rb', line 82

def_delegator :@body, :connection

#content_lengthnil, Integer

Value of the Content-Length header.

Returns:

  • (nil)

    if Content-Length was not given, or it's value was invalid (not an integer, e.g. empty string or string with non-digits).

  • (Integer)

    otherwise



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/http/response.rb', line 108

def content_length
  # http://greenbytes.de/tech/webdav/rfc7230.html#rfc.section.3.3.3
  # Clause 3: "If a message is received with both a Transfer-Encoding
  # and a Content-Length header field, the Transfer-Encoding overrides the Content-Length.
  return nil if @headers.include?(Headers::TRANSFER_ENCODING)

  value = @headers[Headers::CONTENT_LENGTH]
  return nil unless value

  Integer(value, exception: false)
end

#content_typeHTTP::ContentType

Parsed Content-Type header

Returns:



123
124
125
# File 'lib/http/response.rb', line 123

def content_type
  @content_type ||= ContentType.parse headers[Headers::CONTENT_TYPE]
end

#cookiesObject



137
138
139
140
141
# File 'lib/http/response.rb', line 137

def cookies
  @cookies ||= headers.get(Headers::SET_COOKIE).each_with_object CookieJar.new do |v, jar|
    jar.parse(v, uri)
  end
end

#flushResponse

Flushes body and returns self-reference

Returns:



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

def flush
  body.to_s
  self
end

#inspectObject

Inspect a response



164
165
166
# File 'lib/http/response.rb', line 164

def inspect
  "#<#{self.class}/#{@version} #{code} #{reason} #{headers.to_h.inspect}>"
end

#mime_typeString?

MIME type of response (if any)

Returns:

  • (String, nil)


130
# File 'lib/http/response.rb', line 130

def_delegator :content_type, :mime_type

#parse(type = nil) ⇒ Object

Parse response body with corresponding MIME type adapter.

Parameters:

  • type (#to_s) (defaults to: nil)

    Parse as given MIME type.

Returns:

  • (Object)

Raises:

  • (Error)

    if no adapter found



157
158
159
160
161
# File 'lib/http/response.rb', line 157

def parse(type = nil)
  MimeType[type || mime_type].decode to_s
rescue => e
  raise ParseError, e.message
end

#readpartialObject



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

def_delegator :@body, :readpartial

#reasonString?

Returns status message.

Returns:

  • (String, nil)

    status message



65
# File 'lib/http/response.rb', line 65

def_delegator :@status, :reason

#to_aArray(Fixnum, Hash, String)

Returns an Array ala Rack: [status, headers, body]

Returns:

  • (Array(Fixnum, Hash, String))


91
92
93
# File 'lib/http/response.rb', line 91

def to_a
  [status.to_i, headers.to_h, body.to_s]
end

#to_sString Also known as: to_str

Returns eagerly consume the entire body as a string.

Returns:

  • (String)

    eagerly consume the entire body as a string



73
# File 'lib/http/response.rb', line 73

def_delegator :@body, :to_s

#uriObject



86
# File 'lib/http/response.rb', line 86

def_delegator :@request, :uri