blob: d89df3ae35e219fedaf28b4f61820c695af234f9 [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"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011
Mark Lodatoa2c63552010-02-09 10:12:43 -050012/* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */
13static const char* url_escape_table[256] = {
John Keeping438bac62015-03-08 16:32:27 +000014 "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
15 "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
16 "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
17 "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
18 "%20", NULL, "%22", "%23", NULL, "%25", "%26", "%27",
19 NULL, NULL, NULL, "%2b", NULL, NULL, NULL, NULL,
20 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
21 NULL, NULL, NULL, NULL, "%3c", "%3d", "%3e", "%3f",
22 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
23 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
24 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
25 NULL, NULL, NULL, NULL, "%5c", NULL, "%5e", NULL,
26 "%60", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
27 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
28 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
29 NULL, NULL, NULL, "%7b", "%7c", "%7d", NULL, "%7f",
30 "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
31 "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
32 "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
33 "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
34 "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
35 "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
36 "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
37 "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
38 "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
39 "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
40 "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
41 "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
42 "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
43 "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
44 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
45 "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
Mark Lodatoa2c63552010-02-09 10:12:43 -050046};
47
Lars Hjemli0d169ad2006-12-09 15:18:17 +010048char *fmt(const char *format, ...)
49{
50 static char buf[8][1024];
51 static int bufidx;
52 int len;
53 va_list args;
54
55 bufidx++;
56 bufidx &= 7;
57
58 va_start(args, format);
59 len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
60 va_end(args);
Lukas Fleischer53bc7472013-03-03 16:04:29 +010061 if (len > sizeof(buf[bufidx])) {
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010062 fprintf(stderr, "[html.c] string truncated: %s\n", format);
63 exit(1);
64 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +010065 return buf[bufidx];
66}
67
John Keepingfd00d2f2013-04-07 14:40:50 +010068char *fmtalloc(const char *format, ...)
69{
70 struct strbuf sb = STRBUF_INIT;
71 va_list args;
72
73 va_start(args, format);
74 strbuf_vaddf(&sb, format, args);
75 va_end(args);
76
77 return strbuf_detach(&sb, NULL);
78}
79
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020080void html_raw(const char *data, size_t size)
81{
John Keepingfd31aa62014-01-12 17:13:48 +000082 if (write(STDOUT_FILENO, data, size) != size)
John Keeping1fec7cd2013-05-18 15:57:03 +010083 die_errno("write error on html output");
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020084}
85
Lars Hjemli0d169ad2006-12-09 15:18:17 +010086void html(const char *txt)
87{
Jason A. Donenfeld6d7e3592013-03-20 20:44:20 +010088 html_raw(txt, strlen(txt));
Lars Hjemli0d169ad2006-12-09 15:18:17 +010089}
90
91void htmlf(const char *format, ...)
92{
John Keepingfd00d2f2013-04-07 14:40:50 +010093 va_list args;
94 struct strbuf buf = STRBUF_INIT;
95
96 va_start(args, format);
97 strbuf_vaddf(&buf, format, args);
98 va_end(args);
99 html(buf.buf);
100 strbuf_release(&buf);
101}
102
103void html_txtf(const char *format, ...)
104{
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100105 va_list args;
106
107 va_start(args, format);
John Keepingfd00d2f2013-04-07 14:40:50 +0100108 html_vtxtf(format, args);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100109 va_end(args);
John Keepingfd00d2f2013-04-07 14:40:50 +0100110}
111
112void html_vtxtf(const char *format, va_list ap)
113{
114 va_list cp;
115 struct strbuf buf = STRBUF_INIT;
116
117 va_copy(cp, ap);
118 strbuf_vaddf(&buf, format, cp);
119 va_end(cp);
120 html_txt(buf.buf);
121 strbuf_release(&buf);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100122}
123
Mark Lodato8aab27f2010-02-08 23:04:41 -0500124void html_txt(const char *txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100125{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500126 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500127 while (t && *t) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100128 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100129 if (c == '<' || c == '>' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400130 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100131 if (c == '>')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100132 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100133 else if (c == '<')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100134 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100135 else if (c == '&')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100136 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100137 txt = t + 1;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100138 }
139 t++;
140 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100141 if (t != txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100142 html(txt);
143}
144
Mark Lodato8aab27f2010-02-08 23:04:41 -0500145void html_ntxt(int len, const char *txt)
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100146{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500147 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500148 while (t && *t && len--) {
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100149 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100150 if (c == '<' || c == '>' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400151 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100152 if (c == '>')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100153 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100154 else if (c == '<')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100155 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100156 else if (c == '&')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100157 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100158 txt = t + 1;
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100159 }
160 t++;
161 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100162 if (t != txt)
Mark Lodatod187b982010-09-04 14:18:16 -0400163 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100164 if (len < 0)
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100165 html("...");
166}
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100167
John Keepingfd00d2f2013-04-07 14:40:50 +0100168void html_attrf(const char *fmt, ...)
169{
170 va_list ap;
171 struct strbuf sb = STRBUF_INIT;
172
173 va_start(ap, fmt);
174 strbuf_vaddf(&sb, fmt, ap);
175 va_end(ap);
176
177 html_attr(sb.buf);
178 strbuf_release(&sb);
179}
180
Mark Lodato8aab27f2010-02-08 23:04:41 -0500181void html_attr(const char *txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100182{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500183 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500184 while (t && *t) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100185 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100186 if (c == '<' || c == '>' || c == '\'' || c == '\"' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400187 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100188 if (c == '>')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100189 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100190 else if (c == '<')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100191 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100192 else if (c == '\'')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100193 html("&#x27;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100194 else if (c == '"')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100195 html("&quot;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100196 else if (c == '&')
Lukas Fleischer69382322011-05-24 20:38:40 +0200197 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100198 txt = t + 1;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100199 }
200 t++;
201 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100202 if (t != txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100203 html(txt);
204}
205
Mark Lodato8aab27f2010-02-08 23:04:41 -0500206void html_url_path(const char *txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200207{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500208 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500209 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000210 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500211 const char *e = url_escape_table[c];
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100212 if (e && c != '+' && c != '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400213 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700214 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100215 txt = t + 1;
Lars Hjemli22a597e2008-10-05 16:52:57 +0200216 }
217 t++;
218 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100219 if (t != txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200220 html(txt);
221}
222
Mark Lodato8aab27f2010-02-08 23:04:41 -0500223void html_url_arg(const char *txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200224{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500225 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500226 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000227 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500228 const char *e = url_escape_table[c];
Jonathon Mah74152742011-04-10 04:10:03 -0700229 if (c == ' ')
230 e = "+";
Mark Lodatoa2c63552010-02-09 10:12:43 -0500231 if (e) {
Mark Lodatod187b982010-09-04 14:18:16 -0400232 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700233 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100234 txt = t + 1;
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200235 }
236 t++;
237 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100238 if (t != txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200239 html(txt);
240}
241
Jason A. Donenfeld513b3862016-01-14 14:28:37 +0100242void html_header_arg_in_quotes(const char *txt)
243{
244 const char *t = txt;
245 while (t && *t) {
246 unsigned char c = *t;
247 const char *e = NULL;
248 if (c == '\\')
249 e = "\\\\";
250 else if (c == '\r')
251 e = "\\r";
252 else if (c == '\n')
253 e = "\\n";
254 else if (c == '"')
255 e = "\\\"";
256 if (e) {
257 html_raw(txt, t - txt);
258 html(e);
259 txt = t + 1;
260 }
261 t++;
262 }
263 if (t != txt)
264 html(txt);
265
266}
267
Mark Lodato8aab27f2010-02-08 23:04:41 -0500268void html_hidden(const char *name, const char *value)
Lars Hjemlie39d7382006-12-28 02:01:49 +0100269{
270 html("<input type='hidden' name='");
271 html_attr(name);
272 html("' value='");
273 html_attr(value);
274 html("'/>");
275}
276
Mark Lodato8aab27f2010-02-08 23:04:41 -0500277void html_option(const char *value, const char *text, const char *selected_value)
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100278{
279 html("<option value='");
280 html_attr(value);
281 html("'");
282 if (selected_value && !strcmp(selected_value, value))
Lars Hjemli29154832007-11-11 13:04:28 +0100283 html(" selected='selected'");
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100284 html(">");
285 html_txt(text);
286 html("</option>\n");
287}
288
Lars Hjemli1a64fd22011-03-06 23:57:26 +0100289void html_intoption(int value, const char *text, int selected_value)
290{
291 htmlf("<option value='%d'%s>", value,
292 value == selected_value ? " selected='selected'" : "");
293 html_txt(text);
294 html("</option>");
295}
296
Mark Lodato8aab27f2010-02-08 23:04:41 -0500297void html_link_open(const char *url, const char *title, const char *class)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100298{
299 html("<a href='");
300 html_attr(url);
301 if (title) {
302 html("' title='");
303 html_attr(title);
304 }
305 if (class) {
306 html("' class='");
307 html_attr(class);
308 }
309 html("'>");
310}
311
312void html_link_close(void)
313{
314 html("</a>");
315}
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100316
317void html_fileperm(unsigned short mode)
318{
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100319 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100320 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
321}
322
Lars Hjemli5e751282007-05-18 23:56:10 +0200323int html_include(const char *filename)
324{
325 FILE *f;
326 char buf[4096];
327 size_t len;
328
Harley Laue112b2082008-04-29 17:59:53 +0200329 if (!(f = fopen(filename, "r"))) {
330 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
331 filename, strerror(errno), errno);
Lars Hjemli5e751282007-05-18 23:56:10 +0200332 return -1;
Harley Laue112b2082008-04-29 17:59:53 +0200333 }
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500334 while ((len = fread(buf, 1, 4096, f)) > 0)
Mark Lodatod187b982010-09-04 14:18:16 -0400335 html_raw(buf, len);
Lars Hjemli5e751282007-05-18 23:56:10 +0200336 fclose(f);
337 return 0;
338}
Lars Hjemlie87e8962008-04-08 21:11:36 +0200339
Lukas Fleischerbafab422013-03-04 08:52:33 +0100340static int hextoint(char c)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200341{
342 if (c >= 'a' && c <= 'f')
343 return 10 + c - 'a';
344 else if (c >= 'A' && c <= 'F')
345 return 10 + c - 'A';
346 else if (c >= '0' && c <= '9')
347 return c - '0';
348 else
349 return -1;
350}
351
Lukas Fleischerbafab422013-03-04 08:52:33 +0100352static char *convert_query_hexchar(char *txt)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200353{
Mark Lodato48434782010-08-27 21:02:27 -0400354 int d1, d2, n;
355 n = strlen(txt);
356 if (n < 3) {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200357 *txt = '\0';
358 return txt-1;
359 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100360 d1 = hextoint(*(txt + 1));
361 d2 = hextoint(*(txt + 2));
362 if (d1 < 0 || d2 < 0) {
363 memmove(txt, txt + 3, n - 2);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200364 return txt-1;
365 } else {
366 *txt = d1 * 16 + d2;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100367 memmove(txt + 1, txt + 3, n - 2);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200368 return txt;
369 }
370}
371
Mark Lodato8aab27f2010-02-08 23:04:41 -0500372int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value))
Lars Hjemlie87e8962008-04-08 21:11:36 +0200373{
Lukas Fleischer070e1092011-03-31 01:21:39 +0200374 char *o, *t, *txt, *value = NULL, c;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200375
Mark Lodato8aab27f2010-02-08 23:04:41 -0500376 if (!txt_)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200377 return 0;
378
Lukas Fleischer3edfd832013-04-06 13:30:54 +0200379 o = t = txt = xstrdup(txt_);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500380 while ((c=*t) != '\0') {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100381 if (c == '=') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200382 *t = '\0';
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100383 value = t + 1;
384 } else if (c == '+') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200385 *t = ' ';
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100386 } else if (c == '%') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200387 t = convert_query_hexchar(t);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100388 } else if (c == '&') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200389 *t = '\0';
390 (*fn)(txt, value);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100391 txt = t + 1;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200392 value = NULL;
393 }
394 t++;
395 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100396 if (t != txt)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200397 (*fn)(txt, value);
Lukas Fleischer070e1092011-03-31 01:21:39 +0200398 free(o);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200399 return 0;
400}