class Rack::Response

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 #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 #finish.

Constants

CHUNKED
STATUS_WITH_NO_ENTITY_BODY

Attributes

body[RW]
header[R]
headers[R]
length[RW]
status[RW]

Public Class Methods

[](status, headers, body) click to toggle source
# File lib/rack/response.rb, line 18
def self.[](status, headers, body)
  self.new(body, status, headers)
end
new(body = nil, status = 200, headers = {}) { |self| ... } click to toggle source

Initialize the response object with the specified body, status and headers.

@param body [nil, each, to_str] the response body. @param status [Integer] the integer status as defined by the HTTP protocol RFCs. @param headers [#each] a list of key-value header pairs which conform to the HTTP protocol RFCs.

Providing a body which responds to to_str is legacy behaviour.

# File lib/rack/response.rb, line 41
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

Public Instance Methods

[](key)
Alias for: get_header
[]=(key, v)
Alias for: set_header
chunked?() click to toggle source
# File lib/rack/response.rb, line 72
def chunked?
  CHUNKED == get_header(TRANSFER_ENCODING)
end
close() click to toggle source
# File lib/rack/response.rb, line 117
def close
  @body.close if @body.respond_to?(:close)
end
delete_header(key) click to toggle source
# File lib/rack/response.rb, line 128
def delete_header(key); headers.delete key; end
each(&callback) click to toggle source
# File lib/rack/response.rb, line 97
def each(&callback)
  @body.each(&callback)
  @buffered = true

  if @block
    @writer = callback
    @block.call(self)
  end
end
empty?() click to toggle source
# File lib/rack/response.rb, line 121
def empty?
  @block == nil && @body.empty?
end
finish(&block) click to toggle source

Generate a response array consistent with the requirements of the SPEC. @return [Array] a 3-tuple suitable of `[status, headers, body]` which is suitable to be returned from the middleware `#call(env)` method.

# File lib/rack/response.rb, line 79
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
Also aliased as: to_a
get_header(key) click to toggle source
# File lib/rack/response.rb, line 126
def get_header(key);    headers[key];       end
Also aliased as: []
has_header?(key) click to toggle source
# File lib/rack/response.rb, line 125
def has_header?(key);   headers.key? key;   end
redirect(target, status = 302) click to toggle source
# File lib/rack/response.rb, line 67
def redirect(target, status = 302)
  self.status = status
  self.location = target
end
set_header(key, v) click to toggle source
# File lib/rack/response.rb, line 127
def set_header(key, v); headers[key] = v;   end
Also aliased as: []=
to_a(&block)
Alias for: finish
write(chunk) click to toggle source

Append to body and update Content-Length.

NOTE: Do not mix write and direct body access!

# File lib/rack/response.rb, line 111
def write(chunk)
  buffered_body!

  @writer.call(chunk.to_s)
end