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 ? pc->stylesheet : "";
40 const char *prfx = clt->clt_script_name;
41 const char *path = clt->clt_path_info;
42 !}
43 <!doctype html>
44 <html{{ if *lang != '\0' }}{{ " " }}lang="{{ lang }}"{{ end }}>
45 <head>
46 <meta name="viewport" content="initial-scale=1" />
48 {{ if *stylesheet == 0 }}
49 <link rel="stylesheet" href="{{ prfx }}galileo.css" />
50 {{ else }}
51 <link rel="stylesheet" href="{{ stylesheet|urlescape }}" />
52 {{ end }}
54 <title>
55 {{ if title }}
56 {{ title }}
57 {{ else if pc }}
58 gemini://{{ pc->proxy_name }}{{ path }}
59 {{ end }}
60 </title>
61 </head>
62 <body>
63 {{ end }}
65 {{ define tp_foot(struct template *tp) }}
66 {!
67 struct client *clt = tp->tp_arg;
68 struct proxy_config *pc = clt->clt_pc;
69 const char *path = clt->clt_path_info;
71 if (path == NULL)
72 path = "";
73 !}
74 {{ if pc }}
75 <footer>
76 <hr />
77 <dl>
78 <dt>Original URL:</dt>
79 <dd>
80 <a href="gemini://{{ pc->proxy_name | urlescape }}{{ path | urlescape }}">
81 gemini://{{ pc->proxy_name }}{{ path }}
82 </a>
83 </dd>
84 </dl>
85 </footer>
86 {{ end }}
87 </body>
88 </html>
89 {{ end }}
91 {{ define tp_figure(struct template *tp, const char *url,
92 const char *label) }}
93 {!
94 struct client *clt = tp->tp_arg;
95 const char *path = "";
96 int relativify;
98 relativify = *url == '/' || strstr(url, "//") == NULL;
99 if (relativify) {
100 path = clt->clt_script_name;
101 url++; /* skip leading / */
103 !}
104 <figure>
105 <a href="{{ path | urlescape }}{{ url | urlescape }}">
106 <img src="{{ path | urlescape }}{{ url | urlescape }}" />
107 </a>
108 <figcaption>
109 {{ label }}
110 </figcaption>
111 </figure>
112 {{ end }}
114 {{ define tp_pre_open(struct template *tp, const char *label) }}
115 <figure>
116 {{ if label && *label != '\0' }}
117 <figcaption>{{ label }}</figcaption>
118 {{ end }}
119 <pre>
120 {{ end }}
122 {{ define tp_pre_close(struct template *tp) }}
123 </pre>
124 </figure>
125 {{ end }}
127 {{ define tp_error(struct template *tp, int code, const char *reason) }}
128 {!
129 char scode[32];
130 int r;
132 r = snprintf(scode, sizeof(code), "%d", code);
133 if (r < 0 || (size_t)r >= sizeof(code))
134 return (0);
136 !}
137 {{ render tp_head(tp, "en", "Proxy error") }}
138 <main>
139 <h1>Proxy error</h1>
140 {{ if code != -1 }}
141 <p>Request failed with code: <code>{{ scode }}</code>.</p>
142 <p>The server says: {{ reason }}.</p>
143 {{ else }}
144 <p>Unable to serve the page due to: {{ reason }}.</p>
145 {{ end }}
146 </main>
147 {{ render tp_foot(tp) }}
148 {{ end }}
150 {{ define tp_inputpage(struct template *tp, const char *prompt) }}
151 {{ render tp_head(tp, "en", "input request") }}
152 <p>The server ask for input: <q>{{ prompt }}</q>.</p>
153 <form method="post" enctype="{{ FORM_URLENCODED }}">
154 <label for="reply">{{ "response " }}</label>
155 <input type="text" value="" id="reply" name="q" />
156 {{ " " }}
157 <button type="submit">Submit!</button>
158 </form>
159 {{ render tp_foot(tp) }}
160 {{ end }}