Module | Sinatra::Helpers |
In: |
lib/sinatra/base.rb
|
Set the Content-Disposition to "attachment" with the specified filename, instructing the user agents to prompt to save.
# File lib/sinatra/base.rb, line 120 120: def attachment(filename=nil) 121: response['Content-Disposition'] = 'attachment' 122: if filename 123: params = '; filename="%s"' % File.basename(filename) 124: response['Content-Disposition'] << params 125: end 126: end
Set or retrieve the response body. When a block is given, evaluation is deferred until the body is read with each.
# File lib/sinatra/base.rb, line 67 67: def body(value=nil, &block) 68: if block_given? 69: def block.each ; yield call ; end 70: response.body = block 71: else 72: response.body = value 73: end 74: end
Set the Content-Type of the response body given a media type or file extension.
# File lib/sinatra/base.rb, line 107 107: def content_type(type, params={}) 108: media_type = self.media_type(type) 109: fail "Unknown media type: %p" % type if media_type.nil? 110: if params.any? 111: params = params.collect { |kv| "%s=%s" % kv }.join(', ') 112: response['Content-Type'] = [media_type, params].join(";") 113: else 114: response['Content-Type'] = media_type 115: end 116: end
Set the response entity tag (HTTP ‘ETag’ header) and halt if conditional GET matches. The value argument is an identifier that uniquely identifies the current version of the resource. The strength argument indicates whether the etag should be used as a :strong (default) or :weak cache validator.
When the current request includes an ‘If-None-Match’ header with a matching etag, execution is immediately halted. If the request method is GET or HEAD, a ‘304 Not Modified’ response is sent.
# File lib/sinatra/base.rb, line 176 176: def etag(value, kind=:strong) 177: raise TypeError, ":strong or :weak expected" if ![:strong,:weak].include?(kind) 178: value = '"%s"' % value 179: value = 'W/' + value if kind == :weak 180: response['ETag'] = value 181: 182: # Conditional GET check 183: if etags = env['HTTP_IF_NONE_MATCH'] 184: etags = etags.split(/\s*,\s*/) 185: halt 304 if etags.include?(value) || etags.include?('*') 186: end 187: end
Set the last modified time of the resource (HTTP ‘Last-Modified’ header) and halt if conditional GET matches. The time argument is a Time, DateTime, or other object that responds to to_time.
When the current request includes an ‘If-Modified-Since’ header that matches the time specified, execution is immediately halted with a ‘304 Not Modified’ response.
# File lib/sinatra/base.rb, line 159 159: def last_modified(time) 160: time = time.to_time if time.respond_to?(:to_time) 161: time = time.httpdate if time.respond_to?(:httpdate) 162: response['Last-Modified'] = time 163: halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE'] 164: time 165: end
Halt processing and return a 404 Not Found.
# File lib/sinatra/base.rb, line 91 91: def not_found(body=nil) 92: error 404, body 93: end
Use the contents of the file as the response body and attempt to
# File lib/sinatra/base.rb, line 129 129: def send_file(path, opts={}) 130: stat = File.stat(path) 131: last_modified stat.mtime 132: content_type media_type(opts[:type]) || 133: media_type(File.extname(path)) || 134: response['Content-Type'] || 135: 'application/octet-stream' 136: response['Content-Length'] ||= (opts[:length] || stat.size).to_s 137: halt StaticFile.open(path, 'rb') 138: rescue Errno::ENOENT 139: not_found 140: end