]> git.cameronkatri.com Git - cgit.git/blob - html.c
clone: use cgit_print_error_page() instead of html_status()
[cgit.git] / 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
12 /* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */
13 static const char* url_escape_table[256] = {
14 "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
15 "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
16 "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
17 "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
18 "%20", NULL, "%22", "%23", NULL, "%25", "%26", "%27",
19 NULL, NULL, NULL, "%2b", NULL, NULL, NULL, NULL,
20 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
21 NULL, NULL, NULL, NULL, "%3c", "%3d", "%3e", "%3f",
22 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
23 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
24 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
25 NULL, NULL, NULL, NULL, "%5c", NULL, "%5e", NULL,
26 "%60", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
27 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
28 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
29 NULL, NULL, NULL, "%7b", "%7c", "%7d", NULL, "%7f",
30 "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
31 "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
32 "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
33 "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
34 "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
35 "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
36 "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
37 "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
38 "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
39 "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
40 "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
41 "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
42 "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
43 "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
44 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
45 "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
46 };
47
48 char *fmt(const char *format, ...)
49 {
50 static char buf[8][1024];
51 static int bufidx;
52 int len;
53 va_list args;
54
55 bufidx++;
56 bufidx &= 7;
57
58 va_start(args, format);
59 len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
60 va_end(args);
61 if (len > sizeof(buf[bufidx])) {
62 fprintf(stderr, "[html.c] string truncated: %s\n", format);
63 exit(1);
64 }
65 return buf[bufidx];
66 }
67
68 char *fmtalloc(const char *format, ...)
69 {
70 struct strbuf sb = STRBUF_INIT;
71 va_list args;
72
73 va_start(args, format);
74 strbuf_vaddf(&sb, format, args);
75 va_end(args);
76
77 return strbuf_detach(&sb, NULL);
78 }
79
80 void html_raw(const char *data, size_t size)
81 {
82 if (write(STDOUT_FILENO, data, size) != size)
83 die_errno("write error on html output");
84 }
85
86 void html(const char *txt)
87 {
88 html_raw(txt, strlen(txt));
89 }
90
91 void htmlf(const char *format, ...)
92 {
93 va_list args;
94 struct strbuf buf = STRBUF_INIT;
95
96 va_start(args, format);
97 strbuf_vaddf(&buf, format, args);
98 va_end(args);
99 html(buf.buf);
100 strbuf_release(&buf);
101 }
102
103 void html_txtf(const char *format, ...)
104 {
105 va_list args;
106
107 va_start(args, format);
108 html_vtxtf(format, args);
109 va_end(args);
110 }
111
112 void html_vtxtf(const char *format, va_list ap)
113 {
114 va_list cp;
115 struct strbuf buf = STRBUF_INIT;
116
117 va_copy(cp, ap);
118 strbuf_vaddf(&buf, format, cp);
119 va_end(cp);
120 html_txt(buf.buf);
121 strbuf_release(&buf);
122 }
123
124 void html_status(int code, const char *msg, int more_headers)
125 {
126 htmlf("Status: %d %s\n", code, msg);
127 if (!more_headers)
128 html("\n");
129 }
130
131 void html_txt(const char *txt)
132 {
133 const char *t = txt;
134 while (t && *t) {
135 int c = *t;
136 if (c == '<' || c == '>' || c == '&') {
137 html_raw(txt, t - txt);
138 if (c == '>')
139 html("&gt;");
140 else if (c == '<')
141 html("&lt;");
142 else if (c == '&')
143 html("&amp;");
144 txt = t + 1;
145 }
146 t++;
147 }
148 if (t != txt)
149 html(txt);
150 }
151
152 void html_ntxt(int len, const char *txt)
153 {
154 const char *t = txt;
155 while (t && *t && len--) {
156 int c = *t;
157 if (c == '<' || c == '>' || c == '&') {
158 html_raw(txt, t - txt);
159 if (c == '>')
160 html("&gt;");
161 else if (c == '<')
162 html("&lt;");
163 else if (c == '&')
164 html("&amp;");
165 txt = t + 1;
166 }
167 t++;
168 }
169 if (t != txt)
170 html_raw(txt, t - txt);
171 if (len < 0)
172 html("...");
173 }
174
175 void html_attrf(const char *fmt, ...)
176 {
177 va_list ap;
178 struct strbuf sb = STRBUF_INIT;
179
180 va_start(ap, fmt);
181 strbuf_vaddf(&sb, fmt, ap);
182 va_end(ap);
183
184 html_attr(sb.buf);
185 strbuf_release(&sb);
186 }
187
188 void html_attr(const char *txt)
189 {
190 const char *t = txt;
191 while (t && *t) {
192 int c = *t;
193 if (c == '<' || c == '>' || c == '\'' || c == '\"' || c == '&') {
194 html_raw(txt, t - txt);
195 if (c == '>')
196 html("&gt;");
197 else if (c == '<')
198 html("&lt;");
199 else if (c == '\'')
200 html("&#x27;");
201 else if (c == '"')
202 html("&quot;");
203 else if (c == '&')
204 html("&amp;");
205 txt = t + 1;
206 }
207 t++;
208 }
209 if (t != txt)
210 html(txt);
211 }
212
213 void html_url_path(const char *txt)
214 {
215 const char *t = txt;
216 while (t && *t) {
217 unsigned char c = *t;
218 const char *e = url_escape_table[c];
219 if (e && c != '+' && c != '&') {
220 html_raw(txt, t - txt);
221 html(e);
222 txt = t + 1;
223 }
224 t++;
225 }
226 if (t != txt)
227 html(txt);
228 }
229
230 void html_url_arg(const char *txt)
231 {
232 const char *t = txt;
233 while (t && *t) {
234 unsigned char c = *t;
235 const char *e = url_escape_table[c];
236 if (c == ' ')
237 e = "+";
238 if (e) {
239 html_raw(txt, t - txt);
240 html(e);
241 txt = t + 1;
242 }
243 t++;
244 }
245 if (t != txt)
246 html(txt);
247 }
248
249 void html_hidden(const char *name, const char *value)
250 {
251 html("<input type='hidden' name='");
252 html_attr(name);
253 html("' value='");
254 html_attr(value);
255 html("'/>");
256 }
257
258 void html_option(const char *value, const char *text, const char *selected_value)
259 {
260 html("<option value='");
261 html_attr(value);
262 html("'");
263 if (selected_value && !strcmp(selected_value, value))
264 html(" selected='selected'");
265 html(">");
266 html_txt(text);
267 html("</option>\n");
268 }
269
270 void html_intoption(int value, const char *text, int selected_value)
271 {
272 htmlf("<option value='%d'%s>", value,
273 value == selected_value ? " selected='selected'" : "");
274 html_txt(text);
275 html("</option>");
276 }
277
278 void html_link_open(const char *url, const char *title, const char *class)
279 {
280 html("<a href='");
281 html_attr(url);
282 if (title) {
283 html("' title='");
284 html_attr(title);
285 }
286 if (class) {
287 html("' class='");
288 html_attr(class);
289 }
290 html("'>");
291 }
292
293 void html_link_close(void)
294 {
295 html("</a>");
296 }
297
298 void html_fileperm(unsigned short mode)
299 {
300 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
301 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
302 }
303
304 int html_include(const char *filename)
305 {
306 FILE *f;
307 char buf[4096];
308 size_t len;
309
310 if (!(f = fopen(filename, "r"))) {
311 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
312 filename, strerror(errno), errno);
313 return -1;
314 }
315 while ((len = fread(buf, 1, 4096, f)) > 0)
316 html_raw(buf, len);
317 fclose(f);
318 return 0;
319 }
320
321 static int hextoint(char c)
322 {
323 if (c >= 'a' && c <= 'f')
324 return 10 + c - 'a';
325 else if (c >= 'A' && c <= 'F')
326 return 10 + c - 'A';
327 else if (c >= '0' && c <= '9')
328 return c - '0';
329 else
330 return -1;
331 }
332
333 static char *convert_query_hexchar(char *txt)
334 {
335 int d1, d2, n;
336 n = strlen(txt);
337 if (n < 3) {
338 *txt = '\0';
339 return txt-1;
340 }
341 d1 = hextoint(*(txt + 1));
342 d2 = hextoint(*(txt + 2));
343 if (d1 < 0 || d2 < 0) {
344 memmove(txt, txt + 3, n - 2);
345 return txt-1;
346 } else {
347 *txt = d1 * 16 + d2;
348 memmove(txt + 1, txt + 3, n - 2);
349 return txt;
350 }
351 }
352
353 int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value))
354 {
355 char *o, *t, *txt, *value = NULL, c;
356
357 if (!txt_)
358 return 0;
359
360 o = t = txt = xstrdup(txt_);
361 while ((c=*t) != '\0') {
362 if (c == '=') {
363 *t = '\0';
364 value = t + 1;
365 } else if (c == '+') {
366 *t = ' ';
367 } else if (c == '%') {
368 t = convert_query_hexchar(t);
369 } else if (c == '&') {
370 *t = '\0';
371 (*fn)(txt, value);
372 txt = t + 1;
373 value = NULL;
374 }
375 t++;
376 }
377 if (t != txt)
378 (*fn)(txt, value);
379 free(o);
380 return 0;
381 }