Class | Rack::Response |
In: |
lib/rack/response.rb
|
Parent: | Object |
Rack::Response provides a convenient interface to create a Rack response.
It allows setting of headers and cookies, and provides useful defaults (a OK response containing HTML).
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 syncronous with the Rack response.
Your application‘s call should end returning Response#finish.
header | -> | headers |
body | [RW] | |
header | [R] | |
status | [RW] |
# File lib/rack/response.rb, line 19 19: def initialize(body=[], status=200, header={}, &block) 20: @status = status 21: @header = Utils::HeaderHash.new({"Content-Type" => "text/html"}. 22: merge(header)) 23: 24: @writer = lambda { |x| @body << x } 25: @block = nil 26: @length = 0 27: 28: @body = [] 29: 30: if body.respond_to? :to_str 31: write body.to_str 32: elsif body.respond_to?(:each) 33: body.each { |part| 34: write part.to_s 35: } 36: else 37: raise TypeError, "stringable or iterable required" 38: end 39: 40: yield self if block_given? 41: end
# File lib/rack/response.rb, line 122 122: def close 123: body.close if body.respond_to?(:close) 124: end
# File lib/rack/response.rb, line 81 81: def delete_cookie(key, value={}) 82: unless Array === self["Set-Cookie"] 83: self["Set-Cookie"] = [self["Set-Cookie"]].compact 84: end 85: 86: self["Set-Cookie"].reject! { |cookie| 87: cookie =~ /\A#{Utils.escape(key)}=/ 88: } 89: 90: set_cookie(key, 91: {:value => '', :path => nil, :domain => nil, 92: :expires => Time.at(0) }.merge(value)) 93: end
# File lib/rack/response.rb, line 109 109: def each(&callback) 110: @body.each(&callback) 111: @writer = callback 112: @block.call(self) if @block 113: end
# File lib/rack/response.rb, line 96 96: def finish(&block) 97: @block = block 98: 99: if [204, 304].include?(status.to_i) 100: header.delete "Content-Type" 101: [status.to_i, header.to_hash, []] 102: else 103: header["Content-Length"] ||= @length.to_s 104: [status.to_i, header.to_hash, self] 105: end 106: end
# File lib/rack/response.rb, line 54 54: def set_cookie(key, value) 55: case value 56: when Hash 57: domain = "; domain=" + value[:domain] if value[:domain] 58: path = "; path=" + value[:path] if value[:path] 59: # According to RFC 2109, we need dashes here. 60: # N.B.: cgi.rb uses spaces... 61: expires = "; expires=" + value[:expires].clone.gmtime. 62: strftime("%a, %d-%b-%Y %H:%M:%S GMT") if value[:expires] 63: secure = "; secure" if value[:secure] 64: value = value[:value] 65: end 66: value = [value] unless Array === value 67: cookie = Utils.escape(key) + "=" + 68: value.map { |v| Utils.escape v }.join("&") + 69: "#{domain}#{path}#{expires}#{secure}" 70: 71: case self["Set-Cookie"] 72: when Array 73: self["Set-Cookie"] << cookie 74: when String 75: self["Set-Cookie"] = [self["Set-Cookie"], cookie] 76: when nil 77: self["Set-Cookie"] = cookie 78: end 79: end