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 folder of `self` file system
52 # Simplify the root path as each file requested will also be simplified
53 root
= root
.simplify_path
55 # Make sure the root ends with '/', this makes a difference in the security
56 # check on each file access.
62 # Error page template for a given `code`
63 fun error_page
(code
: Int): Writable do return new ErrorTemplate(code
)
65 # Header of each directory page
66 var header
: nullable Writable = null is writable
68 # Custom JavaScript code added within a `<script>` block to each page
69 var javascript_header
: nullable Writable = null is writable
71 # Caching attributes of served files, used as the `cache-control` field in response headers
72 var cache_control
= "public, max-age=360" is writable
74 # Show directory listing?
75 var show_directory_listing
= true is writable
77 redef fun answer
(request
, turi
)
81 var local_file
= root
.join_path
(turi
.strip_start_slashes
)
82 local_file
= local_file
.simplify_path
86 # This make sure that the requested file is within the root folder.
87 if (local_file
+ "/").has_prefix
(root
) then
89 if local_file
.file_exists
then
90 if local_file
.file_stat
.is_dir
then
91 # If we target a directory without an ending `/`,
92 # redirect to the directory ending with `/`.
93 if not request
.uri
.is_empty
and
94 request
.uri
.chars
.last
!= '/' then
95 response
= new HttpResponse(303)
96 response
.header
["Location"] = request
.uri
+ "/"
100 # Show index file instead of the directory listing
101 # only if `index.html` or `index.htm` is available
102 var index_file
= local_file
.join_path
("index.html")
103 if index_file
.file_exists
then
104 local_file
= index_file
106 index_file
= local_file
.join_path
("index.htm")
107 if index_file
.file_exists
then local_file
= index_file
111 var is_dir
= local_file
.file_stat
.is_dir
112 if show_directory_listing
and is_dir
then
113 # Show the directory listing
115 var files
= local_file
.files
117 alpha_comparator
.sort files
119 var links
= new Array[String]
120 if turi
.length
> 1 then
121 var path
= (request
.uri
+ "/..").simplify_path
122 links
.add
"<a href=\"{path}/\
">..</a>"
125 var local_path
= local_file
.join_path
(file
).simplify_path
126 var web_path
= file
.simplify_path
127 if local_path
.file_stat
.is_dir
then web_path
= web_path
+ "/"
128 links
.add
"<a href=\"{web_path}\
">{file}</a>"
131 var header
= self.header
133 if header
!= null then
134 header_code
= header
.write_to_string
135 else header_code
= ""
137 response
= new HttpResponse(200)
141 <meta charset="utf-8">
142 <meta http-equiv="X-UA-Compatible" content="IE=edge">
143 <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
145 {{{javascript_header or else ""}}}
147 <title>{{{title}}}</title>
151 <div class="container">
154 <li>{{{links.join("</li>\n\t\t\t<li>")}}}</li>
160 response
.header
["Content-Type"] = media_types
["html"].as(not null)
161 else if not is_dir
then # It's a single file
162 response
= answer_file
(local_file
)
163 else response
= new HttpResponse(404)
164 else response
= new HttpResponse(404)
165 else response
= new HttpResponse(403)
167 if response
.status_code
!= 200 then
168 var tmpl
= error_page
(response
.status_code
)
169 if header
!= null and tmpl
isa ErrorTemplate then tmpl
.header
= header
170 response
.body
= tmpl
.to_s
176 # Build a reponse containing a single `local_file`.
178 # Returns a 404 error if local_file does not exists.
179 fun answer_file
(local_file
: String): HttpResponse do
180 if not local_file
.file_exists
then return new HttpResponse(404)
182 var response
= new HttpResponse(200)
183 response
.files
.add local_file
185 # Set Content-Type depending on the file extension
186 var ext
= local_file
.file_extension
188 var media_type
= media_types
[ext
]
189 if media_type
!= null then
190 response
.header
["Content-Type"] = media_type
191 else response
.header
["Content-Type"] = "application/octet-stream"
195 response
.header
["cache-control"] = cache_control