Permalink
Browse files

Added rhtml, small eruby-fcgi processor

git-svn-id: http://svn.hunch.se/rasmus/rhtml@43 98834e0e-571d-0410-bf09-ac0e1acfc769
  • Loading branch information...
0 parents commit c58f8063c3f57f2f0af6ce0be53c65566ceadcd4 rasmus committed Oct 16, 2007
Showing with 192 additions and 0 deletions.
  1. +26 −0 env.txt
  2. +84 −0 eruby.fcgi
  3. +1 −0 foo.rhtml
  4. +65 −0 lighttpd.conf
  5. +16 −0 test.rhtml
26 env.txt
@@ -0,0 +1,26 @@
+"DOCUMENT_ROOT"=>"/Users/rasmus/src/rhtml/",
+"FCGI_ROLE"=>"RESPONDER",
+"GATEWAY_INTERFACE"=>"CGI/1.1",
+"HTTP_ACCEPT"=>"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
+"HTTP_ACCEPT_CHARSET"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.7",
+"HTTP_ACCEPT_ENCODING"=>"gzip,deflate",
+"HTTP_ACCEPT_LANGUAGE"=>"en-US,en;q=0.9,sv;q=0.9,ja;q=0.8,fr;q=0.8,de;q=0.7,es;q=0.6,it;q=0.6,nl;q=0.5,nb;q=0.4,da;q=0.4,fi;q=0.3,pt;q=0.3,zh-Hans;q=0.2,zh-Hant;q=0.1,ko;q=0.1",
+"HTTP_CACHE_CONTROL"=>"max-age=0",
+"HTTP_CONNECTION"=>"keep-alive",
+"HTTP_HOST"=>"localhost:8080",
+"HTTP_KEEP_ALIVE"=>"300",
+"HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en; rv:1.8.1.6) Gecko/20070809 Camino/1.5.1",
+"PATH_INFO"=>"",
+"QUERY_STRING"=>"",
+"REDIRECT_STATUS"=>"200",
+"REMOTE_ADDR"=>"::ffff:127.0.0.1",
+"REMOTE_PORT"=>"51289",
+"REQUEST_METHOD"=>"GET"}
+"REQUEST_URI"=>"/test.rhtml",
+"SCRIPT_FILENAME"=>"/Users/rasmus/src/rhtml/test.rhtml",
+"SCRIPT_NAME"=>"/test.rhtml",
+"SERVER_ADDR"=>"::ffff:127.0.0.1",
+"SERVER_PORT"=>"8080",
+"SERVER_PROTOCOL"=>"HTTP/1.1",
+"SERVER_SOFTWARE"=>"lighttpd/1.4.18",
+"SERVER_NAME"=>"localhost:8080",
@@ -0,0 +1,84 @@
+#!/usr/bin/env ruby
+require 'eruby'
+require 'fcgi'
+require 'cgi'
+require 'logger'
+require 'benchmark'
+
+class String
+ def xml_safe
+ self.gsub(/&/, "&#38;").gsub(/\"/, "&#34;").gsub(/</, "&#60;").gsub(/>/, "&#62;")
+ end
+end
+
+class ERProcess
+ def initialize()
+ $log = Logger.new(STDERR)
+ $log.level = Logger::INFO
+ @compiler = ERuby::Compiler.new
+ end
+
+ def run()
+ _level_info = $log.level <= Logger::INFO
+ start_time = Time.now
+ request_count = 0.0
+ rs_time = start_time
+
+ FCGI.each {|request|
+ if _level_info then
+ rs_time = Time.now
+ request_count += 1
+ #$log.debug { 'Accepted %s:%d %s' % [
+ # request.env['REMOTE_ADDR'],
+ # request.env['REMOTE_PORT'],
+ # request.env['REQUEST_URI']]
+ # }
+ end
+
+ $stdout = request.out
+
+ print "Content-Type: text/html\r\n\r\n" # XXX
+
+ filename = request.env['SCRIPT_FILENAME']
+ file = open(filename)
+ begin
+ code = @compiler.compile_file(file)
+ eval(code, nil, filename)
+ rescue
+ self.on_error(request)
+ ensure
+ file.close
+ end
+
+ request.finish
+
+ if _level_info then
+ rs = Time.now.to_f-rs_time.to_f
+ $log.info { 'perform: %.1f r/s, load: %.1f r/s, processed: %.0f r, uptime: %.1f s' % [
+ 1.0/rs,
+ request_count/(Time.now.to_f-start_time.to_f),
+ request_count,
+ Time.now.to_f-start_time.to_f ]
+ }
+ end
+ }
+ end
+
+ def on_error(request)
+ print %q{<html>
+ <head><title>500 - Internal Server Error</title></head>
+ <body>
+ <h1>500 - Internal Server Error</h1>
+ <p>%s</p>
+ <ol style="list-style-type:none;">} % $!.message.xml_safe
+ $!.backtrace.each {|frame|
+ print "\n <li><tt>", frame.xml_safe, "</tt></li>"
+ }
+ print "\n </ol>\n</body>\n</html>\n"
+ $log.error { "%s. %s" % [$!.message, $!.backtrace.inspect ]}
+ end
+end
+
+
+ERProcess.new.run
+
@@ -0,0 +1 @@
+<% print "foo" %>
@@ -0,0 +1,65 @@
+# Basic setup
+server.port = 8080
+server.tag = "lighttpd"
+server.use-ipv6 = "enable"
+server.document-root = var.CWD + "/"
+server.errorlog = var.CWD + "/server.log"
+
+server.modules += ("mod_fastcgi")
+#fastcgi.debug = 1
+fastcgi.server = (
+ ".rhtml" => ((
+ "socket" => var.CWD + "/fcgi.sock",
+ "bin-path" => var.CWD + "/eruby.fcgi",
+ #"check-local" => "disable",
+ #"bin-environment" => ("PYTHONOPTIMIZE" => "YES"),
+ #"bin-copy-environment" => ("PATH", "SHELL", "USER"),
+ "min-procs" => 1,
+ "max-procs" => 1
+ )),
+)
+
+# For performance tweaking see:
+# http://www.debianhelp.co.uk/performancelighttpd.htm
+#server.max-fds = 2048
+#server.max-keep-alive-requests = 4
+#server.max-keep-alive-idle = 4
+
+# Server status
+#server.modules += ("mod_status")
+#status.status-url = "/server-status"
+#status.statistics-url = "/server-stats"
+#status.config-url = "/server-config"
+#status.enable-sort = "enable"
+
+# Documentation access
+#server.modules += ("mod_alias")
+#index-file.names = ("index.html")
+#alias.url = ( "/docs/" => var.CWD + "/doc/api/" )
+
+# Debug lighttpd
+#server.max-keep-alive-requests = 0
+#debug.log-request-header = "enable" # log all request headers.
+#debug.log-file-not-found = "enable" # log if a file wasn't found.
+#debug.log-condition-handling = "enable" # log conditionals handling for debugging.
+#debug.log-request-header-on-error = "enable" # log request header, but only when there is an error.
+#debug.log-request-handling = "enable" # log request handling inside lighttpd.
+#debug.log-state-handling = "enable" # log state handling inside lighttpd.
+#debug.log-response-header = "enable" # log the header we send out to the client.
+#debug.dump-unknown-headers = "enable" # log listing of internally unhandled HTTP-headers.
+
+# Some MIME-types
+mimetype.assign = (
+ ".css" => "text/css",
+ ".gif" => "image/gif",
+ ".htm" => "text/html",
+ ".html" => "text/html",
+ ".jpeg" => "image/jpeg",
+ ".jpg" => "image/jpeg",
+ ".js" => "text/javascript",
+ ".png" => "image/png",
+ ".swf" => "application/x-shockwave-flash",
+ ".txt" => "text/plain",
+ ".tar.gz" => "application/x-tgz",
+ ".gz" => "application/x-gzip"
+)
@@ -0,0 +1,16 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
+<html>
+<head>
+<title>eruby test</title>
+</head>
+<body>
+<h1>eruby test</h1>
+<p>
+<% print "hello world!" %>
+current time: <%= Time.now %>
+<%# this is a comment line. %>
+% print "this is a one liner.\n"
+% ERuby.import("foo.rhtml")
+</p>
+</body>
+</html>

0 comments on commit c58f806

Please sign in to comment.