| 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::Favicon - serve a favicon via the web for Phoebe | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This adds an ominous looking Jupiter planet SVG icon as the favicon for the web | ||
| 26 | view of your site. | ||
| 27 | |||
| 28 | There is no configuration. Simply add it to your F<config> file: | ||
| 29 | |||
| 30 | App::Phoebe::Favicon | ||
| 31 | |||
| 32 | It would be nice if this code were to look for a F<favicon.jpg> or | ||
| 33 | F<favicon.svg> in the data directory and served that, only falling back to the | ||
| 34 | Jupiter planet SVG if no such file can be found. We could cache the content of | ||
| 35 | the file in the C<$server> hash reference… Well, if somebody writes it, it shall | ||
| 36 | be merged. 😃 | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | package App::Phoebe::Favicon; | ||
| 41 | use App::Phoebe qw(@extensions $log); | ||
| 42 | use App::Phoebe::Web; | ||
| 43 | use Modern::Perl; | ||
| 44 | |||
| 45 | push(@extensions, \&favicon); | ||
| 46 | |||
| 47 | sub favicon { | ||
| 48 | my $stream = shift; | ||
| 49 | my $request = shift; | ||
| 50 | if ($request =~ m!^GET /favicon.ico HTTP/1\.[01]$!) { | ||
| 51 | serve_favicon_via_http($stream); | ||
| 52 | return 1; | ||
| 53 | } | ||
| 54 | return 0; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub serve_favicon_via_http { | ||
| 58 | my $stream = shift; | ||
| 59 | $log->info("Serving favicon via HTTP"); | ||
| 60 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 61 | $stream->write("Content-Type: image/svg+xml\r\n"); | ||
| 62 | $stream->write("Cache-Control: public, max-age=86400, immutable\r\n"); # 24h | ||
| 63 | $stream->write("\r\n"); | ||
| 64 | $stream->write(<<'EOT'); | ||
| 65 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
| 66 | <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"> | ||
| 67 | <circle cx="50" cy="50" r="45" fill="white" stroke="black" stroke-width="5"/> | ||
| 68 | <line x1="12" y1="25" x2="88" y2="25" stroke="black" stroke-width="4"/> | ||
| 69 | <line x1="5" y1="45" x2="95" y2="45" stroke="black" stroke-width="7"/> | ||
| 70 | <line x1="5" y1="60" x2="95" y2="60" stroke="black" stroke-width="4"/> | ||
| 71 | <path d="M20,73 C30,65 40,63 60,70 C70,72 80,73 90,72 | ||
| 72 | L90,74 C80,75 70,74 60,76 C40,83 30,81 20,73" fill="black"/> | ||
| 73 | <ellipse cx="40" cy="73" rx="11.5" ry="4.5" fill="red"/> | ||
| 74 | <line x1="22" y1="85" x2="78" y2="85" stroke="black" stroke-width="3"/> | ||
| 75 | </svg> | ||
| 76 | EOT | ||
| 77 | } |