summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormakoto kuwata <kwa@kuwata-lab.com>2010-06-27 21:01:55 +0900
committermakoto kuwata <kwa@kuwata-lab.com>2010-06-27 21:02:23 +0900
commitd83b5c4fd4f66e19978aaa298270e3003bbbbe4f (patch)
tree8133e4e00e1af2908e4bb071da7fc73acdf23b24
parent9fcd240869280c3bf302e93314989437c485cc07 (diff)
downloaderubis-d83b5c4fd4f66e19978aaa298270e3003bbbbe4f.tar.gz
[add] add 'contrib/index.cgi' and 'contrib/_htaccess'
-rw-r--r--contrib/_htaccess8
-rw-r--r--contrib/index.cgi67
2 files changed, 75 insertions, 0 deletions
diff --git a/contrib/_htaccess b/contrib/_htaccess
new file mode 100644
index 0000000..532cbf2
--- /dev/null
+++ b/contrib/_htaccess
@@ -0,0 +1,8 @@
+## enable mod_rewrie
+RewriteEngine on
+## deny access to *.rhtml and *.cache
+RewriteRule \.(rhtml|cache)$ - [R=404,L]
+## rewrite only if requested file is not found
+RewriteCond %{SCRIPT_FILENAME} !-f
+## handle request to *.html by index.cgi
+RewriteRule ^.*\.html$ index.cgi
diff --git a/contrib/index.cgi b/contrib/index.cgi
new file mode 100644
index 0000000..8d8f7a4
--- /dev/null
+++ b/contrib/index.cgi
@@ -0,0 +1,67 @@
+#!/usr/bin/env ruby
+
+###
+### CGI script to handle *.rhtml with Erubis
+###
+### Licsense: same as Erubis
+###
+
+require 'cgi'
+require 'erubis'
+include Erubis::XmlHelper
+
+ERUBY = Erubis::Eruby # or Erubis::EscapeEruby
+encoding = 'utf-8'
+
+## helper class to represent http error
+class HttpError < Exception
+ attr_accessor :status
+ def initialize(status, message)
+ super(message)
+ @status = status
+ end
+end
+
+
+## main
+cgi = CGI.new
+content_type = "text/html; charset=#{encoding}"
+begin
+
+ ## check environment variables
+ document_root = ENV['DOCUMENT_ROOT'] or raise "ENV['DOCUMENT_ROOT'] is not set."
+ request_uri = ENV['REQUEST_URI'] or raise "ENV['REQUEST_URI'] is not set."
+ ## get filepath
+ basepath = request_uri.split(/\?/, 2).first
+ filepath = File.join(document_root, basepath)
+ filepath.gsub!(/\.html\z/, '.rhtml') or # expected '*.html'
+ raise HttpError.new('500 Internal Error', 'invalid .htaccess configuration.')
+ File.file?(filepath) or # file not found
+ raise HttpError.new('404 Not Found', "#{basepath}: not found.")
+ basepath != ENV['SCRIPT_NAME'] or # can't access to index.cgi
+ raise HttpError.new('403 Forbidden', "#{basepath}: not accessable.")
+ ## process as eRuby file
+ eruby = ERUBY.load_file(filepath) # or ERUBY.new(File.read(filepath))
+ html = eruby.result()
+ ## send response
+ print cgi.header('Content-Type'=>content_type, 'Content-Length'=>html.length)
+ print html
+
+rescue HttpError => ex
+ ## handle http error (such as 404 Not Found)
+ print cgi.header('Content-Type'=>content_type, 'Status'=>ex.status)
+ print "<h2>#{h(ex.status)}</h2>\n"
+ print "<p>#{h(ex.message)}</p>"
+
+rescue Exception => ex
+ ## print exception backtrace
+ print cgi.header('Content-Type'=>content_type, 'Status'=>'500 Internal Error')
+ arr = ex.backtrace
+ print "<pre>\n"
+ print "<b>#{h(arr[0])}: #{h(ex.message)} (#{h(ex.class.name)})</b>\n"
+ arr[1..-1].each do |item|
+ print " from #{h(item)}\n"
+ end
+ print "</pre>\n"
+
+end