summaryrefslogtreecommitdiffabout
path: root/cgit.c
authorLars Hjemli <hjemli@gmail.com>2009-06-07 18:43:08 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2009-06-07 18:43:08 (UTC)
commite429fb0cca1e8c78da0ec38fe578bafdeec65534 (patch) (unidiff)
tree58c172f939509879045192fb8f1ba1d79160e67f /cgit.c
parent45e7fcecc1117440e6274ce3c6ab7d893c4986ee (diff)
downloadcgit-e429fb0cca1e8c78da0ec38fe578bafdeec65534.zip
cgit-e429fb0cca1e8c78da0ec38fe578bafdeec65534.tar.gz
cgit-e429fb0cca1e8c78da0ec38fe578bafdeec65534.tar.bz2
Return http statuscode 404 on unknown branch
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'cgit.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c2
1 files changed, 2 insertions, 0 deletions
diff --git a/cgit.c b/cgit.c
index 64d95f9..19adadd 100644
--- a/cgit.c
+++ b/cgit.c
@@ -224,128 +224,130 @@ int find_current_ref(const char *refname, const unsigned char *sha1,
224 info->match = 1; 224 info->match = 1;
225 if (!info->first_ref) 225 if (!info->first_ref)
226 info->first_ref = xstrdup(refname); 226 info->first_ref = xstrdup(refname);
227 return info->match; 227 return info->match;
228} 228}
229 229
230char *find_default_branch(struct cgit_repo *repo) 230char *find_default_branch(struct cgit_repo *repo)
231{ 231{
232 struct refmatch info; 232 struct refmatch info;
233 char *ref; 233 char *ref;
234 234
235 info.req_ref = repo->defbranch; 235 info.req_ref = repo->defbranch;
236 info.first_ref = NULL; 236 info.first_ref = NULL;
237 info.match = 0; 237 info.match = 0;
238 for_each_branch_ref(find_current_ref, &info); 238 for_each_branch_ref(find_current_ref, &info);
239 if (info.match) 239 if (info.match)
240 ref = info.req_ref; 240 ref = info.req_ref;
241 else 241 else
242 ref = info.first_ref; 242 ref = info.first_ref;
243 if (ref) 243 if (ref)
244 ref = xstrdup(ref); 244 ref = xstrdup(ref);
245 return ref; 245 return ref;
246} 246}
247 247
248static int prepare_repo_cmd(struct cgit_context *ctx) 248static int prepare_repo_cmd(struct cgit_context *ctx)
249{ 249{
250 char *tmp; 250 char *tmp;
251 unsigned char sha1[20]; 251 unsigned char sha1[20];
252 int nongit = 0; 252 int nongit = 0;
253 253
254 setenv("GIT_DIR", ctx->repo->path, 1); 254 setenv("GIT_DIR", ctx->repo->path, 1);
255 setup_git_directory_gently(&nongit); 255 setup_git_directory_gently(&nongit);
256 if (nongit) { 256 if (nongit) {
257 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, 257 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
258 "config error"); 258 "config error");
259 tmp = fmt("Not a git repository: '%s'", ctx->repo->path); 259 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
260 ctx->repo = NULL; 260 ctx->repo = NULL;
261 cgit_print_http_headers(ctx); 261 cgit_print_http_headers(ctx);
262 cgit_print_docstart(ctx); 262 cgit_print_docstart(ctx);
263 cgit_print_pageheader(ctx); 263 cgit_print_pageheader(ctx);
264 cgit_print_error(tmp); 264 cgit_print_error(tmp);
265 cgit_print_docend(); 265 cgit_print_docend();
266 return 1; 266 return 1;
267 } 267 }
268 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); 268 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
269 269
270 if (!ctx->qry.head) { 270 if (!ctx->qry.head) {
271 ctx->qry.nohead = 1; 271 ctx->qry.nohead = 1;
272 ctx->qry.head = find_default_branch(ctx->repo); 272 ctx->qry.head = find_default_branch(ctx->repo);
273 ctx->repo->defbranch = ctx->qry.head; 273 ctx->repo->defbranch = ctx->qry.head;
274 } 274 }
275 275
276 if (!ctx->qry.head) { 276 if (!ctx->qry.head) {
277 cgit_print_http_headers(ctx); 277 cgit_print_http_headers(ctx);
278 cgit_print_docstart(ctx); 278 cgit_print_docstart(ctx);
279 cgit_print_pageheader(ctx); 279 cgit_print_pageheader(ctx);
280 cgit_print_error("Repository seems to be empty"); 280 cgit_print_error("Repository seems to be empty");
281 cgit_print_docend(); 281 cgit_print_docend();
282 return 1; 282 return 1;
283 } 283 }
284 284
285 if (get_sha1(ctx->qry.head, sha1)) { 285 if (get_sha1(ctx->qry.head, sha1)) {
286 tmp = xstrdup(ctx->qry.head); 286 tmp = xstrdup(ctx->qry.head);
287 ctx->qry.head = ctx->repo->defbranch; 287 ctx->qry.head = ctx->repo->defbranch;
288 ctx->page.status = 404;
289 ctx->page.statusmsg = "not found";
288 cgit_print_http_headers(ctx); 290 cgit_print_http_headers(ctx);
289 cgit_print_docstart(ctx); 291 cgit_print_docstart(ctx);
290 cgit_print_pageheader(ctx); 292 cgit_print_pageheader(ctx);
291 cgit_print_error(fmt("Invalid branch: %s", tmp)); 293 cgit_print_error(fmt("Invalid branch: %s", tmp));
292 cgit_print_docend(); 294 cgit_print_docend();
293 return 1; 295 return 1;
294 } 296 }
295 return 0; 297 return 0;
296} 298}
297 299
298static void process_request(void *cbdata) 300static void process_request(void *cbdata)
299{ 301{
300 struct cgit_context *ctx = cbdata; 302 struct cgit_context *ctx = cbdata;
301 struct cgit_cmd *cmd; 303 struct cgit_cmd *cmd;
302 304
303 cmd = cgit_get_cmd(ctx); 305 cmd = cgit_get_cmd(ctx);
304 if (!cmd) { 306 if (!cmd) {
305 ctx->page.title = "cgit error"; 307 ctx->page.title = "cgit error";
306 cgit_print_http_headers(ctx); 308 cgit_print_http_headers(ctx);
307 cgit_print_docstart(ctx); 309 cgit_print_docstart(ctx);
308 cgit_print_pageheader(ctx); 310 cgit_print_pageheader(ctx);
309 cgit_print_error("Invalid request"); 311 cgit_print_error("Invalid request");
310 cgit_print_docend(); 312 cgit_print_docend();
311 return; 313 return;
312 } 314 }
313 315
314 if (cmd->want_repo && !ctx->repo) { 316 if (cmd->want_repo && !ctx->repo) {
315 cgit_print_http_headers(ctx); 317 cgit_print_http_headers(ctx);
316 cgit_print_docstart(ctx); 318 cgit_print_docstart(ctx);
317 cgit_print_pageheader(ctx); 319 cgit_print_pageheader(ctx);
318 cgit_print_error(fmt("No repository selected")); 320 cgit_print_error(fmt("No repository selected"));
319 cgit_print_docend(); 321 cgit_print_docend();
320 return; 322 return;
321 } 323 }
322 324
323 if (ctx->repo && prepare_repo_cmd(ctx)) 325 if (ctx->repo && prepare_repo_cmd(ctx))
324 return; 326 return;
325 327
326 if (cmd->want_layout) { 328 if (cmd->want_layout) {
327 cgit_print_http_headers(ctx); 329 cgit_print_http_headers(ctx);
328 cgit_print_docstart(ctx); 330 cgit_print_docstart(ctx);
329 cgit_print_pageheader(ctx); 331 cgit_print_pageheader(ctx);
330 } 332 }
331 333
332 cmd->fn(ctx); 334 cmd->fn(ctx);
333 335
334 if (cmd->want_layout) 336 if (cmd->want_layout)
335 cgit_print_docend(); 337 cgit_print_docend();
336} 338}
337 339
338int cmp_repos(const void *a, const void *b) 340int cmp_repos(const void *a, const void *b)
339{ 341{
340 const struct cgit_repo *ra = a, *rb = b; 342 const struct cgit_repo *ra = a, *rb = b;
341 return strcmp(ra->url, rb->url); 343 return strcmp(ra->url, rb->url);
342} 344}
343 345
344void print_repo(struct cgit_repo *repo) 346void print_repo(struct cgit_repo *repo)
345{ 347{
346 printf("repo.url=%s\n", repo->url); 348 printf("repo.url=%s\n", repo->url);
347 printf("repo.name=%s\n", repo->name); 349 printf("repo.name=%s\n", repo->name);
348 printf("repo.path=%s\n", repo->path); 350 printf("repo.path=%s\n", repo->path);
349 if (repo->owner) 351 if (repo->owner)
350 printf("repo.owner=%s\n", repo->owner); 352 printf("repo.owner=%s\n", repo->owner);
351 if (repo->desc) 353 if (repo->desc)