Class | Rack::Response |
In: |
lib/rack/response.rb
|
Parent: | Object |
Rack::Request 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 Request#write to iteratively generate your response, but note that this is buffered by Rack::Request 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 Request#finish.
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: 26: @body = [] 27: 28: if body.kind_of?(String) 29: write body 30: elsif body.respond_to?(:each) 31: body.each { |part| 32: write part.to_s 33: } 34: else 35: raise TypeError, "String or iterable required" 36: end 37: 38: yield self if block_given? 39: end
# File lib/rack/response.rb, line 76 76: def delete_cookie(key, value={}) 77: unless Array === self["Set-Cookie"] 78: self["Set-Cookie"] = [self["Set-Cookie"]] 79: end 80: 81: self["Set-Cookie"].reject! { |cookie| 82: cookie =~ /\A#{Utils.escape(key)}=/ 83: } 84: 85: set_cookie(key, 86: {:value => '', :path => nil, :domain => nil, 87: :expires => Time.at(0) }.merge(value)) 88: end
# File lib/rack/response.rb, line 103 103: def each(&callback) 104: @body.each(&callback) 105: @writer = callback 106: @block.call(self) if @block 107: end
# File lib/rack/response.rb, line 91 91: def finish(&block) 92: @block = block 93: 94: if [201, 204, 304].include?(status.to_i) 95: header.delete "Content-Type" 96: [status.to_i, header.to_hash, []] 97: else 98: [status.to_i, header.to_hash, self] 99: end 100: end
# File lib/rack/response.rb, line 52 52: def set_cookie(key, value) 53: case value 54: when Hash 55: domain = "; domain=" + value[:domain] if value[:domain] 56: path = "; path=" + value[:path] if value[:path] 57: expires = "; expires=" + value[:expires].clone.gmtime. 58: strftime("%a, %d %b %Y %H:%M:%S GMT") if value[:expires] 59: value = value[:value] 60: end 61: value = [value] unless Array === value 62: cookie = Utils.escape(key) + "=" + 63: value.map { |v| Utils.escape v }.join("&") + 64: "#{domain}#{path}#{expires}" 65: 66: case self["Set-Cookie"] 67: when Array 68: self["Set-Cookie"] << cookie 69: when String 70: self["Set-Cookie"] = [self["Set-Cookie"], cookie] 71: when nil 72: self["Set-Cookie"] = cookie 73: end 74: end