Class: Servel::App
- Inherits:
-
Object
- Object
- Servel::App
- Defined in:
- lib/servel/app.rb
Constant Summary collapse
- UTF_8 =
Encoding::UTF_8
- SCRIPT_NAME =
Rack::SCRIPT_NAME
- PATH_INFO =
Rack::PATH_INFO
Instance Method Summary collapse
- #call(env) ⇒ Object
- #clean_url_path(path) ⇒ Object
-
#initialize(root) ⇒ App
constructor
A new instance of App.
- #redirect(location) ⇒ Object
- #try_encode(string) ⇒ Object
Constructor Details
#initialize(root) ⇒ App
Returns a new instance of App.
6 7 8 9 |
# File 'lib/servel/app.rb', line 6 def initialize(root) @root = Pathname.new(root) @file_server = Rack::File.new(root.to_s) end |
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/servel/app.rb', line 11 def call(env) env[SCRIPT_NAME] = try_encode(env[SCRIPT_NAME]) env[PATH_INFO] = try_encode(env[PATH_INFO]) url_root = env[SCRIPT_NAME] url_path = clean_url_path(env[PATH_INFO]) return redirect("#{url_root}/") if env[PATH_INFO] == "" fs_path = @root + url_path[1..-1] return @file_server.call(env) if fs_path.file? return redirect("#{url_root}#{url_path}/") unless env[PATH_INFO].end_with?("/") return [404, {}, []] unless fs_path.exist? Servel::Index.new(url_root: url_root, url_path: url_path, fs_path: fs_path).render end |
#clean_url_path(path) ⇒ Object
35 36 37 38 39 |
# File 'lib/servel/app.rb', line 35 def clean_url_path(path) url_path = Rack::Utils.unescape_path(path) raise unless Rack::Utils.valid_path?(url_path) Rack::Utils.clean_path_info(url_path) end |
#redirect(location) ⇒ Object
31 32 33 |
# File 'lib/servel/app.rb', line 31 def redirect(location) [302, { "Location" => Rack::Utils.escape_path(location) }, []] end |
#try_encode(string) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/servel/app.rb', line 41 def try_encode(string) return string if string.encoding == UTF_8 string.encode(UTF_8) rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError string end |