| 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::Web - serve Phoebe wiki pages via the web | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Phoebe doesn’t have to live behind another web server like Apache or nginx. It | ||
| 26 | can be a (simple) web server, too! | ||
| 27 | |||
| 28 | This package gives web visitors read-only access to Phoebe. HTML is served via | ||
| 29 | HTTPS on the same port as everything else, i.e. 1965 by default. | ||
| 30 | |||
| 31 | There is no configuration. Simply add it to your F<config> file: | ||
| 32 | |||
| 33 | use App::Phoebe::Web; | ||
| 34 | |||
| 35 | Beware: these days browser will refuse to connect to sites that have self-signed | ||
| 36 | certificates. You’ll have to click buttons and make exceptions and all of that, | ||
| 37 | or get your certificate from Let’s Encrypt or the like. That in turn is | ||
| 38 | aggravating for your Gemini visitors, since you are changing the certificate | ||
| 39 | every few months. | ||
| 40 | |||
| 41 | If you want to allow web visitors to comment on your pages, see | ||
| 42 | L<App::Phoebe::WebComments>; if you want to allow web visitors to edit pages, | ||
| 43 | see L<App::Phoebe::WebEdit>. | ||
| 44 | |||
| 45 | You can serve the wiki both on the standard Gemini port and on the standard | ||
| 46 | HTTPS port: | ||
| 47 | |||
| 48 | phoebe --port=443 --port=1965 | ||
| 49 | |||
| 50 | Note that 443 is a priviledge port. Thus, you either need to grant Perl the | ||
| 51 | permission to listen on a priviledged port, or you need to run Phoebe as a super | ||
| 52 | user. Both are potential security risk, but the first option is much less of a | ||
| 53 | problem, I think. | ||
| 54 | |||
| 55 | If you want to try this, run the following as root: | ||
| 56 | |||
| 57 | setcap 'cap_net_bind_service=+ep' $(which perl) | ||
| 58 | |||
| 59 | Verify it: | ||
| 60 | |||
| 61 | getcap $(which perl) | ||
| 62 | |||
| 63 | If you want to undo this: | ||
| 64 | |||
| 65 | setcap -r $(which perl) | ||
| 66 | |||
| 67 | If you don't do any of the above, you'll get a permission error on startup: | ||
| 68 | "Mojo::Reactor::Poll: Timer failed: Can't create listen socket: Permission | ||
| 69 | denied…" You could, of course, always use a traditional web server like Apache | ||
| 70 | as a front-end, proxying all requests to your site on port 443 to port 1965. | ||
| 71 | This server config also needs access to the same certificates that Phoebe is | ||
| 72 | using, for port 443. The example below doesn’t rewrite C</.well-known> URLs | ||
| 73 | because these are used by Let’s Encrypt and others. | ||
| 74 | |||
| 75 | <VirtualHost *:80> | ||
| 76 | ServerName transjovian.org | ||
| 77 | RewriteEngine on | ||
| 78 | # Do not redirect /.well-known URL | ||
| 79 | RewriteCond %{REQUEST_URI} !^/\.well-known/ | ||
| 80 | RewriteRule ^/(.*) https://%{HTTP_HOST}:1965/$1 | ||
| 81 | </VirtualHost> | ||
| 82 | <VirtualHost *:443> | ||
| 83 | ServerName transjovian.org | ||
| 84 | RewriteEngine on | ||
| 85 | # Do not redirect /.well-known URL | ||
| 86 | RewriteCond %{REQUEST_URI} !^/\.well-known/ | ||
| 87 | RewriteRule ^/(.*) https://%{HTTP_HOST}:1965/$1 | ||
| 88 | SSLEngine on | ||
| 89 | SSLCertificateFile /var/lib/dehydrated/certs/transjovian.org/cert.pem | ||
| 90 | SSLCertificateKeyFile /var/lib/dehydrated/certs/transjovian.org/privkey.pem | ||
| 91 | SSLCertificateChainFile /var/lib/dehydrated/certs/transjovian.org/chain.pem | ||
| 92 | SSLVerifyClient None | ||
| 93 | </VirtualHost> | ||
| 94 | |||
| 95 | Here’s an example where we wrap one the subroutines in App::Phoebe::Web in order | ||
| 96 | to change the default CSS that gets served. We keep a code reference to the | ||
| 97 | original, substitute our own, and when it gets called, it first calls the old | ||
| 98 | code to print some CSS, and then we append some CSS of our own. Also note how we | ||
| 99 | import C<$log>. | ||
| 100 | |||
| 101 | # tested by t/example-dark-mode.t | ||
| 102 | package App::Phoebe::DarkMode; | ||
| 103 | use App::Phoebe qw($log); | ||
| 104 | use App::Phoebe::Web; | ||
| 105 | no warnings qw(redefine); | ||
| 106 | |||
| 107 | # fully qualified because we're in a different package! | ||
| 108 | *old_serve_css_via_http = \&App::Phoebe::Web::serve_css_via_http; | ||
| 109 | *App::Phoebe::Web::serve_css_via_http = \&serve_css_via_http; | ||
| 110 | |||
| 111 | sub serve_css_via_http { | ||
| 112 | my $stream = shift; | ||
| 113 | old_serve_css_via_http($stream); | ||
| 114 | $log->info("Adding more CSS via HTTP (for dark mode)"); | ||
| 115 | $stream->write(<<'EOT'); | ||
| 116 | @media (prefers-color-scheme: dark) { | ||
| 117 | body { color: #eeeee8; background-color: #333333; } | ||
| 118 | a:link { color: #1e90ff } | ||
| 119 | a:hover { color: #63b8ff } | ||
| 120 | a:visited { color: #7a67ee } | ||
| 121 | } | ||
| 122 | EOT | ||
| 123 | } | ||
| 124 | |||
| 125 | 1; | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | package App::Phoebe::Web; | ||
| 130 | require Exporter; | ||
| 131 | our @ISA = qw(Exporter); | ||
| 132 | our @EXPORT_OK = qw(http_error handle_http_header); | ||
| 133 | use App::Phoebe qw(@request_handlers port space host_regex space_regex run_extensions text quote_html blog_pages | ||
| 134 | html_page to_html wiki_dir changes all_logs pages rss atom files $server $log @footer | ||
| 135 | space_links diff); | ||
| 136 | use File::Slurper qw(read_text read_binary); | ||
| 137 | use Encode qw(encode_utf8 decode_utf8); | ||
| 138 | use List::Util qw(min); | ||
| 139 | use Modern::Perl; | ||
| 140 | use URI::Escape; | ||
| 141 | use utf8; | ||
| 142 | |||
| 143 | unshift(@request_handlers, '^GET .* HTTP/1\.[01]$' => \&handle_http_header); | ||
| 144 | |||
| 145 | sub handle_http_header { | ||
| 146 | my $stream = shift; | ||
| 147 | my $data = shift; | ||
| 148 | # $log->debug("Reading HTTP headers"); | ||
| 149 | my @lines = split(/\r\n/, $data->{buffer}, -1); # including the empty line at the end | ||
| 150 | foreach (@lines) { | ||
| 151 | if (/^(\S+?): (.+?)\s*$/) { | ||
| 152 | my $key = lc($1); | ||
| 153 | $data->{headers}->{$key} = $2; | ||
| 154 | my $data->{header_size} += length($_); | ||
| 155 | # $log->debug("Header $key"); | ||
| 156 | } elsif ($_ eq "") { | ||
| 157 | $data->{buffer} =~ s/^.*?\r\n\r\n//s; # possibly HTTP body | ||
| 158 | # $log->debug("Handle HTTP request"); | ||
| 159 | $data->{headers}->{host} .= ":" . port($stream) if $data->{headers}->{host} and $data->{headers}->{host} !~ /:\d+$/; | ||
| 160 | $log->debug("HTTP headers: " . join(", ", map { "$_ => '$data->{headers}->{$_}'" } keys %{$data->{headers}})); | ||
| 161 | my $length = $data->{headers}->{'content-length'} || 0; | ||
| 162 | return http_error($stream, "Content length invalid") if $length !~ /^\d+$/; | ||
| 163 | return http_error($stream, "Content too long") if $length > $server->{wiki_page_size_limit}; | ||
| 164 | my $actual = length($data->{buffer}); | ||
| 165 | return http_error($stream, "Content longer than what the header says ($actual > $length):\n" . $data->{buffer}) if $actual > $length; | ||
| 166 | if ($length == $actual) { | ||
| 167 | # got the entire body as part of the first part | ||
| 168 | process_http($stream, $data->{request}, $data->{headers}, $data->{buffer}); | ||
| 169 | $stream->close_gracefully(); | ||
| 170 | return; | ||
| 171 | } elsif ($length) { | ||
| 172 | # read body if it was sent in multiple parts | ||
| 173 | $data->{handler} = \&handle_http_body; | ||
| 174 | handle_http_body($stream, $data); | ||
| 175 | return; | ||
| 176 | } | ||
| 177 | # otherwise wait for more header bytes | ||
| 178 | } | ||
| 179 | if ($data->{header_size} and $data->{header_size} > $server->{wiki_page_size_limit}) { | ||
| 180 | $log->debug("This wiki does not allow more than $server->{wiki_page_size_limit} bytes of headers"); | ||
| 181 | result($stream, "400", "Bad request: headers too long"); | ||
| 182 | $stream->close_gracefully(); | ||
| 183 | return; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | # if we came here, the last line didn't match and needs more bytes | ||
| 187 | $data->{buffer} = $lines[$#lines]; | ||
| 188 | $log->debug("Waiting for more HTTP headers ('$data->{buffer}')"); | ||
| 189 | return; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub http_error { | ||
| 193 | my $stream = shift; | ||
| 194 | my $message = shift; | ||
| 195 | $stream->write("HTTP/1.1 400 Bad Request\r\n"); | ||
| 196 | $stream->write("Content-Type: text/plain\r\n"); | ||
| 197 | $stream->write("\r\n"); | ||
| 198 | $stream->write("$message\n"); | ||
| 199 | $stream->close_gracefully(); | ||
| 200 | return 0; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub handle_http_body { | ||
| 204 | my $stream = shift; | ||
| 205 | my $data = shift; | ||
| 206 | $log->debug("Reading HTTP body"); | ||
| 207 | my $length = $data->{headers}->{'content-length'} || 0; | ||
| 208 | my $actual = length($data->{buffer}); | ||
| 209 | if ($length == $actual) { | ||
| 210 | # got the entire body | ||
| 211 | process_http($stream, $data->{request}, $data->{headers}, $data->{buffer}); | ||
| 212 | $stream->close_gracefully(); | ||
| 213 | return; | ||
| 214 | } | ||
| 215 | $log->debug("Received $actual/$length bytes"); | ||
| 216 | } | ||
| 217 | |||
| 218 | sub process_http { | ||
| 219 | my $stream = shift; | ||
| 220 | my $request = shift; | ||
| 221 | my $headers = shift; | ||
| 222 | my $buffer = shift; | ||
| 223 | eval { | ||
| 224 | local $SIG{'ALRM'} = sub { | ||
| 225 | $log->error("Timeout processing $request"); | ||
| 226 | }; | ||
| 227 | alarm(10); # timeout | ||
| 228 | my $hosts = host_regex(); | ||
| 229 | my $port = port($stream); | ||
| 230 | my $spaces = space_regex(); | ||
| 231 | $log->info("Looking at $request"); | ||
| 232 | my ($host, $space, $id, $n, $filter); | ||
| 233 | if (run_extensions($stream, $request, $headers, $buffer)) { | ||
| 234 | # config file goes first | ||
| 235 | } elsif ($request =~ m!^GET /default.css HTTP/1\.[01]$! | ||
| 236 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 237 | serve_css_via_http($stream, $host); | ||
| 238 | } elsif (($space) = $request =~ m!^GET (?:(?:/($spaces)/?)?|/) HTTP/1\.[01]$! | ||
| 239 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 240 | serve_main_menu_via_http($stream, $host, space($stream, $host, $space)); | ||
| 241 | } elsif (($space, $id, $n) = $request =~ m!^GET (?:/($spaces))?/page/([^/]*)(?:/(\d+))? HTTP/1\.[01]$! | ||
| 242 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 243 | serve_page_via_http($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n); | ||
| 244 | } elsif (($space, $id, $n) = $request =~ m!^GET (?:/($spaces))?/file/([^/]*)(?:/(\d+))? HTTP/1\.[01]$! | ||
| 245 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 246 | serve_file_via_http($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n); | ||
| 247 | } elsif (($space, $id, $n) = $request =~ m!^GET (?:/($spaces))?/history/([^/]*)(?:/(\d+))? HTTP/1\.[01]$! | ||
| 248 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 249 | serve_history_via_http($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n||10); | ||
| 250 | } elsif (($space, $id, $n) = $request =~ m!^GET (?:/($spaces))?/diff/([^/]*)(?:/(\d+))? HTTP/1\.[01]$! | ||
| 251 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 252 | serve_diff_via_http($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n||10); | ||
| 253 | } elsif (($space, $id, $n) = $request =~ m!^GET (?:/($spaces))?/raw/([^/]*)(?:/(\d+))? HTTP/1\.[01]$! | ||
| 254 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 255 | serve_raw_via_http($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n); | ||
| 256 | } elsif ($request =~ m!^GET /robots.txt(?:[#?].*)? HTTP/1\.[01]$! | ||
| 257 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 258 | serve_raw_via_http($stream, $host, undef, 'robots'); | ||
| 259 | } elsif (($space, $id, $n) = $request =~ m!^GET (?:/($spaces))?/do/changes(?:/(\d+))? HTTP/1\.[01]$! | ||
| 260 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 261 | serve_changes_via_http($stream, $host, space($stream, $host, $space), $n||100); | ||
| 262 | } elsif (($filter, $n) = $request =~ m!^GET /do/all(?:/(latest))?/changes(?:/(\d+))? HTTP/1\.[01]$! | ||
| 263 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 264 | serve_all_changes_via_http($stream, $host, $n||100, $filter||""); | ||
| 265 | } elsif (($space, $id, $n) = $request =~ m!^GET (?:/($spaces))?/do/index HTTP/1\.[01]$! | ||
| 266 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 267 | serve_index_via_http($stream, $host, space($stream, $host, $space)); | ||
| 268 | } elsif (($space, $id, $n) = $request =~ m!^GET (?:/($spaces))?/do/files HTTP/1\.[01]$! | ||
| 269 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 270 | serve_files_via_http($stream, $host, space($stream, $host, $space)); | ||
| 271 | } elsif (($space, $id, $n) = $request =~ m!^GET (?:/($spaces))?/do/spaces HTTP/1\.[01]$! | ||
| 272 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 273 | serve_spaces_via_http($stream, $host, $port); | ||
| 274 | } elsif (($space, $id, $n) = $request =~ m!^GET (?:/($spaces))?/do/rss HTTP/1\.[01]$! | ||
| 275 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 276 | serve_rss_via_http($stream, $host, space($stream, $host, $space)); | ||
| 277 | } elsif (($space, $id, $n) = $request =~ m!^GET (?:/($spaces))?/do/atom HTTP/1\.[01]$! | ||
| 278 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 279 | serve_atom_via_http($stream, $host, space($stream, $host, $space)); | ||
| 280 | } elsif (($space, $n) = $request =~ m!^GET /do/all/atom HTTP/1\.[01]$! | ||
| 281 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 282 | serve_all_atom_via_http($stream, $host); | ||
| 283 | } else { | ||
| 284 | $log->debug("No http handler for $request"); | ||
| 285 | http_error($stream, "Don't know how to handle $request"); | ||
| 286 | } | ||
| 287 | $log->debug("Done"); | ||
| 288 | }; | ||
| 289 | $log->error("Error: $@") if $@; | ||
| 290 | alarm(0); | ||
| 291 | $stream->close_gracefully(); | ||
| 292 | } | ||
| 293 | |||
| 294 | sub serve_main_menu_via_http { | ||
| 295 | my $stream = shift; | ||
| 296 | my $host = shift; | ||
| 297 | my $space = shift; | ||
| 298 | $log->info("Serving main menu via HTTP"); | ||
| 299 | my $page = $server->{wiki_main_page}; | ||
| 300 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 301 | $stream->write("Content-Type: text/html\r\n"); | ||
| 302 | $stream->write("\r\n"); | ||
| 303 | $stream->write("<!DOCTYPE html>\n"); | ||
| 304 | $stream->write("<html>\n"); | ||
| 305 | $stream->write("<head>\n"); | ||
| 306 | $stream->write("<meta charset=\"utf-8\">\n"); | ||
| 307 | if ($page) { | ||
| 308 | $stream->write(encode_utf8 "<title>" . quote_html($page) . "</title>\n"); | ||
| 309 | } else { | ||
| 310 | $stream->write("<title>Phoebe</title>\n"); | ||
| 311 | } | ||
| 312 | $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n"); | ||
| 313 | $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"); | ||
| 314 | $stream->write("</head>\n"); | ||
| 315 | $stream->write("<body>\n"); | ||
| 316 | if ($page) { | ||
| 317 | $stream->write(encode_utf8 to_html(text($stream, $host, $space, $page)) . "\n"); | ||
| 318 | } else { | ||
| 319 | $stream->write("<h1>Welcome to Phoebe!</h1>\n"); | ||
| 320 | } | ||
| 321 | blog_html($stream, $host, $space); | ||
| 322 | $stream->write("<p>Important links:\n"); | ||
| 323 | $stream->write("<ul>\n"); | ||
| 324 | my @pages = @{$server->{wiki_page}}; | ||
| 325 | for my $id (@pages) { | ||
| 326 | $stream->write(encode_utf8 "<li>" . link_html($stream, $host, $space, $id) . "\n"); | ||
| 327 | } | ||
| 328 | $stream->write(encode_utf8 "<li>" . link_html($stream, $host, $space, "Changes", "do/changes") . "\n"); | ||
| 329 | $stream->write(encode_utf8 "<li>" . link_html($stream, $host, $space, "Index of all pages", "do/index") . "\n"); | ||
| 330 | $stream->write(encode_utf8 "<li>" . link_html($stream, $host, $space, "Index of all files", "do/files") . "\n") | ||
| 331 | if @{$server->{wiki_mime_type}}; | ||
| 332 | $stream->write(encode_utf8 "<li>" . link_html($stream, $host, undef, "Index of all spaces", "do/spaces") . "\n") | ||
| 333 | if @{$server->{wiki_space}} or keys %{$server->{host}} > 1; | ||
| 334 | # a requirement of the GNU Affero General Public License | ||
| 335 | $stream->write("<li><a href=\"https://metacpan.org/pod/App::Phoebe\">Source</a>\n"); | ||
| 336 | $stream->write("</ul>\n"); | ||
| 337 | $stream->write("</body>\n"); | ||
| 338 | $stream->write("</html>\n"); | ||
| 339 | } | ||
| 340 | |||
| 341 | sub link_html { | ||
| 342 | my $stream = shift; | ||
| 343 | my $host = shift; | ||
| 344 | my $space = shift; | ||
| 345 | my $title = shift; | ||
| 346 | my $id = shift; | ||
| 347 | if (not $id) { | ||
| 348 | $id = "page/$title"; | ||
| 349 | } | ||
| 350 | my $port = port($stream); | ||
| 351 | # don't encode the slash | ||
| 352 | return "<a href=\"https://$host:$port/" | ||
| 353 | . ($space && $space ne $host ? uri_escape_utf8($space) . "/" : "") | ||
| 354 | . join("/", map { uri_escape_utf8($_) } split (/\//, $id)) | ||
| 355 | . "\">" | ||
| 356 | . quote_html($title) | ||
| 357 | . "</a>"; | ||
| 358 | } | ||
| 359 | |||
| 360 | sub blog_html { | ||
| 361 | my $stream = shift; | ||
| 362 | my $host = shift; | ||
| 363 | my $space = shift; | ||
| 364 | my $n = shift || 10; | ||
| 365 | my @blog = blog_pages($stream, $host, $space, $n); | ||
| 366 | return unless @blog; | ||
| 367 | $stream->write("<p>Blog:\n"); | ||
| 368 | $stream->write("<ul>\n"); | ||
| 369 | # we should check for pages marked for deletion! | ||
| 370 | for my $id (@blog[0 .. min($#blog, $n - 1)]) { | ||
| 371 | $stream->write(encode_utf8 "<li>" . link_html($stream, $host, $space, $id) . "\n"); | ||
| 372 | } | ||
| 373 | $stream->write("</ul>\n"); | ||
| 374 | } | ||
| 375 | |||
| 376 | sub serve_css_via_http { | ||
| 377 | my $stream = shift; | ||
| 378 | $log->info("Serving CSS via HTTP"); | ||
| 379 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 380 | $stream->write("Content-Type: text/css\r\n"); | ||
| 381 | $stream->write("Cache-Control: public, max-age=86400, immutable\r\n"); # 24h | ||
| 382 | $stream->write("\r\n"); | ||
| 383 | $stream->write("html { max-width: 70ch; padding: 2ch; margin: auto; color: #111; background: #ffe; }\n"); | ||
| 384 | $stream->write(".del { color: rgb(222,56,43); }\n"); # diff: deleted | ||
| 385 | $stream->write(".ins { color: rgb(57,181,74); }\n"); # diff: inserted | ||
| 386 | } | ||
| 387 | |||
| 388 | sub serve_index_via_http { | ||
| 389 | my $stream = shift; | ||
| 390 | my $host = shift; | ||
| 391 | my $space = shift; | ||
| 392 | $log->info("Serving index of all pages via HTTP"); | ||
| 393 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 394 | $stream->write("Content-Type: text/html\r\n"); | ||
| 395 | $stream->write("\r\n"); | ||
| 396 | $stream->write("<!DOCTYPE html>\n"); | ||
| 397 | $stream->write("<html>\n"); | ||
| 398 | $stream->write("<head>\n"); | ||
| 399 | $stream->write("<meta charset=\"utf-8\">\n"); | ||
| 400 | $stream->write("<title>All Pages</title>\n"); | ||
| 401 | $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n"); | ||
| 402 | $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"); | ||
| 403 | $stream->write("</head>\n"); | ||
| 404 | $stream->write("<body>\n"); | ||
| 405 | $stream->write("<h1>All Pages</h1>\n"); | ||
| 406 | my @pages = pages($stream, $host, $space); | ||
| 407 | if (@pages) { | ||
| 408 | $stream->write("<ul>\n"); | ||
| 409 | for my $id (@pages) { | ||
| 410 | $stream->write(encode_utf8 "<li>" . link_html($stream, $host, $space, $id) . "\n"); | ||
| 411 | } | ||
| 412 | $stream->write("</ul>\n"); | ||
| 413 | } else { | ||
| 414 | $stream->write("<p>The are no pages.\n"); | ||
| 415 | } | ||
| 416 | } | ||
| 417 | |||
| 418 | sub serve_files_via_http { | ||
| 419 | my $stream = shift; | ||
| 420 | my $host = shift; | ||
| 421 | my $space = shift; | ||
| 422 | $log->info("Serving all files via HTTP"); | ||
| 423 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 424 | $stream->write("Content-Type: text/html\r\n"); | ||
| 425 | $stream->write("\r\n"); | ||
| 426 | $stream->write("<!DOCTYPE html>\n"); | ||
| 427 | $stream->write("<html>\n"); | ||
| 428 | $stream->write("<head>\n"); | ||
| 429 | $stream->write("<meta charset=\"utf-8\">\n"); | ||
| 430 | $stream->write("<title>All Files</title>\n"); | ||
| 431 | $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n"); | ||
| 432 | $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"); | ||
| 433 | $stream->write("</head>\n"); | ||
| 434 | $stream->write("<body>\n"); | ||
| 435 | $stream->write("<h1>All Files</h1>\n"); | ||
| 436 | my @files = files($stream, $host, $space); | ||
| 437 | if (@files) { | ||
| 438 | $stream->write("<ul>\n"); | ||
| 439 | for my $id (sort @files) { | ||
| 440 | $stream->write(encode_utf8 "<li>" . link_html($stream, $host, $space, $id, "file/$id") . "\n"); | ||
| 441 | } | ||
| 442 | $stream->write("</ul>\n"); | ||
| 443 | } else { | ||
| 444 | $stream->write("<p>The are no files.\n"); | ||
| 445 | } | ||
| 446 | } | ||
| 447 | |||
| 448 | sub serve_spaces_via_http { | ||
| 449 | my $stream = shift; | ||
| 450 | my $host = shift; | ||
| 451 | my $port = shift; | ||
| 452 | $log->info("Serving all spaces via HTTP"); | ||
| 453 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 454 | $stream->write("Content-Type: text/html\r\n"); | ||
| 455 | $stream->write("\r\n"); | ||
| 456 | $stream->write("<!DOCTYPE html>\n"); | ||
| 457 | $stream->write("<html>\n"); | ||
| 458 | $stream->write("<head>\n"); | ||
| 459 | $stream->write("<meta charset=\"utf-8\">\n"); | ||
| 460 | $stream->write("<title>All Spaces</title>\n"); | ||
| 461 | $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n"); | ||
| 462 | $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"); | ||
| 463 | $stream->write("</head>\n"); | ||
| 464 | $stream->write("<body>\n"); | ||
| 465 | $stream->write("<h1>All Spaces</h1>\n"); | ||
| 466 | $stream->write("<ul>\n"); | ||
| 467 | my $spaces = space_links($stream, "https", $host, $port); | ||
| 468 | for my $url (sort keys %$spaces) { | ||
| 469 | $stream->write(encode_utf8 "<li><a href=\"$url\">$spaces->{$url}</a>\n"); | ||
| 470 | } | ||
| 471 | $stream->write("</ul>\n"); | ||
| 472 | } | ||
| 473 | |||
| 474 | sub serve_changes_via_http { | ||
| 475 | my $stream = shift; | ||
| 476 | my $host = shift; | ||
| 477 | my $space = shift; | ||
| 478 | my $n = shift; | ||
| 479 | $log->info("Serving $n changes via HTTP"); | ||
| 480 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 481 | $stream->write("Content-Type: text/html\r\n"); | ||
| 482 | $stream->write("\r\n"); | ||
| 483 | $stream->write("<!DOCTYPE html>\n"); | ||
| 484 | $stream->write("<html>\n"); | ||
| 485 | $stream->write("<head>\n"); | ||
| 486 | $stream->write("<meta charset=\"utf-8\">\n"); | ||
| 487 | $stream->write("<title>Changes</title>\n"); | ||
| 488 | $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n"); | ||
| 489 | $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"); | ||
| 490 | $stream->write("</head>\n"); | ||
| 491 | $stream->write("<body>\n"); | ||
| 492 | $stream->write("<h1>Changes</h1>\n"); | ||
| 493 | $stream->write("<ul>\n"); | ||
| 494 | $stream->write("<li>" . link_html($stream, $host, undef, "Changes for all spaces", "do/all/changes") . "\n") | ||
| 495 | if @{$server->{wiki_space}}; | ||
| 496 | $stream->write("<li>" . link_html($stream, $host, $space, "Atom feed", "do/atom") . "\n"); | ||
| 497 | $stream->write("<li>" . link_html($stream, $host, $space, "RSS feed", "do/rss") . "\n"); | ||
| 498 | $stream->write("</ul>\n"); | ||
| 499 | my $dir = wiki_dir($host, $space); | ||
| 500 | my $log = "$dir/changes.log"; | ||
| 501 | if (not -e $log) { | ||
| 502 | $stream->write("<p>No changes.\n"); | ||
| 503 | return; | ||
| 504 | } | ||
| 505 | $stream->write("<p>Showing up to $n changes.\n"); | ||
| 506 | my $fh = File::ReadBackwards->new($log); | ||
| 507 | my $more = changes($stream, | ||
| 508 | $n, | ||
| 509 | sub { $stream->write(encode_utf8 "<h2>" . shift . "</h2>\n") }, | ||
| 510 | sub { $stream->write("<p>" . shift . " by " . colourize_html($stream, shift) . "\n") }, | ||
| 511 | sub { | ||
| 512 | my ($host, $space, $title, $id) = @_; | ||
| 513 | $stream->write(encode_utf8 "<br> → " . link_html($stream, $host, $space, $title, $id) . "\n"); | ||
| 514 | }, | ||
| 515 | sub { $stream->write(encode_utf8 "<br> → $_[0]\n") }, | ||
| 516 | sub { | ||
| 517 | return unless $_ = decode_utf8($fh->readline); | ||
| 518 | chomp; | ||
| 519 | split(/\x1f/), $host, $space, 0 }); | ||
| 520 | return unless $more; | ||
| 521 | $stream->write("<p>" . link_html($stream, $host, $space, "More...", "do/changes/" . 10 * $n) . "\n"); | ||
| 522 | } | ||
| 523 | |||
| 524 | sub serve_all_changes_via_http { | ||
| 525 | my $stream = shift; | ||
| 526 | my $host = shift; | ||
| 527 | my $n = shift; | ||
| 528 | my $filter = shift; | ||
| 529 | $log->info($filter ? "Serving $n all $filter changes via HTTP" : "Serving $n all changes via HTTP"); | ||
| 530 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 531 | $stream->write("Content-Type: text/html\r\n"); | ||
| 532 | $stream->write("\r\n"); | ||
| 533 | $stream->write("<!DOCTYPE html>\n"); | ||
| 534 | $stream->write("<html>\n"); | ||
| 535 | $stream->write("<head>\n"); | ||
| 536 | $stream->write("<meta charset=\"utf-8\">\n"); | ||
| 537 | $stream->write("<title>Changes for all spaces</title>\n"); | ||
| 538 | $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n"); | ||
| 539 | $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"); | ||
| 540 | $stream->write("</head>\n"); | ||
| 541 | $stream->write("<body>\n"); | ||
| 542 | $stream->write("<h1>Changes for all spaces</h1>\n"); | ||
| 543 | $stream->write("<ul>\n"); | ||
| 544 | $stream->write("<li>" . link_html($stream, $host, undef, "Atom feed", "do/all/atom") . "\n"); | ||
| 545 | if ($filter) { $stream->write("<li>" . link_html($stream, $host, undef, "All changes", "do/all/changes/$n") . "\n") } | ||
| 546 | else { $stream->write("<li>" . link_html($stream, $host, undef, "Latest changes", "do/all/latest/changes/$n") . "\n") } | ||
| 547 | $stream->write("</ul>\n"); | ||
| 548 | my $log = all_logs($stream, $host, $n); | ||
| 549 | if (not @$log) { | ||
| 550 | $stream->write("<p>No changes.\n"); | ||
| 551 | return; | ||
| 552 | } | ||
| 553 | # taking the head of the @$log to get new log entries | ||
| 554 | $stream->write("<p>Showing up to $n $filter changes.\n"); | ||
| 555 | my $more = changes($stream, | ||
| 556 | $n, | ||
| 557 | sub { $stream->write("<h2>" . shift . "</h2>\n") }, | ||
| 558 | sub { $stream->write("<p>" . shift . " by " . colourize_html($stream, shift) . "\n") }, | ||
| 559 | sub { $stream->write(encode_utf8 "<br> → " . link_html($stream, @_) . "\n") }, | ||
| 560 | sub { $stream->write(encode_utf8 "<br> → $_[0]\n") }, | ||
| 561 | sub { @{shift(@$log) }, 1 if @$log }, | ||
| 562 | undef, | ||
| 563 | $filter); | ||
| 564 | return unless $more; | ||
| 565 | $stream->write("<p>" . link_html($stream, $host, undef, "More...", "do/all/changes/" . 10 * $n) . "\n"); | ||
| 566 | } | ||
| 567 | |||
| 568 | # https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit | ||
| 569 | sub colourize_html { | ||
| 570 | my $stream = shift; | ||
| 571 | my $code = shift; | ||
| 572 | my %rgb = ( | ||
| 573 | 0 => "0,0,0", | ||
| 574 | 1 => "222,56,43", | ||
| 575 | 2 => "57,181,74", | ||
| 576 | 3 => "255,199,6", | ||
| 577 | 4 => "0,111,184", | ||
| 578 | 5 => "118,38,113", | ||
| 579 | 6 => "44,181,233", | ||
| 580 | 7 => "204,204,204"); | ||
| 581 | $code = join("", map { | ||
| 582 | "<span style=\"color: rgb($rgb{$_}); background-color: rgb($rgb{$_})\">$_</span>"; | ||
| 583 | } split //, $code); | ||
| 584 | return $code; | ||
| 585 | } | ||
| 586 | |||
| 587 | sub serve_rss_via_http { | ||
| 588 | my $stream = shift; | ||
| 589 | my $host = shift; | ||
| 590 | my $space = shift; | ||
| 591 | $log->info("Serving RSS via HTTP"); | ||
| 592 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 593 | $stream->write("Content-Type: application/xml\r\n"); | ||
| 594 | $stream->write("\r\n"); | ||
| 595 | rss($stream, $host, $space, 'https'); | ||
| 596 | } | ||
| 597 | |||
| 598 | sub serve_atom_via_http { | ||
| 599 | my $stream = shift; | ||
| 600 | my $host = shift; | ||
| 601 | my $space = shift; | ||
| 602 | $log->info("Serving Atom via HTTP"); | ||
| 603 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 604 | $stream->write("Content-Type: application/xml\r\n"); | ||
| 605 | $stream->write("\r\n"); | ||
| 606 | my $dir = wiki_dir($host, $space); | ||
| 607 | my $log = "$dir/changes.log"; | ||
| 608 | my $fh = File::ReadBackwards->new($log); | ||
| 609 | atom($stream, sub { | ||
| 610 | return unless $_ = decode_utf8($fh->readline); | ||
| 611 | chomp; | ||
| 612 | split(/\x1f/), $host, $space, 0 | ||
| 613 | }, $host, $space, 'https'); | ||
| 614 | } | ||
| 615 | |||
| 616 | sub serve_all_atom_via_http { | ||
| 617 | my $stream = shift; | ||
| 618 | my $host = shift; | ||
| 619 | $log->info("Serving Atom via HTTP"); | ||
| 620 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 621 | $stream->write("Content-Type: application/xml\r\n"); | ||
| 622 | $stream->write("\r\n"); | ||
| 623 | my $log = all_logs($stream, $host, 30); | ||
| 624 | atom($stream, sub { @{shift(@$log) }, 1 if @$log }, $host, undef, 'https'); | ||
| 625 | } | ||
| 626 | |||
| 627 | sub serve_raw_via_http { | ||
| 628 | my $stream = shift; | ||
| 629 | my $host = shift; | ||
| 630 | my $space = shift; | ||
| 631 | my $id = shift; | ||
| 632 | my $revision = shift; | ||
| 633 | $log->info("Serving raw $id via HTTP"); | ||
| 634 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 635 | $stream->write("Content-Type: text/plain; charset=UTF-8\r\n"); | ||
| 636 | $stream->write("\r\n"); | ||
| 637 | $stream->write(encode_utf8 text($stream, $host, $space, $id, $revision)); | ||
| 638 | } | ||
| 639 | |||
| 640 | sub serve_diff_via_http { | ||
| 641 | my $stream = shift; | ||
| 642 | my $host = shift; | ||
| 643 | my $space = shift; | ||
| 644 | my $id = shift; | ||
| 645 | my $revision = shift; | ||
| 646 | $log->info("Serving the diff of $id via HTTP"); | ||
| 647 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 648 | $stream->write("Content-Type: text/html\r\n"); | ||
| 649 | $stream->write("\r\n"); | ||
| 650 | $stream->write("<!DOCTYPE html>\n"); | ||
| 651 | $stream->write("<html>\n"); | ||
| 652 | $stream->write("<head>\n"); | ||
| 653 | $stream->write("<meta charset=\"utf-8\">\n"); | ||
| 654 | $stream->write(encode_utf8 "<title>Differences for " . quote_html($id) . "</title>\n"); | ||
| 655 | $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n"); | ||
| 656 | $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"); | ||
| 657 | $stream->write("</head>\n"); | ||
| 658 | $stream->write("<body>\n"); | ||
| 659 | $stream->write(encode_utf8 "<h1>Differences for " . quote_html($id) . "</h1>\n"); | ||
| 660 | $stream->write("<p>Showing the differences between revision $revision and the current revision.\n"); | ||
| 661 | my $new = text($stream, $host, $space, $id); | ||
| 662 | my $old = text($stream, $host, $space, $id, $revision); | ||
| 663 | diff($old, $new, | ||
| 664 | sub { $stream->write(encode_utf8 "<p>$_\n") for @_ }, | ||
| 665 | sub { $stream->write(encode_utf8 "<p class=\"del\">" . join("<br>", map { $_||"⏎" } @_) . "\n") }, | ||
| 666 | sub { $stream->write(encode_utf8 "<p class=\"ins\">" . join("<br>", map { $_||"⏎" } @_) . "\n") }, | ||
| 667 | sub { "<strong>$_</strong>" }); | ||
| 668 | } | ||
| 669 | |||
| 670 | sub serve_page_via_http { | ||
| 671 | my $stream = shift; | ||
| 672 | my $host = shift; | ||
| 673 | my $space = shift; | ||
| 674 | my $id = shift; | ||
| 675 | my $revision = shift; | ||
| 676 | $log->info("Serving $id as HTML via HTTP"); | ||
| 677 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 678 | $stream->write("Content-Type: text/html\r\n"); | ||
| 679 | $stream->write("\r\n"); | ||
| 680 | html_page($stream, $host, $space, $id, $revision); | ||
| 681 | } | ||
| 682 | |||
| 683 | sub serve_history_via_http { | ||
| 684 | my $stream = shift; | ||
| 685 | my $host = shift; | ||
| 686 | my $space = shift; | ||
| 687 | my $id = shift; | ||
| 688 | my $n = shift; | ||
| 689 | $log->info("Serve history for $id via HTTP"); | ||
| 690 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 691 | $stream->write("Content-Type: text/html\r\n"); | ||
| 692 | $stream->write("\r\n"); | ||
| 693 | $stream->write("<!DOCTYPE html>\n"); | ||
| 694 | $stream->write("<html>\n"); | ||
| 695 | $stream->write("<head>\n"); | ||
| 696 | $stream->write("<meta charset=\"utf-8\">\n"); | ||
| 697 | $stream->write(encode_utf8 "<title>Page history for " . quote_html($id) . "</title>\n"); | ||
| 698 | $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n"); | ||
| 699 | $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"); | ||
| 700 | $stream->write("</head>\n"); | ||
| 701 | $stream->write("<body>\n"); | ||
| 702 | $stream->write(encode_utf8 "<h1>Page history for " . quote_html($id) . "</h1>\n"); | ||
| 703 | my $dir = wiki_dir($host, $space); | ||
| 704 | my $log = "$dir/changes.log"; | ||
| 705 | if (not -e $log) { | ||
| 706 | $stream->write("<p>No changes.\n"); | ||
| 707 | return; | ||
| 708 | } | ||
| 709 | $stream->write("<p>Showing up to $n changes.\n"); | ||
| 710 | my $fh = File::ReadBackwards->new($log); | ||
| 711 | my $first = 1; | ||
| 712 | my $more = changes($stream, | ||
| 713 | $n, | ||
| 714 | sub { $stream->write(encode_utf8 "<h2>" . shift . "</h2>\n") }, | ||
| 715 | sub { $stream->write(encode_utf8 "<p>" . shift . " by " . colourize_html($stream, shift) . "\n") }, | ||
| 716 | sub { | ||
| 717 | my ($host, $space, $title, $id) = @_; | ||
| 718 | $stream->write(encode_utf8 "<br> → " . link_html($stream, $host, $space, $title, $id) . "\n"); | ||
| 719 | }, | ||
| 720 | sub { "<br> → $_[0]" }, | ||
| 721 | sub { | ||
| 722 | READ: | ||
| 723 | return unless $_ = decode_utf8($fh->readline); | ||
| 724 | chomp; | ||
| 725 | my ($ts, $id_log, $revision, $code) = split(/\x1f/); | ||
| 726 | goto READ if $id_log ne $id; | ||
| 727 | $ts, $id_log, $revision, $code, $host, $space, 0 }); | ||
| 728 | return unless $more; | ||
| 729 | $stream->write("<p>" . link_html($stream, $host, $space, "More...", "history/" . uri_escape_utf8($id) . "/" . 10 * $n) . "\n"); | ||
| 730 | } | ||
| 731 | |||
| 732 | sub serve_file_via_http { | ||
| 733 | my $stream = shift; | ||
| 734 | my $host = shift; | ||
| 735 | my $space = shift; | ||
| 736 | my $id = shift; | ||
| 737 | my $revision = shift; | ||
| 738 | $log->info("Serve file $id"); | ||
| 739 | my $dir = wiki_dir($host, $space); | ||
| 740 | my $file = "$dir/file/$id"; | ||
| 741 | my $meta = "$dir/meta/$id"; | ||
| 742 | if (not -f $file) { | ||
| 743 | $stream->write("HTTP/1.1 404 Error\r\n"); | ||
| 744 | $stream->write("Content-Type: text/plain\r\n"); | ||
| 745 | $stream->write("\r\n"); | ||
| 746 | $stream->write("File not found\r\n"); | ||
| 747 | return; | ||
| 748 | } elsif (not -f $meta) { | ||
| 749 | $stream->write("HTTP/1.1 500 Error\r\n"); | ||
| 750 | $stream->write("Content-Type: text/plain\r\n"); | ||
| 751 | $stream->write("\r\n"); | ||
| 752 | $stream->write("Metadata not found\r\n"); | ||
| 753 | return; | ||
| 754 | } | ||
| 755 | my %meta = (map { split(/: /, $_, 2) } split /\n/, read_text $meta); | ||
| 756 | if (not $meta{'content-type'}) { | ||
| 757 | $stream->write("HTTP/1.1 500 Error\r\n"); | ||
| 758 | $stream->write("Content-Type: text/plain\r\n"); | ||
| 759 | $stream->write("\r\n"); | ||
| 760 | $stream->write("Metadata corrupt\r\n"); | ||
| 761 | return; | ||
| 762 | } | ||
| 763 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 764 | $stream->write("Content-Type: " . $meta{'content-type'} ."\r\n"); | ||
| 765 | $stream->write("\r\n"); | ||
| 766 | $stream->write(read_binary($file)); | ||
| 767 | } |