-rw-r--r-- | ui-shared.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/ui-shared.c b/ui-shared.c index de77bbf..749ea35 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -1,228 +1,239 @@ | |||
1 | /* ui-shared.c: common web output functions | 1 | /* ui-shared.c: common web output functions |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | #include "cmd.h" | 10 | #include "cmd.h" |
11 | #include "html.h" | 11 | #include "html.h" |
12 | 12 | ||
13 | const char cgit_doctype[] = | 13 | const char cgit_doctype[] = |
14 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" | 14 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" |
15 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; | 15 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; |
16 | 16 | ||
17 | static char *http_date(time_t t) | 17 | static char *http_date(time_t t) |
18 | { | 18 | { |
19 | static char day[][4] = | 19 | static char day[][4] = |
20 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | 20 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
21 | static char month[][4] = | 21 | static char month[][4] = |
22 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | 22 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
23 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; | 23 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; |
24 | struct tm *tm = gmtime(&t); | 24 | struct tm *tm = gmtime(&t); |
25 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], | 25 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], |
26 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, | 26 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, |
27 | tm->tm_hour, tm->tm_min, tm->tm_sec); | 27 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
28 | } | 28 | } |
29 | 29 | ||
30 | void cgit_print_error(char *msg) | 30 | void cgit_print_error(char *msg) |
31 | { | 31 | { |
32 | html("<div class='error'>"); | 32 | html("<div class='error'>"); |
33 | html_txt(msg); | 33 | html_txt(msg); |
34 | html("</div>\n"); | 34 | html("</div>\n"); |
35 | } | 35 | } |
36 | 36 | ||
37 | char *cgit_httpscheme() | ||
38 | { | ||
39 | char *https; | ||
40 | |||
41 | https = getenv("HTTPS"); | ||
42 | if (https != NULL && strcmp(https, "on") == 0) | ||
43 | return "https://"; | ||
44 | else | ||
45 | return "http://"; | ||
46 | } | ||
47 | |||
37 | char *cgit_hosturl() | 48 | char *cgit_hosturl() |
38 | { | 49 | { |
39 | char *host, *port; | 50 | char *host, *port; |
40 | 51 | ||
41 | host = getenv("HTTP_HOST"); | 52 | host = getenv("HTTP_HOST"); |
42 | if (host) { | 53 | if (host) { |
43 | host = xstrdup(host); | 54 | host = xstrdup(host); |
44 | } else { | 55 | } else { |
45 | host = getenv("SERVER_NAME"); | 56 | host = getenv("SERVER_NAME"); |
46 | if (!host) | 57 | if (!host) |
47 | return NULL; | 58 | return NULL; |
48 | port = getenv("SERVER_PORT"); | 59 | port = getenv("SERVER_PORT"); |
49 | if (port && atoi(port) != 80) | 60 | if (port && atoi(port) != 80) |
50 | host = xstrdup(fmt("%s:%d", host, atoi(port))); | 61 | host = xstrdup(fmt("%s:%d", host, atoi(port))); |
51 | else | 62 | else |
52 | host = xstrdup(host); | 63 | host = xstrdup(host); |
53 | } | 64 | } |
54 | return host; | 65 | return host; |
55 | } | 66 | } |
56 | 67 | ||
57 | char *cgit_rooturl() | 68 | char *cgit_rooturl() |
58 | { | 69 | { |
59 | if (ctx.cfg.virtual_root) | 70 | if (ctx.cfg.virtual_root) |
60 | return fmt("%s/", ctx.cfg.virtual_root); | 71 | return fmt("%s/", ctx.cfg.virtual_root); |
61 | else | 72 | else |
62 | return ctx.cfg.script_name; | 73 | return ctx.cfg.script_name; |
63 | } | 74 | } |
64 | 75 | ||
65 | char *cgit_repourl(const char *reponame) | 76 | char *cgit_repourl(const char *reponame) |
66 | { | 77 | { |
67 | if (ctx.cfg.virtual_root) { | 78 | if (ctx.cfg.virtual_root) { |
68 | return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); | 79 | return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); |
69 | } else { | 80 | } else { |
70 | return fmt("?r=%s", reponame); | 81 | return fmt("?r=%s", reponame); |
71 | } | 82 | } |
72 | } | 83 | } |
73 | 84 | ||
74 | char *cgit_fileurl(const char *reponame, const char *pagename, | 85 | char *cgit_fileurl(const char *reponame, const char *pagename, |
75 | const char *filename, const char *query) | 86 | const char *filename, const char *query) |
76 | { | 87 | { |
77 | char *tmp; | 88 | char *tmp; |
78 | char *delim; | 89 | char *delim; |
79 | 90 | ||
80 | if (ctx.cfg.virtual_root) { | 91 | if (ctx.cfg.virtual_root) { |
81 | tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, | 92 | tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, |
82 | pagename, (filename ? filename:"")); | 93 | pagename, (filename ? filename:"")); |
83 | delim = "?"; | 94 | delim = "?"; |
84 | } else { | 95 | } else { |
85 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, | 96 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, |
86 | (filename ? filename : "")); | 97 | (filename ? filename : "")); |
87 | delim = "&"; | 98 | delim = "&"; |
88 | } | 99 | } |
89 | if (query) | 100 | if (query) |
90 | tmp = fmt("%s%s%s", tmp, delim, query); | 101 | tmp = fmt("%s%s%s", tmp, delim, query); |
91 | return tmp; | 102 | return tmp; |
92 | } | 103 | } |
93 | 104 | ||
94 | char *cgit_pageurl(const char *reponame, const char *pagename, | 105 | char *cgit_pageurl(const char *reponame, const char *pagename, |
95 | const char *query) | 106 | const char *query) |
96 | { | 107 | { |
97 | return cgit_fileurl(reponame,pagename,0,query); | 108 | return cgit_fileurl(reponame,pagename,0,query); |
98 | } | 109 | } |
99 | 110 | ||
100 | const char *cgit_repobasename(const char *reponame) | 111 | const char *cgit_repobasename(const char *reponame) |
101 | { | 112 | { |
102 | /* I assume we don't need to store more than one repo basename */ | 113 | /* I assume we don't need to store more than one repo basename */ |
103 | static char rvbuf[1024]; | 114 | static char rvbuf[1024]; |
104 | int p; | 115 | int p; |
105 | const char *rv; | 116 | const char *rv; |
106 | strncpy(rvbuf,reponame,sizeof(rvbuf)); | 117 | strncpy(rvbuf,reponame,sizeof(rvbuf)); |
107 | if(rvbuf[sizeof(rvbuf)-1]) | 118 | if(rvbuf[sizeof(rvbuf)-1]) |
108 | die("cgit_repobasename: truncated repository name '%s'", reponame); | 119 | die("cgit_repobasename: truncated repository name '%s'", reponame); |
109 | p = strlen(rvbuf)-1; | 120 | p = strlen(rvbuf)-1; |
110 | /* strip trailing slashes */ | 121 | /* strip trailing slashes */ |
111 | while(p && rvbuf[p]=='/') rvbuf[p--]=0; | 122 | while(p && rvbuf[p]=='/') rvbuf[p--]=0; |
112 | /* strip trailing .git */ | 123 | /* strip trailing .git */ |
113 | if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { | 124 | if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { |
114 | p -= 3; rvbuf[p--] = 0; | 125 | p -= 3; rvbuf[p--] = 0; |
115 | } | 126 | } |
116 | /* strip more trailing slashes if any */ | 127 | /* strip more trailing slashes if any */ |
117 | while( p && rvbuf[p]=='/') rvbuf[p--]=0; | 128 | while( p && rvbuf[p]=='/') rvbuf[p--]=0; |
118 | /* find last slash in the remaining string */ | 129 | /* find last slash in the remaining string */ |
119 | rv = strrchr(rvbuf,'/'); | 130 | rv = strrchr(rvbuf,'/'); |
120 | if(rv) | 131 | if(rv) |
121 | return ++rv; | 132 | return ++rv; |
122 | return rvbuf; | 133 | return rvbuf; |
123 | } | 134 | } |
124 | 135 | ||
125 | char *cgit_currurl() | 136 | char *cgit_currurl() |
126 | { | 137 | { |
127 | if (!ctx.cfg.virtual_root) | 138 | if (!ctx.cfg.virtual_root) |
128 | return ctx.cfg.script_name; | 139 | return ctx.cfg.script_name; |
129 | else if (ctx.qry.page) | 140 | else if (ctx.qry.page) |
130 | return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); | 141 | return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); |
131 | else if (ctx.qry.repo) | 142 | else if (ctx.qry.repo) |
132 | return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); | 143 | return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); |
133 | else | 144 | else |
134 | return fmt("%s/", ctx.cfg.virtual_root); | 145 | return fmt("%s/", ctx.cfg.virtual_root); |
135 | } | 146 | } |
136 | 147 | ||
137 | static void site_url(char *page, char *search, int ofs) | 148 | static void site_url(char *page, char *search, int ofs) |
138 | { | 149 | { |
139 | char *delim = "?"; | 150 | char *delim = "?"; |
140 | 151 | ||
141 | if (ctx.cfg.virtual_root) { | 152 | if (ctx.cfg.virtual_root) { |
142 | html_attr(ctx.cfg.virtual_root); | 153 | html_attr(ctx.cfg.virtual_root); |
143 | if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') | 154 | if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') |
144 | html("/"); | 155 | html("/"); |
145 | } else | 156 | } else |
146 | html(ctx.cfg.script_name); | 157 | html(ctx.cfg.script_name); |
147 | 158 | ||
148 | if (page) { | 159 | if (page) { |
149 | htmlf("?p=%s", page); | 160 | htmlf("?p=%s", page); |
150 | delim = "&"; | 161 | delim = "&"; |
151 | } | 162 | } |
152 | if (search) { | 163 | if (search) { |
153 | html(delim); | 164 | html(delim); |
154 | html("q="); | 165 | html("q="); |
155 | html_attr(search); | 166 | html_attr(search); |
156 | delim = "&"; | 167 | delim = "&"; |
157 | } | 168 | } |
158 | if (ofs) { | 169 | if (ofs) { |
159 | html(delim); | 170 | html(delim); |
160 | htmlf("ofs=%d", ofs); | 171 | htmlf("ofs=%d", ofs); |
161 | } | 172 | } |
162 | } | 173 | } |
163 | 174 | ||
164 | static void site_link(char *page, char *name, char *title, char *class, | 175 | static void site_link(char *page, char *name, char *title, char *class, |
165 | char *search, int ofs) | 176 | char *search, int ofs) |
166 | { | 177 | { |
167 | html("<a"); | 178 | html("<a"); |
168 | if (title) { | 179 | if (title) { |
169 | html(" title='"); | 180 | html(" title='"); |
170 | html_attr(title); | 181 | html_attr(title); |
171 | html("'"); | 182 | html("'"); |
172 | } | 183 | } |
173 | if (class) { | 184 | if (class) { |
174 | html(" class='"); | 185 | html(" class='"); |
175 | html_attr(class); | 186 | html_attr(class); |
176 | html("'"); | 187 | html("'"); |
177 | } | 188 | } |
178 | html(" href='"); | 189 | html(" href='"); |
179 | site_url(page, search, ofs); | 190 | site_url(page, search, ofs); |
180 | html("'>"); | 191 | html("'>"); |
181 | html_txt(name); | 192 | html_txt(name); |
182 | html("</a>"); | 193 | html("</a>"); |
183 | } | 194 | } |
184 | 195 | ||
185 | void cgit_index_link(char *name, char *title, char *class, char *pattern, | 196 | void cgit_index_link(char *name, char *title, char *class, char *pattern, |
186 | int ofs) | 197 | int ofs) |
187 | { | 198 | { |
188 | site_link(NULL, name, title, class, pattern, ofs); | 199 | site_link(NULL, name, title, class, pattern, ofs); |
189 | } | 200 | } |
190 | 201 | ||
191 | static char *repolink(char *title, char *class, char *page, char *head, | 202 | static char *repolink(char *title, char *class, char *page, char *head, |
192 | char *path) | 203 | char *path) |
193 | { | 204 | { |
194 | char *delim = "?"; | 205 | char *delim = "?"; |
195 | 206 | ||
196 | html("<a"); | 207 | html("<a"); |
197 | if (title) { | 208 | if (title) { |
198 | html(" title='"); | 209 | html(" title='"); |
199 | html_attr(title); | 210 | html_attr(title); |
200 | html("'"); | 211 | html("'"); |
201 | } | 212 | } |
202 | if (class) { | 213 | if (class) { |
203 | html(" class='"); | 214 | html(" class='"); |
204 | html_attr(class); | 215 | html_attr(class); |
205 | html("'"); | 216 | html("'"); |
206 | } | 217 | } |
207 | html(" href='"); | 218 | html(" href='"); |
208 | if (ctx.cfg.virtual_root) { | 219 | if (ctx.cfg.virtual_root) { |
209 | html_url_path(ctx.cfg.virtual_root); | 220 | html_url_path(ctx.cfg.virtual_root); |
210 | if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') | 221 | if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') |
211 | html("/"); | 222 | html("/"); |
212 | html_url_path(ctx.repo->url); | 223 | html_url_path(ctx.repo->url); |
213 | if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') | 224 | if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') |
214 | html("/"); | 225 | html("/"); |
215 | if (page) { | 226 | if (page) { |
216 | html_url_path(page); | 227 | html_url_path(page); |
217 | html("/"); | 228 | html("/"); |
218 | if (path) | 229 | if (path) |
219 | html_url_path(path); | 230 | html_url_path(path); |
220 | } | 231 | } |
221 | } else { | 232 | } else { |
222 | html(ctx.cfg.script_name); | 233 | html(ctx.cfg.script_name); |
223 | html("?url="); | 234 | html("?url="); |
224 | html_url_arg(ctx.repo->url); | 235 | html_url_arg(ctx.repo->url); |
225 | if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') | 236 | if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') |
226 | html("/"); | 237 | html("/"); |
227 | if (page) { | 238 | if (page) { |
228 | html_url_arg(page); | 239 | html_url_arg(page); |