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,
sig/http.rbs
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.
-
#default_encoding ⇒ Encoding
private
Determine the default encoding for the body.
-
#flush ⇒ Response
Flushes body and returns self-reference.
-
#init_body(body, connection, encoding) ⇒ Body
private
Initialize the response body.
-
#init_request(request, uri) ⇒ HTTP::Request
private
Initialize an HTTP::Request.
-
#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 |
#default_encoding ⇒ Encoding
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determine the default encoding for the body
315 316 317 318 319 |
# File 'lib/http/response.rb', line 315 def default_encoding return Encoding::UTF_8 if mime_type == "application/json" Encoding::BINARY 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 |
#init_body(body, connection, encoding) ⇒ Body
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize the response body
325 326 327 328 329 330 331 332 333 |
# File 'lib/http/response.rb', line 325 def init_body(body, connection, encoding) if body body else encoding ||= charset || default_encoding Response::Body.new(connection, encoding: encoding) end end |
#init_request(request, uri) ⇒ HTTP::Request
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize an HTTP::Request
339 340 341 342 343 344 345 346 347 348 |
# File 'lib/http/response.rb', line 339 def init_request(request, uri) raise ArgumentError, ":uri is for backwards compatibility and conflicts with :request" if request && uri # For backwards compatibility if uri HTTP::Request.new(uri: uri, verb: :get) else request end 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 |