blob: 7f81965fddcd837cc880f3a28b120d9fe71b504c [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* html.c: helper functions for html output
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli7640d902006-12-10 22:41:14 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lukas Fleischer3edfd832013-04-06 13:30:54 +02009#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "html.h"
Lukas Fleischer927b0ae2016-09-29 08:38:45 +020011#include "url.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010012
Mark Lodatoa2c63552010-02-09 10:12:43 -050013/* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */
14static const char* url_escape_table[256] = {
John Keeping438bac62015-03-08 16:32:27 +000015 "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
16 "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
17 "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
18 "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
19 "%20", NULL, "%22", "%23", NULL, "%25", "%26", "%27",
20 NULL, NULL, NULL, "%2b", NULL, NULL, NULL, NULL,
21 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
22 NULL, NULL, NULL, NULL, "%3c", "%3d", "%3e", "%3f",
23 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
24 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
25 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
26 NULL, NULL, NULL, NULL, "%5c", NULL, "%5e", NULL,
27 "%60", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
28 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
29 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
30 NULL, NULL, NULL, "%7b", "%7c", "%7d", NULL, "%7f",
31 "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
32 "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
33 "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
34 "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
35 "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
36 "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
37 "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
38 "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
39 "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
40 "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
41 "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
42 "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
43 "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
44 "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
45 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
46 "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
Mark Lodatoa2c63552010-02-09 10:12:43 -050047};
48
Lars Hjemli0d169ad2006-12-09 15:18:17 +010049char *fmt(const char *format, ...)
50{
51 static char buf[8][1024];
52 static int bufidx;
53 int len;
54 va_list args;
55
56 bufidx++;
57 bufidx &= 7;
58
59 va_start(args, format);
60 len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
61 va_end(args);
Lukas Fleischer53bc7472013-03-03 16:04:29 +010062 if (len > sizeof(buf[bufidx])) {
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010063 fprintf(stderr, "[html.c] string truncated: %s\n", format);
64 exit(1);
65 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +010066 return buf[bufidx];
67}
68
John Keepingfd00d2f2013-04-07 14:40:50 +010069char *fmtalloc(const char *format, ...)
70{
71 struct strbuf sb = STRBUF_INIT;
72 va_list args;
73
74 va_start(args, format);
75 strbuf_vaddf(&sb, format, args);
76 va_end(args);
77
78 return strbuf_detach(&sb, NULL);
79}
80
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020081void html_raw(const char *data, size_t size)
82{
John Keepingfd31aa62014-01-12 17:13:48 +000083 if (write(STDOUT_FILENO, data, size) != size)
John Keeping1fec7cd2013-05-18 15:57:03 +010084 die_errno("write error on html output");
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020085}
86
Lars Hjemli0d169ad2006-12-09 15:18:17 +010087void html(const char *txt)
88{
Jason A. Donenfeld6d7e3592013-03-20 20:44:20 +010089 html_raw(txt, strlen(txt));
Lars Hjemli0d169ad2006-12-09 15:18:17 +010090}
91
92void htmlf(const char *format, ...)
93{
John Keepingfd00d2f2013-04-07 14:40:50 +010094 va_list args;
95 struct strbuf buf = STRBUF_INIT;
96
97 va_start(args, format);
98 strbuf_vaddf(&buf, format, args);
99 va_end(args);
100 html(buf.buf);
101 strbuf_release(&buf);
102}
103
104void html_txtf(const char *format, ...)
105{
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100106 va_list args;
107
108 va_start(args, format);
John Keepingfd00d2f2013-04-07 14:40:50 +0100109 html_vtxtf(format, args);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100110 va_end(args);
John Keepingfd00d2f2013-04-07 14:40:50 +0100111}
112
113void html_vtxtf(const char *format, va_list ap)
114{
115 va_list cp;
116 struct strbuf buf = STRBUF_INIT;
117
118 va_copy(cp, ap);
119 strbuf_vaddf(&buf, format, cp);
120 va_end(cp);
121 html_txt(buf.buf);
122 strbuf_release(&buf);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100123}
124
Mark Lodato8aab27f2010-02-08 23:04:41 -0500125void html_txt(const char *txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100126{
Jeff Smith70787252017-10-01 23:39:05 -0500127 if (txt)
128 html_ntxt(txt, strlen(txt));
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100129}
130
Jeff Smith70787252017-10-01 23:39:05 -0500131ssize_t html_ntxt(const char *txt, size_t len)
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100132{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500133 const char *t = txt;
Jeff Smith70787252017-10-01 23:39:05 -0500134 ssize_t slen;
135
136 if (len > SSIZE_MAX)
137 return -1;
138
139 slen = (ssize_t) len;
140 while (t && *t && slen--) {
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100141 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100142 if (c == '<' || c == '>' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400143 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100144 if (c == '>')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100145 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100146 else if (c == '<')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100147 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100148 else if (c == '&')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100149 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100150 txt = t + 1;
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100151 }
152 t++;
153 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100154 if (t != txt)
Mark Lodatod187b982010-09-04 14:18:16 -0400155 html_raw(txt, t - txt);
Jeff Smith70787252017-10-01 23:39:05 -0500156 return slen;
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100157}
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100158
John Keepingfd00d2f2013-04-07 14:40:50 +0100159void html_attrf(const char *fmt, ...)
160{
161 va_list ap;
162 struct strbuf sb = STRBUF_INIT;
163
164 va_start(ap, fmt);
165 strbuf_vaddf(&sb, fmt, ap);
166 va_end(ap);
167
168 html_attr(sb.buf);
169 strbuf_release(&sb);
170}
171
Mark Lodato8aab27f2010-02-08 23:04:41 -0500172void html_attr(const char *txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100173{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500174 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500175 while (t && *t) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100176 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100177 if (c == '<' || c == '>' || c == '\'' || c == '\"' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400178 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100179 if (c == '>')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100180 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100181 else if (c == '<')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100182 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100183 else if (c == '\'')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100184 html("&#x27;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100185 else if (c == '"')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100186 html("&quot;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100187 else if (c == '&')
Lukas Fleischer69382322011-05-24 20:38:40 +0200188 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100189 txt = t + 1;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100190 }
191 t++;
192 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100193 if (t != txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100194 html(txt);
195}
196
Mark Lodato8aab27f2010-02-08 23:04:41 -0500197void html_url_path(const char *txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200198{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500199 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500200 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000201 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500202 const char *e = url_escape_table[c];
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100203 if (e && c != '+' && c != '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400204 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700205 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100206 txt = t + 1;
Lars Hjemli22a597e2008-10-05 16:52:57 +0200207 }
208 t++;
209 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100210 if (t != txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200211 html(txt);
212}
213
Mark Lodato8aab27f2010-02-08 23:04:41 -0500214void html_url_arg(const char *txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200215{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500216 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500217 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000218 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500219 const char *e = url_escape_table[c];
Jonathon Mah74152742011-04-10 04:10:03 -0700220 if (c == ' ')
221 e = "+";
Mark Lodatoa2c63552010-02-09 10:12:43 -0500222 if (e) {
Mark Lodatod187b982010-09-04 14:18:16 -0400223 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700224 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100225 txt = t + 1;
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200226 }
227 t++;
228 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100229 if (t != txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200230 html(txt);
231}
232
Jason A. Donenfeld513b3862016-01-14 14:28:37 +0100233void html_header_arg_in_quotes(const char *txt)
234{
235 const char *t = txt;
236 while (t && *t) {
237 unsigned char c = *t;
238 const char *e = NULL;
239 if (c == '\\')
240 e = "\\\\";
241 else if (c == '\r')
242 e = "\\r";
243 else if (c == '\n')
244 e = "\\n";
245 else if (c == '"')
246 e = "\\\"";
247 if (e) {
248 html_raw(txt, t - txt);
249 html(e);
250 txt = t + 1;
251 }
252 t++;
253 }
254 if (t != txt)
255 html(txt);
256
257}
258
Mark Lodato8aab27f2010-02-08 23:04:41 -0500259void html_hidden(const char *name, const char *value)
Lars Hjemlie39d7382006-12-28 02:01:49 +0100260{
261 html("<input type='hidden' name='");
262 html_attr(name);
263 html("' value='");
264 html_attr(value);
265 html("'/>");
266}
267
Mark Lodato8aab27f2010-02-08 23:04:41 -0500268void html_option(const char *value, const char *text, const char *selected_value)
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100269{
270 html("<option value='");
271 html_attr(value);
272 html("'");
273 if (selected_value && !strcmp(selected_value, value))
Lars Hjemli29154832007-11-11 13:04:28 +0100274 html(" selected='selected'");
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100275 html(">");
276 html_txt(text);
277 html("</option>\n");
278}
279
Lars Hjemli1a64fd22011-03-06 23:57:26 +0100280void html_intoption(int value, const char *text, int selected_value)
281{
282 htmlf("<option value='%d'%s>", value,
283 value == selected_value ? " selected='selected'" : "");
284 html_txt(text);
285 html("</option>");
286}
287
Mark Lodato8aab27f2010-02-08 23:04:41 -0500288void html_link_open(const char *url, const char *title, const char *class)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100289{
290 html("<a href='");
291 html_attr(url);
292 if (title) {
293 html("' title='");
294 html_attr(title);
295 }
296 if (class) {
297 html("' class='");
298 html_attr(class);
299 }
300 html("'>");
301}
302
303void html_link_close(void)
304{
305 html("</a>");
306}
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100307
308void html_fileperm(unsigned short mode)
309{
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100310 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100311 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
312}
313
Lars Hjemli5e751282007-05-18 23:56:10 +0200314int html_include(const char *filename)
315{
316 FILE *f;
317 char buf[4096];
318 size_t len;
319
Harley Laue112b2082008-04-29 17:59:53 +0200320 if (!(f = fopen(filename, "r"))) {
321 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
322 filename, strerror(errno), errno);
Lars Hjemli5e751282007-05-18 23:56:10 +0200323 return -1;
Harley Laue112b2082008-04-29 17:59:53 +0200324 }
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500325 while ((len = fread(buf, 1, 4096, f)) > 0)
Mark Lodatod187b982010-09-04 14:18:16 -0400326 html_raw(buf, len);
Lars Hjemli5e751282007-05-18 23:56:10 +0200327 fclose(f);
328 return 0;
329}
Lars Hjemlie87e8962008-04-08 21:11:36 +0200330
Lukas Fleischer927b0ae2016-09-29 08:38:45 +0200331void http_parse_querystring(const char *txt, void (*fn)(const char *name, const char *value))
Lars Hjemlie87e8962008-04-08 21:11:36 +0200332{
Lukas Fleischer927b0ae2016-09-29 08:38:45 +0200333 const char *t = txt;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200334
Lukas Fleischer927b0ae2016-09-29 08:38:45 +0200335 while (t && *t) {
336 char *name = url_decode_parameter_name(&t);
337 if (*name) {
338 char *value = url_decode_parameter_value(&t);
339 fn(name, value);
340 free(value);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200341 }
Lukas Fleischer927b0ae2016-09-29 08:38:45 +0200342 free(name);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200343 }
Lars Hjemlie87e8962008-04-08 21:11:36 +0200344}