From e5da4bca54574522b28f88cab0dc8ebad9e35a73 Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Wed, 6 Aug 2008 10:53:50 +0200 Subject: Implement plain view This implements a way to access plain blobs by path (similar to the tree view) instead of by sha1. Signed-off-by: Lars Hjemli --- ui-plain.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 ui-plain.c (limited to 'ui-plain.c') diff --git a/ui-plain.c b/ui-plain.c new file mode 100644 index 0000000..28deae5 --- /dev/null +++ b/ui-plain.c @@ -0,0 +1,82 @@ +/* ui-plain.c: functions for output of plain blobs by path + * + * Copyright (C) 2008 Lars Hjemli + * + * Licensed under GNU General Public License v2 + * (see COPYING for full license text) + */ + +#include "cgit.h" +#include "html.h" +#include "ui-shared.h" + +char *curr_rev; +char *match_path; +int match; + +static void print_object(const unsigned char *sha1, const char *path) +{ + enum object_type type; + char *buf; + size_t size; + + type = sha1_object_info(sha1, &size); + if (type == OBJ_BAD) { + html_status(404, 0); + return; + } + + buf = read_sha1_file(sha1, &type, &size); + if (!buf) { + html_status(404, 0); + return; + } + ctx.page.mimetype = "text/plain"; + ctx.page.filename = fmt("%s", path); + ctx.page.size = size; + cgit_print_http_headers(&ctx); + html_raw(buf, size); + match = 1; +} + +static int walk_tree(const unsigned char *sha1, const char *base, int baselen, + const char *pathname, unsigned mode, int stage, + void *cbdata) +{ + fprintf(stderr, "[cgit] walk_tree.pathname=%s", pathname); + + if (!pathname || strcmp(match_path, pathname)) + return READ_TREE_RECURSIVE; + + if (S_ISREG(mode)) + print_object(sha1, pathname); + + return 0; +} + +void cgit_print_plain(struct cgit_context *ctx) +{ + const char *rev = ctx->qry.sha1; + unsigned char sha1[20]; + struct commit *commit; + const char *paths[] = {ctx->qry.path, NULL}; + + if (!rev) + rev = ctx->qry.head; + + curr_rev = xstrdup(rev); + if (get_sha1(rev, sha1)) { + html_status(404, 0); + return; + } + commit = lookup_commit_reference(sha1); + if (!commit || parse_commit(commit)) { + html_status(404, 0); + return; + } + match_path = ctx->qry.path; + fprintf(stderr, "[cgit] match_path=%s", match_path); + read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL); + if (!match) + html_status(404, 0); +} -- cgit v1.2.3-70-g09d2 From 885096c189574b1cf2e0897cc05aadd7b092a677 Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Wed, 6 Aug 2008 22:57:44 +0200 Subject: Supply status description to html_status() Signed-off-by: Lars Hjemli --- html.c | 4 ++-- html.h | 2 +- ui-clone.c | 10 ++++------ ui-plain.c | 10 +++++----- 4 files changed, 12 insertions(+), 14 deletions(-) (limited to 'ui-plain.c') diff --git a/html.c b/html.c index 83fc7a9..36e9a2f 100644 --- a/html.c +++ b/html.c @@ -56,9 +56,9 @@ void htmlf(const char *format, ...) html(buf); } -void html_status(int code, int more_headers) +void html_status(int code, const char *msg, int more_headers) { - htmlf("Status: %d\n", code); + htmlf("Status: %d %s\n", code, msg); if (!more_headers) html("\n"); } diff --git a/html.h b/html.h index 49462a2..3c32935 100644 --- a/html.h +++ b/html.h @@ -6,7 +6,7 @@ extern int htmlfd; extern void html_raw(const char *txt, size_t size); extern void html(const char *txt); extern void htmlf(const char *format,...); -extern void html_status(int code, int more_headers); +extern void html_status(int code, const char *msg, int more_headers); extern void html_txt(char *txt); extern void html_ntxt(int len, char *txt); extern void html_attr(char *txt); diff --git a/ui-clone.c b/ui-clone.c index 3a037ad..81e7a4e 100644 --- a/ui-clone.c +++ b/ui-clone.c @@ -48,20 +48,18 @@ static void print_pack_info(struct cgit_context *ctx) static void send_file(struct cgit_context *ctx, char *path) { struct stat st; - int err; if (stat(path, &st)) { switch (errno) { case ENOENT: - err = 404; + html_status(404, "Not found", 0); break; case EACCES: - err = 403; + html_status(403, "Forbidden", 0); break; default: - err = 400; + html_status(400, "Bad request", 0); } - html_status(err, 0); return; } ctx->page.mimetype = "application/octet-stream"; @@ -86,7 +84,7 @@ void cgit_clone_info(struct cgit_context *ctx) void cgit_clone_objects(struct cgit_context *ctx) { if (!ctx->qry.path) { - html_status(400, 0); + html_status(400, "Bad request", 0); return; } diff --git a/ui-plain.c b/ui-plain.c index 28deae5..35888a0 100644 --- a/ui-plain.c +++ b/ui-plain.c @@ -22,13 +22,13 @@ static void print_object(const unsigned char *sha1, const char *path) type = sha1_object_info(sha1, &size); if (type == OBJ_BAD) { - html_status(404, 0); + html_status(404, "Not found", 0); return; } buf = read_sha1_file(sha1, &type, &size); if (!buf) { - html_status(404, 0); + html_status(404, "Not found", 0); return; } ctx.page.mimetype = "text/plain"; @@ -66,17 +66,17 @@ void cgit_print_plain(struct cgit_context *ctx) curr_rev = xstrdup(rev); if (get_sha1(rev, sha1)) { - html_status(404, 0); + html_status(404, "Not found", 0); return; } commit = lookup_commit_reference(sha1); if (!commit || parse_commit(commit)) { - html_status(404, 0); + html_status(404, "Not found", 0); return; } match_path = ctx->qry.path; fprintf(stderr, "[cgit] match_path=%s", match_path); read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL); if (!match) - html_status(404, 0); + html_status(404, "Not found", 0); } -- cgit v1.2.3-70-g09d2 eader'>2008-03-06Used better exceptions for configuration parserRené 'Necoro' Neumann1-26/+114 2008-03-06Update translationRené 'Necoro' Neumann2-349/+393 2008-03-06Update createpot.shRené 'Necoro' Neumann1-3/+2 2008-03-06Use 'nofork' instead of 'nolistener'René 'Necoro' Neumann1-3/+3 2008-03-05Install glade files into template dir and not data dirRené 'Necoro' Neumann2-2/+1 2008-03-05Added dependency listRené 'Necoro' Neumann3-117/+237