]> gitweb.ps.run Git - ps-cgit/blob - html.c
patch: reapply path limit
[ps-cgit] / html.c
1 /* html.c: helper functions for html output
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10 #include "html.h"
11 #include "url.h"
12
13 /* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */
14 static const char* url_escape_table[256] = {
15         "%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"
47 };
48
49 char *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);
62         if (len > sizeof(buf[bufidx])) {
63                 fprintf(stderr, "[html.c] string truncated: %s\n", format);
64                 exit(1);
65         }
66         return buf[bufidx];
67 }
68
69 char *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
81 void html_raw(const char *data, size_t size)
82 {
83         if (write(STDOUT_FILENO, data, size) != size)
84                 die_errno("write error on html output");
85 }
86
87 void html(const char *txt)
88 {
89         html_raw(txt, strlen(txt));
90 }
91
92 void htmlf(const char *format, ...)
93 {
94         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
104 void html_txtf(const char *format, ...)
105 {
106         va_list args;
107
108         va_start(args, format);
109         html_vtxtf(format, args);
110         va_end(args);
111 }
112
113 void 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);
123 }
124
125 void html_txt(const char *txt)
126 {
127         const char *t = txt;
128         while (t && *t) {
129                 int c = *t;
130                 if (c == '<' || c == '>' || c == '&') {
131                         html_raw(txt, t - txt);
132                         if (c == '>')
133                                 html("&gt;");
134                         else if (c == '<')
135                                 html("&lt;");
136                         else if (c == '&')
137                                 html("&amp;");
138                         txt = t + 1;
139                 }
140                 t++;
141         }
142         if (t != txt)
143                 html(txt);
144 }
145
146 void html_ntxt(int len, const char *txt)
147 {
148         const char *t = txt;
149         while (t && *t && len--) {
150                 int c = *t;
151                 if (c == '<' || c == '>' || c == '&') {
152                         html_raw(txt, t - txt);
153                         if (c == '>')
154                                 html("&gt;");
155                         else if (c == '<')
156                                 html("&lt;");
157                         else if (c == '&')
158                                 html("&amp;");
159                         txt = t + 1;
160                 }
161                 t++;
162         }
163         if (t != txt)
164                 html_raw(txt, t - txt);
165         if (len < 0)
166                 html("...");
167 }
168
169 void 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
182 void html_attr(const char *txt)
183 {
184         const char *t = txt;
185         while (t && *t) {
186                 int c = *t;
187                 if (c == '<' || c == '>' || c == '\'' || c == '\"' || c == '&') {
188                         html_raw(txt, t - txt);
189                         if (c == '>')
190                                 html("&gt;");
191                         else if (c == '<')
192                                 html("&lt;");
193                         else if (c == '\'')
194                                 html("&#x27;");
195                         else if (c == '"')
196                                 html("&quot;");
197                         else if (c == '&')
198                                 html("&amp;");
199                         txt = t + 1;
200                 }
201                 t++;
202         }
203         if (t != txt)
204                 html(txt);
205 }
206
207 void html_url_path(const char *txt)
208 {
209         const char *t = txt;
210         while (t && *t) {
211                 unsigned char c = *t;
212                 const char *e = url_escape_table[c];
213                 if (e && c != '+' && c != '&') {
214                         html_raw(txt, t - txt);
215                         html(e);
216                         txt = t + 1;
217                 }
218                 t++;
219         }
220         if (t != txt)
221                 html(txt);
222 }
223
224 void html_url_arg(const char *txt)
225 {
226         const char *t = txt;
227         while (t && *t) {
228                 unsigned char c = *t;
229                 const char *e = url_escape_table[c];
230                 if (c == ' ')
231                         e = "+";
232                 if (e) {
233                         html_raw(txt, t - txt);
234                         html(e);
235                         txt = t + 1;
236                 }
237                 t++;
238         }
239         if (t != txt)
240                 html(txt);
241 }
242
243 void 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
269 void html_hidden(const char *name, const char *value)
270 {
271         html("<input type='hidden' name='");
272         html_attr(name);
273         html("' value='");
274         html_attr(value);
275         html("'/>");
276 }
277
278 void html_option(const char *value, const char *text, const char *selected_value)
279 {
280         html("<option value='");
281         html_attr(value);
282         html("'");
283         if (selected_value && !strcmp(selected_value, value))
284                 html(" selected='selected'");
285         html(">");
286         html_txt(text);
287         html("</option>\n");
288 }
289
290 void 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
298 void html_link_open(const char *url, const char *title, const char *class)
299 {
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
313 void html_link_close(void)
314 {
315         html("</a>");
316 }
317
318 void html_fileperm(unsigned short mode)
319 {
320         htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
321               (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
322 }
323
324 int html_include(const char *filename)
325 {
326         FILE *f;
327         char buf[4096];
328         size_t len;
329
330         if (!(f = fopen(filename, "r"))) {
331                 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
332                         filename, strerror(errno), errno);
333                 return -1;
334         }
335         while ((len = fread(buf, 1, 4096, f)) > 0)
336                 html_raw(buf, len);
337         fclose(f);
338         return 0;
339 }
340
341 void http_parse_querystring(const char *txt, void (*fn)(const char *name, const char *value))
342 {
343         const char *t = txt;
344
345         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);
351                 }
352                 free(name);
353         }
354 }