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

Constants

CHUNKED
STATUS_WITH_NO_ENTITY_BODY

Attributes

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

Public Class Methods

[](status, headers, body) click to toggle source
# File lib/rack/response.rb, line 24
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.

If the body is nil, construct an empty response object with internal buffering.

If the body responds to to_str, assume it’s a string-like object and construct a buffered response object containing using that string as the initial contents of the buffer.

Otherwise it is expected body conforms to the normal requirements of a Rack response body, typically implementing one of each (enumerable body) or call (streaming body).

The status defaults to 200 which is the “OK” HTTP status code. You can provide any other valid status code.

The headers must be a Hash of key-value header pairs which conform to the Rack specification for response headers. The key must be a String instance and the value can be either a String or Array instance.

# File lib/rack/response.rb, line 61
def initialize(body = nil, status = 200, headers = {})
  @status = status.to_i

  unless headers.is_a?(Hash)
    warn "Providing non-hash headers to Rack::Response is deprecated and will be removed in Rack 3.1", uplevel: 1
  end

  @headers = Headers.new
  # Convert headers input to a plain hash with lowercase keys.
  headers.each do |k, v|
    @headers[k] = v
  end

  @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 = nil # undetermined as of yet.
    @length = 0
  end

  yield self if block_given?
end

Public Instance Methods

[](key)
Alias for: get_header
[]=(key, value)
Alias for: set_header
chunked?() click to toggle source
# File lib/rack/response.rb, line 101
def chunked?
  CHUNKED == get_header(TRANSFER_ENCODING)
end
close() click to toggle source
# File lib/rack/response.rb, line 151
def close
  @body.close if @body.respond_to?(:close)
end
delete_header(key) click to toggle source
# File lib/rack/response.rb, line 171
def delete_header(key)
  raise ArgumentError unless key.is_a?(String)
  @headers.delete key
end
each(&callback) click to toggle source
# File lib/rack/response.rb, line 131
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 155
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 113
def finish(&block)
  if no_entity_body?
    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 163
def get_header(key)
  raise ArgumentError unless key.is_a?(String)
  @headers[key]
end
Also aliased as: []
has_header?(key) click to toggle source
# File lib/rack/response.rb, line 159
def has_header?(key)
  raise ArgumentError unless key.is_a?(String)
  @headers.key?(key)
end
header() click to toggle source

Deprecated, use headers instead.

# File lib/rack/response.rb, line 35
def header
  warn 'Rack::Response#header is deprecated and will be removed in Rack 3.1', uplevel: 1

  headers
end
no_entity_body?() click to toggle source
# File lib/rack/response.rb, line 105
def no_entity_body?
  # The response body is an enumerable body and it is not allowed to have an entity body.
  @body.respond_to?(:each) && STATUS_WITH_NO_ENTITY_BODY[@status]
end
redirect(target, status = 302) click to toggle source
# File lib/rack/response.rb, line 96
def redirect(target, status = 302)
  self.status = status
  self.location = target
end
set_header(key, value) click to toggle source
# File lib/rack/response.rb, line 167
def set_header(key, value)
  raise ArgumentError unless key.is_a?(String)
  @headers[key] = value
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 145
def write(chunk)
  buffered_body!

  @writer.call(chunk.to_s)
end