lib/wunderbar/cgi-methods.rb in wunderbar-0.8.13 vs lib/wunderbar/cgi-methods.rb in wunderbar-0.8.14
- old
+ new
@@ -1,135 +1,163 @@
-# produce json
-def $cgi.json(&block)
- return unless $XHR_JSON
- $param.each {|key,value| instance_variable_set "@#{key}", value.first}
- output = instance_eval(&block)
-rescue Exception => exception
- Kernel.print "Status: 500 Internal Error\r\n"
- output = {
- :exception => exception.inspect,
- :backtrace => exception.backtrace
- }
-ensure
- if $XHR_JSON
- Kernel.print "Status: 404 Not Found\r\n" unless output
- $cgi.out? 'type' => 'application/json', 'Cache-Control' => 'no-cache' do
- begin
- JSON.pretty_generate(output)+ "\n"
- rescue
- output.to_json + "\n"
+module Wunderbar
+
+ module CGI
+
+ # produce json
+ def self.json(&block)
+ $param.each do |key,value|
+ instance_variable_set "@#{key}", value.first if key =~ /^\w+$/
end
+ output = instance_eval(&block)
+ rescue Exception => exception
+ Kernel.print "Status: 500 Internal Error\r\n"
+ output = {
+ :exception => exception.inspect,
+ :backtrace => exception.backtrace
+ }
+ ensure
+ Kernel.print "Status: 404 Not Found\r\n" unless output
+ out? 'type' => 'application/json', 'Cache-Control' => 'no-cache' do
+ begin
+ JSON.pretty_generate(output)+ "\n"
+ rescue
+ output.to_json + "\n"
+ end
+ end
end
- end
-end
-# produce json and quit
-def $cgi.json! &block
- return unless $XHR_JSON
- json(&block)
- Process.exit
-end
+ # produce text
+ def self.text &block
+ require 'stringio'
+ buffer = StringIO.new
+ $param.each do |key,value|
+ instance_variable_set "@#{key}", value.first if key =~ /^\w+$/
+ end
+ buffer.instance_eval &block
+ rescue Exception => exception
+ Kernel.print "Status: 500 Internal Error\r\n"
+ buffer << "\n" unless buffer.size == 0
+ buffer << exception.inspect + "\n"
+ exception.backtrace.each {|frame| buffer << " #{frame}\n"}
+ ensure
+ Kernel.print "Status: 404 Not Found\r\n" if buffer.size == 0
+ out? 'type' => 'text/plain', 'Cache-Control' => 'no-cache' do
+ buffer.string
+ end
+ end
-# produce text
-def $cgi.text &block
- return unless $TEXT
- @output = []
- def $cgi.puts(line='')
- @output << line + "\n"
- end
- def $cgi.print(line=nil)
- @output << line
- end
- $param.each {|key,value| instance_variable_set "@#{key}", value.first}
- self.instance_eval &block
-rescue Exception => exception
- Kernel.print "Status: 500 Internal Error\r\n"
- @output << "\n" unless @output.empty?
- @output << exception.inspect + "\n"
- exception.backtrace.each {|frame| @output << " #{frame}\n"}
-ensure
- class << $cgi
- undef puts
- undef print
- end
- if $TEXT
- Kernel.print "Status: 404 Not Found\r\n" if @output.empty?
- $cgi.out? 'type' => 'text/plain', 'Cache-Control' => 'no-cache' do
- @output.join
+ # Conditionally provide output, based on ETAG
+ def self.out?(headers, &block)
+ content = block.call
+ require 'digest/md5'
+ etag = Digest::MD5.hexdigest(content)
+
+ if ENV['HTTP_IF_NONE_MATCH'] == etag.inspect
+ Kernel.print "Status: 304 Not Modified\r\n\r\n"
+ else
+ $cgi.out headers.merge('Etag' => etag.inspect) do
+ content
+ end
+ end
+ rescue
end
- @output = nil
- end
-end
-# produce text and quit
-def $cgi.text! &block
- return unless $TEXT
- json(&block)
- Process.exit
-end
+ # produce html/xhtml
+ def self.html(*args, &block)
+ args << {} if args.empty?
+ if Hash === args.first
+ args.first[:xmlns] ||= 'http://www.w3.org/1999/xhtml'
+ end
+ mimetype = ($XHTML ? 'application/xhtml+xml' : 'text/html')
+ x = HtmlMarkup.new
+ x._! "\xEF\xBB\xBF"
+ x._.declare :DOCTYPE, :html
-# Conditionally provide output, based on ETAG
-def $cgi.out?(headers, &block)
- content = block.call
- require 'digest/md5'
- etag = Digest::MD5.hexdigest(content)
+ begin
+ output = x.html *args, &block
+ rescue ::Exception => exception
+ Kernel.print "Status: 500 Internal Error\r\n"
+ x.clear!
+ x._! "\xEF\xBB\xBF"
+ x._.declare :DOCTYPE, :html
+ output = x.html(*args) do
+ _head do
+ _title 'Internal Error'
+ end
+ _body do
+ _h1 'Internal Error'
+ text = exception.inspect
+ Wunderbar.error text
+ exception.backtrace.each do |frame|
+ next if frame =~ %r{/wunderbar/}
+ next if frame =~ %r{/gems/.*/builder/}
+ Wunderbar.warn " #{frame}"
+ text += "\n #{frame}"
+ end
+
+ _pre text
+ end
+ end
+ end
- if ENV['HTTP_IF_NONE_MATCH'] == etag.inspect
- Kernel.print "Status: 304 Not Modified\r\n\r\n"
- else
- $cgi.out headers.merge('Etag' => etag.inspect) do
- content
+ out? 'type' => mimetype, 'charset' => 'UTF-8' do
+ output
+ end
end
end
-end
-# produce html/xhtml
-def $cgi.html(*args, &block)
- return if $XHR_JSON or $TEXT
- args << {} if args.empty?
- args.first[:xmlns] ||= 'http://www.w3.org/1999/xhtml' if Hash === args.first
- mimetype = ($XHTML ? 'application/xhtml+xml' : 'text/html')
- x = HtmlMarkup.new
- $cgi.out? 'type' => mimetype, 'charset' => 'UTF-8' do
- x._! "\xEF\xBB\xBF"
- x._.declare :DOCTYPE, :html
- x.html *args, &block
- end
-end
+ @queue = []
-# produce html and quit
-def $cgi.html! *args, &block
- return if $XHR_JSON or $TEXT
- html(*args, &block)
- Process.exit
-end
-
-# post specific logic (doesn't produce output)
-def $cgi.post
- yield if $HTTP_POST
-end
-
-# post specific content (produces output)
-def $cgi.post! &block
- html!(&block) if $HTTP_POST
-end
-
-# canonical interface
-module Wunderbar
+ # canonical interface
def self.html(*args, &block)
- $XHTML = false unless ARGV.delete('--xhtml')
- $cgi.html!(*args, &block)
+ @queue << [:html, args, block]
end
def self.xhtml(*args, &block)
- $XHTML = false if ARGV.delete('--html')
- $cgi.html!(*args, &block)
+ @queue << [:xhtml, args, block]
end
def self.json(*args, &block)
- $cgi.json!(*args, &block)
+ @queue << [:json, args, block]
end
def self.text(*args, &block)
- $cgi.text!(*args, &block)
+ @queue << [:text, args, block]
end
+
+ def self.evaluate
+ queue, @queue = @queue, []
+ xhtml = ARGV.delete('--xhtml')
+ html = ARGV.delete('--html')
+
+ queue.each do |type, args, block|
+ case type
+ when :html
+ unless $XHR_JSON or $TEXT
+ $XHTML = false unless xhtml
+ CGI.html(*args, &block)
+ break
+ end
+ when :xhtml
+ unless $XHR_JSON or $TEXT
+ $XHTML = false if html
+ CGI.html(*args, &block)
+ break
+ end
+ when :json
+ if $XHR_JSON
+ CGI.json(*args, &block)
+ break
+ end
+ when :text
+ if $TEXT
+ CGI.text(*args, &block)
+ break
+ end
+ end
+ end
+ end
+end
+
+at_exit do
+ Wunderbar.evaluate
end