summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--Makefile2
-rw-r--r--cgit.c94
-rw-r--r--cgit.h9
-rw-r--r--ui-shared.c76
4 files changed, 99 insertions, 82 deletions
diff --git a/Makefile b/Makefile
index eab7926..a4291f7 100644
--- a/Makefile
+++ b/Makefile
@@ -7,3 +7,3 @@ CACHE_ROOT = /var/cache/cgit
7EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto 7EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto
8OBJECTS = parsing.o html.o cache.o 8OBJECTS = parsing.o html.o cache.o ui-shared.o
9 9
diff --git a/cgit.c b/cgit.c
index 5567859..5438fa1 100644
--- a/cgit.c
+++ b/cgit.c
@@ -12,12 +12,2 @@ const char cgit_version[] = CGIT_VERSION;
12 12
13const char cgit_doctype[] =
14"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
15" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
16
17const char cgit_error[] =
18"<div class='error'>%s</div>";
19
20const char cgit_lib_error[] =
21"<div class='error'>%s: %s</div>";
22
23int htmlfd = 0; 13int htmlfd = 0;
@@ -146,59 +136,3 @@ static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1,
146 136
147/* Sun, 06 Nov 1994 08:49:37 GMT */ 137static void cgit_print_repolist(struct cacheitem *item)
148static char *http_date(time_t t)
149{
150 static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
151 static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
152 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
153 struct tm *tm = gmtime(&t);
154 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
155 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
156 tm->tm_hour, tm->tm_min, tm->tm_sec);
157}
158
159static int ttl_seconds(int ttl)
160{
161 if (ttl<0)
162 return 60 * 60 * 24 * 365;
163 else
164 return ttl * 60;
165}
166
167static void cgit_print_docstart(char *title)
168{
169 html("Content-Type: text/html; charset=utf-8\n");
170 htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime));
171 htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime +
172 ttl_seconds(cacheitem.ttl)));
173 html("\n");
174 html(cgit_doctype);
175 html("<html>\n");
176 html("<head>\n");
177 html("<title>");
178 html_txt(title);
179 html("</title>\n");
180 htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version);
181 html("<link rel='stylesheet' type='text/css' href='");
182 html_attr(cgit_css);
183 html("'/>\n");
184 html("</head>\n");
185 html("<body>\n");
186}
187
188static void cgit_print_docend()
189{
190 html("</body>\n</html>\n");
191}
192
193static void cgit_print_pageheader(char *title)
194{
195 html("<div id='header'>");
196 htmlf("<a href='%s'>", cgit_logo_link);
197 htmlf("<img id='logo' src='%s'/>\n", cgit_logo);
198 htmlf("</a>");
199 html_txt(title);
200 html("</div>");
201}
202
203static void cgit_print_repolist()
204{ 138{
@@ -210,3 +144,3 @@ static void cgit_print_repolist()
210 chdir(cgit_root); 144 chdir(cgit_root);
211 cgit_print_docstart(cgit_root_title); 145 cgit_print_docstart(cgit_root_title, item);
212 cgit_print_pageheader(cgit_root_title); 146 cgit_print_pageheader(cgit_root_title);
@@ -214,4 +148,4 @@ static void cgit_print_repolist()
214 if (!(d = opendir("."))) { 148 if (!(d = opendir("."))) {
215 htmlf(cgit_lib_error, "Unable to scan repository directory", 149 cgit_print_error(fmt("Unable to scan repository directory: %s",
216 strerror(errno)); 150 strerror(errno)));
217 cgit_print_docend(); 151 cgit_print_docend();
@@ -381,3 +315,3 @@ static void cgit_print_object(char *hex)
381 if (get_sha1_hex(hex, sha1)){ 315 if (get_sha1_hex(hex, sha1)){
382 htmlf(cgit_error, "Bad hex value"); 316 cgit_print_error(fmt("Bad hex value: %s", hex));
383 return; 317 return;
@@ -386,3 +320,3 @@ static void cgit_print_object(char *hex)
386 if (sha1_object_info(sha1, type, NULL)){ 320 if (sha1_object_info(sha1, type, NULL)){
387 htmlf(cgit_error, "Bad object name"); 321 cgit_print_error("Bad object name");
388 return; 322 return;
@@ -392,3 +326,3 @@ static void cgit_print_object(char *hex)
392 if (!buf) { 326 if (!buf) {
393 htmlf(cgit_error, "Error reading object"); 327 cgit_print_error("Error reading object");
394 return; 328 return;
@@ -404,3 +338,3 @@ static void cgit_print_object(char *hex)
404 338
405static void cgit_print_repo_page() 339static void cgit_print_repo_page(struct cacheitem *item)
406{ 340{
@@ -409,6 +343,6 @@ static void cgit_print_repo_page()
409 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 343 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
410 cgit_print_docstart(title); 344 cgit_print_docstart(title, item);
411 cgit_print_pageheader(title); 345 cgit_print_pageheader(title);
412 htmlf(cgit_lib_error, "Unable to scan repository", 346 cgit_print_error(fmt("Unable to scan repository: %s",
413 strerror(errno)); 347 strerror(errno)));
414 cgit_print_docend(); 348 cgit_print_docend();
@@ -418,3 +352,3 @@ static void cgit_print_repo_page()
418 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); 352 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
419 cgit_print_docstart(title); 353 cgit_print_docstart(title, item);
420 cgit_print_pageheader(title); 354 cgit_print_pageheader(title);
@@ -435,5 +369,5 @@ static void cgit_fill_cache(struct cacheitem *item)
435 if (cgit_query_repo) 369 if (cgit_query_repo)
436 cgit_print_repo_page(); 370 cgit_print_repo_page(item);
437 else 371 else
438 cgit_print_repolist(); 372 cgit_print_repolist(item);
439} 373}
diff --git a/cgit.h b/cgit.h
index 6c0aa3b..e64fbd7 100644
--- a/cgit.h
+++ b/cgit.h
@@ -17,2 +17,4 @@ struct cacheitem {
17 17
18extern const char cgit_version[];
19
18extern char *cgit_root; 20extern char *cgit_root;
@@ -56,3 +58,2 @@ extern void html_link_close(void);
56 58
57
58extern int cgit_read_config(const char *filename, configfn fn); 59extern int cgit_read_config(const char *filename, configfn fn);
@@ -66,2 +67,8 @@ extern int cache_expired(struct cacheitem *item);
66 67
68extern void cgit_print_error(char *msg);
69extern void cgit_print_docstart(char *title, struct cacheitem *item);
70extern void cgit_print_docend();
71extern void cgit_print_pageheader(char *title);
72
73
67#endif /* CGIT_H */ 74#endif /* CGIT_H */
diff --git a/ui-shared.c b/ui-shared.c
new file mode 100644
index 0000000..e795043
--- a/dev/null
+++ b/ui-shared.c
@@ -0,0 +1,76 @@
1/* ui-shared.c: common web output functions
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
9#include "cgit.h"
10
11const char cgit_doctype[] =
12"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
13" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
14
15static char *http_date(time_t t)
16{
17 static char day[][4] =
18 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
19 static char month[][4] =
20 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
21 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
22 struct tm *tm = gmtime(&t);
23 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
24 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
25 tm->tm_hour, tm->tm_min, tm->tm_sec);
26}
27
28static int ttl_seconds(int ttl)
29{
30 if (ttl<0)
31 return 60 * 60 * 24 * 365;
32 else
33 return ttl * 60;
34}
35
36void cgit_print_error(char *msg)
37{
38 html("<div class='error'>");
39 html_txt(msg);
40 html("</div>\n");
41}
42void cgit_print_docstart(char *title, struct cacheitem *item)
43{
44 html("Content-Type: text/html; charset=utf-8\n");
45 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
46 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
47 ttl_seconds(item->ttl)));
48 html("\n");
49 html(cgit_doctype);
50 html("<html>\n");
51 html("<head>\n");
52 html("<title>");
53 html_txt(title);
54 html("</title>\n");
55 htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version);
56 html("<link rel='stylesheet' type='text/css' href='");
57 html_attr(cgit_css);
58 html("'/>\n");
59 html("</head>\n");
60 html("<body>\n");
61}
62
63void cgit_print_docend()
64{
65 html("</body>\n</html>\n");
66}
67
68void cgit_print_pageheader(char *title)
69{
70 html("<div id='header'>");
71 htmlf("<a href='%s'>", cgit_logo_link);
72 htmlf("<img id='logo' src='%s'/>\n", cgit_logo);
73 htmlf("</a>");
74 html_txt(title);
75 html("</div>");
76}