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;
71 !}
72 {{ if pc }}
73 <footer>
74 <hr />
75 <dl>
76 <dt>Original URL:</dt>
77 <dd>
78 <a href="gemini://{{ pc->proxy_name | urlescape }}{{ path | urlescape }}">
79 gemini://{{ pc->proxy_name }}{{ path }}
80 </a>
81 </dd>
82 </dl>
83 </footer>
84 {{ end }}
85 </body>
86 </html>
87 {{ end }}
89 {{ define tp_figure(struct template *tp, const char *url, const char *label) }}
90 <figure>
91 <a href="{{ url | urlescape }}">
92 <img src="{{ url | urlescape }}" />
93 </a>
94 <figcaption>
95 {{ label }}
96 </figcaption>
97 </figure>
98 {{ end }}
100 {{ define tp_pre_open(struct template *tp, const char *label) }}
101 <figure>
102 {{ if label && *label != '\0' }}
103 <figcaption>{{ label }}</figcaption>
104 {{ end }}
105 <pre>
106 {{ end }}
108 {{ define tp_pre_close(struct template *tp) }}
109 </pre>
110 </figure>
111 {{ end }}
113 {{ define tp_error(struct template *tp, int code, const char *reason) }}
114 {!
115 char scode[32];
116 int r;
118 r = snprintf(scode, sizeof(code), "%d", code);
119 if (r < 0 || (size_t)r >= sizeof(code))
120 return (0);
122 !}
123 {{ render tp_head(tp, "en", "Proxy error") }}
124 <main>
125 <h1>Proxy error</h1>
126 {{ if code != -1 }}
127 <p>Request failed with code: <code>{{ scode }}</code>.</p>
128 <p>The server says: {{ reason }}.</p>
129 {{ else }}
130 <p>Unable to serve the page due to: {{ reason }}.</p>
131 {{ end }}
132 </main>
133 {{ render tp_foot(tp) }}
134 {{ end }}
136 {{ define tp_inputpage(struct template *tp, const char *prompt) }}
137 {{ render tp_head(tp, "en", "input request") }}
138 <p>The server ask for input: <q>{{ prompt }}</q>.</p>
139 <form method="post" enctype="{{ FORM_URLENCODED }}">
140 <label for="reply">{{ "response " }}</label>
141 <input type="text" value="" id="reply" name="q" autofocus />
142 {{ " " }}
143 <button type="submit">Submit!</button>
144 </form>
145 {{ render tp_foot(tp) }}
146 {{ end }}