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 e3c1cbab 2022-09-20 op {! struct client *clt = tp->tp_arg; !}
37 e3c1cbab 2022-09-20 op {! struct proxy_config *pc = clt->clt_pc; !}
38 e3c1cbab 2022-09-20 op {! const char *stylesheet = pc->stylesheet; !}
39 e3c1cbab 2022-09-20 op {! const char *path = clt->clt_path_info; !}
40 a80e4b65 2022-09-19 op <!doctype html>
41 a80e4b65 2022-09-19 op <html{{ if *lang != '\0' }}lang="{{ lang }}"{{ end }}>
42 a80e4b65 2022-09-19 op <head>
43 a80e4b65 2022-09-19 op <meta name="viewport" content="initial-scale=1" />
44 a80e4b65 2022-09-19 op {{ if *stylesheet != '\0' }}
45 a80e4b65 2022-09-19 op <link rel="stylesheet" href="{{ stylesheet|urlescape }}" />
46 a80e4b65 2022-09-19 op {{ end }}
47 e3c1cbab 2022-09-20 op <title>
48 e3c1cbab 2022-09-20 op {{ if title }}
49 e3c1cbab 2022-09-20 op {{ title }}
50 e3c1cbab 2022-09-20 op {{ else }}
51 e3c1cbab 2022-09-20 op gemini://{{ pc->proxy_name }}{{ path }}
52 e3c1cbab 2022-09-20 op {{ end }}
53 e3c1cbab 2022-09-20 op </title>
54 a80e4b65 2022-09-19 op </head>
55 a80e4b65 2022-09-19 op <body>
56 a80e4b65 2022-09-19 op {{ end }}
57 a80e4b65 2022-09-19 op
58 a80e4b65 2022-09-19 op {{ define tp_foot(struct template *tp) }}
59 a80e4b65 2022-09-19 op {! struct client *clt = tp->tp_arg; !}
60 a80e4b65 2022-09-19 op {! struct proxy_config *pc = clt->clt_pc; !}
61 a80e4b65 2022-09-19 op {! const char *host = pc->proxy_name; !}
62 a80e4b65 2022-09-19 op {! const char *path = clt->clt_path_info; !}
63 a80e4b65 2022-09-19 op
64 a80e4b65 2022-09-19 op <footer>
65 a80e4b65 2022-09-19 op <hr />
66 a80e4b65 2022-09-19 op <dl>
67 a80e4b65 2022-09-19 op <dt>Original URL:</dt>
68 a80e4b65 2022-09-19 op <dd>
69 a80e4b65 2022-09-19 op <a href="gemini://{{ host | urlescape }}{{ path | urlescape }}">
70 a80e4b65 2022-09-19 op gemini://{{ host }}{{ path }}
71 a80e4b65 2022-09-19 op </a>
72 a80e4b65 2022-09-19 op </dd>
73 a80e4b65 2022-09-19 op </dl>
74 a80e4b65 2022-09-19 op </footer>
75 a80e4b65 2022-09-19 op </body>
76 a80e4b65 2022-09-19 op </html>
77 a80e4b65 2022-09-19 op {{ end }}
78 a80e4b65 2022-09-19 op
79 a80e4b65 2022-09-19 op {{ define tp_figure(struct template *tp, const char *url,
80 a80e4b65 2022-09-19 op const char *label) }}
81 54acddc9 2022-09-19 op {!
82 54acddc9 2022-09-19 op struct client *clt = tp->tp_arg;
83 54acddc9 2022-09-19 op const char *path = "";
84 54acddc9 2022-09-19 op int relativify;
85 54acddc9 2022-09-19 op
86 54acddc9 2022-09-19 op relativify = *url == '/' || strstr(url, "//") == NULL;
87 54acddc9 2022-09-19 op if (relativify) {
88 54acddc9 2022-09-19 op path = clt->clt_script_name;
89 54acddc9 2022-09-19 op url++; /* skip leading / */
90 54acddc9 2022-09-19 op }
91 54acddc9 2022-09-19 op !}
92 a80e4b65 2022-09-19 op <figure>
93 54acddc9 2022-09-19 op <a href="{{ path | urlescape }}{{ url | urlescape }}">
94 07922252 2022-09-19 op <img src="{{ path | urlescape }}{{ url | urlescape }}" />
95 a80e4b65 2022-09-19 op </a>
96 a80e4b65 2022-09-19 op <figcaption>
97 a80e4b65 2022-09-19 op {{ label }}
98 a80e4b65 2022-09-19 op </figcaption>
99 a80e4b65 2022-09-19 op </figure>
100 a80e4b65 2022-09-19 op {{ end }}
101 e3c1cbab 2022-09-20 op
102 abe17781 2022-09-20 op {{ define tp_error(struct template *tp, int code, const char *reason) }}
103 abe17781 2022-09-20 op {!
104 abe17781 2022-09-20 op char scode[32];
105 abe17781 2022-09-20 op int r;
106 abe17781 2022-09-20 op
107 abe17781 2022-09-20 op r = snprintf(scode, sizeof(code), "%d", code);
108 abe17781 2022-09-20 op if (r < 0 || (size_t)r >= sizeof(code))
109 abe17781 2022-09-20 op return 0;
110 abe17781 2022-09-20 op
111 abe17781 2022-09-20 op !}
112 e3c1cbab 2022-09-20 op {{ render tp_head(tp, "en", "Proxy error") }}
113 e3c1cbab 2022-09-20 op <main>
114 e3c1cbab 2022-09-20 op <h1>Proxy error</h1>
115 abe17781 2022-09-20 op {{ if code != -1 }}
116 abe17781 2022-09-20 op <p>Request failed with code: <code>{{ scode }}</code>.</p>
117 abe17781 2022-09-20 op <p>The server says: {{ reason }}.</p>
118 abe17781 2022-09-20 op {{ else }}
119 abe17781 2022-09-20 op <p>Unable to serve the page due to: {{ reason }}.</p>
120 abe17781 2022-09-20 op {{ end }}
121 e3c1cbab 2022-09-20 op </main>
122 e3c1cbab 2022-09-20 op {{ render tp_foot(tp) }}
123 e3c1cbab 2022-09-20 op {{ end }}
124 8857131d 2022-09-20 op
125 8857131d 2022-09-20 op {{ define tp_inputpage(struct template *tp, const char *prompt) }}
126 8857131d 2022-09-20 op {{ render tp_head(tp, "en", "input request") }}
127 9c83d68a 2022-09-22 op <p>The server ask for input: <q>{{ prompt }}</q>.</p>
128 8857131d 2022-09-20 op <form method="post" enctype="{{ FORM_URLENCODED }}">
129 9c83d68a 2022-09-22 op <label for="reply">{{ "response " }}</label>
130 8857131d 2022-09-20 op <input type="text" value="" id="reply" name="q" />
131 9c83d68a 2022-09-22 op {{ " " }}
132 9c83d68a 2022-09-22 op <button type="submit">Submit!</button>
133 8857131d 2022-09-20 op </form>
134 8857131d 2022-09-20 op {{ render tp_foot(tp) }}
135 8857131d 2022-09-20 op {{ end }}