blob: 8c45ba69daa1482456172281850b29689152581a [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
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020066void html_raw(const char *data, size_t size)
67{
Jason A. Donenfeld6d7e3592013-03-20 20:44:20 +010068 if (write(htmlfd, data, size) != size)
69 fprintf(stderr, "[html.c] html output truncated.\n");
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020070}
71
Lars Hjemli0d169ad2006-12-09 15:18:17 +010072void html(const char *txt)
73{
Jason A. Donenfeld6d7e3592013-03-20 20:44:20 +010074 html_raw(txt, strlen(txt));
Lars Hjemli0d169ad2006-12-09 15:18:17 +010075}
76
77void htmlf(const char *format, ...)
78{
Lars Hjemli25105d72006-12-10 22:31:36 +010079 static char buf[65536];
Lars Hjemli0d169ad2006-12-09 15:18:17 +010080 va_list args;
81
82 va_start(args, format);
Lars Hjemli25105d72006-12-10 22:31:36 +010083 vsnprintf(buf, sizeof(buf), format, args);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010084 va_end(args);
Lars Hjemli25105d72006-12-10 22:31:36 +010085 html(buf);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010086}
87
Lars Hjemli885096c2008-08-06 22:57:44 +020088void html_status(int code, const char *msg, int more_headers)
Lars Hjemli02a545e2008-08-06 01:20:24 +020089{
Lars Hjemli885096c2008-08-06 22:57:44 +020090 htmlf("Status: %d %s\n", code, msg);
Lars Hjemli02a545e2008-08-06 01:20:24 +020091 if (!more_headers)
92 html("\n");
93}
94
Mark Lodato8aab27f2010-02-08 23:04:41 -050095void html_txt(const char *txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +010096{
Mark Lodato8aab27f2010-02-08 23:04:41 -050097 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050098 while (t && *t) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +010099 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100100 if (c == '<' || c == '>' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400101 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100102 if (c == '>')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100103 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100104 else if (c == '<')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100105 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100106 else if (c == '&')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100107 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100108 txt = t + 1;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100109 }
110 t++;
111 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100112 if (t != txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100113 html(txt);
114}
115
Mark Lodato8aab27f2010-02-08 23:04:41 -0500116void html_ntxt(int len, const char *txt)
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100117{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500118 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500119 while (t && *t && len--) {
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100120 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100121 if (c == '<' || c == '>' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400122 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100123 if (c == '>')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100124 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100125 else if (c == '<')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100126 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100127 else if (c == '&')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100128 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100129 txt = t + 1;
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100130 }
131 t++;
132 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100133 if (t != txt)
Mark Lodatod187b982010-09-04 14:18:16 -0400134 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100135 if (len < 0)
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100136 html("...");
137}
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100138
Mark Lodato8aab27f2010-02-08 23:04:41 -0500139void html_attr(const char *txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100140{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500141 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500142 while (t && *t) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100143 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100144 if (c == '<' || c == '>' || c == '\'' || c == '\"' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400145 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100146 if (c == '>')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100147 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100148 else if (c == '<')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100149 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100150 else if (c == '\'')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100151 html("&#x27;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100152 else if (c == '"')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100153 html("&quot;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100154 else if (c == '&')
Lukas Fleischer69382322011-05-24 20:38:40 +0200155 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100156 txt = t + 1;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100157 }
158 t++;
159 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100160 if (t != txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100161 html(txt);
162}
163
Mark Lodato8aab27f2010-02-08 23:04:41 -0500164void html_url_path(const char *txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200165{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500166 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500167 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000168 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500169 const char *e = url_escape_table[c];
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100170 if (e && c != '+' && c != '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400171 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700172 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100173 txt = t + 1;
Lars Hjemli22a597e2008-10-05 16:52:57 +0200174 }
175 t++;
176 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100177 if (t != txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200178 html(txt);
179}
180
Mark Lodato8aab27f2010-02-08 23:04:41 -0500181void html_url_arg(const char *txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200182{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500183 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500184 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000185 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500186 const char *e = url_escape_table[c];
Jonathon Mah74152742011-04-10 04:10:03 -0700187 if (c == ' ')
188 e = "+";
Mark Lodatoa2c63552010-02-09 10:12:43 -0500189 if (e) {
Mark Lodatod187b982010-09-04 14:18:16 -0400190 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700191 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100192 txt = t + 1;
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200193 }
194 t++;
195 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100196 if (t != txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200197 html(txt);
198}
199
Mark Lodato8aab27f2010-02-08 23:04:41 -0500200void html_hidden(const char *name, const char *value)
Lars Hjemlie39d7382006-12-28 02:01:49 +0100201{
202 html("<input type='hidden' name='");
203 html_attr(name);
204 html("' value='");
205 html_attr(value);
206 html("'/>");
207}
208
Mark Lodato8aab27f2010-02-08 23:04:41 -0500209void html_option(const char *value, const char *text, const char *selected_value)
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100210{
211 html("<option value='");
212 html_attr(value);
213 html("'");
214 if (selected_value && !strcmp(selected_value, value))
Lars Hjemli29154832007-11-11 13:04:28 +0100215 html(" selected='selected'");
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100216 html(">");
217 html_txt(text);
218 html("</option>\n");
219}
220
Lars Hjemli1a64fd22011-03-06 23:57:26 +0100221void html_intoption(int value, const char *text, int selected_value)
222{
223 htmlf("<option value='%d'%s>", value,
224 value == selected_value ? " selected='selected'" : "");
225 html_txt(text);
226 html("</option>");
227}
228
Mark Lodato8aab27f2010-02-08 23:04:41 -0500229void html_link_open(const char *url, const char *title, const char *class)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100230{
231 html("<a href='");
232 html_attr(url);
233 if (title) {
234 html("' title='");
235 html_attr(title);
236 }
237 if (class) {
238 html("' class='");
239 html_attr(class);
240 }
241 html("'>");
242}
243
244void html_link_close(void)
245{
246 html("</a>");
247}
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100248
249void html_fileperm(unsigned short mode)
250{
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100251 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100252 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
253}
254
Lars Hjemli5e751282007-05-18 23:56:10 +0200255int html_include(const char *filename)
256{
257 FILE *f;
258 char buf[4096];
259 size_t len;
260
Harley Laue112b2082008-04-29 17:59:53 +0200261 if (!(f = fopen(filename, "r"))) {
262 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
263 filename, strerror(errno), errno);
Lars Hjemli5e751282007-05-18 23:56:10 +0200264 return -1;
Harley Laue112b2082008-04-29 17:59:53 +0200265 }
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500266 while ((len = fread(buf, 1, 4096, f)) > 0)
Mark Lodatod187b982010-09-04 14:18:16 -0400267 html_raw(buf, len);
Lars Hjemli5e751282007-05-18 23:56:10 +0200268 fclose(f);
269 return 0;
270}
Lars Hjemlie87e8962008-04-08 21:11:36 +0200271
Lukas Fleischerbafab422013-03-04 08:52:33 +0100272static int hextoint(char c)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200273{
274 if (c >= 'a' && c <= 'f')
275 return 10 + c - 'a';
276 else if (c >= 'A' && c <= 'F')
277 return 10 + c - 'A';
278 else if (c >= '0' && c <= '9')
279 return c - '0';
280 else
281 return -1;
282}
283
Lukas Fleischerbafab422013-03-04 08:52:33 +0100284static char *convert_query_hexchar(char *txt)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200285{
Mark Lodato48434782010-08-27 21:02:27 -0400286 int d1, d2, n;
287 n = strlen(txt);
288 if (n < 3) {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200289 *txt = '\0';
290 return txt-1;
291 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100292 d1 = hextoint(*(txt + 1));
293 d2 = hextoint(*(txt + 2));
294 if (d1 < 0 || d2 < 0) {
295 memmove(txt, txt + 3, n - 2);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200296 return txt-1;
297 } else {
298 *txt = d1 * 16 + d2;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100299 memmove(txt + 1, txt + 3, n - 2);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200300 return txt;
301 }
302}
303
Mark Lodato8aab27f2010-02-08 23:04:41 -0500304int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value))
Lars Hjemlie87e8962008-04-08 21:11:36 +0200305{
Lukas Fleischer070e1092011-03-31 01:21:39 +0200306 char *o, *t, *txt, *value = NULL, c;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200307
Mark Lodato8aab27f2010-02-08 23:04:41 -0500308 if (!txt_)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200309 return 0;
310
Lukas Fleischer3edfd832013-04-06 13:30:54 +0200311 o = t = txt = xstrdup(txt_);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500312 while ((c=*t) != '\0') {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100313 if (c == '=') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200314 *t = '\0';
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100315 value = t + 1;
316 } else if (c == '+') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200317 *t = ' ';
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100318 } else if (c == '%') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200319 t = convert_query_hexchar(t);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100320 } else if (c == '&') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200321 *t = '\0';
322 (*fn)(txt, value);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100323 txt = t + 1;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200324 value = NULL;
325 }
326 t++;
327 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100328 if (t != txt)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200329 (*fn)(txt, value);
Lukas Fleischer070e1092011-03-31 01:21:39 +0200330 free(o);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200331 return 0;
332}