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 <limits.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "galileo.h"
32 !}
34 {{ define tp_head(struct template *tp, const char *lang,
35 const char *title) }}
36 {!
37 struct client *clt = tp->tp_arg;
38 struct proxy_config *pc = clt->clt_pc;
39 const char *stylesheet = pc->stylesheet;
40 const char *path = clt->clt_path_info;
41 !}
42 <!doctype html>
43 <html{{ if *lang != '\0' }}lang="{{ lang }}"{{ end }}>
44 <head>
45 <meta name="viewport" content="initial-scale=1" />
47 {{ if *stylesheet == '\0' }}
48 <link rel="stylesheet" href="{{ prfx|urlescape }}/galileo.css" />
49 {{ else }}
50 <link rel="stylesheet" href="{{ stylesheet|urlescape }}" />
51 {{ end }}
53 <title>
54 {{ if title }}
55 {{ title }}
56 {{ else }}
57 gemini://{{ pc->proxy_name }}{{ path }}
58 {{ end }}
59 </title>
60 </head>
61 <body>
62 {{ end }}
64 {{ define tp_foot(struct template *tp) }}
65 {!
66 struct client *clt = tp->tp_arg;
67 struct proxy_config *pc = clt->clt_pc;
68 const char *host = pc->proxy_name;
69 const char *path = clt->clt_path_info;
70 !}
72 <footer>
73 <hr />
74 <dl>
75 <dt>Original URL:</dt>
76 <dd>
77 <a href="gemini://{{ host | urlescape }}{{ path | urlescape }}">
78 gemini://{{ host }}{{ path }}
79 </a>
80 </dd>
81 </dl>
82 </footer>
83 </body>
84 </html>
85 {{ end }}
87 {{ define tp_figure(struct template *tp, const char *url,
88 const char *label) }}
89 {!
90 struct client *clt = tp->tp_arg;
91 const char *path = "";
92 int relativify;
94 relativify = *url == '/' || strstr(url, "//") == NULL;
95 if (relativify) {
96 path = clt->clt_script_name;
97 url++; /* skip leading / */
98 }
99 !}
100 <figure>
101 <a href="{{ path | urlescape }}{{ url | urlescape }}">
102 <img src="{{ path | urlescape }}{{ url | urlescape }}" />
103 </a>
104 <figcaption>
105 {{ label }}
106 </figcaption>
107 </figure>
108 {{ end }}
110 {{ define tp_error(struct template *tp, int code, const char *reason) }}
111 {!
112 char scode[32];
113 int r;
115 r = snprintf(scode, sizeof(code), "%d", code);
116 if (r < 0 || (size_t)r >= sizeof(code))
117 return 0;
119 !}
120 {{ render tp_head(tp, "en", "Proxy error") }}
121 <main>
122 <h1>Proxy error</h1>
123 {{ if code != -1 }}
124 <p>Request failed with code: <code>{{ scode }}</code>.</p>
125 <p>The server says: {{ reason }}.</p>
126 {{ else }}
127 <p>Unable to serve the page due to: {{ reason }}.</p>
128 {{ end }}
129 </main>
130 {{ render tp_foot(tp) }}
131 {{ end }}
133 {{ define tp_inputpage(struct template *tp, const char *prompt) }}
134 {{ render tp_head(tp, "en", "input request") }}
135 <p>The server ask for input: <q>{{ prompt }}</q>.</p>
136 <form method="post" enctype="{{ FORM_URLENCODED }}">
137 <label for="reply">{{ "response " }}</label>
138 <input type="text" value="" id="reply" name="q" />
139 {{ " " }}
140 <button type="submit">Submit!</button>
141 </form>
142 {{ render tp_foot(tp) }}
143 {{ end }}