Blame


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