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