module HTTP module Static MIME_TYPES = { :html => 'text/html'.freeze, :css => 'text/css'.freeze, :js => 'text/javascript'.freeze, :gif => 'image/gif'.freeze, :jpg => 'image/jpeg'.freeze, :jpeg => 'image/jpeg'.freeze, :png => 'image/png'.freeze, :ico => 'image/x-icon'.freeze } MIME_TYPES.default = 'text/plain'.freeze CACHE_AGES = {} CACHE_AGES.default = 86400 # one day INVALID_PATH_RE = /\.\./.freeze # Serves a static file or directory. def serve_static(fn) if fn =~ INVALID_PATH_RE raise MalformedRequestError, "Invalid path specified (#{@uri})" elsif !File.exists?(fn) raise FileNotFoundError, "File not found (#{@uri})" end if File.directory?(fn) send_directory_representation(fn) else send_file_representation(fn) end end # Sends a file representation, setting caching-related headers. def send_file_representation(fn) ext = File.extension(fn) expires = Time.now + CACHE_AGES[ext] cache :etag => File.etag(fn), :expires => expires, :last_modified => File.mtime(fn) do send_file(STATUS_OK, MIME_TYPES[ext], fn) end end DIR_TEMPLATE = '