1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Jean-Philippe Caissy <jpcaissy@piji.ca>
4 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # Provides the `FileServer` action, which is a standard and minimal file server
27 # Returns a `String` copy of `self` without any of the prefixed '/'s
31 # assert "/home/".strip_start_slashes == "home/"
32 # assert "////home/".strip_start_slashes == "home/"
33 # assert "../home/".strip_start_slashes == "../home/"
34 fun strip_start_slashes
: String
36 for i
in chars
.length
.times
do if chars
[i
] != '/' then return substring_from
(i
)
41 # A simple file server
45 # Root of `self` file system
48 # Error page template for a given `code`
49 fun error_page
(code
: Int): Streamable do return new ErrorTemplate(code
)
51 # Header of each directory page
52 var header
: nullable Streamable = null is writable
54 redef fun answer
(request
, turi
)
58 var local_file
= root
.join_path
(turi
.strip_start_slashes
)
59 local_file
= local_file
.simplify_path
62 if turi
== "/" then local_file
= root
65 if local_file
.has_prefix
(root
) then
67 if local_file
.file_exists
then
68 response
= new HttpResponse(200)
70 if local_file
.file_stat
.is_dir
then
71 # Show index.html instead of the directory listing
72 var index_file
= local_file
.join_path
("index.html")
73 if index_file
.file_exists
then
74 local_file
= index_file
76 index_file
= local_file
.join_path
("index.htm")
77 if index_file
.file_exists
then local_file
= index_file
81 if local_file
.file_stat
.is_dir
then
82 # Show the directory listing
84 var files
= local_file
.files
86 var links
= new Array[String]
87 if local_file
.length
> 1 then
88 # The extra / is a hack
89 var path
= "/" + (turi
+ "/..").simplify_path
90 links
.add
"<a href=\"{path}\
">..</a>"
93 var path
= (turi
+ "/" + file
).simplify_path
94 links
.add
"<a href=\"{path}\
">{file}</a>"
97 var header
= self.header
99 if header
!= null then
100 header_code
= header
.write_to_string
101 else header_code
= ""
106 <meta charset="utf-8">
107 <meta http-equiv="X-UA-Compatible" content="IE=edge">
108 <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
109 <title>{{{title}}}</title>
113 <div class="container">
116 <li>{{{links.join("</li>\n\t\t\t<li>")}}}</li>
122 response
.header
["Content-Type"] = media_types
["html"].as(not null)
125 var file
= new IFStream.open
(local_file
)
126 response
.body
= file
.read_all
128 var ext
= local_file
.file_extension
130 var media_type
= media_types
[ext
]
131 if media_type
!= null then
132 response
.header
["Content-Type"] = media_type
133 else response
.header
["Content-Type"] = "application/octet-stream"
139 else response
= new HttpResponse(404)
140 else response
= new HttpResponse(403)
142 if response
.status_code
!= 200 then
143 var tmpl
= error_page
(response
.status_code
)
144 if header
!= null and tmpl
isa ErrorTemplate then tmpl
.header
= header
145 response
.body
= tmpl
.to_s