bin/httphere in httphere-1.0.0 vs bin/httphere in httphere-1.0.1

- old
+ new

@@ -532,23 +532,46 @@ end end require 'iconv' require 'rubygems' +module Renderers + autoload :Markdown, 'httphere/markdown' + autoload :Textile, 'httphere/textile' +end require 'UniversalDetector' require 'shared-mime-info' class FileServer < EventParsers::Http11Parser::Request # This is where the routing is processed. def process # Get the filename desired - filename = resource_uri.sub(/^\//,'') + filename, query = resource_uri.split('?',2) + filename = filename.sub(/^\//,'') + # Default to any file named index.* + filename = Dir["index.*"].first if filename.to_s == '' && Dir["index.*"].length > 0 + file_extension = (filename.match(/\.([^\.]+)$/) || [])[1] + if File.exists?(filename) && !File.directory?(filename) content_type = MIME.check(filename).type file_body = File.read(filename) + + # If .markdown, render as Markdown + if file_extension == 'markdown' + file_body = Renderers::Markdown.render_content(file_body) + content_type = 'text/html' + end + + # If .textile, render as Textile + if file_extension == 'textile' + file_body = Renderers::Textile.render_content(file_body) + content_type = 'text/html' + end + + # Send Response respond!('200 Ok', content_type, file_body) else - respond!('400 Not Found', 'text/plain', "Could not find file: '#{resource_uri}'") + respond!('404 Not Found', 'text/plain', "Could not find file: '#{resource_uri}'") end end def respond!(status, content_type, body) respond(status, content_type, body) @@ -569,10 +592,10 @@ span = (Time.now - connection.time).to_f content_type puts (status =~ /200/ ? "Served #{resource_uri} (#{content_type})" : "404 #{resource_uri}" - ) + " at #{1 / span} requests/second" + ) + " at #{1 / span} requests/second" unless status =~ /404/ && resource_uri == '/favicon.ico' end end # Requires that a module includes this and defines an initialize method that defines @options class Http11Server