chiark / gitweb /
Make a licensing decision: it's all AGPLv3+.
[odin-cgi] / mason / shorturl / dhandler
1 %# -*-html-*-
2 %#
3 %# URL shortener webservice for Odin
4 %#
5 %# (c) 2015 Mark Wooding
6 %#
7 %#----- Licensing notice ----------------------------------------------------
8 %#
9 %# This file is part of the `odin.gg' service, `odin-cgi'.
10 %#
11 %# `odin-cgi' is free software; you can redistribute it and/or modify
12 %# it under the terms of the GNU Affero General Public License as
13 %# published by the Free Software Foundation; either version 3 of the
14 %# License, or (at your option) any later version.
15 %#
16 %# `odin-cgi' is distributed in the hope that it will be useful,
17 %# but WITHOUT ANY WARRANTY; without even the implied warranty of
18 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 %# GNU Affero General Public License for more details.
20 %#
21 %# You should have received a copy of the GNU Affero General Public
22 %# License along with `odin-cgi'; if not, see
23 %# <http://www.gnu.org/licenses/>.
24 %#
25 <&| SELF:wrapper, title => "odin.gg URL Shortener" &>
26 <h1><tt>odin.gg</tt> URL Shortener</h1>
27 <form method="POST" accept-charset="UTF-8" enctype="multipart/form-data">
28   <label for="url" accesskey="u"><u>U</u>RL to shorten:</label>
29   <input id="url" name="u" value="<% $u // "" |h %>">
30   <button type="submit">Go</button>
31 </form>
32 % if (defined $tag) {
33 <p>Shortened to: <a href="<% "$Odin::SHORTURL/$tag" %>"><%
34         "$Odin::SHORTURL/$tag" %></a>
35 % }
36 </&>
37 %#
38 <%init>
39         my $tag = $m->dhandler_arg;
40         if (length $tag) {
41           my $url = Odin::get_shorturl $tag;
42           if ($q) { $m->comp(".query", url => $url); }
43           else { $m->redirect($url, 301); }
44           return;
45         } elsif (defined $u) {
46           $tag = Odin::new_shorturl $u;
47         } else {
48           Odin::path_info($r) =~ m:/$:
49             or $m->redirect("$Odin::SHORTURL/", 301);
50           $tag = undef;
51         }
52 </%init>
53 %#
54 <%args>
55         $q => undef
56         $u => undef
57 </%args>
58 %#
59 <%def .query>\
60 % $r->content_type("text/plain; charset=utf8");
61 <% $url %>
62 <%args>
63         $url
64 </%args>
65 </%def>
66 %#
67 <%def .notfound>\
68 <&| SELF:error, title => "not found", status => 404 &>\
69 tag &lsquo;<% $tag |h %>&rsquo; not found
70 </&>
71 <%args>
72         $tag
73 </%args>
74 </%def>
75 %#
76 <%def .badurl>\
77 <&| SELF:error, title => "invalid url", status => 404 &>\
78 &lsquo;<tt><% $u |h %></tt>&rsquo; is not a valid URL
79 </&>
80 <%args>
81         $u
82 </%args>
83 </%def>
84 %#
85 <%once>
86         use utf8;
87         use Odin;
88 </%once>