Class: Rack::Response
Overview
Rack::Response provides a convenient interface to create a Rack response.
It allows setting of headers and cookies, and provides useful defaults (an OK response with empty headers and body).
You can use Response#write to iteratively generate your response, but note that this is buffered by Rack::Response until you call finish
. finish
however can take a block inside which calls to write
are synchronous with the Rack response.
Your application’s call
should end returning Response#finish.
Direct Known Subclasses
Defined Under Namespace
Constant Summary collapse
- CHUNKED =
'chunked'
- STATUS_WITH_NO_ENTITY_BODY =
Utils::STATUS_WITH_NO_ENTITY_BODY
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#headers ⇒ Object
(also: #header)
readonly
Returns the value of attribute headers.
-
#length ⇒ Object
Returns the value of attribute length.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
- #chunked? ⇒ Boolean
- #close ⇒ Object
- #delete_header(key) ⇒ Object
- #each(&callback) ⇒ Object
- #empty? ⇒ Boolean
-
#finish(&block) ⇒ Array
(also: #to_a)
Generate a response array consistent with the requirements of the SPEC.
- #get_header(key) ⇒ Object (also: #[])
- #has_header?(key) ⇒ Boolean
-
#initialize(body = nil, status = 200, headers = {}) {|_self| ... } ⇒ Response
constructor
Initialize the response object with the specified body, status and headers.
- #redirect(target, status = 302) ⇒ Object
- #set_header(key, v) ⇒ Object (also: #[]=)
-
#write(chunk) ⇒ Object
Append to body and update Content-Length.
Methods included from Helpers
#accepted?, #add_header, #bad_request?, #cache!, #cache_control, #cache_control=, #client_error?, #content_length, #content_type, #content_type=, #created?, #delete_cookie, #do_not_cache!, #etag, #etag=, #forbidden?, #include?, #informational?, #invalid?, #location, #location=, #media_type, #media_type_params, #method_not_allowed?, #moved_permanently?, #no_content?, #not_found?, #ok?, #precondition_failed?, #redirect?, #redirection?, #server_error?, #set_cookie, #set_cookie_header, #set_cookie_header=, #successful?, #unauthorized?, #unprocessable?
Constructor Details
#initialize(body = nil, status = 200, headers = {}) {|_self| ... } ⇒ Response
Initialize the response object with the specified body, status and headers.
HTTP protocol RFCs. conform to the HTTP protocol RFCs.
Providing a body which responds to #to_str is legacy behaviour.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rack/response.rb', line 42 def initialize(body = nil, status = 200, headers = {}) @status = status.to_i @headers = Utils::HeaderHash[headers] @writer = self.method(:append) @block = nil # Keep track of whether we have expanded the user supplied body. if body.nil? @body = [] @buffered = true @length = 0 elsif body.respond_to?(:to_str) @body = [body] @buffered = true @length = body.to_str.bytesize else @body = body @buffered = false @length = 0 end yield self if block_given? end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
26 27 28 |
# File 'lib/rack/response.rb', line 26 def body @body end |
#headers ⇒ Object (readonly) Also known as: header
Returns the value of attribute headers.
27 28 29 |
# File 'lib/rack/response.rb', line 27 def headers @headers end |
#length ⇒ Object
Returns the value of attribute length.
26 27 28 |
# File 'lib/rack/response.rb', line 26 def length @length end |
#status ⇒ Object
Returns the value of attribute status.
26 27 28 |
# File 'lib/rack/response.rb', line 26 def status @status end |
Class Method Details
.[](status, headers, body) ⇒ Object
19 20 21 |
# File 'lib/rack/response.rb', line 19 def self.[](status, headers, body) self.new(body, status, headers) end |
Instance Method Details
#chunked? ⇒ Boolean
73 74 75 |
# File 'lib/rack/response.rb', line 73 def chunked? CHUNKED == get_header(TRANSFER_ENCODING) end |
#close ⇒ Object
118 119 120 |
# File 'lib/rack/response.rb', line 118 def close @body.close if @body.respond_to?(:close) end |
#delete_header(key) ⇒ Object
129 |
# File 'lib/rack/response.rb', line 129 def delete_header(key); headers.delete key; end |
#each(&callback) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/rack/response.rb', line 98 def each(&callback) @body.each(&callback) @buffered = true if @block @writer = callback @block.call(self) end end |
#empty? ⇒ Boolean
122 123 124 |
# File 'lib/rack/response.rb', line 122 def empty? @block == nil && @body.empty? end |
#finish(&block) ⇒ Array Also known as: to_a
Generate a response array consistent with the requirements of the SPEC. which is suitable to be returned from the middleware ‘#call(env)` method.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rack/response.rb', line 80 def finish(&block) if STATUS_WITH_NO_ENTITY_BODY[status.to_i] delete_header CONTENT_TYPE delete_header CONTENT_LENGTH close return [@status, @headers, []] else if block_given? @block = block return [@status, @headers, self] else return [@status, @headers, @body] end end end |
#get_header(key) ⇒ Object Also known as: []
127 |
# File 'lib/rack/response.rb', line 127 def get_header(key); headers[key]; end |
#has_header?(key) ⇒ Boolean
126 |
# File 'lib/rack/response.rb', line 126 def has_header?(key); headers.key? key; end |
#redirect(target, status = 302) ⇒ Object
68 69 70 71 |
# File 'lib/rack/response.rb', line 68 def redirect(target, status = 302) self.status = status self.location = target end |
#set_header(key, v) ⇒ Object Also known as: []=
128 |
# File 'lib/rack/response.rb', line 128 def set_header(key, v); headers[key] = v; end |
#write(chunk) ⇒ Object
Append to body and update Content-Length.
NOTE: Do not mix #write and direct #body access!
112 113 114 115 116 |
# File 'lib/rack/response.rb', line 112 def write(chunk) buffered_body! @writer.call(chunk.to_s) end |