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