# This file is part of NIT ( http://www.nitlanguage.org ). # # Copyright 2013 Jean-Philippe Caissy # Copyright 2014 Alexis Laferrière # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Provides the `FileServer` action, which is a standard and minimal file server module file_server import reactor import sessions import media_types import http_errors redef class String # Returns a `String` copy of `self` without any of the prefixed '/'s # # Examples: # # assert "/home/".strip_start_slashes == "home/" # assert "////home/".strip_start_slashes == "home/" # assert "../home/".strip_start_slashes == "../home/" fun strip_start_slashes: String do for i in chars.length.times do if chars[i] != '/' then return substring_from(i) return "" end end # A simple file server class FileServer super Action # Root folder of `self` file system var root: String init do var root = self.root # Simplify the root path as each file requested will also be simplified root = root.simplify_path # Make sure the root ends with '/', this makes a difference in the security # check on each file access. root = root + "/" self.root = root end # Error page template for a given `code` fun error_page(code: Int): Writable do return new ErrorTemplate(code) # Header of each directory page var header: nullable Writable = null is writable # Custom JavaScript code added within a ` {{{title}}} {{{header_code}}}

{{{title}}}

  • {{{links.join("
  • \n\t\t\t
  • ")}}}
""" response.header["Content-Type"] = media_types["html"].as(not null) return response end end