Class: HTTP::Response
- Inherits:
-
Object
- Object
- HTTP::Response
- Extended by:
- Forwardable
- 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
Overview
Represents an HTTP response with status, headers, and body
Defined Under Namespace
Classes: Body, Inflater, Parser, Status
Instance Attribute Summary collapse
-
#body ⇒ Body
readonly
The response body.
-
#headers ⇒ HTTP::Headers
readonly
The HTTP headers collection.
-
#proxy_headers ⇒ Hash
readonly
The proxy headers.
-
#request ⇒ Request
readonly
The original request.
-
#status ⇒ Status
readonly
The response status.
-
#version ⇒ String
readonly
The HTTP version.
Instance Method Summary collapse
-
#charset ⇒ String?
Charset of response (if any).
-
#chunked? ⇒ Boolean
Check if the response uses chunked transfer encoding.
-
#code ⇒ Integer
Return the numeric status code.
-
#connection ⇒ HTTP::Connection
Return the underlying connection object.
-
#content_length ⇒ nil, Integer
Value of the Content-Length header.
-
#content_type ⇒ HTTP::ContentType
Parsed Content-Type header.
-
#cookies ⇒ Array<HTTP::Cookie>
Cookies from Set-Cookie headers.
-
#deconstruct_keys(keys) ⇒ Hash{Symbol => Object}
Pattern matching interface for matching against response attributes.
-
#flush ⇒ Response
Flushes body and returns self-reference.
-
#initialize(status:, version:, headers: {}, proxy_headers: {}, connection: nil, encoding: nil, body: nil, request: nil, uri: nil) ⇒ Response
constructor
Create a new Response instance.
-
#inspect ⇒ String
Inspect a response.
-
#mime_type ⇒ String?
MIME type of response (if any).
-
#parse(type = nil) ⇒ Object
Parse response body with corresponding MIME type adapter.
-
#readpartial ⇒ String
Read a chunk of the response body.
-
#reason ⇒ String?
Return the reason phrase for the response status.
-
#to_a ⇒ Array(Fixnum, Hash, String)
(also: #deconstruct)
Returns an Array ala Rack: ‘[status, headers, body]`.
-
#to_s ⇒ String
(also: #to_str)
Consume the response body as a string.
-
#uri ⇒ HTTP::URI
Return the URI of the original request.
Constructor Details
#initialize(status:, version:, headers: {}, proxy_headers: {}, connection: nil, encoding: nil, body: nil, request: nil, uri: nil) ⇒ Response
Create a new Response instance
89 90 91 92 93 94 95 96 97 |
# File 'lib/http/response.rb', line 89 def initialize(status:, version:, headers: {}, proxy_headers: {}, connection: nil, encoding: nil, body: nil, request: nil, uri: nil) @version = version @request = init_request(request, uri) @status = HTTP::Response::Status.new(status) @headers = HTTP::Headers.coerce(headers) @proxy_headers = HTTP::Headers.coerce(proxy_headers) @body = init_body(body, connection, encoding) end |
Instance Attribute Details
#body ⇒ Body (readonly)
The response body
44 45 46 |
# File 'lib/http/response.rb', line 44 def body @body end |
#headers ⇒ HTTP::Headers (readonly)
The HTTP headers collection
62 63 64 |
# File 'lib/http/response.rb', line 62 def headers @headers end |
#proxy_headers ⇒ Hash (readonly)
The proxy headers
71 72 73 |
# File 'lib/http/response.rb', line 71 def proxy_headers @proxy_headers end |
#request ⇒ Request (readonly)
The original request
53 54 55 |
# File 'lib/http/response.rb', line 53 def request @request end |
#status ⇒ Status (readonly)
The response status
26 27 28 |
# File 'lib/http/response.rb', line 26 def status @status end |
#version ⇒ String (readonly)
The HTTP version
35 36 37 |
# File 'lib/http/response.rb', line 35 def version @version end |
Instance Method Details
#charset ⇒ String?
Charset of response (if any)
256 |
# File 'lib/http/response.rb', line 256 def_delegator :content_type, :charset |
#chunked? ⇒ Boolean
Check if the response uses chunked transfer encoding
276 277 278 279 280 281 282 |
# File 'lib/http/response.rb', line 276 def chunked? return false unless @headers.include?(Headers::TRANSFER_ENCODING) encoding = @headers.get(Headers::TRANSFER_ENCODING) encoding.last == Headers::CHUNKED end |
#code ⇒ Integer
Return the numeric status code
113 |
# File 'lib/http/response.rb', line 113 def_delegator :@status, :code |
#connection ⇒ HTTP::Connection
Return the underlying connection object
139 |
# File 'lib/http/response.rb', line 139 def_delegator :@body, :connection |
#content_length ⇒ nil, Integer
Value of the Content-Length header
217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/http/response.rb', line 217 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) # RFC 7230 Section 3.3.2: If multiple Content-Length values are present, # they must all be identical; otherwise treat as invalid. values = @headers.get(Headers::CONTENT_LENGTH).uniq return nil unless values.one? Integer(values.first, exception: false) end |
#content_type ⇒ HTTP::ContentType
Parsed Content-Type header
238 239 240 |
# File 'lib/http/response.rb', line 238 def content_type @content_type ||= ContentType.parse headers[Headers::CONTENT_TYPE] end |
#cookies ⇒ Array<HTTP::Cookie>
Cookies from Set-Cookie headers
265 266 267 |
# File 'lib/http/response.rb', line 265 def @cookies ||= headers.get(Headers::SET_COOKIE).flat_map { |v| HTTP::Cookie.parse(v, uri) } end |
#deconstruct_keys(keys) ⇒ Hash{Symbol => Object}
Pattern matching interface for matching against response attributes
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/http/response.rb', line 184 def deconstruct_keys(keys) hash = { status: @status, version: @version, headers: @headers, body: @body, request: @request, proxy_headers: @proxy_headers } keys ? hash.slice(*keys) : hash end |
#flush ⇒ Response
Flushes body and returns self-reference
203 204 205 206 |
# File 'lib/http/response.rb', line 203 def flush body.to_s self end |
#inspect ⇒ String
Inspect a response
306 307 308 |
# File 'lib/http/response.rb', line 306 def inspect "#<#{self.class}/#{@version} #{code} #{reason} #{mime_type}>" end |
#mime_type ⇒ String?
MIME type of response (if any)
248 |
# File 'lib/http/response.rb', line 248 def_delegator :content_type, :mime_type |
#parse(type = nil) ⇒ Object
Parse response body with corresponding MIME type adapter
293 294 295 296 297 |
# File 'lib/http/response.rb', line 293 def parse(type = nil) MimeType[type || mime_type].decode to_s rescue => e raise ParseError, e. end |
#readpartial ⇒ String
Read a chunk of the response body
131 |
# File 'lib/http/response.rb', line 131 def_delegator :@body, :readpartial |
#reason ⇒ String?
Return the reason phrase for the response status
105 |
# File 'lib/http/response.rb', line 105 def_delegator :@status, :reason |
#to_a ⇒ Array(Fixnum, Hash, String) Also known as: deconstruct
Returns an Array ala Rack: ‘[status, headers, body]`
156 157 158 |
# File 'lib/http/response.rb', line 156 def to_a [status.to_i, headers.to_h, body.to_s] end |
#to_s ⇒ String Also known as: to_str
Consume the response body as a string
121 |
# File 'lib/http/response.rb', line 121 def_delegator :@body, :to_s |
#uri ⇒ HTTP::URI
Return the URI of the original request
147 |
# File 'lib/http/response.rb', line 147 def_delegator :@request, :uri |