summaryrefslogtreecommitdiffabout
path: root/cgit.c
authorLars Hjemli <hjemli@gmail.com>2009-02-13 19:43:30 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2009-07-25 10:55:15 (UTC)
commitc4d46c7035d07070ac1ebf0c0b44df927358687f (patch) (unidiff)
tree562cf4f01afe78b92b13ad5be5dc01e07db04a66 /cgit.c
parent286a905842dc0bec6d21a614ec4a97c5f19d5bc4 (diff)
downloadcgit-c4d46c7035d07070ac1ebf0c0b44df927358687f.zip
cgit-c4d46c7035d07070ac1ebf0c0b44df927358687f.tar.gz
cgit-c4d46c7035d07070ac1ebf0c0b44df927358687f.tar.bz2
Add support for mime type registration and lookup
This patch makes it possible to register mappings from filename extension to mime type in cgitrc and use this mapping when returning blob content in `plain` view. The reason for adding this mapping to cgitrc (as opposed to parsing something like /etc/mime.types) is to allow quick lookup of a limited number of filename extensions (/etc/mime-types on my machine currently contains over 700 entries). NB: A nice addition to this patch would be to parse /etc/mime.types when `plain` view is requested for a file with an extension for which there is no mapping registered in cgitrc. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'cgit.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/cgit.c b/cgit.c
index 2039ab1..4f414c3 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,498 +1,509 @@
1/* cgit.c: cgi for the git scm 1/* cgit.c: cgi for the git scm
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 "cache.h" 10#include "cache.h"
11#include "cmd.h" 11#include "cmd.h"
12#include "configfile.h" 12#include "configfile.h"
13#include "html.h" 13#include "html.h"
14#include "ui-shared.h" 14#include "ui-shared.h"
15#include "ui-stats.h" 15#include "ui-stats.h"
16#include "scan-tree.h" 16#include "scan-tree.h"
17 17
18const char *cgit_version = CGIT_VERSION; 18const char *cgit_version = CGIT_VERSION;
19 19
20void add_mimetype(const char *name, const char *value)
21{
22 struct string_list_item *item;
23
24 item = string_list_insert(xstrdup(name), &ctx.cfg.mimetypes);
25 item->util = xstrdup(value);
26}
27
20void config_cb(const char *name, const char *value) 28void config_cb(const char *name, const char *value)
21{ 29{
22 if (!strcmp(name, "root-title")) 30 if (!strcmp(name, "root-title"))
23 ctx.cfg.root_title = xstrdup(value); 31 ctx.cfg.root_title = xstrdup(value);
24 else if (!strcmp(name, "root-desc")) 32 else if (!strcmp(name, "root-desc"))
25 ctx.cfg.root_desc = xstrdup(value); 33 ctx.cfg.root_desc = xstrdup(value);
26 else if (!strcmp(name, "root-readme")) 34 else if (!strcmp(name, "root-readme"))
27 ctx.cfg.root_readme = xstrdup(value); 35 ctx.cfg.root_readme = xstrdup(value);
28 else if (!strcmp(name, "css")) 36 else if (!strcmp(name, "css"))
29 ctx.cfg.css = xstrdup(value); 37 ctx.cfg.css = xstrdup(value);
30 else if (!strcmp(name, "favicon")) 38 else if (!strcmp(name, "favicon"))
31 ctx.cfg.favicon = xstrdup(value); 39 ctx.cfg.favicon = xstrdup(value);
32 else if (!strcmp(name, "footer")) 40 else if (!strcmp(name, "footer"))
33 ctx.cfg.footer = xstrdup(value); 41 ctx.cfg.footer = xstrdup(value);
34 else if (!strcmp(name, "head-include")) 42 else if (!strcmp(name, "head-include"))
35 ctx.cfg.head_include = xstrdup(value); 43 ctx.cfg.head_include = xstrdup(value);
36 else if (!strcmp(name, "header")) 44 else if (!strcmp(name, "header"))
37 ctx.cfg.header = xstrdup(value); 45 ctx.cfg.header = xstrdup(value);
38 else if (!strcmp(name, "logo")) 46 else if (!strcmp(name, "logo"))
39 ctx.cfg.logo = xstrdup(value); 47 ctx.cfg.logo = xstrdup(value);
40 else if (!strcmp(name, "index-header")) 48 else if (!strcmp(name, "index-header"))
41 ctx.cfg.index_header = xstrdup(value); 49 ctx.cfg.index_header = xstrdup(value);
42 else if (!strcmp(name, "index-info")) 50 else if (!strcmp(name, "index-info"))
43 ctx.cfg.index_info = xstrdup(value); 51 ctx.cfg.index_info = xstrdup(value);
44 else if (!strcmp(name, "logo-link")) 52 else if (!strcmp(name, "logo-link"))
45 ctx.cfg.logo_link = xstrdup(value); 53 ctx.cfg.logo_link = xstrdup(value);
46 else if (!strcmp(name, "module-link")) 54 else if (!strcmp(name, "module-link"))
47 ctx.cfg.module_link = xstrdup(value); 55 ctx.cfg.module_link = xstrdup(value);
48 else if (!strcmp(name, "virtual-root")) { 56 else if (!strcmp(name, "virtual-root")) {
49 ctx.cfg.virtual_root = trim_end(value, '/'); 57 ctx.cfg.virtual_root = trim_end(value, '/');
50 if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) 58 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
51 ctx.cfg.virtual_root = ""; 59 ctx.cfg.virtual_root = "";
52 } else if (!strcmp(name, "nocache")) 60 } else if (!strcmp(name, "nocache"))
53 ctx.cfg.nocache = atoi(value); 61 ctx.cfg.nocache = atoi(value);
54 else if (!strcmp(name, "noheader")) 62 else if (!strcmp(name, "noheader"))
55 ctx.cfg.noheader = atoi(value); 63 ctx.cfg.noheader = atoi(value);
56 else if (!strcmp(name, "snapshots")) 64 else if (!strcmp(name, "snapshots"))
57 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); 65 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
58 else if (!strcmp(name, "enable-index-links")) 66 else if (!strcmp(name, "enable-index-links"))
59 ctx.cfg.enable_index_links = atoi(value); 67 ctx.cfg.enable_index_links = atoi(value);
60 else if (!strcmp(name, "enable-log-filecount")) 68 else if (!strcmp(name, "enable-log-filecount"))
61 ctx.cfg.enable_log_filecount = atoi(value); 69 ctx.cfg.enable_log_filecount = atoi(value);
62 else if (!strcmp(name, "enable-log-linecount")) 70 else if (!strcmp(name, "enable-log-linecount"))
63 ctx.cfg.enable_log_linecount = atoi(value); 71 ctx.cfg.enable_log_linecount = atoi(value);
64 else if (!strcmp(name, "max-stats")) 72 else if (!strcmp(name, "max-stats"))
65 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL); 73 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
66 else if (!strcmp(name, "cache-size")) 74 else if (!strcmp(name, "cache-size"))
67 ctx.cfg.cache_size = atoi(value); 75 ctx.cfg.cache_size = atoi(value);
68 else if (!strcmp(name, "cache-root")) 76 else if (!strcmp(name, "cache-root"))
69 ctx.cfg.cache_root = xstrdup(value); 77 ctx.cfg.cache_root = xstrdup(value);
70 else if (!strcmp(name, "cache-root-ttl")) 78 else if (!strcmp(name, "cache-root-ttl"))
71 ctx.cfg.cache_root_ttl = atoi(value); 79 ctx.cfg.cache_root_ttl = atoi(value);
72 else if (!strcmp(name, "cache-repo-ttl")) 80 else if (!strcmp(name, "cache-repo-ttl"))
73 ctx.cfg.cache_repo_ttl = atoi(value); 81 ctx.cfg.cache_repo_ttl = atoi(value);
74 else if (!strcmp(name, "cache-static-ttl")) 82 else if (!strcmp(name, "cache-static-ttl"))
75 ctx.cfg.cache_static_ttl = atoi(value); 83 ctx.cfg.cache_static_ttl = atoi(value);
76 else if (!strcmp(name, "cache-dynamic-ttl")) 84 else if (!strcmp(name, "cache-dynamic-ttl"))
77 ctx.cfg.cache_dynamic_ttl = atoi(value); 85 ctx.cfg.cache_dynamic_ttl = atoi(value);
78 else if (!strcmp(name, "embedded")) 86 else if (!strcmp(name, "embedded"))
79 ctx.cfg.embedded = atoi(value); 87 ctx.cfg.embedded = atoi(value);
80 else if (!strcmp(name, "max-message-length")) 88 else if (!strcmp(name, "max-message-length"))
81 ctx.cfg.max_msg_len = atoi(value); 89 ctx.cfg.max_msg_len = atoi(value);
82 else if (!strcmp(name, "max-repodesc-length")) 90 else if (!strcmp(name, "max-repodesc-length"))
83 ctx.cfg.max_repodesc_len = atoi(value); 91 ctx.cfg.max_repodesc_len = atoi(value);
84 else if (!strcmp(name, "max-repo-count")) 92 else if (!strcmp(name, "max-repo-count"))
85 ctx.cfg.max_repo_count = atoi(value); 93 ctx.cfg.max_repo_count = atoi(value);
86 else if (!strcmp(name, "max-commit-count")) 94 else if (!strcmp(name, "max-commit-count"))
87 ctx.cfg.max_commit_count = atoi(value); 95 ctx.cfg.max_commit_count = atoi(value);
88 else if (!strcmp(name, "summary-log")) 96 else if (!strcmp(name, "summary-log"))
89 ctx.cfg.summary_log = atoi(value); 97 ctx.cfg.summary_log = atoi(value);
90 else if (!strcmp(name, "summary-branches")) 98 else if (!strcmp(name, "summary-branches"))
91 ctx.cfg.summary_branches = atoi(value); 99 ctx.cfg.summary_branches = atoi(value);
92 else if (!strcmp(name, "summary-tags")) 100 else if (!strcmp(name, "summary-tags"))
93 ctx.cfg.summary_tags = atoi(value); 101 ctx.cfg.summary_tags = atoi(value);
94 else if (!strcmp(name, "agefile")) 102 else if (!strcmp(name, "agefile"))
95 ctx.cfg.agefile = xstrdup(value); 103 ctx.cfg.agefile = xstrdup(value);
96 else if (!strcmp(name, "renamelimit")) 104 else if (!strcmp(name, "renamelimit"))
97 ctx.cfg.renamelimit = atoi(value); 105 ctx.cfg.renamelimit = atoi(value);
98 else if (!strcmp(name, "robots")) 106 else if (!strcmp(name, "robots"))
99 ctx.cfg.robots = xstrdup(value); 107 ctx.cfg.robots = xstrdup(value);
100 else if (!strcmp(name, "clone-prefix")) 108 else if (!strcmp(name, "clone-prefix"))
101 ctx.cfg.clone_prefix = xstrdup(value); 109 ctx.cfg.clone_prefix = xstrdup(value);
102 else if (!strcmp(name, "local-time")) 110 else if (!strcmp(name, "local-time"))
103 ctx.cfg.local_time = atoi(value); 111 ctx.cfg.local_time = atoi(value);
112 else if (!prefixcmp(name, "mimetype."))
113 add_mimetype(name + 9, value);
104 else if (!strcmp(name, "repo.group")) 114 else if (!strcmp(name, "repo.group"))
105 ctx.cfg.repo_group = xstrdup(value); 115 ctx.cfg.repo_group = xstrdup(value);
106 else if (!strcmp(name, "repo.url")) 116 else if (!strcmp(name, "repo.url"))
107 ctx.repo = cgit_add_repo(value); 117 ctx.repo = cgit_add_repo(value);
108 else if (!strcmp(name, "repo.name")) 118 else if (!strcmp(name, "repo.name"))
109 ctx.repo->name = xstrdup(value); 119 ctx.repo->name = xstrdup(value);
110 else if (ctx.repo && !strcmp(name, "repo.path")) 120 else if (ctx.repo && !strcmp(name, "repo.path"))
111 ctx.repo->path = trim_end(value, '/'); 121 ctx.repo->path = trim_end(value, '/');
112 else if (ctx.repo && !strcmp(name, "repo.clone-url")) 122 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
113 ctx.repo->clone_url = xstrdup(value); 123 ctx.repo->clone_url = xstrdup(value);
114 else if (ctx.repo && !strcmp(name, "repo.desc")) 124 else if (ctx.repo && !strcmp(name, "repo.desc"))
115 ctx.repo->desc = xstrdup(value); 125 ctx.repo->desc = xstrdup(value);
116 else if (ctx.repo && !strcmp(name, "repo.owner")) 126 else if (ctx.repo && !strcmp(name, "repo.owner"))
117 ctx.repo->owner = xstrdup(value); 127 ctx.repo->owner = xstrdup(value);
118 else if (ctx.repo && !strcmp(name, "repo.defbranch")) 128 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
119 ctx.repo->defbranch = xstrdup(value); 129 ctx.repo->defbranch = xstrdup(value);
120 else if (ctx.repo && !strcmp(name, "repo.snapshots")) 130 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
121 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ 131 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
122 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) 132 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
123 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); 133 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
124 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) 134 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
125 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); 135 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
126 else if (ctx.repo && !strcmp(name, "repo.max-stats")) 136 else if (ctx.repo && !strcmp(name, "repo.max-stats"))
127 ctx.repo->max_stats = cgit_find_stats_period(value, NULL); 137 ctx.repo->max_stats = cgit_find_stats_period(value, NULL);
128 else if (ctx.repo && !strcmp(name, "repo.module-link")) 138 else if (ctx.repo && !strcmp(name, "repo.module-link"))
129 ctx.repo->module_link= xstrdup(value); 139 ctx.repo->module_link= xstrdup(value);
130 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { 140 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
131 if (*value == '/') 141 if (*value == '/')
132 ctx.repo->readme = xstrdup(value); 142 ctx.repo->readme = xstrdup(value);
133 else 143 else
134 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); 144 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
135 } else if (!strcmp(name, "include")) 145 } else if (!strcmp(name, "include"))
136 parse_configfile(value, config_cb); 146 parse_configfile(value, config_cb);
137} 147}
138 148
139static void querystring_cb(const char *name, const char *value) 149static void querystring_cb(const char *name, const char *value)
140{ 150{
141 if (!strcmp(name,"r")) { 151 if (!strcmp(name,"r")) {
142 ctx.qry.repo = xstrdup(value); 152 ctx.qry.repo = xstrdup(value);
143 ctx.repo = cgit_get_repoinfo(value); 153 ctx.repo = cgit_get_repoinfo(value);
144 } else if (!strcmp(name, "p")) { 154 } else if (!strcmp(name, "p")) {
145 ctx.qry.page = xstrdup(value); 155 ctx.qry.page = xstrdup(value);
146 } else if (!strcmp(name, "url")) { 156 } else if (!strcmp(name, "url")) {
147 ctx.qry.url = xstrdup(value); 157 ctx.qry.url = xstrdup(value);
148 cgit_parse_url(value); 158 cgit_parse_url(value);
149 } else if (!strcmp(name, "qt")) { 159 } else if (!strcmp(name, "qt")) {
150 ctx.qry.grep = xstrdup(value); 160 ctx.qry.grep = xstrdup(value);
151 } else if (!strcmp(name, "q")) { 161 } else if (!strcmp(name, "q")) {
152 ctx.qry.search = xstrdup(value); 162 ctx.qry.search = xstrdup(value);
153 } else if (!strcmp(name, "h")) { 163 } else if (!strcmp(name, "h")) {
154 ctx.qry.head = xstrdup(value); 164 ctx.qry.head = xstrdup(value);
155 ctx.qry.has_symref = 1; 165 ctx.qry.has_symref = 1;
156 } else if (!strcmp(name, "id")) { 166 } else if (!strcmp(name, "id")) {
157 ctx.qry.sha1 = xstrdup(value); 167 ctx.qry.sha1 = xstrdup(value);
158 ctx.qry.has_sha1 = 1; 168 ctx.qry.has_sha1 = 1;
159 } else if (!strcmp(name, "id2")) { 169 } else if (!strcmp(name, "id2")) {
160 ctx.qry.sha2 = xstrdup(value); 170 ctx.qry.sha2 = xstrdup(value);
161 ctx.qry.has_sha1 = 1; 171 ctx.qry.has_sha1 = 1;
162 } else if (!strcmp(name, "ofs")) { 172 } else if (!strcmp(name, "ofs")) {
163 ctx.qry.ofs = atoi(value); 173 ctx.qry.ofs = atoi(value);
164 } else if (!strcmp(name, "path")) { 174 } else if (!strcmp(name, "path")) {
165 ctx.qry.path = trim_end(value, '/'); 175 ctx.qry.path = trim_end(value, '/');
166 } else if (!strcmp(name, "name")) { 176 } else if (!strcmp(name, "name")) {
167 ctx.qry.name = xstrdup(value); 177 ctx.qry.name = xstrdup(value);
168 } else if (!strcmp(name, "mimetype")) { 178 } else if (!strcmp(name, "mimetype")) {
169 ctx.qry.mimetype = xstrdup(value); 179 ctx.qry.mimetype = xstrdup(value);
170 } else if (!strcmp(name, "s")){ 180 } else if (!strcmp(name, "s")){
171 ctx.qry.sort = xstrdup(value); 181 ctx.qry.sort = xstrdup(value);
172 } else if (!strcmp(name, "showmsg")) { 182 } else if (!strcmp(name, "showmsg")) {
173 ctx.qry.showmsg = atoi(value); 183 ctx.qry.showmsg = atoi(value);
174 } else if (!strcmp(name, "period")) { 184 } else if (!strcmp(name, "period")) {
175 ctx.qry.period = xstrdup(value); 185 ctx.qry.period = xstrdup(value);
176 } 186 }
177} 187}
178 188
179static void prepare_context(struct cgit_context *ctx) 189static void prepare_context(struct cgit_context *ctx)
180{ 190{
181 memset(ctx, 0, sizeof(ctx)); 191 memset(ctx, 0, sizeof(ctx));
182 ctx->cfg.agefile = "info/web/last-modified"; 192 ctx->cfg.agefile = "info/web/last-modified";
183 ctx->cfg.nocache = 0; 193 ctx->cfg.nocache = 0;
184 ctx->cfg.cache_size = 0; 194 ctx->cfg.cache_size = 0;
185 ctx->cfg.cache_dynamic_ttl = 5; 195 ctx->cfg.cache_dynamic_ttl = 5;
186 ctx->cfg.cache_max_create_time = 5; 196 ctx->cfg.cache_max_create_time = 5;
187 ctx->cfg.cache_repo_ttl = 5; 197 ctx->cfg.cache_repo_ttl = 5;
188 ctx->cfg.cache_root = CGIT_CACHE_ROOT; 198 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
189 ctx->cfg.cache_root_ttl = 5; 199 ctx->cfg.cache_root_ttl = 5;
190 ctx->cfg.cache_static_ttl = -1; 200 ctx->cfg.cache_static_ttl = -1;
191 ctx->cfg.css = "/cgit.css"; 201 ctx->cfg.css = "/cgit.css";
192 ctx->cfg.logo = "/git-logo.png"; 202 ctx->cfg.logo = "/git-logo.png";
193 ctx->cfg.local_time = 0; 203 ctx->cfg.local_time = 0;
194 ctx->cfg.max_repo_count = 50; 204 ctx->cfg.max_repo_count = 50;
195 ctx->cfg.max_commit_count = 50; 205 ctx->cfg.max_commit_count = 50;
196 ctx->cfg.max_lock_attempts = 5; 206 ctx->cfg.max_lock_attempts = 5;
197 ctx->cfg.max_msg_len = 80; 207 ctx->cfg.max_msg_len = 80;
198 ctx->cfg.max_repodesc_len = 80; 208 ctx->cfg.max_repodesc_len = 80;
199 ctx->cfg.max_stats = 0; 209 ctx->cfg.max_stats = 0;
200 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; 210 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
201 ctx->cfg.renamelimit = -1; 211 ctx->cfg.renamelimit = -1;
202 ctx->cfg.robots = "index, nofollow"; 212 ctx->cfg.robots = "index, nofollow";
203 ctx->cfg.root_title = "Git repository browser"; 213 ctx->cfg.root_title = "Git repository browser";
204 ctx->cfg.root_desc = "a fast webinterface for the git dscm"; 214 ctx->cfg.root_desc = "a fast webinterface for the git dscm";
205 ctx->cfg.script_name = CGIT_SCRIPT_NAME; 215 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
206 ctx->cfg.summary_branches = 10; 216 ctx->cfg.summary_branches = 10;
207 ctx->cfg.summary_log = 10; 217 ctx->cfg.summary_log = 10;
208 ctx->cfg.summary_tags = 10; 218 ctx->cfg.summary_tags = 10;
209 ctx->page.mimetype = "text/html"; 219 ctx->page.mimetype = "text/html";
210 ctx->page.charset = PAGE_ENCODING; 220 ctx->page.charset = PAGE_ENCODING;
211 ctx->page.filename = NULL; 221 ctx->page.filename = NULL;
212 ctx->page.size = 0; 222 ctx->page.size = 0;
213 ctx->page.modified = time(NULL); 223 ctx->page.modified = time(NULL);
214 ctx->page.expires = ctx->page.modified; 224 ctx->page.expires = ctx->page.modified;
215 ctx->page.etag = NULL; 225 ctx->page.etag = NULL;
226 memset(&ctx->cfg.mimetypes, 0, sizeof(struct string_list));
216} 227}
217 228
218struct refmatch { 229struct refmatch {
219 char *req_ref; 230 char *req_ref;
220 char *first_ref; 231 char *first_ref;
221 int match; 232 int match;
222}; 233};
223 234
224int find_current_ref(const char *refname, const unsigned char *sha1, 235int find_current_ref(const char *refname, const unsigned char *sha1,
225 int flags, void *cb_data) 236 int flags, void *cb_data)
226{ 237{
227 struct refmatch *info; 238 struct refmatch *info;
228 239
229 info = (struct refmatch *)cb_data; 240 info = (struct refmatch *)cb_data;
230 if (!strcmp(refname, info->req_ref)) 241 if (!strcmp(refname, info->req_ref))
231 info->match = 1; 242 info->match = 1;
232 if (!info->first_ref) 243 if (!info->first_ref)
233 info->first_ref = xstrdup(refname); 244 info->first_ref = xstrdup(refname);
234 return info->match; 245 return info->match;
235} 246}
236 247
237char *find_default_branch(struct cgit_repo *repo) 248char *find_default_branch(struct cgit_repo *repo)
238{ 249{
239 struct refmatch info; 250 struct refmatch info;
240 char *ref; 251 char *ref;
241 252
242 info.req_ref = repo->defbranch; 253 info.req_ref = repo->defbranch;
243 info.first_ref = NULL; 254 info.first_ref = NULL;
244 info.match = 0; 255 info.match = 0;
245 for_each_branch_ref(find_current_ref, &info); 256 for_each_branch_ref(find_current_ref, &info);
246 if (info.match) 257 if (info.match)
247 ref = info.req_ref; 258 ref = info.req_ref;
248 else 259 else
249 ref = info.first_ref; 260 ref = info.first_ref;
250 if (ref) 261 if (ref)
251 ref = xstrdup(ref); 262 ref = xstrdup(ref);
252 return ref; 263 return ref;
253} 264}
254 265
255static int prepare_repo_cmd(struct cgit_context *ctx) 266static int prepare_repo_cmd(struct cgit_context *ctx)
256{ 267{
257 char *tmp; 268 char *tmp;
258 unsigned char sha1[20]; 269 unsigned char sha1[20];
259 int nongit = 0; 270 int nongit = 0;
260 271
261 setenv("GIT_DIR", ctx->repo->path, 1); 272 setenv("GIT_DIR", ctx->repo->path, 1);
262 setup_git_directory_gently(&nongit); 273 setup_git_directory_gently(&nongit);
263 if (nongit) { 274 if (nongit) {
264 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, 275 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
265 "config error"); 276 "config error");
266 tmp = fmt("Not a git repository: '%s'", ctx->repo->path); 277 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
267 ctx->repo = NULL; 278 ctx->repo = NULL;
268 cgit_print_http_headers(ctx); 279 cgit_print_http_headers(ctx);
269 cgit_print_docstart(ctx); 280 cgit_print_docstart(ctx);
270 cgit_print_pageheader(ctx); 281 cgit_print_pageheader(ctx);
271 cgit_print_error(tmp); 282 cgit_print_error(tmp);
272 cgit_print_docend(); 283 cgit_print_docend();
273 return 1; 284 return 1;
274 } 285 }
275 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); 286 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
276 287
277 if (!ctx->qry.head) { 288 if (!ctx->qry.head) {
278 ctx->qry.nohead = 1; 289 ctx->qry.nohead = 1;
279 ctx->qry.head = find_default_branch(ctx->repo); 290 ctx->qry.head = find_default_branch(ctx->repo);
280 ctx->repo->defbranch = ctx->qry.head; 291 ctx->repo->defbranch = ctx->qry.head;
281 } 292 }
282 293
283 if (!ctx->qry.head) { 294 if (!ctx->qry.head) {
284 cgit_print_http_headers(ctx); 295 cgit_print_http_headers(ctx);
285 cgit_print_docstart(ctx); 296 cgit_print_docstart(ctx);
286 cgit_print_pageheader(ctx); 297 cgit_print_pageheader(ctx);
287 cgit_print_error("Repository seems to be empty"); 298 cgit_print_error("Repository seems to be empty");
288 cgit_print_docend(); 299 cgit_print_docend();
289 return 1; 300 return 1;
290 } 301 }
291 302
292 if (get_sha1(ctx->qry.head, sha1)) { 303 if (get_sha1(ctx->qry.head, sha1)) {
293 tmp = xstrdup(ctx->qry.head); 304 tmp = xstrdup(ctx->qry.head);
294 ctx->qry.head = ctx->repo->defbranch; 305 ctx->qry.head = ctx->repo->defbranch;
295 ctx->page.status = 404; 306 ctx->page.status = 404;
296 ctx->page.statusmsg = "not found"; 307 ctx->page.statusmsg = "not found";
297 cgit_print_http_headers(ctx); 308 cgit_print_http_headers(ctx);
298 cgit_print_docstart(ctx); 309 cgit_print_docstart(ctx);
299 cgit_print_pageheader(ctx); 310 cgit_print_pageheader(ctx);
300 cgit_print_error(fmt("Invalid branch: %s", tmp)); 311 cgit_print_error(fmt("Invalid branch: %s", tmp));
301 cgit_print_docend(); 312 cgit_print_docend();
302 return 1; 313 return 1;
303 } 314 }
304 return 0; 315 return 0;
305} 316}
306 317
307static void process_request(void *cbdata) 318static void process_request(void *cbdata)
308{ 319{
309 struct cgit_context *ctx = cbdata; 320 struct cgit_context *ctx = cbdata;
310 struct cgit_cmd *cmd; 321 struct cgit_cmd *cmd;
311 322
312 cmd = cgit_get_cmd(ctx); 323 cmd = cgit_get_cmd(ctx);
313 if (!cmd) { 324 if (!cmd) {
314 ctx->page.title = "cgit error"; 325 ctx->page.title = "cgit error";
315 cgit_print_http_headers(ctx); 326 cgit_print_http_headers(ctx);
316 cgit_print_docstart(ctx); 327 cgit_print_docstart(ctx);
317 cgit_print_pageheader(ctx); 328 cgit_print_pageheader(ctx);
318 cgit_print_error("Invalid request"); 329 cgit_print_error("Invalid request");
319 cgit_print_docend(); 330 cgit_print_docend();
320 return; 331 return;
321 } 332 }
322 333
323 if (cmd->want_repo && !ctx->repo) { 334 if (cmd->want_repo && !ctx->repo) {
324 cgit_print_http_headers(ctx); 335 cgit_print_http_headers(ctx);
325 cgit_print_docstart(ctx); 336 cgit_print_docstart(ctx);
326 cgit_print_pageheader(ctx); 337 cgit_print_pageheader(ctx);
327 cgit_print_error(fmt("No repository selected")); 338 cgit_print_error(fmt("No repository selected"));
328 cgit_print_docend(); 339 cgit_print_docend();
329 return; 340 return;
330 } 341 }
331 342
332 if (ctx->repo && prepare_repo_cmd(ctx)) 343 if (ctx->repo && prepare_repo_cmd(ctx))
333 return; 344 return;
334 345
335 if (cmd->want_layout) { 346 if (cmd->want_layout) {
336 cgit_print_http_headers(ctx); 347 cgit_print_http_headers(ctx);
337 cgit_print_docstart(ctx); 348 cgit_print_docstart(ctx);
338 cgit_print_pageheader(ctx); 349 cgit_print_pageheader(ctx);
339 } 350 }
340 351
341 cmd->fn(ctx); 352 cmd->fn(ctx);
342 353
343 if (cmd->want_layout) 354 if (cmd->want_layout)
344 cgit_print_docend(); 355 cgit_print_docend();
345} 356}
346 357
347int cmp_repos(const void *a, const void *b) 358int cmp_repos(const void *a, const void *b)
348{ 359{
349 const struct cgit_repo *ra = a, *rb = b; 360 const struct cgit_repo *ra = a, *rb = b;
350 return strcmp(ra->url, rb->url); 361 return strcmp(ra->url, rb->url);
351} 362}
352 363
353void print_repo(struct cgit_repo *repo) 364void print_repo(struct cgit_repo *repo)
354{ 365{
355 printf("repo.url=%s\n", repo->url); 366 printf("repo.url=%s\n", repo->url);
356 printf("repo.name=%s\n", repo->name); 367 printf("repo.name=%s\n", repo->name);
357 printf("repo.path=%s\n", repo->path); 368 printf("repo.path=%s\n", repo->path);
358 if (repo->owner) 369 if (repo->owner)
359 printf("repo.owner=%s\n", repo->owner); 370 printf("repo.owner=%s\n", repo->owner);
360 if (repo->desc) 371 if (repo->desc)
361 printf("repo.desc=%s\n", repo->desc); 372 printf("repo.desc=%s\n", repo->desc);
362 if (repo->readme) 373 if (repo->readme)
363 printf("repo.readme=%s\n", repo->readme); 374 printf("repo.readme=%s\n", repo->readme);
364 printf("\n"); 375 printf("\n");
365} 376}
366 377
367void print_repolist(struct cgit_repolist *list) 378void print_repolist(struct cgit_repolist *list)
368{ 379{
369 int i; 380 int i;
370 381
371 for(i = 0; i < list->count; i++) 382 for(i = 0; i < list->count; i++)
372 print_repo(&list->repos[i]); 383 print_repo(&list->repos[i]);
373} 384}
374 385
375 386
376static void cgit_parse_args(int argc, const char **argv) 387static void cgit_parse_args(int argc, const char **argv)
377{ 388{
378 int i; 389 int i;
379 int scan = 0; 390 int scan = 0;
380 391
381 for (i = 1; i < argc; i++) { 392 for (i = 1; i < argc; i++) {
382 if (!strncmp(argv[i], "--cache=", 8)) { 393 if (!strncmp(argv[i], "--cache=", 8)) {
383 ctx.cfg.cache_root = xstrdup(argv[i]+8); 394 ctx.cfg.cache_root = xstrdup(argv[i]+8);
384 } 395 }
385 if (!strcmp(argv[i], "--nocache")) { 396 if (!strcmp(argv[i], "--nocache")) {
386 ctx.cfg.nocache = 1; 397 ctx.cfg.nocache = 1;
387 } 398 }
388 if (!strncmp(argv[i], "--query=", 8)) { 399 if (!strncmp(argv[i], "--query=", 8)) {
389 ctx.qry.raw = xstrdup(argv[i]+8); 400 ctx.qry.raw = xstrdup(argv[i]+8);
390 } 401 }
391 if (!strncmp(argv[i], "--repo=", 7)) { 402 if (!strncmp(argv[i], "--repo=", 7)) {
392 ctx.qry.repo = xstrdup(argv[i]+7); 403 ctx.qry.repo = xstrdup(argv[i]+7);
393 } 404 }
394 if (!strncmp(argv[i], "--page=", 7)) { 405 if (!strncmp(argv[i], "--page=", 7)) {
395 ctx.qry.page = xstrdup(argv[i]+7); 406 ctx.qry.page = xstrdup(argv[i]+7);
396 } 407 }
397 if (!strncmp(argv[i], "--head=", 7)) { 408 if (!strncmp(argv[i], "--head=", 7)) {
398 ctx.qry.head = xstrdup(argv[i]+7); 409 ctx.qry.head = xstrdup(argv[i]+7);
399 ctx.qry.has_symref = 1; 410 ctx.qry.has_symref = 1;
400 } 411 }
401 if (!strncmp(argv[i], "--sha1=", 7)) { 412 if (!strncmp(argv[i], "--sha1=", 7)) {
402 ctx.qry.sha1 = xstrdup(argv[i]+7); 413 ctx.qry.sha1 = xstrdup(argv[i]+7);
403 ctx.qry.has_sha1 = 1; 414 ctx.qry.has_sha1 = 1;
404 } 415 }
405 if (!strncmp(argv[i], "--ofs=", 6)) { 416 if (!strncmp(argv[i], "--ofs=", 6)) {
406 ctx.qry.ofs = atoi(argv[i]+6); 417 ctx.qry.ofs = atoi(argv[i]+6);
407 } 418 }
408 if (!strncmp(argv[i], "--scan-tree=", 12)) { 419 if (!strncmp(argv[i], "--scan-tree=", 12)) {
409 scan++; 420 scan++;
410 scan_tree(argv[i] + 12); 421 scan_tree(argv[i] + 12);
411 } 422 }
412 } 423 }
413 if (scan) { 424 if (scan) {
414 qsort(cgit_repolist.repos, cgit_repolist.count, 425 qsort(cgit_repolist.repos, cgit_repolist.count,
415 sizeof(struct cgit_repo), cmp_repos); 426 sizeof(struct cgit_repo), cmp_repos);
416 print_repolist(&cgit_repolist); 427 print_repolist(&cgit_repolist);
417 exit(0); 428 exit(0);
418 } 429 }
419} 430}
420 431
421static int calc_ttl() 432static int calc_ttl()
422{ 433{
423 if (!ctx.repo) 434 if (!ctx.repo)
424 return ctx.cfg.cache_root_ttl; 435 return ctx.cfg.cache_root_ttl;
425 436
426 if (!ctx.qry.page) 437 if (!ctx.qry.page)
427 return ctx.cfg.cache_repo_ttl; 438 return ctx.cfg.cache_repo_ttl;
428 439
429 if (ctx.qry.has_symref) 440 if (ctx.qry.has_symref)
430 return ctx.cfg.cache_dynamic_ttl; 441 return ctx.cfg.cache_dynamic_ttl;
431 442
432 if (ctx.qry.has_sha1) 443 if (ctx.qry.has_sha1)
433 return ctx.cfg.cache_static_ttl; 444 return ctx.cfg.cache_static_ttl;
434 445
435 return ctx.cfg.cache_repo_ttl; 446 return ctx.cfg.cache_repo_ttl;
436} 447}
437 448
438int main(int argc, const char **argv) 449int main(int argc, const char **argv)
439{ 450{
440 const char *cgit_config_env = getenv("CGIT_CONFIG"); 451 const char *cgit_config_env = getenv("CGIT_CONFIG");
441 const char *method = getenv("REQUEST_METHOD"); 452 const char *method = getenv("REQUEST_METHOD");
442 const char *path; 453 const char *path;
443 char *qry; 454 char *qry;
444 int err, ttl; 455 int err, ttl;
445 456
446 prepare_context(&ctx); 457 prepare_context(&ctx);
447 cgit_repolist.length = 0; 458 cgit_repolist.length = 0;
448 cgit_repolist.count = 0; 459 cgit_repolist.count = 0;
449 cgit_repolist.repos = NULL; 460 cgit_repolist.repos = NULL;
450 461
451 if (getenv("SCRIPT_NAME")) 462 if (getenv("SCRIPT_NAME"))
452 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); 463 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
453 if (getenv("QUERY_STRING")) 464 if (getenv("QUERY_STRING"))
454 ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); 465 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
455 cgit_parse_args(argc, argv); 466 cgit_parse_args(argc, argv);
456 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 467 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
457 config_cb); 468 config_cb);
458 ctx.repo = NULL; 469 ctx.repo = NULL;
459 http_parse_querystring(ctx.qry.raw, querystring_cb); 470 http_parse_querystring(ctx.qry.raw, querystring_cb);
460 471
461 /* If virtual-root isn't specified in cgitrc, lets pretend 472 /* If virtual-root isn't specified in cgitrc, lets pretend
462 * that virtual-root equals SCRIPT_NAME. 473 * that virtual-root equals SCRIPT_NAME.
463 */ 474 */
464 if (!ctx.cfg.virtual_root) 475 if (!ctx.cfg.virtual_root)
465 ctx.cfg.virtual_root = ctx.cfg.script_name; 476 ctx.cfg.virtual_root = ctx.cfg.script_name;
466 477
467 /* If no url parameter is specified on the querystring, lets 478 /* If no url parameter is specified on the querystring, lets
468 * use PATH_INFO as url. This allows cgit to work with virtual 479 * use PATH_INFO as url. This allows cgit to work with virtual
469 * urls without the need for rewriterules in the webserver (as 480 * urls without the need for rewriterules in the webserver (as
470 * long as PATH_INFO is included in the cache lookup key). 481 * long as PATH_INFO is included in the cache lookup key).
471 */ 482 */
472 path = getenv("PATH_INFO"); 483 path = getenv("PATH_INFO");
473 if (!ctx.qry.url && path) { 484 if (!ctx.qry.url && path) {
474 if (path[0] == '/') 485 if (path[0] == '/')
475 path++; 486 path++;
476 ctx.qry.url = xstrdup(path); 487 ctx.qry.url = xstrdup(path);
477 if (ctx.qry.raw) { 488 if (ctx.qry.raw) {
478 qry = ctx.qry.raw; 489 qry = ctx.qry.raw;
479 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); 490 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
480 free(qry); 491 free(qry);
481 } else 492 } else
482 ctx.qry.raw = ctx.qry.url; 493 ctx.qry.raw = ctx.qry.url;
483 cgit_parse_url(ctx.qry.url); 494 cgit_parse_url(ctx.qry.url);
484 } 495 }
485 496
486 ttl = calc_ttl(); 497 ttl = calc_ttl();
487 ctx.page.expires += ttl*60; 498 ctx.page.expires += ttl*60;
488 if (method && !strcmp(method, "HEAD")) 499 if (method && !strcmp(method, "HEAD"))
489 ctx.cfg.nocache = 1; 500 ctx.cfg.nocache = 1;
490 if (ctx.cfg.nocache) 501 if (ctx.cfg.nocache)
491 ctx.cfg.cache_size = 0; 502 ctx.cfg.cache_size = 0;
492 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, 503 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
493 ctx.qry.raw, ttl, process_request, &ctx); 504 ctx.qry.raw, ttl, process_request, &ctx);
494 if (err) 505 if (err)
495 cgit_print_error(fmt("Error processing page: %s (%d)", 506 cgit_print_error(fmt("Error processing page: %s (%d)",
496 strerror(err), err)); 507 strerror(err), err));
497 return err; 508 return err;
498} 509}