From dba3a1c74fe9bf2e2988625bc29486ed7992dd2e Mon Sep 17 00:00:00 2001 From: makoto kuwata Date: Sat, 19 Mar 2011 12:00:25 +0900 Subject: [move] '_htaccess' and 'index.cgi' from 'contrib' to 'public_html' --- contrib/_htaccess | 8 --- contrib/index.cgi | 151 -------------------------------------------------- public_html/_htaccess | 8 +++ public_html/index.cgi | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 159 deletions(-) delete mode 100644 contrib/_htaccess delete mode 100755 contrib/index.cgi create mode 100644 public_html/_htaccess create mode 100755 public_html/index.cgi diff --git a/contrib/_htaccess b/contrib/_htaccess deleted file mode 100644 index 99a215c..0000000 --- a/contrib/_htaccess +++ /dev/null @@ -1,8 +0,0 @@ -## 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$ /~yourname/index.cgi diff --git a/contrib/index.cgi b/contrib/index.cgi deleted file mode 100755 index f38f7fd..0000000 --- a/contrib/index.cgi +++ /dev/null @@ -1,151 +0,0 @@ -#!/usr/bin/env ruby - -### -### CGI script to handle *.rhtml with Erubis -### -### Licsense: same as Erubis -### - -## add directory path where Erubis installed -#$LOAD_PATH << "/home/yourname/lib/ruby" - -## configuration -$ENCODING = nil -$LAYOUT = '_layout.rhtml' - -## load Erubis -begin - require 'erubis' -rescue LoadError => ex - begin - require 'rubygems' - require 'erubis' - rescue LoadError # => ex - print "Status: 500 Internal Server Error\r\n" - print "Content-Type: text/plain\r\n" - print "\r\n" - print "LoadError: #{ex.message}" - exit - end -end - - -## helper class to represent http error -class HttpError < Exception - attr_accessor :status - def initialize(status, message) - super(message) - @status = status - end -end - - -class ErubisHandler - include Erubis::XmlHelper - - ERUBY = Erubis::Eruby # or Erubis::EscapeEruby - - def initialize - @encoding = $ENCODING - @layout = $LAYOUT - end - - attr_accessor :encoding, :layout - - def handle(env) - ## 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 = basepath =~ /\A\/(~[-.\w]+)/ \ - ? File.join(File.expand_path($1), "public_html", $') \ - : File.join(document_root, basepath) - filepath.gsub!(/\.html\z/, '.rhtml') or # expected '*.html' - raise HttpError.new(500, 'invalid .htaccess configuration.') - File.file?(filepath) or # file not found - raise HttpError.new(404, "#{basepath}: not found.") - basepath != env['SCRIPT_NAME'] or # can't access to index.cgi - raise HttpError.new(403, "#{basepath}: not accessable.") - ## process as eRuby file - #eruby = ERUBY.new(File.read(filepath)) # not create cache file (slower) - eruby = ERUBY.load_file(filepath) # create cache file (faster) - html = eruby.evaluate(self) - ## use layout template - if @layout && File.file?(@layout) - @content = html - html = ERUBY.load_file(@layout).evaluate(self) - end - return html - end - -end - - -class ErubisApplication - include Erubis::XmlHelper - - RESPONSE_STATUS = { - 200 => "200 OK", - 403 => "Forbidden", - 404 => "404 Not Found", - 500 => "500 Internal Server Error", - } - - protected - - def get_handler - return ErubisHandler.new() - end - - def handle_request(env) - handler = get_handler() - output = handler.handle(env) - cont_type = "text/html" - cont_type << ";charset=#{handler.encoding}" if handler.encoding - return [200, [["Content-Type", cont_type]], [output]] - end - - def handle_http_error(ex) - output = "

#{h(RESPONSE_STATUS[ex.status])}

\n

#{h(ex.message)}

\n" - return [ex.status, [["Content-Type", "text/html"]], [output]] - end - - def handle_error(ex) - arr = ex.backtrace - output = "" - output << "
\n"
-    output <<   "#{h(arr[0])}:
#{h(ex.message)} (#{h(ex.class.name)})
\n" - arr[1..-1].each do |item| - output << " from #{h(item)}\n" - end - output << "
\n" - return [500, [["Content-Type", "text/html"]], [output]] - end - - public - - def call(env) - begin - return handle_request(env) - rescue HttpError => ex - return handle_http_error(ex) - rescue => ex - return handle_error(ex) - end - end - - def run(env=ENV, stdout=$stdout) - status, headers, output_arr = call(env) - stdout << "Status: #{RESPONSE_STATUS[status]}\r\n" unless status == 200 - headers.each {|k, v| stdout << "#{k}: #{v}\r\n" } - stdout << "\r\n" - output_arr.each {|str| stdout << str } - end - -end - - -if __FILE__ == $0 - ErubisApplication.new.run() -end diff --git a/public_html/_htaccess b/public_html/_htaccess new file mode 100644 index 0000000..99a215c --- /dev/null +++ b/public_html/_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$ /~yourname/index.cgi diff --git a/public_html/index.cgi b/public_html/index.cgi new file mode 100755 index 0000000..f38f7fd --- /dev/null +++ b/public_html/index.cgi @@ -0,0 +1,151 @@ +#!/usr/bin/env ruby + +### +### CGI script to handle *.rhtml with Erubis +### +### Licsense: same as Erubis +### + +## add directory path where Erubis installed +#$LOAD_PATH << "/home/yourname/lib/ruby" + +## configuration +$ENCODING = nil +$LAYOUT = '_layout.rhtml' + +## load Erubis +begin + require 'erubis' +rescue LoadError => ex + begin + require 'rubygems' + require 'erubis' + rescue LoadError # => ex + print "Status: 500 Internal Server Error\r\n" + print "Content-Type: text/plain\r\n" + print "\r\n" + print "LoadError: #{ex.message}" + exit + end +end + + +## helper class to represent http error +class HttpError < Exception + attr_accessor :status + def initialize(status, message) + super(message) + @status = status + end +end + + +class ErubisHandler + include Erubis::XmlHelper + + ERUBY = Erubis::Eruby # or Erubis::EscapeEruby + + def initialize + @encoding = $ENCODING + @layout = $LAYOUT + end + + attr_accessor :encoding, :layout + + def handle(env) + ## 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 = basepath =~ /\A\/(~[-.\w]+)/ \ + ? File.join(File.expand_path($1), "public_html", $') \ + : File.join(document_root, basepath) + filepath.gsub!(/\.html\z/, '.rhtml') or # expected '*.html' + raise HttpError.new(500, 'invalid .htaccess configuration.') + File.file?(filepath) or # file not found + raise HttpError.new(404, "#{basepath}: not found.") + basepath != env['SCRIPT_NAME'] or # can't access to index.cgi + raise HttpError.new(403, "#{basepath}: not accessable.") + ## process as eRuby file + #eruby = ERUBY.new(File.read(filepath)) # not create cache file (slower) + eruby = ERUBY.load_file(filepath) # create cache file (faster) + html = eruby.evaluate(self) + ## use layout template + if @layout && File.file?(@layout) + @content = html + html = ERUBY.load_file(@layout).evaluate(self) + end + return html + end + +end + + +class ErubisApplication + include Erubis::XmlHelper + + RESPONSE_STATUS = { + 200 => "200 OK", + 403 => "Forbidden", + 404 => "404 Not Found", + 500 => "500 Internal Server Error", + } + + protected + + def get_handler + return ErubisHandler.new() + end + + def handle_request(env) + handler = get_handler() + output = handler.handle(env) + cont_type = "text/html" + cont_type << ";charset=#{handler.encoding}" if handler.encoding + return [200, [["Content-Type", cont_type]], [output]] + end + + def handle_http_error(ex) + output = "

#{h(RESPONSE_STATUS[ex.status])}

\n

#{h(ex.message)}

\n" + return [ex.status, [["Content-Type", "text/html"]], [output]] + end + + def handle_error(ex) + arr = ex.backtrace + output = "" + output << "
\n"
+    output <<   "#{h(arr[0])}:
#{h(ex.message)} (#{h(ex.class.name)})
\n" + arr[1..-1].each do |item| + output << " from #{h(item)}\n" + end + output << "
\n" + return [500, [["Content-Type", "text/html"]], [output]] + end + + public + + def call(env) + begin + return handle_request(env) + rescue HttpError => ex + return handle_http_error(ex) + rescue => ex + return handle_error(ex) + end + end + + def run(env=ENV, stdout=$stdout) + status, headers, output_arr = call(env) + stdout << "Status: #{RESPONSE_STATUS[status]}\r\n" unless status == 200 + headers.each {|k, v| stdout << "#{k}: #{v}\r\n" } + stdout << "\r\n" + output_arr.each {|str| stdout << str } + end + +end + + +if __FILE__ == $0 + ErubisApplication.new.run() +end -- cgit v1.2.1