Blame


1 a80e4b65 2022-09-19 op {!
2 a80e4b65 2022-09-19 op /*
3 a80e4b65 2022-09-19 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 a80e4b65 2022-09-19 op *
5 a80e4b65 2022-09-19 op * Permission to use, copy, modify, and distribute this software for any
6 a80e4b65 2022-09-19 op * purpose with or without fee is hereby granted, provided that the above
7 a80e4b65 2022-09-19 op * copyright notice and this permission notice appear in all copies.
8 a80e4b65 2022-09-19 op *
9 a80e4b65 2022-09-19 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 a80e4b65 2022-09-19 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 a80e4b65 2022-09-19 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 a80e4b65 2022-09-19 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 a80e4b65 2022-09-19 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 a80e4b65 2022-09-19 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 a80e4b65 2022-09-19 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 a80e4b65 2022-09-19 op */
17 a80e4b65 2022-09-19 op
18 a80e4b65 2022-09-19 op #include <sys/queue.h>
19 a80e4b65 2022-09-19 op #include <sys/tree.h>
20 a80e4b65 2022-09-19 op
21 a80e4b65 2022-09-19 op #include <netinet/in.h>
22 a80e4b65 2022-09-19 op #include <arpa/inet.h>
23 a80e4b65 2022-09-19 op
24 a80e4b65 2022-09-19 op #include <event.h>
25 a80e4b65 2022-09-19 op #include <limits.h>
26 a80e4b65 2022-09-19 op #include <stdint.h>
27 abe17781 2022-09-20 op #include <stdio.h>
28 54acddc9 2022-09-19 op #include <string.h>
29 a80e4b65 2022-09-19 op
30 a80e4b65 2022-09-19 op #include "galileo.h"
31 a80e4b65 2022-09-19 op
32 a80e4b65 2022-09-19 op !}
33 a80e4b65 2022-09-19 op
34 a80e4b65 2022-09-19 op {{ define tp_head(struct template *tp, const char *lang,
35 e3c1cbab 2022-09-20 op const char *title) }}
36 7b11f549 2022-09-23 op {!
37 7b11f549 2022-09-23 op struct client *clt = tp->tp_arg;
38 7b11f549 2022-09-23 op struct proxy_config *pc = clt->clt_pc;
39 f267a4be 2022-09-30 op const char *stylesheet = pc ? pc->stylesheet : "";
40 7905878b 2022-09-23 op const char *prfx = clt->clt_script_name;
41 7b11f549 2022-09-23 op const char *path = clt->clt_path_info;
42 7b11f549 2022-09-23 op !}
43 a80e4b65 2022-09-19 op <!doctype html>
44 db3f5b3d 2022-09-23 op <html{{ if *lang != '\0' }}{{ " " }}lang="{{ lang }}"{{ end }}>
45 a80e4b65 2022-09-19 op <head>
46 a80e4b65 2022-09-19 op <meta name="viewport" content="initial-scale=1" />
47 5188543c 2022-09-23 op
48 44ec372b 2022-09-23 op {{ if *stylesheet == 0 }}
49 44ec372b 2022-09-23 op <link rel="stylesheet" href="{{ prfx }}galileo.css" />
50 5188543c 2022-09-23 op {{ else }}
51 a80e4b65 2022-09-19 op <link rel="stylesheet" href="{{ stylesheet|urlescape }}" />
52 a80e4b65 2022-09-19 op {{ end }}
53 5188543c 2022-09-23 op
54 e3c1cbab 2022-09-20 op <title>
55 e3c1cbab 2022-09-20 op {{ if title }}
56 e3c1cbab 2022-09-20 op {{ title }}
57 f267a4be 2022-09-30 op {{ else if pc }}
58 e3c1cbab 2022-09-20 op gemini://{{ pc->proxy_name }}{{ path }}
59 e3c1cbab 2022-09-20 op {{ end }}
60 e3c1cbab 2022-09-20 op </title>
61 a80e4b65 2022-09-19 op </head>
62 a80e4b65 2022-09-19 op <body>
63 a80e4b65 2022-09-19 op {{ end }}
64 a80e4b65 2022-09-19 op
65 a80e4b65 2022-09-19 op {{ define tp_foot(struct template *tp) }}
66 7b11f549 2022-09-23 op {!
67 7b11f549 2022-09-23 op struct client *clt = tp->tp_arg;
68 7b11f549 2022-09-23 op struct proxy_config *pc = clt->clt_pc;
69 7b11f549 2022-09-23 op const char *path = clt->clt_path_info;
70 70baff95 2022-09-30 op
71 70baff95 2022-09-30 op if (path == NULL)
72 70baff95 2022-09-30 op path = "";
73 7b11f549 2022-09-23 op !}
74 f267a4be 2022-09-30 op {{ if pc }}
75 a80e4b65 2022-09-19 op <footer>
76 a80e4b65 2022-09-19 op <hr />
77 a80e4b65 2022-09-19 op <dl>
78 a80e4b65 2022-09-19 op <dt>Original URL:</dt>
79 a80e4b65 2022-09-19 op <dd>
80 f267a4be 2022-09-30 op <a href="gemini://{{ pc->proxy_name | urlescape }}{{ path | urlescape }}">
81 f267a4be 2022-09-30 op gemini://{{ pc->proxy_name }}{{ path }}
82 a80e4b65 2022-09-19 op </a>
83 a80e4b65 2022-09-19 op </dd>
84 a80e4b65 2022-09-19 op </dl>
85 a80e4b65 2022-09-19 op </footer>
86 f267a4be 2022-09-30 op {{ end }}
87 a80e4b65 2022-09-19 op </body>
88 a80e4b65 2022-09-19 op </html>
89 a80e4b65 2022-09-19 op {{ end }}
90 a80e4b65 2022-09-19 op
91 a80e4b65 2022-09-19 op {{ define tp_figure(struct template *tp, const char *url,
92 a80e4b65 2022-09-19 op const char *label) }}
93 54acddc9 2022-09-19 op {!
94 54acddc9 2022-09-19 op struct client *clt = tp->tp_arg;
95 54acddc9 2022-09-19 op const char *path = "";
96 54acddc9 2022-09-19 op int relativify;
97 54acddc9 2022-09-19 op
98 54acddc9 2022-09-19 op relativify = *url == '/' || strstr(url, "//") == NULL;
99 54acddc9 2022-09-19 op if (relativify) {
100 54acddc9 2022-09-19 op path = clt->clt_script_name;
101 54acddc9 2022-09-19 op url++; /* skip leading / */
102 54acddc9 2022-09-19 op }
103 54acddc9 2022-09-19 op !}
104 a80e4b65 2022-09-19 op <figure>
105 54acddc9 2022-09-19 op <a href="{{ path | urlescape }}{{ url | urlescape }}">
106 07922252 2022-09-19 op <img src="{{ path | urlescape }}{{ url | urlescape }}" />
107 a80e4b65 2022-09-19 op </a>
108 a80e4b65 2022-09-19 op <figcaption>
109 a80e4b65 2022-09-19 op {{ label }}
110 a80e4b65 2022-09-19 op </figcaption>
111 a80e4b65 2022-09-19 op </figure>
112 a80e4b65 2022-09-19 op {{ end }}
113 e3c1cbab 2022-09-20 op
114 ceb3f79c 2022-09-29 op {{ define tp_pre_open(struct template *tp, const char *label) }}
115 ceb3f79c 2022-09-29 op <figure>
116 ceb3f79c 2022-09-29 op {{ if label && *label != '\0' }}
117 ceb3f79c 2022-09-29 op <figcaption>{{ label }}</figcaption>
118 ceb3f79c 2022-09-29 op {{ end }}
119 ceb3f79c 2022-09-29 op <pre>
120 ceb3f79c 2022-09-29 op {{ end }}
121 ceb3f79c 2022-09-29 op
122 ceb3f79c 2022-09-29 op {{ define tp_pre_close(struct template *tp) }}
123 ceb3f79c 2022-09-29 op </pre>
124 ceb3f79c 2022-09-29 op </figure>
125 ceb3f79c 2022-09-29 op {{ end }}
126 ceb3f79c 2022-09-29 op
127 abe17781 2022-09-20 op {{ define tp_error(struct template *tp, int code, const char *reason) }}
128 abe17781 2022-09-20 op {!
129 abe17781 2022-09-20 op char scode[32];
130 abe17781 2022-09-20 op int r;
131 abe17781 2022-09-20 op
132 abe17781 2022-09-20 op r = snprintf(scode, sizeof(code), "%d", code);
133 abe17781 2022-09-20 op if (r < 0 || (size_t)r >= sizeof(code))
134 c9e6d547 2022-09-23 op return (0);
135 abe17781 2022-09-20 op
136 abe17781 2022-09-20 op !}
137 e3c1cbab 2022-09-20 op {{ render tp_head(tp, "en", "Proxy error") }}
138 e3c1cbab 2022-09-20 op <main>
139 e3c1cbab 2022-09-20 op <h1>Proxy error</h1>
140 abe17781 2022-09-20 op {{ if code != -1 }}
141 abe17781 2022-09-20 op <p>Request failed with code: <code>{{ scode }}</code>.</p>
142 abe17781 2022-09-20 op <p>The server says: {{ reason }}.</p>
143 abe17781 2022-09-20 op {{ else }}
144 abe17781 2022-09-20 op <p>Unable to serve the page due to: {{ reason }}.</p>
145 abe17781 2022-09-20 op {{ end }}
146 e3c1cbab 2022-09-20 op </main>
147 e3c1cbab 2022-09-20 op {{ render tp_foot(tp) }}
148 e3c1cbab 2022-09-20 op {{ end }}
149 8857131d 2022-09-20 op
150 8857131d 2022-09-20 op {{ define tp_inputpage(struct template *tp, const char *prompt) }}
151 8857131d 2022-09-20 op {{ render tp_head(tp, "en", "input request") }}
152 9c83d68a 2022-09-22 op <p>The server ask for input: <q>{{ prompt }}</q>.</p>
153 8857131d 2022-09-20 op <form method="post" enctype="{{ FORM_URLENCODED }}">
154 9c83d68a 2022-09-22 op <label for="reply">{{ "response " }}</label>
155 8857131d 2022-09-20 op <input type="text" value="" id="reply" name="q" />
156 9c83d68a 2022-09-22 op {{ " " }}
157 9c83d68a 2022-09-22 op <button type="submit">Submit!</button>
158 8857131d 2022-09-20 op </form>
159 8857131d 2022-09-20 op {{ render tp_foot(tp) }}
160 8857131d 2022-09-20 op {{ end }}