Phoebe/WebStaticFiles.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 134 CC 6 SCAR 30.2
CC
6
Cov
0%
static_web_routes
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 20.5
1 # -*- mode: perl -*-
2 # Copyright (C) 2017–2021 Alex Schroeder <alex@gnu.org>
3  
4 # This program is free software: you can redistribute it and/or modify it under
5 # the terms of the GNU Affero General Public License as published by the Free
6 # Software Foundation, either version 3 of the License, or (at your option) any
7 # later version.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU Affero General Public License along
15 # with this program. If not, see <https://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 App::Phoebe::WebStaticFiles - serve static files via the web
22  
23 =head1 DESCRIPTION
24  
25 Serving static files, via the web. This is an add-on to L<App::Phoebe::Web> and
26 L<App::Phoebe::StaticFiles>.
27  
28 Here is an example setup where we assume that the route contains an UTF-8
29 encoded characters, and the directory name used also contains UTF-8 encoded
30 characters.
31  
32 package App::Phoebe::StaticFiles;
33 use utf8;
34 our %routes = (
35 "zürich" => "/home/alex/Pictures/2020/Zürich",
36 "amaryllis" => "/home/alex/Pictures/2021/Amaryllis", );
37 use App::Phoebe::WebStaticFiles;
38  
39 The setup does not allow recursive traversal of the file system.
40  
41 You still need to add a link to C</do/static> somewhere in your wiki.
42  
43 =cut
44  
45 package App::Phoebe::WebStaticFiles;
46 use App::Phoebe qw(@extensions $log host_regex port);
47 use App::Phoebe::Web qw(http_error);
48 use App::Phoebe::StaticFiles qw(%routes mime_type);
49 use File::Slurper qw(read_text read_binary read_dir);
50 use Encode qw(encode_utf8 decode_utf8);
51 use Modern::Perl;
52 use URI::Escape;
53  
54 # add a code reference to the list of extensions
55 push(@extensions, \&static_web_routes);
56  
57 sub static_web_routes {
58 my ($stream, $request, $headers) = @_;
59 my $hosts = host_regex();
60 my $port = port($stream);
61 my ($host, $route, $file);
62 if ($request =~ m!^GET /do/static/? HTTP/1\.[01]$!
63 and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) {
64 $log->debug("Serving the list of static routes via the web");
65 $stream->write("HTTP/1.1 200 OK\r\n");
66 $stream->write("Content-Type: text/html\r\n");
67 $stream->write("\r\n");
68 $stream->write("<!DOCTYPE html>\n");
69 $stream->write("<html>\n");
70 $stream->write("<head>\n");
71 $stream->write("<meta charset=\"utf-8\">\n");
72 $stream->write("<title>All Directories</title>\n");
73 $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n");
74 $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n");
75 $stream->write("</head>\n");
76 $stream->write("<body>\n");
77 $stream->write("<h1>All Directories</h1>\n");
78 $stream->write("<ul>\n");
79 for my $route (sort keys %routes) {
80 $stream->write("<li><a href=\"/do/static/" . uri_escape_utf8($route) . "\">"
81 . encode_utf8($route) . "</a>\n");
82 }
83 $stream->write("</ul>\n");
84 $stream->write("</body>\n");
85 $stream->write("</html>\n");
86 return 1;
87 } elsif (($route) = $request =~ m!^GET /do/static/([^/]+)/? HTTP/1\.[01]$!
88 and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) {
89 my $route = decode_utf8(uri_unescape($route));
90 my $dir = $routes{$route};
91 if (not $dir) {
92 http_error($stream, "Unknown route: " . encode_utf8($route));
93 return 1;
94 }
95 $log->debug("Serving list of files at $route via the web, reading $dir");
96 $stream->write("HTTP/1.1 200 OK\r\n");
97 $stream->write("Content-Type: text/html\r\n");
98 $stream->write("\r\n");
99 $stream->write("<!DOCTYPE html>\n");
100 $stream->write("<html>\n");
101 $stream->write("<head>\n");
102 $stream->write("<meta charset=\"utf-8\">\n");
103 $stream->write("<title>Files</title>\n");
104 $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n");
105 $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n");
106 $stream->write("</head>\n");
107 $stream->write("<body>\n");
108 $stream->write("<h1>Files</h1>\n");
109 $stream->write("<ul>\n");
110 for my $file (sort map { decode_utf8($_) } grep !/^\./, read_dir($dir)) {
111 $stream->write(encode_utf8 "<li><a href=\"/do/static/" . uri_escape_utf8($route) . "/"
112 . uri_escape_utf8($file) . "\">"
113 . encode_utf8($file) . "</a>\n");
114 }
115 $stream->write("</ul>\n");
116 $stream->write("</body>\n");
117 $stream->write("</html>\n");
118 return 1;
119 } elsif (($route, $file) = $request =~ m!^GET /do/static/([^/]+)/([^.].*) HTTP/1\.[01]$!
120 and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) {
121 my $route = decode_utf8(uri_unescape($route));
122 my $file = decode_utf8(uri_unescape($file));
123 $log->debug("Serving $route/$file via the web");
124 my $dir = $routes{$route};
125 # no slashes in the file name!
126 if ($file =~ /\// or not $dir or not -f "$dir/$file") {
127 http_error($stream, "Unknown file: " . encode_utf8($file));
128 return 1;
129 }
130 my $mime = mime_type($file);
131 $stream->write("HTTP/1.1 200 OK\r\n");
132 $stream->write("Content-Type: $mime\r\n");
133 $stream->write("\r\n");
134 $stream->write(read_binary("$dir/$file"));
135 return 1;
136 }
137 return;
138 }