Permalink
Browse files

map Ruby CGI headers to Rack headers

  • Loading branch information...
1 parent 68b0c72 commit fa1f8a97b67dd7d6783d2499f7a0bf381f21bff1 @rubys committed Apr 4, 2012
Showing with 23 additions and 16 deletions.
  1. +8 −10 lib/wunderbar/cgi-methods.rb
  2. +8 −0 lib/wunderbar/server.rb
  3. +7 −6 test/test_cgi.rb
View
18 lib/wunderbar/cgi-methods.rb
@@ -4,12 +4,13 @@ module CGI
# produce json
def self.json(&block)
+ headers = { 'type' => 'application/json', 'Cache-Control' => 'no-cache' }
builder = JsonBuilder.new
output = builder.encode($params, &block)
- Kernel.print "Status: 404 Not Found\r\n" if output == {}
+ headers['status'] = "404 Not Found" if output == {}
rescue Exception => exception
- Kernel.print "Status: 500 Internal Error\r\n"
Wunderbar.error exception.inspect
+ headers['status'] = "500 Internal Server Error"
backtrace = []
exception.backtrace.each do |frame|
next if frame =~ %r{/wunderbar/}
@@ -21,19 +22,18 @@ def self.json(&block)
builder._exception exception.inspect
builder._backtrace backtrace
ensure
- out? 'type' => 'application/json', 'Cache-Control' => 'no-cache' do
- builder.target!
- end
+ out?(headers) { builder.target!}
end
# produce text
def self.text &block
+ headers = {'type' => 'text/plain', 'charset' => 'UTF-8'}
builder = TextBuilder.new
output = builder.encode($params, &block)
- Kernel.print "Status: 404 Not Found\r\n" if output == ''
+ headers['status'] = "404 Not Found" if output == ''
rescue Exception => exception
Wunderbar.error exception.inspect
- Kernel.print "Status: 500 Internal Error\r\n"
+ headers['status'] = "500 Internal Server Error"
builder.puts unless builder.size == 0
builder.puts exception.inspect
exception.backtrace.each do |frame|
@@ -43,9 +43,7 @@ def self.text &block
builder.puts " #{frame}"
end
ensure
- out? 'type' => 'text/plain', 'Cache-Control' => 'no-cache' do
- builder.target!
- end
+ out?(headers) { builder.target!}
end
# Conditionally provide output, based on ETAG
View
8 lib/wunderbar/server.rb
@@ -22,6 +22,14 @@ def $cgi.call(env)
# redirect the output produced
def $cgi.out(headers,&block)
+ # map Ruby CGI headers to Rack headers
+ status = headers.delete('status')
+ @response.status = status if status
+ type = headers.delete('type') || 'text/html'
+ charset = headers.delete('charset')
+ type = "#{type}; charset=#{charset}" if charset
+ headers['Content-Type'] ||= type
+
headers.each { |key, value| @response[key] = value }
@response.write block.call unless @request.head?
end
View
13 test/test_cgi.rb
@@ -118,7 +118,7 @@ def test_json_failure
Wunderbar::CGI.call(ENV)
- assert_match %r{Status: 500 Internal Error\r\n}, $stdout.string
+ assert_match %r{Status: 500 Internal Server Error\r\n}, $stdout.string
assert_match %r{^Content-Type: application/json\r\n}, $stdout.string
assert_match %r{^\s+"exception": ".*NameError.*error_undefined},
$stdout.string
@@ -146,7 +146,8 @@ def test_text_success
Wunderbar::CGI.call(ENV)
- assert_match %r{^Content-Type: text/plain\r\n}, $stdout.string
+ assert_match %r{^Content-Type: text/plain; charset=UTF-8\r\n},
+ $stdout.string
assert_match %r{\r\n\r\nIt Worked!\n\Z}, $stdout.string
end
@@ -159,7 +160,7 @@ def test_text_methods
Wunderbar::CGI.call(ENV)
- assert_match %r{^Content-Type: text/plain\r\n}, $stdout.string
+ assert_match %r{^Content-Type: text/plain}, $stdout.string
assert_match %r{\r\n\r\nIt Worked!\n\Z}, $stdout.string
end
@@ -172,7 +173,7 @@ def test_text_missing
Wunderbar::CGI.call(ENV)
assert_match %r{Status: 404 Not Found\r\n}, $stdout.string
- assert_match %r{^Content-Type: text/plain\r\n}, $stdout.string
+ assert_match %r{^Content-Type: text/plain}, $stdout.string
assert_match %r{\r\n\r\n\Z}, $stdout.string
end
@@ -185,8 +186,8 @@ def test_text_failure
Wunderbar::CGI.call(ENV)
- assert_match %r{Status: 500 Internal Error\r\n}, $stdout.string
- assert_match %r{^Content-Type: text/plain\r\n}, $stdout.string
+ assert_match %r{Status: 500 Internal Server Error\r\n}, $stdout.string
+ assert_match %r{^Content-Type: text/plain}, $stdout.string
assert_match %r{NameError.*error_undefined}, $stdout.string
assert_match %r{^_ERROR.*NameError.*error_undefined}, $stderr.string
end

0 comments on commit fa1f8a9

Please sign in to comment.