FileSystem
struct FileSystem
Implementions
Methods
new
pub fn new(path: string, show_files_listing: bool = false, index_file: string? = nil, redirect_to_slash: bool = false, fallback_to_index: bool = false, prefer_utf8: bool = true): FileSystem
[src]
pub fn new(
path: string,
show_files_listing: bool = false,
index_file: string? = nil,
redirect_to_slash: bool = false,
fallback_to_index: bool = false,
prefer_utf8: bool = true
): FileSystem {
let engine = TemplateEngine.new();
try! engine
.add(
"list",
`
<html>
<head>
<title>Index of {{ req_path }}</title>
</head>
<body>
<h1>Index of {{ req_path }}</h1>
<ul>
{% for entry in entries %}
<li>
{% if entry.is_dir %}
<a href="{{ entry.url }}">{{ entry.filename }}/</a>
{% else %}
<a href="{{ entry.url }}">{{ entry.filename }}</a>
{% endif %}
</li>
{% endfor %}
</ul>
</body>
</html>
`
);
return FileSystem {
path,
show_files_listing,
index_file,
fallback_to_index,
redirect_to_slash,
prefer_utf8,
engine,
};
}
new
pub fn new(path: string, show_files_listing: bool = false, index_file: string? = nil, redirect_to_slash: bool = false, fallback_to_index: bool = false, prefer_utf8: bool = true): FileSystem
[src]
pub fn new(
path: string,
show_files_listing: bool = false,
index_file: string? = nil,
redirect_to_slash: bool = false,
fallback_to_index: bool = false,
prefer_utf8: bool = true
): FileSystem {
let engine = TemplateEngine.new();
try! engine
.add(
"list",
`
<html>
<head>
<title>Index of {{ req_path }}</title>
</head>
<body>
<h1>Index of {{ req_path }}</h1>
<ul>
{% for entry in entries %}
<li>
{% if entry.is_dir %}
<a href="{{ entry.url }}">{{ entry.filename }}/</a>
{% else %}
<a href="{{ entry.url }}">{{ entry.filename }}</a>
{% endif %}
</li>
{% endfor %}
</ul>
</body>
</html>
`
);
return FileSystem {
path,
show_files_listing,
index_file,
fallback_to_index,
redirect_to_slash,
prefer_utf8,
engine,
};
}
[src]
pub fn handle(self, request: Request): Response throws {
if (request.method() != Method.Get) {
return Response.new(MethodNotAllowed);
}
let req_path = path_unescape(request.uri().path().strip_prefix("/").strip_suffix("/"));
let file_path: string? = self.path;
for (let segment in path.segments(req_path)) {
if (segment == ".") {
continue;
} else if (segment == "..") {
file_path = file_path.and_then(|file_path| path.parent(file_path));
} else {
file_path = file_path.and_then(|file_path| path.join(file_path, segment));
}
}
let file_path = file_path else {
return Response.new(NotFound);
};
if (!file_path.starts_with(self.path)) {
return Response.new(Forbidden);
}
if (!path.exists(file_path)) {
if (self.fallback_to_index) {
if (let index_file = self.index_file) {
let index_path = path.join(self.path, index_file);
if (path.is_file(index_path)) {
return try create_file_response(request, index_path, self.prefer_utf8);
}
}
}
return Response.new(NotFound);
}
if (path.is_file(file_path)) {
return try create_file_response(request, file_path, self.prefer_utf8);
} else {
if (self.redirect_to_slash && request.uri().path().ends_with("/") && (self
.index_file
.is_some() || self.show_files_listing)) {
let redirect_to = `${request.uri().path()}/`;
return Response.new(Found).set_header("location", redirect_to);
}
if (let index_file = self.index_file) {
let index_path = path.join(file_path, index_file);
if (path.is_file(index_path)) {
return try create_file_response(request, index_path, self.prefer_utf8);
}
}
if (self.show_files_listing) {
let entries = try fs.read_dir(file_path);
let items: [FileListEntry] = [];
for (let entry in entries) {
if (let filename = path.file_name(entry)) {
let base = request.uri().path();
if (!base.ends_with("/")) {
base += "/";
}
items
.push(FileListEntry {
filename,
url: `${base}${path_escape(filename)}`,
is_dir: path.is_dir(entry),
});
}
}
return Response
.html(try! self.engine.render("list", TemplateContent { req_path, entries: items }));
}
return Response.new(NotFound);
}
}
[src]
pub fn handle(self, request: Request): Response throws {
if (request.method() != Method.Get) {
return Response.new(MethodNotAllowed);
}
let req_path = path_unescape(request.uri().path().strip_prefix("/").strip_suffix("/"));
let file_path: string? = self.path;
for (let segment in path.segments(req_path)) {
if (segment == ".") {
continue;
} else if (segment == "..") {
file_path = file_path.and_then(|file_path| path.parent(file_path));
} else {
file_path = file_path.and_then(|file_path| path.join(file_path, segment));
}
}
let file_path = file_path else {
return Response.new(NotFound);
};
if (!file_path.starts_with(self.path)) {
return Response.new(Forbidden);
}
if (!path.exists(file_path)) {
if (self.fallback_to_index) {
if (let index_file = self.index_file) {
let index_path = path.join(self.path, index_file);
if (path.is_file(index_path)) {
return try create_file_response(request, index_path, self.prefer_utf8);
}
}
}
return Response.new(NotFound);
}
if (path.is_file(file_path)) {
return try create_file_response(request, file_path, self.prefer_utf8);
} else {
if (self.redirect_to_slash && request.uri().path().ends_with("/") && (self
.index_file
.is_some() || self.show_files_listing)) {
let redirect_to = `${request.uri().path()}/`;
return Response.new(Found).set_header("location", redirect_to);
}
if (let index_file = self.index_file) {
let index_path = path.join(file_path, index_file);
if (path.is_file(index_path)) {
return try create_file_response(request, index_path, self.prefer_utf8);
}
}
if (self.show_files_listing) {
let entries = try fs.read_dir(file_path);
let items: [FileListEntry] = [];
for (let entry in entries) {
if (let filename = path.file_name(entry)) {
let base = request.uri().path();
if (!base.ends_with("/")) {
base += "/";
}
items
.push(FileListEntry {
filename,
url: `${base}${path_escape(filename)}`,
is_dir: path.is_dir(entry),
});
}
}
return Response
.html(try! self.engine.render("list", TemplateContent { req_path, entries: items }));
}
return Response.new(NotFound);
}
}