blob: e7e6e075949bac31292cf84acd687dc40b320d64 [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{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500127 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500128 while (t && *t) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100129 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100130 if (c == '<' || c == '>' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400131 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100132 if (c == '>')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100133 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100134 else if (c == '<')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100135 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100136 else if (c == '&')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100137 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100138 txt = t + 1;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100139 }
140 t++;
141 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100142 if (t != txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100143 html(txt);
144}
145
Mark Lodato8aab27f2010-02-08 23:04:41 -0500146void html_ntxt(int len, const char *txt)
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100147{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500148 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500149 while (t && *t && len--) {
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100150 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100151 if (c == '<' || c == '>' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400152 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100153 if (c == '>')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100154 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100155 else if (c == '<')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100156 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100157 else if (c == '&')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100158 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100159 txt = t + 1;
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100160 }
161 t++;
162 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100163 if (t != txt)
Mark Lodatod187b982010-09-04 14:18:16 -0400164 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100165 if (len < 0)
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100166 html("...");
167}
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100168
John Keepingfd00d2f2013-04-07 14:40:50 +0100169void html_attrf(const char *fmt, ...)
170{
171 va_list ap;
172 struct strbuf sb = STRBUF_INIT;
173
174 va_start(ap, fmt);
175 strbuf_vaddf(&sb, fmt, ap);
176 va_end(ap);
177
178 html_attr(sb.buf);
179 strbuf_release(&sb);
180}
181
Mark Lodato8aab27f2010-02-08 23:04:41 -0500182void html_attr(const char *txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100183{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500184 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500185 while (t && *t) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100186 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100187 if (c == '<' || c == '>' || c == '\'' || c == '\"' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400188 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100189 if (c == '>')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100190 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100191 else if (c == '<')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100192 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100193 else if (c == '\'')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100194 html("&#x27;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100195 else if (c == '"')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100196 html("&quot;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100197 else if (c == '&')
Lukas Fleischer69382322011-05-24 20:38:40 +0200198 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100199 txt = t + 1;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100200 }
201 t++;
202 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100203 if (t != txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100204 html(txt);
205}
206
Mark Lodato8aab27f2010-02-08 23:04:41 -0500207void html_url_path(const char *txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200208{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500209 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500210 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000211 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500212 const char *e = url_escape_table[c];
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100213 if (e && c != '+' && c != '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400214 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700215 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100216 txt = t + 1;
Lars Hjemli22a597e2008-10-05 16:52:57 +0200217 }
218 t++;
219 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100220 if (t != txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200221 html(txt);
222}
223
Mark Lodato8aab27f2010-02-08 23:04:41 -0500224void html_url_arg(const char *txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200225{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500226 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500227 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000228 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500229 const char *e = url_escape_table[c];
Jonathon Mah74152742011-04-10 04:10:03 -0700230 if (c == ' ')
231 e = "+";
Mark Lodatoa2c63552010-02-09 10:12:43 -0500232 if (e) {
Mark Lodatod187b982010-09-04 14:18:16 -0400233 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700234 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100235 txt = t + 1;
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200236 }
237 t++;
238 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100239 if (t != txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200240 html(txt);
241}
242
Jason A. Donenfeld513b3862016-01-14 14:28:37 +0100243void html_header_arg_in_quotes(const char *txt)
244{
245 const char *t = txt;
246 while (t && *t) {
247 unsigned char c = *t;
248 const char *e = NULL;
249 if (c == '\\')
250 e = "\\\\";
251 else if (c == '\r')
252 e = "\\r";
253 else if (c == '\n')
254 e = "\\n";
255 else if (c == '"')
256 e = "\\\"";
257 if (e) {
258 html_raw(txt, t - txt);
259 html(e);
260 txt = t + 1;
261 }
262 t++;
263 }
264 if (t != txt)
265 html(txt);
266
267}
268
Mark Lodato8aab27f2010-02-08 23:04:41 -0500269void html_hidden(const char *name, const char *value)
Lars Hjemlie39d7382006-12-28 02:01:49 +0100270{
271 html("<input type='hidden' name='");
272 html_attr(name);
273 html("' value='");
274 html_attr(value);
275 html("'/>");
276}
277
Mark Lodato8aab27f2010-02-08 23:04:41 -0500278void html_option(const char *value, const char *text, const char *selected_value)
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100279{
280 html("<option value='");
281 html_attr(value);
282 html("'");
283 if (selected_value && !strcmp(selected_value, value))
Lars Hjemli29154832007-11-11 13:04:28 +0100284 html(" selected='selected'");
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100285 html(">");
286 html_txt(text);
287 html("</option>\n");
288}
289
Lars Hjemli1a64fd22011-03-06 23:57:26 +0100290void html_intoption(int value, const char *text, int selected_value)
291{
292 htmlf("<option value='%d'%s>", value,
293 value == selected_value ? " selected='selected'" : "");
294 html_txt(text);
295 html("</option>");
296}
297
Mark Lodato8aab27f2010-02-08 23:04:41 -0500298void html_link_open(const char *url, const char *title, const char *class)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100299{
300 html("<a href='");
301 html_attr(url);
302 if (title) {
303 html("' title='");
304 html_attr(title);
305 }
306 if (class) {
307 html("' class='");
308 html_attr(class);
309 }
310 html("'>");
311}
312
313void html_link_close(void)
314{
315 html("</a>");
316}
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100317
318void html_fileperm(unsigned short mode)
319{
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100320 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100321 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
322}
323
Lars Hjemli5e751282007-05-18 23:56:10 +0200324int html_include(const char *filename)
325{
326 FILE *f;
327 char buf[4096];
328 size_t len;
329
Harley Laue112b2082008-04-29 17:59:53 +0200330 if (!(f = fopen(filename, "r"))) {
331 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
332 filename, strerror(errno), errno);
Lars Hjemli5e751282007-05-18 23:56:10 +0200333 return -1;
Harley Laue112b2082008-04-29 17:59:53 +0200334 }
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500335 while ((len = fread(buf, 1, 4096, f)) > 0)
Mark Lodatod187b982010-09-04 14:18:16 -0400336 html_raw(buf, len);
Lars Hjemli5e751282007-05-18 23:56:10 +0200337 fclose(f);
338 return 0;
339}
Lars Hjemlie87e8962008-04-08 21:11:36 +0200340
Lukas Fleischer927b0ae2016-09-29 08:38:45 +0200341void http_parse_querystring(const char *txt, void (*fn)(const char *name, const char *value))
Lars Hjemlie87e8962008-04-08 21:11:36 +0200342{
Lukas Fleischer927b0ae2016-09-29 08:38:45 +0200343 const char *t = txt;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200344
Lukas Fleischer927b0ae2016-09-29 08:38:45 +0200345 while (t && *t) {
346 char *name = url_decode_parameter_name(&t);
347 if (*name) {
348 char *value = url_decode_parameter_value(&t);
349 fn(name, value);
350 free(value);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200351 }
Lukas Fleischer927b0ae2016-09-29 08:38:45 +0200352 free(name);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200353 }
Lars Hjemlie87e8962008-04-08 21:11:36 +0200354}