content_type response helper with mime type lookup and parameter support.

ResponseHelpers#content_type takes a media type and parameters and sets
the Content-Type response header accordingly.
This commit is contained in:
Ryan Tomayko 2008-04-13 05:32:23 -04:00
parent d343d27d87
commit ccc19b0436
3 changed files with 50 additions and 4 deletions

View File

@ -100,7 +100,7 @@ This will render <tt>./views/index.haml</tt>
=== Sass
get '/stylesheet.css' do
header 'Content-Type' => 'text/css; charset=utf-8'
content_type 'text/css', :charset => 'utf-8'
sass :stylesheet
end
@ -174,7 +174,7 @@ This works like Haml except you use <tt>erb</tt> instead of <tt>haml</tt>
=== Sass
This works like Haml except you use <tt>sass</tt> instead of <tt>haml</tt>. It's also a good idea to add <tt>header 'Content-Type' => 'text/css; charset=utf-8'</tt> before your call to <tt>sass</tt> so Sinatra returns the proper content type header with the file.
This works like Haml except you use <tt>sass</tt> instead of <tt>haml</tt>. It's also a good idea to add <tt>content_type 'text/css', :charset => 'utf-8'</tt> before your call to <tt>sass</tt> so Sinatra returns the proper content type header with the file.
=== Builder

View File

@ -405,7 +405,7 @@ module Sinatra
# underlying headers Hash. With a Hash argument, add or overwrite
# existing response headers with the values provided:
#
# headers 'Content-Type' => "text/html; charset=utf-8",
# headers 'Content-Type' => "text/html;charset=utf-8",
# 'Last-Modified' => Time.now.httpdate,
# 'X-UA-Compatible' => 'IE=edge'
#
@ -416,8 +416,32 @@ module Sinatra
end
alias :header :headers
# Set the content type of the response body (HTTP 'Content-Type' header).
#
# The +type+ argument may be an internet media type (e.g., 'text/html',
# 'application/xml+atom', 'image/png') or a Symbol key into the
# Rack::File::MIME_TYPES table.
#
# Media type parameters, such as "charset", may also be specified using the
# optional hash argument:
#
# get '/foo.html' do
# content_type 'text/html', :charset => 'utf-8'
# "<h1>Hello World</h1>"
# end
#
def content_type(type, params={})
type = Rack::File::MIME_TYPES[type.to_s] if type.kind_of?(Symbol)
fail "Invalid or undefined media_type: #{type}" if type.nil?
if params.any?
params = params.collect { |kv| "%s=%s" % kv }.join(', ')
type = [ type, params ].join(";")
end
response.header['Content-Type'] = type
end
end
module RenderingHelpers
def render(renderer, template, options={})

View File

@ -113,6 +113,28 @@ context "Sinatra" do
end
specify "should easily set response Content-Type" do
get '/foo.html' do
content_type 'text/html', :charset => 'utf-8'
"<h1>Hello, World</h1>"
end
get_it '/foo.html'
should.be.ok
headers['Content-Type'].should.equal 'text/html;charset=utf-8'
body.should.equal '<h1>Hello, World</h1>'
get '/foo.xml' do
content_type :xml
"<feed></feed>"
end
get_it '/foo.xml'
should.be.ok
headers['Content-Type'].should.equal 'application/xml'
body.should.equal '<feed></feed>'
end
specify "delegates HEAD requests to GET handlers" do
get '/invisible' do
"I am invisible to the world"