|
diff --git a/html.c b/html.c index 24a03a5..5b07aa0 100644 --- a/ html.c+++ b/ html.c |
|
@@ -37,274 +37,274 @@ static const char* url_escape_table[256] = { |
37 | "%ea", "%eb", "%ec", "%ed", "%ee", "%ef", "%f0", "%f1", "%f2", "%f3", |
37 | "%ea", "%eb", "%ec", "%ed", "%ee", "%ef", "%f0", "%f1", "%f2", "%f3", |
38 | "%f4", "%f5", "%f6", "%f7", "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", |
38 | "%f4", "%f5", "%f6", "%f7", "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", |
39 | "%fe", "%ff" |
39 | "%fe", "%ff" |
40 | }; |
40 | }; |
41 | |
41 | |
42 | int htmlfd = STDOUT_FILENO; |
42 | int htmlfd = STDOUT_FILENO; |
43 | |
43 | |
44 | char *fmt(const char *format, ...) |
44 | char *fmt(const char *format, ...) |
45 | { |
45 | { |
46 | static char buf[8][1024]; |
46 | static char buf[8][1024]; |
47 | static int bufidx; |
47 | static int bufidx; |
48 | int len; |
48 | int len; |
49 | va_list args; |
49 | va_list args; |
50 | |
50 | |
51 | bufidx++; |
51 | bufidx++; |
52 | bufidx &= 7; |
52 | bufidx &= 7; |
53 | |
53 | |
54 | va_start(args, format); |
54 | va_start(args, format); |
55 | len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); |
55 | len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); |
56 | va_end(args); |
56 | va_end(args); |
57 | if (len>sizeof(buf[bufidx])) { |
57 | if (len>sizeof(buf[bufidx])) { |
58 | fprintf(stderr, "[html.c] string truncated: %s\n", format); |
58 | fprintf(stderr, "[html.c] string truncated: %s\n", format); |
59 | exit(1); |
59 | exit(1); |
60 | } |
60 | } |
61 | return buf[bufidx]; |
61 | return buf[bufidx]; |
62 | } |
62 | } |
63 | |
63 | |
64 | void html_raw(const char *data, size_t size) |
64 | void html_raw(const char *data, size_t size) |
65 | { |
65 | { |
66 | write(htmlfd, data, size); |
66 | write(htmlfd, data, size); |
67 | } |
67 | } |
68 | |
68 | |
69 | void html(const char *txt) |
69 | void html(const char *txt) |
70 | { |
70 | { |
71 | write(htmlfd, txt, strlen(txt)); |
71 | write(htmlfd, txt, strlen(txt)); |
72 | } |
72 | } |
73 | |
73 | |
74 | void htmlf(const char *format, ...) |
74 | void htmlf(const char *format, ...) |
75 | { |
75 | { |
76 | static char buf[65536]; |
76 | static char buf[65536]; |
77 | va_list args; |
77 | va_list args; |
78 | |
78 | |
79 | va_start(args, format); |
79 | va_start(args, format); |
80 | vsnprintf(buf, sizeof(buf), format, args); |
80 | vsnprintf(buf, sizeof(buf), format, args); |
81 | va_end(args); |
81 | va_end(args); |
82 | html(buf); |
82 | html(buf); |
83 | } |
83 | } |
84 | |
84 | |
85 | void html_status(int code, const char *msg, int more_headers) |
85 | void html_status(int code, const char *msg, int more_headers) |
86 | { |
86 | { |
87 | htmlf("Status: %d %s\n", code, msg); |
87 | htmlf("Status: %d %s\n", code, msg); |
88 | if (!more_headers) |
88 | if (!more_headers) |
89 | html("\n"); |
89 | html("\n"); |
90 | } |
90 | } |
91 | |
91 | |
92 | void html_txt(const char *txt) |
92 | void html_txt(const char *txt) |
93 | { |
93 | { |
94 | const char *t = txt; |
94 | const char *t = txt; |
95 | while(t && *t){ |
95 | while(t && *t){ |
96 | int c = *t; |
96 | int c = *t; |
97 | if (c=='<' || c=='>' || c=='&') { |
97 | if (c=='<' || c=='>' || c=='&') { |
98 | html_raw(txt, t - txt); |
98 | html_raw(txt, t - txt); |
99 | if (c=='>') |
99 | if (c=='>') |
100 | html(">"); |
100 | html(">"); |
101 | else if (c=='<') |
101 | else if (c=='<') |
102 | html("<"); |
102 | html("<"); |
103 | else if (c=='&') |
103 | else if (c=='&') |
104 | html("&"); |
104 | html("&"); |
105 | txt = t+1; |
105 | txt = t+1; |
106 | } |
106 | } |
107 | t++; |
107 | t++; |
108 | } |
108 | } |
109 | if (t!=txt) |
109 | if (t!=txt) |
110 | html(txt); |
110 | html(txt); |
111 | } |
111 | } |
112 | |
112 | |
113 | void html_ntxt(int len, const char *txt) |
113 | void html_ntxt(int len, const char *txt) |
114 | { |
114 | { |
115 | const char *t = txt; |
115 | const char *t = txt; |
116 | while(t && *t && len--){ |
116 | while(t && *t && len--){ |
117 | int c = *t; |
117 | int c = *t; |
118 | if (c=='<' || c=='>' || c=='&') { |
118 | if (c=='<' || c=='>' || c=='&') { |
119 | html_raw(txt, t - txt); |
119 | html_raw(txt, t - txt); |
120 | if (c=='>') |
120 | if (c=='>') |
121 | html(">"); |
121 | html(">"); |
122 | else if (c=='<') |
122 | else if (c=='<') |
123 | html("<"); |
123 | html("<"); |
124 | else if (c=='&') |
124 | else if (c=='&') |
125 | html("&"); |
125 | html("&"); |
126 | txt = t+1; |
126 | txt = t+1; |
127 | } |
127 | } |
128 | t++; |
128 | t++; |
129 | } |
129 | } |
130 | if (t!=txt) |
130 | if (t!=txt) |
131 | html_raw(txt, t - txt); |
131 | html_raw(txt, t - txt); |
132 | if (len<0) |
132 | if (len<0) |
133 | html("..."); |
133 | html("..."); |
134 | } |
134 | } |
135 | |
135 | |
136 | void html_attr(const char *txt) |
136 | void html_attr(const char *txt) |
137 | { |
137 | { |
138 | const char *t = txt; |
138 | const char *t = txt; |
139 | while(t && *t){ |
139 | while(t && *t){ |
140 | int c = *t; |
140 | int c = *t; |
141 | if (c=='<' || c=='>' || c=='\'' || c=='\"' || c=='&') { |
141 | if (c=='<' || c=='>' || c=='\'' || c=='\"' || c=='&') { |
142 | html_raw(txt, t - txt); |
142 | html_raw(txt, t - txt); |
143 | if (c=='>') |
143 | if (c=='>') |
144 | html(">"); |
144 | html(">"); |
145 | else if (c=='<') |
145 | else if (c=='<') |
146 | html("<"); |
146 | html("<"); |
147 | else if (c=='\'') |
147 | else if (c=='\'') |
148 | html("'"); |
148 | html("'"); |
149 | else if (c=='"') |
149 | else if (c=='"') |
150 | html("""); |
150 | html("""); |
151 | else if (c=='&') |
151 | else if (c=='&') |
152 | html("&"); |
152 | html("&"); |
153 | txt = t+1; |
153 | txt = t+1; |
154 | } |
154 | } |
155 | t++; |
155 | t++; |
156 | } |
156 | } |
157 | if (t!=txt) |
157 | if (t!=txt) |
158 | html(txt); |
158 | html(txt); |
159 | } |
159 | } |
160 | |
160 | |
161 | void html_url_path(const char *txt) |
161 | void html_url_path(const char *txt) |
162 | { |
162 | { |
163 | const char *t = txt; |
163 | const char *t = txt; |
164 | while(t && *t){ |
164 | while(t && *t){ |
165 | int c = *t; |
165 | unsigned char c = *t; |
166 | const char *e = url_escape_table[c]; |
166 | const char *e = url_escape_table[c]; |
167 | if (e && c!='+' && c!='&') { |
167 | if (e && c!='+' && c!='&') { |
168 | html_raw(txt, t - txt); |
168 | html_raw(txt, t - txt); |
169 | html(e); |
169 | html(e); |
170 | txt = t+1; |
170 | txt = t+1; |
171 | } |
171 | } |
172 | t++; |
172 | t++; |
173 | } |
173 | } |
174 | if (t!=txt) |
174 | if (t!=txt) |
175 | html(txt); |
175 | html(txt); |
176 | } |
176 | } |
177 | |
177 | |
178 | void html_url_arg(const char *txt) |
178 | void html_url_arg(const char *txt) |
179 | { |
179 | { |
180 | const char *t = txt; |
180 | const char *t = txt; |
181 | while(t && *t){ |
181 | while(t && *t){ |
182 | int c = *t; |
182 | unsigned char c = *t; |
183 | const char *e = url_escape_table[c]; |
183 | const char *e = url_escape_table[c]; |
184 | if (c == ' ') |
184 | if (c == ' ') |
185 | e = "+"; |
185 | e = "+"; |
186 | if (e) { |
186 | if (e) { |
187 | html_raw(txt, t - txt); |
187 | html_raw(txt, t - txt); |
188 | html(e); |
188 | html(e); |
189 | txt = t+1; |
189 | txt = t+1; |
190 | } |
190 | } |
191 | t++; |
191 | t++; |
192 | } |
192 | } |
193 | if (t!=txt) |
193 | if (t!=txt) |
194 | html(txt); |
194 | html(txt); |
195 | } |
195 | } |
196 | |
196 | |
197 | void html_hidden(const char *name, const char *value) |
197 | void html_hidden(const char *name, const char *value) |
198 | { |
198 | { |
199 | html("<input type='hidden' name='"); |
199 | html("<input type='hidden' name='"); |
200 | html_attr(name); |
200 | html_attr(name); |
201 | html("' value='"); |
201 | html("' value='"); |
202 | html_attr(value); |
202 | html_attr(value); |
203 | html("'/>"); |
203 | html("'/>"); |
204 | } |
204 | } |
205 | |
205 | |
206 | void html_option(const char *value, const char *text, const char *selected_value) |
206 | void html_option(const char *value, const char *text, const char *selected_value) |
207 | { |
207 | { |
208 | html("<option value='"); |
208 | html("<option value='"); |
209 | html_attr(value); |
209 | html_attr(value); |
210 | html("'"); |
210 | html("'"); |
211 | if (selected_value && !strcmp(selected_value, value)) |
211 | if (selected_value && !strcmp(selected_value, value)) |
212 | html(" selected='selected'"); |
212 | html(" selected='selected'"); |
213 | html(">"); |
213 | html(">"); |
214 | html_txt(text); |
214 | html_txt(text); |
215 | html("</option>\n"); |
215 | html("</option>\n"); |
216 | } |
216 | } |
217 | |
217 | |
218 | void html_link_open(const char *url, const char *title, const char *class) |
218 | void html_link_open(const char *url, const char *title, const char *class) |
219 | { |
219 | { |
220 | html("<a href='"); |
220 | html("<a href='"); |
221 | html_attr(url); |
221 | html_attr(url); |
222 | if (title) { |
222 | if (title) { |
223 | html("' title='"); |
223 | html("' title='"); |
224 | html_attr(title); |
224 | html_attr(title); |
225 | } |
225 | } |
226 | if (class) { |
226 | if (class) { |
227 | html("' class='"); |
227 | html("' class='"); |
228 | html_attr(class); |
228 | html_attr(class); |
229 | } |
229 | } |
230 | html("'>"); |
230 | html("'>"); |
231 | } |
231 | } |
232 | |
232 | |
233 | void html_link_close(void) |
233 | void html_link_close(void) |
234 | { |
234 | { |
235 | html("</a>"); |
235 | html("</a>"); |
236 | } |
236 | } |
237 | |
237 | |
238 | void html_fileperm(unsigned short mode) |
238 | void html_fileperm(unsigned short mode) |
239 | { |
239 | { |
240 | htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), |
240 | htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), |
241 | (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); |
241 | (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); |
242 | } |
242 | } |
243 | |
243 | |
244 | int html_include(const char *filename) |
244 | int html_include(const char *filename) |
245 | { |
245 | { |
246 | FILE *f; |
246 | FILE *f; |
247 | char buf[4096]; |
247 | char buf[4096]; |
248 | size_t len; |
248 | size_t len; |
249 | |
249 | |
250 | if (!(f = fopen(filename, "r"))) { |
250 | if (!(f = fopen(filename, "r"))) { |
251 | fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n", |
251 | fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n", |
252 | filename, strerror(errno), errno); |
252 | filename, strerror(errno), errno); |
253 | return -1; |
253 | return -1; |
254 | } |
254 | } |
255 | while((len = fread(buf, 1, 4096, f)) > 0) |
255 | while((len = fread(buf, 1, 4096, f)) > 0) |
256 | html_raw(buf, len); |
256 | html_raw(buf, len); |
257 | fclose(f); |
257 | fclose(f); |
258 | return 0; |
258 | return 0; |
259 | } |
259 | } |
260 | |
260 | |
261 | int hextoint(char c) |
261 | int hextoint(char c) |
262 | { |
262 | { |
263 | if (c >= 'a' && c <= 'f') |
263 | if (c >= 'a' && c <= 'f') |
264 | return 10 + c - 'a'; |
264 | return 10 + c - 'a'; |
265 | else if (c >= 'A' && c <= 'F') |
265 | else if (c >= 'A' && c <= 'F') |
266 | return 10 + c - 'A'; |
266 | return 10 + c - 'A'; |
267 | else if (c >= '0' && c <= '9') |
267 | else if (c >= '0' && c <= '9') |
268 | return c - '0'; |
268 | return c - '0'; |
269 | else |
269 | else |
270 | return -1; |
270 | return -1; |
271 | } |
271 | } |
272 | |
272 | |
273 | char *convert_query_hexchar(char *txt) |
273 | char *convert_query_hexchar(char *txt) |
274 | { |
274 | { |
275 | int d1, d2, n; |
275 | int d1, d2, n; |
276 | n = strlen(txt); |
276 | n = strlen(txt); |
277 | if (n < 3) { |
277 | if (n < 3) { |
278 | *txt = '\0'; |
278 | *txt = '\0'; |
279 | return txt-1; |
279 | return txt-1; |
280 | } |
280 | } |
281 | d1 = hextoint(*(txt+1)); |
281 | d1 = hextoint(*(txt+1)); |
282 | d2 = hextoint(*(txt+2)); |
282 | d2 = hextoint(*(txt+2)); |
283 | if (d1<0 || d2<0) { |
283 | if (d1<0 || d2<0) { |
284 | memmove(txt, txt+3, n-2); |
284 | memmove(txt, txt+3, n-2); |
285 | return txt-1; |
285 | return txt-1; |
286 | } else { |
286 | } else { |
287 | *txt = d1 * 16 + d2; |
287 | *txt = d1 * 16 + d2; |
288 | memmove(txt+1, txt+3, n-2); |
288 | memmove(txt+1, txt+3, n-2); |
289 | return txt; |
289 | return txt; |
290 | } |
290 | } |
291 | } |
291 | } |
292 | |
292 | |
293 | int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value)) |
293 | int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value)) |
294 | { |
294 | { |
295 | char *o, *t, *txt, *value = NULL, c; |
295 | char *o, *t, *txt, *value = NULL, c; |
296 | |
296 | |
297 | if (!txt_) |
297 | if (!txt_) |
298 | return 0; |
298 | return 0; |
299 | |
299 | |
300 | o = t = txt = strdup(txt_); |
300 | o = t = txt = strdup(txt_); |
301 | if (t == NULL) { |
301 | if (t == NULL) { |
302 | printf("Out of memory\n"); |
302 | printf("Out of memory\n"); |
303 | exit(1); |
303 | exit(1); |
304 | } |
304 | } |
305 | while((c=*t) != '\0') { |
305 | while((c=*t) != '\0') { |
306 | if (c=='=') { |
306 | if (c=='=') { |
307 | *t = '\0'; |
307 | *t = '\0'; |
308 | value = t+1; |
308 | value = t+1; |
309 | } else if (c=='+') { |
309 | } else if (c=='+') { |
310 | *t = ' '; |
310 | *t = ' '; |
|