Blob


1 {!
2 /*
3 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
24 #include <event.h>
25 #include <libgen.h>
26 #include <limits.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include "galileo.h"
32 #include "tmpl.h"
34 static int tp_navigation(struct template *);
36 !}
38 {{ define tp_head(struct template *tp, const char *lang,
39 const char *title) }}
40 {!
41 struct client *clt = tp->tp_arg;
42 struct proxy_config *pc = clt->clt_pc;
43 const char *stylesheet = pc ? pc->stylesheet : "";
44 const char *prfx = clt->clt_script_name;
45 const char *path = clt->clt_path_info;
46 !}
47 <!doctype html>
48 <html{{ if *lang != '\0' }}{{ " " }}lang="{{ lang }}"{{ end }}>
49 <head>
50 <meta name="viewport" content="initial-scale=1" />
52 {{ if *stylesheet == 0 }}
53 <link rel="stylesheet" href="{{ prfx }}galileo.css" />
54 {{ else }}
55 <link rel="stylesheet" href="{{ stylesheet|urlescape }}" />
56 {{ end }}
58 <title>
59 {{ if title }}
60 {{ title }}
61 {{ else if pc }}
62 gemini://{{ pc->proxy_name }}{{ path }}
63 {{ end }}
64 </title>
65 </head>
66 <body>
67 {{ if path }}
68 {{ render tp_navigation(tp) }}
69 {{ end }}
70 {{ end }}
72 {{ define tp_foot(struct template *tp) }}
73 {!
74 struct client *clt = tp->tp_arg;
75 struct proxy_config *pc = clt->clt_pc;
76 const char *path = clt->clt_path_info;
77 !}
78 {{ if pc }}
79 <footer>
80 <hr />
81 <dl>
82 <dt>Original URL:</dt>
83 <dd>
84 <a href="gemini://{{ pc->proxy_name | urlescape }}{{ path | urlescape }}">
85 gemini://{{ pc->proxy_name }}{{ path }}
86 </a>
87 </dd>
88 </dl>
89 </footer>
90 {{ end }}
91 </body>
92 </html>
93 {{ end }}
95 {{ define tp_navigation(struct template *tp) }}
96 {!
97 struct client *clt = tp->tp_arg;
98 const char *dname, *dsufx = "/", *prfx = clt->clt_script_name;
99 char *path = clt->clt_path_info;
100 char *tilde, *t, home[GEMINI_MAXLEN], up[GEMINI_MAXLEN];
101 char usr[64];
102 char c;
104 *home = '\0';
105 if ((tilde = strstr(path, "/~")) != NULL &&
106 (t = strchr(tilde + 1, '/')) != NULL) {
107 c = *++t;
108 *t = '\0';
109 (void) strlcpy(home, path + 1, sizeof(home));
110 *t = c;
112 c = *--t;
113 *t = '\0';
114 (void) strlcpy(usr, tilde + 2, sizeof(usr));
115 *t = c;
118 (void) strlcpy(up, path + 1, sizeof(up));
119 dname = dirname(up);
120 if (!strcmp(dname, ".")) {
121 dname = "";
122 dsufx = "";
124 !}
125 <header>
126 <nav>
127 <ul>
128 <li><a href="{{ prfx }}">/ Root</a></li>
130 <li><a href="{{ prfx }}{{ dname }}{{ dsufx }}">
131 ↑ Up
132 </a></li>
134 {{ if *home != '\0' }}
135 <li><a href="{{ prfx }}{{ home }}">
136 🏠 {{usr}}' home
137 </a></li>
138 {{ end }}
139 </ul>
140 </nav>
141 </header>
142 {{ end }}
144 {{ define tp_figure(struct template *tp, const char *url, const char *label) }}
145 <figure>
146 <a href="{{ url | urlescape }}">
147 <img src="{{ url | urlescape }}" />
148 </a>
149 <figcaption>
150 {{ label }}
151 </figcaption>
152 </figure>
153 {{ end }}
155 {{ define tp_pre_open(struct template *tp, const char *label) }}
156 <figure>
157 {{ if label && *label != '\0' }}
158 <figcaption>{{ label }}</figcaption>
159 {{ end }}
160 <pre>
161 {{ end }}
163 {{ define tp_pre_close(struct template *tp) }}
164 </pre>
165 </figure>
166 {{ end }}
168 {{ define tp_error(struct template *tp, int code, const char *reason) }}
169 {!
170 char scode[32];
171 int r;
173 r = snprintf(scode, sizeof(code), "%d", code);
174 if (r < 0 || (size_t)r >= sizeof(code))
175 return (0);
177 !}
178 {{ render tp_head(tp, "en", "Proxy error") }}
179 <main>
180 <h1>Proxy error</h1>
181 {{ if code != -1 }}
182 <p>Request failed with code: <code>{{ scode }}</code>.</p>
183 <p>The server says: {{ reason }}.</p>
184 {{ else }}
185 <p>Unable to serve the page due to: {{ reason }}.</p>
186 {{ end }}
187 </main>
188 {{ render tp_foot(tp) }}
189 {{ end }}
191 {{ define tp_inputpage(struct template *tp, const char *prompt) }}
192 {{ render tp_head(tp, "en", "input request") }}
193 <p>The server ask for input: <q>{{ prompt }}</q>.</p>
194 <form method="post" enctype="{{ FORM_URLENCODED }}">
195 <label for="reply">{{ "response " }}</label>
196 <input type="text" value="" id="reply" name="q" autofocus />
197 {{ " " }}
198 <button type="submit">Submit!</button>
199 </form>
200 {{ render tp_foot(tp) }}
201 {{ end }}