blob: 1104f97faeec025f19ecf0a27f6b00a3af8b0de1 [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
John Keeping8f208792013-04-06 11:37:59 +01009#include "html.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010010#include <unistd.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <stdarg.h>
14#include <string.h>
Harley Laue112b2082008-04-29 17:59:53 +020015#include <errno.h>
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010016
Mark Lodatoa2c63552010-02-09 10:12:43 -050017/* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */
18static const char* url_escape_table[256] = {
19 "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", "%08", "%09",
20 "%0a", "%0b", "%0c", "%0d", "%0e", "%0f", "%10", "%11", "%12", "%13",
21 "%14", "%15", "%16", "%17", "%18", "%19", "%1a", "%1b", "%1c", "%1d",
Jonathon Mah74152742011-04-10 04:10:03 -070022 "%1e", "%1f", "%20", 0, "%22", "%23", 0, "%25", "%26", "%27", 0, 0, 0,
Mark Lodatoa2c63552010-02-09 10:12:43 -050023 "%2b", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "%3c", "%3d",
24 "%3e", "%3f", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25 0, 0, 0, 0, 0, 0, 0, 0, 0, "%5c", 0, "%5e", 0, "%60", 0, 0, 0, 0, 0,
26 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "%7b",
27 "%7c", "%7d", 0, "%7f", "%80", "%81", "%82", "%83", "%84", "%85",
28 "%86", "%87", "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
29 "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97", "%98", "%99",
30 "%9a", "%9b", "%9c", "%9d", "%9e", "%9f", "%a0", "%a1", "%a2", "%a3",
31 "%a4", "%a5", "%a6", "%a7", "%a8", "%a9", "%aa", "%ab", "%ac", "%ad",
32 "%ae", "%af", "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
33 "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf", "%c0", "%c1",
34 "%c2", "%c3", "%c4", "%c5", "%c6", "%c7", "%c8", "%c9", "%ca", "%cb",
35 "%cc", "%cd", "%ce", "%cf", "%d0", "%d1", "%d2", "%d3", "%d4", "%d5",
36 "%d6", "%d7", "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
37 "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7", "%e8", "%e9",
38 "%ea", "%eb", "%ec", "%ed", "%ee", "%ef", "%f0", "%f1", "%f2", "%f3",
39 "%f4", "%f5", "%f6", "%f7", "%f8", "%f9", "%fa", "%fb", "%fc", "%fd",
40 "%fe", "%ff"
41};
42
Lukas Fleischerbafab422013-03-04 08:52:33 +010043static int htmlfd = STDOUT_FILENO;
Lars Hjemli0d169ad2006-12-09 15:18:17 +010044
45char *fmt(const char *format, ...)
46{
47 static char buf[8][1024];
48 static int bufidx;
49 int len;
50 va_list args;
51
52 bufidx++;
53 bufidx &= 7;
54
55 va_start(args, format);
56 len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
57 va_end(args);
Lukas Fleischer53bc7472013-03-03 16:04:29 +010058 if (len > sizeof(buf[bufidx])) {
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010059 fprintf(stderr, "[html.c] string truncated: %s\n", format);
60 exit(1);
61 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +010062 return buf[bufidx];
63}
64
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020065void html_raw(const char *data, size_t size)
66{
Jason A. Donenfeld6d7e3592013-03-20 20:44:20 +010067 if (write(htmlfd, data, size) != size)
68 fprintf(stderr, "[html.c] html output truncated.\n");
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020069}
70
Lars Hjemli0d169ad2006-12-09 15:18:17 +010071void html(const char *txt)
72{
Jason A. Donenfeld6d7e3592013-03-20 20:44:20 +010073 html_raw(txt, strlen(txt));
Lars Hjemli0d169ad2006-12-09 15:18:17 +010074}
75
76void htmlf(const char *format, ...)
77{
Lars Hjemli25105d72006-12-10 22:31:36 +010078 static char buf[65536];
Lars Hjemli0d169ad2006-12-09 15:18:17 +010079 va_list args;
80
81 va_start(args, format);
Lars Hjemli25105d72006-12-10 22:31:36 +010082 vsnprintf(buf, sizeof(buf), format, args);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010083 va_end(args);
Lars Hjemli25105d72006-12-10 22:31:36 +010084 html(buf);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010085}
86
Lars Hjemli885096c2008-08-06 22:57:44 +020087void html_status(int code, const char *msg, int more_headers)
Lars Hjemli02a545e2008-08-06 01:20:24 +020088{
Lars Hjemli885096c2008-08-06 22:57:44 +020089 htmlf("Status: %d %s\n", code, msg);
Lars Hjemli02a545e2008-08-06 01:20:24 +020090 if (!more_headers)
91 html("\n");
92}
93
Mark Lodato8aab27f2010-02-08 23:04:41 -050094void html_txt(const char *txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +010095{
Mark Lodato8aab27f2010-02-08 23:04:41 -050096 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050097 while (t && *t) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +010098 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +010099 if (c == '<' || c == '>' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400100 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100101 if (c == '>')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100102 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100103 else if (c == '<')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100104 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100105 else if (c == '&')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100106 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100107 txt = t + 1;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100108 }
109 t++;
110 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100111 if (t != txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100112 html(txt);
113}
114
Mark Lodato8aab27f2010-02-08 23:04:41 -0500115void html_ntxt(int len, const char *txt)
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100116{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500117 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500118 while (t && *t && len--) {
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100119 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100120 if (c == '<' || c == '>' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400121 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100122 if (c == '>')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100123 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100124 else if (c == '<')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100125 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100126 else if (c == '&')
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100127 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100128 txt = t + 1;
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100129 }
130 t++;
131 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100132 if (t != txt)
Mark Lodatod187b982010-09-04 14:18:16 -0400133 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100134 if (len < 0)
Lars Hjemli9d8d9b62006-12-22 00:58:18 +0100135 html("...");
136}
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100137
Mark Lodato8aab27f2010-02-08 23:04:41 -0500138void html_attr(const char *txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100139{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500140 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500141 while (t && *t) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100142 int c = *t;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100143 if (c == '<' || c == '>' || c == '\'' || c == '\"' || c == '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400144 html_raw(txt, t - txt);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100145 if (c == '>')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100146 html("&gt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100147 else if (c == '<')
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100148 html("&lt;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100149 else if (c == '\'')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100150 html("&#x27;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100151 else if (c == '"')
Lars Hjemli7efcef02009-01-29 22:21:15 +0100152 html("&quot;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100153 else if (c == '&')
Lukas Fleischer69382322011-05-24 20:38:40 +0200154 html("&amp;");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100155 txt = t + 1;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100156 }
157 t++;
158 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100159 if (t != txt)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100160 html(txt);
161}
162
Mark Lodato8aab27f2010-02-08 23:04:41 -0500163void html_url_path(const char *txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200164{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500165 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500166 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000167 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500168 const char *e = url_escape_table[c];
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100169 if (e && c != '+' && c != '&') {
Mark Lodatod187b982010-09-04 14:18:16 -0400170 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700171 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100172 txt = t + 1;
Lars Hjemli22a597e2008-10-05 16:52:57 +0200173 }
174 t++;
175 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100176 if (t != txt)
Lars Hjemli22a597e2008-10-05 16:52:57 +0200177 html(txt);
178}
179
Mark Lodato8aab27f2010-02-08 23:04:41 -0500180void html_url_arg(const char *txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200181{
Mark Lodato8aab27f2010-02-08 23:04:41 -0500182 const char *t = txt;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500183 while (t && *t) {
Eric Wong9cae75d2011-07-21 03:24:54 +0000184 unsigned char c = *t;
Mark Lodatoa2c63552010-02-09 10:12:43 -0500185 const char *e = url_escape_table[c];
Jonathon Mah74152742011-04-10 04:10:03 -0700186 if (c == ' ')
187 e = "+";
Mark Lodatoa2c63552010-02-09 10:12:43 -0500188 if (e) {
Mark Lodatod187b982010-09-04 14:18:16 -0400189 html_raw(txt, t - txt);
Jonathon Mah74152742011-04-10 04:10:03 -0700190 html(e);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100191 txt = t + 1;
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200192 }
193 t++;
194 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100195 if (t != txt)
Lars Hjemlia36a0d92008-10-05 12:49:46 +0200196 html(txt);
197}
198
Mark Lodato8aab27f2010-02-08 23:04:41 -0500199void html_hidden(const char *name, const char *value)
Lars Hjemlie39d7382006-12-28 02:01:49 +0100200{
201 html("<input type='hidden' name='");
202 html_attr(name);
203 html("' value='");
204 html_attr(value);
205 html("'/>");
206}
207
Mark Lodato8aab27f2010-02-08 23:04:41 -0500208void html_option(const char *value, const char *text, const char *selected_value)
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100209{
210 html("<option value='");
211 html_attr(value);
212 html("'");
213 if (selected_value && !strcmp(selected_value, value))
Lars Hjemli29154832007-11-11 13:04:28 +0100214 html(" selected='selected'");
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100215 html(">");
216 html_txt(text);
217 html("</option>\n");
218}
219
Lars Hjemli1a64fd22011-03-06 23:57:26 +0100220void html_intoption(int value, const char *text, int selected_value)
221{
222 htmlf("<option value='%d'%s>", value,
223 value == selected_value ? " selected='selected'" : "");
224 html_txt(text);
225 html("</option>");
226}
227
Mark Lodato8aab27f2010-02-08 23:04:41 -0500228void html_link_open(const char *url, const char *title, const char *class)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100229{
230 html("<a href='");
231 html_attr(url);
232 if (title) {
233 html("' title='");
234 html_attr(title);
235 }
236 if (class) {
237 html("' class='");
238 html_attr(class);
239 }
240 html("'>");
241}
242
243void html_link_close(void)
244{
245 html("</a>");
246}
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100247
248void html_fileperm(unsigned short mode)
249{
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100250 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100251 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
252}
253
Lars Hjemli5e751282007-05-18 23:56:10 +0200254int html_include(const char *filename)
255{
256 FILE *f;
257 char buf[4096];
258 size_t len;
259
Harley Laue112b2082008-04-29 17:59:53 +0200260 if (!(f = fopen(filename, "r"))) {
261 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
262 filename, strerror(errno), errno);
Lars Hjemli5e751282007-05-18 23:56:10 +0200263 return -1;
Harley Laue112b2082008-04-29 17:59:53 +0200264 }
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500265 while ((len = fread(buf, 1, 4096, f)) > 0)
Mark Lodatod187b982010-09-04 14:18:16 -0400266 html_raw(buf, len);
Lars Hjemli5e751282007-05-18 23:56:10 +0200267 fclose(f);
268 return 0;
269}
Lars Hjemlie87e8962008-04-08 21:11:36 +0200270
Lukas Fleischerbafab422013-03-04 08:52:33 +0100271static int hextoint(char c)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200272{
273 if (c >= 'a' && c <= 'f')
274 return 10 + c - 'a';
275 else if (c >= 'A' && c <= 'F')
276 return 10 + c - 'A';
277 else if (c >= '0' && c <= '9')
278 return c - '0';
279 else
280 return -1;
281}
282
Lukas Fleischerbafab422013-03-04 08:52:33 +0100283static char *convert_query_hexchar(char *txt)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200284{
Mark Lodato48434782010-08-27 21:02:27 -0400285 int d1, d2, n;
286 n = strlen(txt);
287 if (n < 3) {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200288 *txt = '\0';
289 return txt-1;
290 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100291 d1 = hextoint(*(txt + 1));
292 d2 = hextoint(*(txt + 2));
293 if (d1 < 0 || d2 < 0) {
294 memmove(txt, txt + 3, n - 2);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200295 return txt-1;
296 } else {
297 *txt = d1 * 16 + d2;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100298 memmove(txt + 1, txt + 3, n - 2);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200299 return txt;
300 }
301}
302
Mark Lodato8aab27f2010-02-08 23:04:41 -0500303int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value))
Lars Hjemlie87e8962008-04-08 21:11:36 +0200304{
Lukas Fleischer070e1092011-03-31 01:21:39 +0200305 char *o, *t, *txt, *value = NULL, c;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200306
Mark Lodato8aab27f2010-02-08 23:04:41 -0500307 if (!txt_)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200308 return 0;
309
Lukas Fleischer070e1092011-03-31 01:21:39 +0200310 o = t = txt = strdup(txt_);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200311 if (t == NULL) {
312 printf("Out of memory\n");
313 exit(1);
314 }
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500315 while ((c=*t) != '\0') {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100316 if (c == '=') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200317 *t = '\0';
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100318 value = t + 1;
319 } else if (c == '+') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200320 *t = ' ';
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100321 } else if (c == '%') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200322 t = convert_query_hexchar(t);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100323 } else if (c == '&') {
Lars Hjemlie87e8962008-04-08 21:11:36 +0200324 *t = '\0';
325 (*fn)(txt, value);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100326 txt = t + 1;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200327 value = NULL;
328 }
329 t++;
330 }
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100331 if (t != txt)
Lars Hjemlie87e8962008-04-08 21:11:36 +0200332 (*fn)(txt, value);
Lukas Fleischer070e1092011-03-31 01:21:39 +0200333 free(o);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200334 return 0;
335}