blob: 959148ca612328a1a1e4c297212943ed30199f28 [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
Mark Lodato8aab27f2010-02-08 23:04:41 -0500242void html_hidden(const char *name, const char *value)
Lars Hjemlie39d7382006-12-28 02:01:49 +0100243{
244 html("<input type='hidden' name='");
245 html_attr(name);
246 html("' value='");
247 html_attr(value);
248 html("'/>");
249}
250
Mark Lodato8aab27f2010-02-08 23:04:41 -0500251void html_option(const char *value, const char *text, const char *selected_value)
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100252{
253 html("<option value='");
254 html_attr(value);
255 html("'");
256 if (selected_value && !strcmp(selected_value, value))
Lars Hjemli29154832007-11-11 13:04:28 +0100257 html(" selected='selected'");
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100258 html(">");
259 html_txt(text);
260 html("</option>\n");
261}
262
Lars Hjemli1a64fd22011-03-06 23:57:26 +0100263void html_intoption(int value, const char *text, int selected_value)
264{
265 htmlf("<option value='%d'%s>", value,
266 value == selected_value ? " selected='selected'" : "");
267 html_txt(text);
268 html("</option>");
269}
270
Mark Lodato8aab27f2010-02-08 23:04:41 -0500271void html_link_open(const char *url, const char *title, const char *class)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100272{
273 html("<a href='");
274 html_attr(url);
275 if (title) {
276 html("' title='");
277 html_attr(title);
278 }
279 if (class) {
280 html("' class='");
281 html_attr(class);
282 }
283 html("'>");
284}
285
286void html_link_close(void)
287{
288 html("</a>");
289}
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100290
291void html_fileperm(unsigned short mode)
292{
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100293 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100294 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
295}
296
Lars Hjemli5e751282007-05-18 23:56:10 +0200297int html_include(const char *filename)
298{
299 FILE *f;
300 char buf[4096];
301 size_t len;
302
Harley Laue112b2082008-04-29 17:59:53 +0200303 if (!(f = fopen(filename, "r"))) {
304 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
305 filename, strerror(errno), errno);
Lars Hjemli5e751282007-05-18 23:56:10 +0200306 return -1;
Harley Laue112b2082008-04-29 17:59:53 +0200307 }
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500308 while ((len = fread(buf, 1, 4096, f)) > 0)
Mark Lodatod187b982010-09-04 14:18:16 -0400309 html_raw(buf, len);
Lars Hjemli5e751282007-05-18 23:56:10 +0200310 fclose(f);
311 return 0;
312}
Lars Hjemlie87e8962008-04-08 21:11:36 +0200313
Lukas Fleischerbafab422013-03-04 08:52:33 +0100314static int hextoint(char c)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200315{
316 if (c >= 'a' && c <= 'f')
317 return 10 + c - 'a';
318 else if (c >= 'A' && c <= 'F')
319 return 10 + c - 'A';
320 else if (c >= '0' && c <= '9')
321 return c - '0';
322 else
323 return -1;
324}
325
Lukas Fleischerbafab422013-03-04 08:52:33 +0100326static char *convert_query_hexchar(char *txt)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200327{
Mark Lodato48434782010-08-27 21:02:27 -0400328 int d1, d2, n;
329 n = strlen(txt);
330 if (n < 3) {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200331 *txt = '\0';
332 return txt-1;
333 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100334 d1 = hextoint(*(txt + 1));
335 d2 = hextoint(*(txt + 2));
336 if (d1 < 0 || d2 < 0) {
337 memmove(txt, txt + 3, n - 2);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200338 return txt-1;
339 } else {
340 *txt = d1 * 16 + d2;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100341 memmove(txt + 1, txt + 3, n - 2);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200342 return txt;
343 }
344}
345
Mark Lodato8aab27f2010-02-08 23:04:41 -0500346int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value))
Lars Hjemlie87e8962008-04-08 21:11:36 +0200347{
Lukas Fleischer070e1092011-03-31 01:21:39 +0200348 char *o, *t, *txt, *value = NULL, c;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200349
Mark Lodato8aab27f2010-02-08 23:04:41 -0500350 if (!txt_)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200351 return 0;
352
Lukas Fleischer3edfd832013-04-06 13:30:54 +0200353 o = t = txt = xstrdup(txt_);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500354 while ((c=*t) != '\0') {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100355 if (c == '=') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200356 *t = '\0';
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100357 value = t + 1;
358 } else if (c == '+') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200359 *t = ' ';
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100360 } else if (c == '%') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200361 t = convert_query_hexchar(t);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100362 } else if (c == '&') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200363 *t = '\0';
364 (*fn)(txt, value);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100365 txt = t + 1;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200366 value = NULL;
367 }
368 t++;
369 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100370 if (t != txt)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200371 (*fn)(txt, value);
Lukas Fleischer070e1092011-03-31 01:21:39 +0200372 free(o);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200373 return 0;
374}