blob: f7772dce1c3284c61e0c4a93dc06e533b85574aa [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* html.c: helper functions for html output
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
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#include <unistd.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <stdarg.h>
15#include <string.h>
Harley Laue112b2082008-04-29 17:59:53 +020016#include <errno.h>
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010017
Mark Lodatoa2c63552010-02-09 10:12:43 -050018/* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */
19static const char* url_escape_table[256] = {
20 "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", "%08", "%09",
21 "%0a", "%0b", "%0c", "%0d", "%0e", "%0f", "%10", "%11", "%12", "%13",
22 "%14", "%15", "%16", "%17", "%18", "%19", "%1a", "%1b", "%1c", "%1d",
Jonathon Mah74152742011-04-10 04:10:03 -070023 "%1e", "%1f", "%20", 0, "%22", "%23", 0, "%25", "%26", "%27", 0, 0, 0,
Mark Lodatoa2c63552010-02-09 10:12:43 -050024 "%2b", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "%3c", "%3d",
25 "%3e", "%3f", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26 0, 0, 0, 0, 0, 0, 0, 0, 0, "%5c", 0, "%5e", 0, "%60", 0, 0, 0, 0, 0,
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "%7b",
28 "%7c", "%7d", 0, "%7f", "%80", "%81", "%82", "%83", "%84", "%85",
29 "%86", "%87", "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
30 "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97", "%98", "%99",
31 "%9a", "%9b", "%9c", "%9d", "%9e", "%9f", "%a0", "%a1", "%a2", "%a3",
32 "%a4", "%a5", "%a6", "%a7", "%a8", "%a9", "%aa", "%ab", "%ac", "%ad",
33 "%ae", "%af", "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
34 "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf", "%c0", "%c1",
35 "%c2", "%c3", "%c4", "%c5", "%c6", "%c7", "%c8", "%c9", "%ca", "%cb",
36 "%cc", "%cd", "%ce", "%cf", "%d0", "%d1", "%d2", "%d3", "%d4", "%d5",
37 "%d6", "%d7", "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
38 "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7", "%e8", "%e9",
39 "%ea", "%eb", "%ec", "%ed", "%ee", "%ef", "%f0", "%f1", "%f2", "%f3",
40 "%f4", "%f5", "%f6", "%f7", "%f8", "%f9", "%fa", "%fb", "%fc", "%fd",
41 "%fe", "%ff"
42};
43
Lukas Fleischerbafab422013-03-04 08:52:33 +010044static int htmlfd = STDOUT_FILENO;
Lars Hjemli0d169ad2006-12-09 15:18:17 +010045
46char *fmt(const char *format, ...)
47{
48 static char buf[8][1024];
49 static int bufidx;
50 int len;
51 va_list args;
52
53 bufidx++;
54 bufidx &= 7;
55
56 va_start(args, format);
57 len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
58 va_end(args);
Lukas Fleischer53bc7472013-03-03 16:04:29 +010059 if (len > sizeof(buf[bufidx])) {
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010060 fprintf(stderr, "[html.c] string truncated: %s\n", format);
61 exit(1);
62 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +010063 return buf[bufidx];
64}
65
John Keepingfd00d2f2013-04-07 14:40:50 +010066char *fmtalloc(const char *format, ...)
67{
68 struct strbuf sb = STRBUF_INIT;
69 va_list args;
70
71 va_start(args, format);
72 strbuf_vaddf(&sb, format, args);
73 va_end(args);
74
75 return strbuf_detach(&sb, NULL);
76}
77
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020078void html_raw(const char *data, size_t size)
79{
Jason A. Donenfeld6d7e3592013-03-20 20:44:20 +010080 if (write(htmlfd, data, size) != size)
81 fprintf(stderr, "[html.c] html output truncated.\n");
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020082}
83
Lars Hjemli0d169ad2006-12-09 15:18:17 +010084void html(const char *txt)
85{
Jason A. Donenfeld6d7e3592013-03-20 20:44:20 +010086 html_raw(txt, strlen(txt));
Lars Hjemli0d169ad2006-12-09 15:18:17 +010087}
88
89void htmlf(const char *format, ...)
90{
John Keepingfd00d2f2013-04-07 14:40:50 +010091 va_list args;
92 struct strbuf buf = STRBUF_INIT;
93
94 va_start(args, format);
95 strbuf_vaddf(&buf, format, args);
96 va_end(args);
97 html(buf.buf);
98 strbuf_release(&buf);
99}
100
101void html_txtf(const char *format, ...)
102{
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100103 va_list args;
104
105 va_start(args, format);
John Keepingfd00d2f2013-04-07 14:40:50 +0100106 html_vtxtf(format, args);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100107 va_end(args);
John Keepingfd00d2f2013-04-07 14:40:50 +0100108}
109
110void html_vtxtf(const char *format, va_list ap)
111{
112 va_list cp;
113 struct strbuf buf = STRBUF_INIT;
114
115 va_copy(cp, ap);
116 strbuf_vaddf(&buf, format, cp);
117 va_end(cp);
118 html_txt(buf.buf);
119 strbuf_release(&buf);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100120}
121
Lars Hjemli885096c2008-08-06 22:57:44 +0200122void html_status(int code, const char *msg, int more_headers)
Lars Hjemli02a545e2008-08-06 01:20:24 +0200123{
Lars Hjemli885096c2008-08-06 22:57:44 +0200124 htmlf("Status: %d %s\n", code, msg);
Lars Hjemli02a545e2008-08-06 01:20:24 +0200125 if (!more_headers)
126 html("\n");
127}
128
Mark Lodato8aab27f2010-02-08 23:04:41 -0500129void html_txt(const char *txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100130{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500131 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500132 while (t && *t) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100133 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100134 if (c == '<' || c == '>' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400135 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100136 if (c == '>')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100137 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100138 else if (c == '<')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100139 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100140 else if (c == '&')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100141 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100142 txt = t + 1;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100143 }
144 t++;
145 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100146 if (t != txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100147 html(txt);
148}
149
Mark Lodato8aab27f2010-02-08 23:04:41 -0500150void html_ntxt(int len, const char *txt)
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100151{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500152 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500153 while (t && *t && len--) {
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100154 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100155 if (c == '<' || c == '>' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400156 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100157 if (c == '>')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100158 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100159 else if (c == '<')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100160 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100161 else if (c == '&')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100162 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100163 txt = t + 1;
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100164 }
165 t++;
166 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100167 if (t != txt)
Mark Lodatod187b982010-09-04 14:18:16 -0400168 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100169 if (len < 0)
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100170 html("...");
171}
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100172
John Keepingfd00d2f2013-04-07 14:40:50 +0100173void html_attrf(const char *fmt, ...)
174{
175 va_list ap;
176 struct strbuf sb = STRBUF_INIT;
177
178 va_start(ap, fmt);
179 strbuf_vaddf(&sb, fmt, ap);
180 va_end(ap);
181
182 html_attr(sb.buf);
183 strbuf_release(&sb);
184}
185
Mark Lodato8aab27f2010-02-08 23:04:41 -0500186void html_attr(const char *txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100187{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500188 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500189 while (t && *t) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100190 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100191 if (c == '<' || c == '>' || c == '\'' || c == '\"' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400192 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100193 if (c == '>')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100194 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100195 else if (c == '<')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100196 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100197 else if (c == '\'')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100198 html("&#x27;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100199 else if (c == '"')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100200 html("&quot;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100201 else if (c == '&')
Lukas Fleischer69382322011-05-24 20:38:40 +0200202 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100203 txt = t + 1;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100204 }
205 t++;
206 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100207 if (t != txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100208 html(txt);
209}
210
Mark Lodato8aab27f2010-02-08 23:04:41 -0500211void html_url_path(const char *txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200212{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500213 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500214 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000215 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500216 const char *e = url_escape_table[c];
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100217 if (e && c != '+' && c != '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400218 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700219 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100220 txt = t + 1;
Lars Hjemli22a597e2008-10-05 16:52:57 +0200221 }
222 t++;
223 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100224 if (t != txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200225 html(txt);
226}
227
Mark Lodato8aab27f2010-02-08 23:04:41 -0500228void html_url_arg(const char *txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200229{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500230 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500231 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000232 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500233 const char *e = url_escape_table[c];
Jonathon Mah74152742011-04-10 04:10:03 -0700234 if (c == ' ')
235 e = "+";
Mark Lodatoa2c63552010-02-09 10:12:43 -0500236 if (e) {
Mark Lodatod187b982010-09-04 14:18:16 -0400237 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700238 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100239 txt = t + 1;
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200240 }
241 t++;
242 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100243 if (t != txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200244 html(txt);
245}
246
Mark Lodato8aab27f2010-02-08 23:04:41 -0500247void html_hidden(const char *name, const char *value)
Lars Hjemlie39d7382006-12-28 02:01:49 +0100248{
249 html("<input type='hidden' name='");
250 html_attr(name);
251 html("' value='");
252 html_attr(value);
253 html("'/>");
254}
255
Mark Lodato8aab27f2010-02-08 23:04:41 -0500256void html_option(const char *value, const char *text, const char *selected_value)
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100257{
258 html("<option value='");
259 html_attr(value);
260 html("'");
261 if (selected_value && !strcmp(selected_value, value))
Lars Hjemli29154832007-11-11 13:04:28 +0100262 html(" selected='selected'");
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100263 html(">");
264 html_txt(text);
265 html("</option>\n");
266}
267
Lars Hjemli1a64fd22011-03-06 23:57:26 +0100268void html_intoption(int value, const char *text, int selected_value)
269{
270 htmlf("<option value='%d'%s>", value,
271 value == selected_value ? " selected='selected'" : "");
272 html_txt(text);
273 html("</option>");
274}
275
Mark Lodato8aab27f2010-02-08 23:04:41 -0500276void html_link_open(const char *url, const char *title, const char *class)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100277{
278 html("<a href='");
279 html_attr(url);
280 if (title) {
281 html("' title='");
282 html_attr(title);
283 }
284 if (class) {
285 html("' class='");
286 html_attr(class);
287 }
288 html("'>");
289}
290
291void html_link_close(void)
292{
293 html("</a>");
294}
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100295
296void html_fileperm(unsigned short mode)
297{
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100298 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100299 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
300}
301
Lars Hjemli5e751282007-05-18 23:56:10 +0200302int html_include(const char *filename)
303{
304 FILE *f;
305 char buf[4096];
306 size_t len;
307
Harley Laue112b2082008-04-29 17:59:53 +0200308 if (!(f = fopen(filename, "r"))) {
309 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
310 filename, strerror(errno), errno);
Lars Hjemli5e751282007-05-18 23:56:10 +0200311 return -1;
Harley Laue112b2082008-04-29 17:59:53 +0200312 }
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500313 while ((len = fread(buf, 1, 4096, f)) > 0)
Mark Lodatod187b982010-09-04 14:18:16 -0400314 html_raw(buf, len);
Lars Hjemli5e751282007-05-18 23:56:10 +0200315 fclose(f);
316 return 0;
317}
Lars Hjemlie87e8962008-04-08 21:11:36 +0200318
Lukas Fleischerbafab422013-03-04 08:52:33 +0100319static int hextoint(char c)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200320{
321 if (c >= 'a' && c <= 'f')
322 return 10 + c - 'a';
323 else if (c >= 'A' && c <= 'F')
324 return 10 + c - 'A';
325 else if (c >= '0' && c <= '9')
326 return c - '0';
327 else
328 return -1;
329}
330
Lukas Fleischerbafab422013-03-04 08:52:33 +0100331static char *convert_query_hexchar(char *txt)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200332{
Mark Lodato48434782010-08-27 21:02:27 -0400333 int d1, d2, n;
334 n = strlen(txt);
335 if (n < 3) {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200336 *txt = '\0';
337 return txt-1;
338 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100339 d1 = hextoint(*(txt + 1));
340 d2 = hextoint(*(txt + 2));
341 if (d1 < 0 || d2 < 0) {
342 memmove(txt, txt + 3, n - 2);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200343 return txt-1;
344 } else {
345 *txt = d1 * 16 + d2;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100346 memmove(txt + 1, txt + 3, n - 2);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200347 return txt;
348 }
349}
350
Mark Lodato8aab27f2010-02-08 23:04:41 -0500351int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value))
Lars Hjemlie87e8962008-04-08 21:11:36 +0200352{
Lukas Fleischer070e1092011-03-31 01:21:39 +0200353 char *o, *t, *txt, *value = NULL, c;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200354
Mark Lodato8aab27f2010-02-08 23:04:41 -0500355 if (!txt_)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200356 return 0;
357
Lukas Fleischer3edfd832013-04-06 13:30:54 +0200358 o = t = txt = xstrdup(txt_);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500359 while ((c=*t) != '\0') {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100360 if (c == '=') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200361 *t = '\0';
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100362 value = t + 1;
363 } else if (c == '+') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200364 *t = ' ';
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100365 } else if (c == '%') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200366 t = convert_query_hexchar(t);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100367 } else if (c == '&') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200368 *t = '\0';
369 (*fn)(txt, value);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100370 txt = t + 1;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200371 value = NULL;
372 }
373 t++;
374 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100375 if (t != txt)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200376 (*fn)(txt, value);
Lukas Fleischer070e1092011-03-31 01:21:39 +0200377 free(o);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200378 return 0;
379}