3 a80e4b65 2022-09-19 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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.
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.
18 a80e4b65 2022-09-19 op #include <sys/queue.h>
19 a80e4b65 2022-09-19 op #include <sys/tree.h>
21 a80e4b65 2022-09-19 op #include <netinet/in.h>
22 a80e4b65 2022-09-19 op #include <arpa/inet.h>
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>
31 a80e4b65 2022-09-19 op #include "galileo.h"
32 8442fa67 2022-12-04 op #include "tmpl.h"
34 9707cd87 2023-03-02 op static int tp_navigation(struct template *);
38 a80e4b65 2022-09-19 op {{ define tp_head(struct template *tp, const char *lang,
39 e3c1cbab 2022-09-20 op const char *title) }}
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;
47 a80e4b65 2022-09-19 op <!doctype html>
48 db3f5b3d 2022-09-23 op <html{{ if *lang != '\0' }}{{ " " }}lang="{{ lang }}"{{ end }}>
50 a80e4b65 2022-09-19 op <meta name="viewport" content="initial-scale=1" />
52 44ec372b 2022-09-23 op {{ if *stylesheet == 0 }}
53 44ec372b 2022-09-23 op <link rel="stylesheet" href="{{ prfx }}galileo.css" />
55 a80e4b65 2022-09-19 op <link rel="stylesheet" href="{{ stylesheet|urlescape }}" />
59 e3c1cbab 2022-09-20 op {{ if title }}
61 f267a4be 2022-09-30 op {{ else if pc }}
62 e3c1cbab 2022-09-20 op gemini://{{ pc->proxy_name }}{{ path }}
68 9707cd87 2023-03-02 op {{ render tp_navigation(tp) }}
72 a80e4b65 2022-09-19 op {{ define tp_foot(struct template *tp) }}
74 7b11f549 2022-09-23 op struct client *clt = tp->tp_arg;
75 7b11f549 2022-09-23 op struct proxy_config *pc = clt->clt_pc;
76 7b11f549 2022-09-23 op const char *path = clt->clt_path_info;
82 a80e4b65 2022-09-19 op <dt>Original URL:</dt>
84 f267a4be 2022-09-30 op <a href="gemini://{{ pc->proxy_name | urlescape }}{{ path | urlescape }}">
85 f267a4be 2022-09-30 op gemini://{{ pc->proxy_name }}{{ path }}
95 9707cd87 2023-03-02 op {{ define tp_navigation(struct template *tp) }}
97 9707cd87 2023-03-02 op struct client *clt = tp->tp_arg;
98 9707cd87 2023-03-02 op const char *prfx = clt->clt_script_name;
99 9707cd87 2023-03-02 op char *path = clt->clt_path_info;
100 9707cd87 2023-03-02 op char *tilde, *t, home[GEMINI_MAXLEN], up[GEMINI_MAXLEN];
104 9707cd87 2023-03-02 op *home = '\0';
105 9707cd87 2023-03-02 op if ((tilde = strstr(path, "/~")) != NULL &&
106 9707cd87 2023-03-02 op (t = strchr(tilde + 1, '/')) != NULL) {
109 9707cd87 2023-03-02 op (void) strlcpy(home, path + 1, sizeof(home));
113 9707cd87 2023-03-02 op (void) strlcpy(up, path + 1, sizeof(up));
114 9707cd87 2023-03-02 op dname = dirname(up);
119 9707cd87 2023-03-02 op <li><a href="{{ prfx }}">Root</a></li>
121 9707cd87 2023-03-02 op {{ if *home != '\0' }}
122 9707cd87 2023-03-02 op <li><a href="{{ prfx }}{{ home }}">Home</a></li>
125 9707cd87 2023-03-02 op {{ if strcmp(dname, ".") != 0 }}
126 9707cd87 2023-03-02 op <li><a href="{{ prfx }}{{ dname }}/">Up</a></li>
133 1a52bb7c 2022-12-04 op {{ define tp_figure(struct template *tp, const char *url, const char *label) }}
135 1d31253b 2022-11-11 op <a href="{{ url | urlescape }}">
136 1d31253b 2022-11-11 op <img src="{{ url | urlescape }}" />
140 a80e4b65 2022-09-19 op </figcaption>
144 ceb3f79c 2022-09-29 op {{ define tp_pre_open(struct template *tp, const char *label) }}
146 ceb3f79c 2022-09-29 op {{ if label && *label != '\0' }}
147 ceb3f79c 2022-09-29 op <figcaption>{{ label }}</figcaption>
152 ceb3f79c 2022-09-29 op {{ define tp_pre_close(struct template *tp) }}
157 abe17781 2022-09-20 op {{ define tp_error(struct template *tp, int code, const char *reason) }}
159 abe17781 2022-09-20 op char scode[32];
162 abe17781 2022-09-20 op r = snprintf(scode, sizeof(code), "%d", code);
163 abe17781 2022-09-20 op if (r < 0 || (size_t)r >= sizeof(code))
167 e3c1cbab 2022-09-20 op {{ render tp_head(tp, "en", "Proxy error") }}
169 e3c1cbab 2022-09-20 op <h1>Proxy error</h1>
170 abe17781 2022-09-20 op {{ if code != -1 }}
171 abe17781 2022-09-20 op <p>Request failed with code: <code>{{ scode }}</code>.</p>
172 abe17781 2022-09-20 op <p>The server says: {{ reason }}.</p>
174 abe17781 2022-09-20 op <p>Unable to serve the page due to: {{ reason }}.</p>
177 e3c1cbab 2022-09-20 op {{ render tp_foot(tp) }}
180 8857131d 2022-09-20 op {{ define tp_inputpage(struct template *tp, const char *prompt) }}
181 8857131d 2022-09-20 op {{ render tp_head(tp, "en", "input request") }}
182 9c83d68a 2022-09-22 op <p>The server ask for input: <q>{{ prompt }}</q>.</p>
183 8857131d 2022-09-20 op <form method="post" enctype="{{ FORM_URLENCODED }}">
184 9c83d68a 2022-09-22 op <label for="reply">{{ "response " }}</label>
185 58b5d440 2022-11-11 op <input type="text" value="" id="reply" name="q" autofocus />
187 9c83d68a 2022-09-22 op <button type="submit">Submit!</button>
189 8857131d 2022-09-20 op {{ render tp_foot(tp) }}