summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--Makefile1
-rw-r--r--cgit.c1
-rw-r--r--cgit.h1
-rw-r--r--cmd.c7
-rw-r--r--html.c5
-rw-r--r--html.h1
-rw-r--r--ui-plain.c82
-rw-r--r--ui-plain.h6
-rw-r--r--ui-shared.c2
9 files changed, 106 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index 78aad10..a305894 100644
--- a/Makefile
+++ b/Makefile
@@ -58,12 +58,13 @@ OBJECTS += shared.o
OBJECTS += ui-blob.o
OBJECTS += ui-clone.o
OBJECTS += ui-commit.o
OBJECTS += ui-diff.o
OBJECTS += ui-log.o
OBJECTS += ui-patch.o
+OBJECTS += ui-plain.o
OBJECTS += ui-refs.o
OBJECTS += ui-repolist.o
OBJECTS += ui-shared.o
OBJECTS += ui-snapshot.o
OBJECTS += ui-summary.o
OBJECTS += ui-tag.o
diff --git a/cgit.c b/cgit.c
index f49fffa..497337b 100644
--- a/cgit.c
+++ b/cgit.c
@@ -184,12 +184,13 @@ static void prepare_context(struct cgit_context *ctx)
ctx->cfg.summary_branches = 10;
ctx->cfg.summary_log = 10;
ctx->cfg.summary_tags = 10;
ctx->page.mimetype = "text/html";
ctx->page.charset = PAGE_ENCODING;
ctx->page.filename = NULL;
+ ctx->page.size = 0;
ctx->page.modified = time(NULL);
ctx->page.expires = ctx->page.modified;
}
struct refmatch {
char *req_ref;
diff --git a/cgit.h b/cgit.h
index b01fa31..e2af0c2 100644
--- a/cgit.h
+++ b/cgit.h
@@ -162,12 +162,13 @@ struct cgit_config {
int summary_tags;
};
struct cgit_page {
time_t modified;
time_t expires;
+ size_t size;
char *mimetype;
char *charset;
char *filename;
char *title;
};
diff --git a/cmd.c b/cmd.c
index 03e165c..2b34189 100644
--- a/cmd.c
+++ b/cmd.c
@@ -13,12 +13,13 @@
#include "ui-blob.h"
#include "ui-clone.h"
#include "ui-commit.h"
#include "ui-diff.h"
#include "ui-log.h"
#include "ui-patch.h"
+#include "ui-plain.h"
#include "ui-refs.h"
#include "ui-repolist.h"
#include "ui-snapshot.h"
#include "ui-summary.h"
#include "ui-tag.h"
#include "ui-tree.h"
@@ -82,12 +83,17 @@ static void repolist_fn(struct cgit_context *ctx)
static void patch_fn(struct cgit_context *ctx)
{
cgit_print_patch(ctx->qry.sha1);
}
+static void plain_fn(struct cgit_context *ctx)
+{
+ cgit_print_plain(ctx);
+}
+
static void refs_fn(struct cgit_context *ctx)
{
cgit_print_refs();
}
static void snapshot_fn(struct cgit_context *ctx)
@@ -125,12 +131,13 @@ struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx)
def_cmd(diff, 1, 1),
def_cmd(info, 1, 0),
def_cmd(log, 1, 1),
def_cmd(ls_cache, 0, 0),
def_cmd(objects, 1, 0),
def_cmd(patch, 1, 0),
+ def_cmd(plain, 1, 0),
def_cmd(refs, 1, 1),
def_cmd(repolist, 0, 0),
def_cmd(snapshot, 1, 0),
def_cmd(summary, 1, 1),
def_cmd(tag, 1, 1),
def_cmd(tree, 1, 1),
diff --git a/html.c b/html.c
index 1237076..83fc7a9 100644
--- a/html.c
+++ b/html.c
@@ -32,12 +32,17 @@ char *fmt(const char *format, ...)
fprintf(stderr, "[html.c] string truncated: %s\n", format);
exit(1);
}
return buf[bufidx];
}
+void html_raw(const char *data, size_t size)
+{
+ write(htmlfd, data, size);
+}
+
void html(const char *txt)
{
write(htmlfd, txt, strlen(txt));
}
void htmlf(const char *format, ...)
diff --git a/html.h b/html.h
index 2bde28d..49462a2 100644
--- a/html.h
+++ b/html.h
@@ -1,11 +1,12 @@
#ifndef HTML_H
#define HTML_H
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_txt(char *txt);
extern void html_ntxt(int len, char *txt);
extern void html_attr(char *txt);
diff --git a/ui-plain.c b/ui-plain.c
new file mode 100644
index 0000000..28deae5
--- a/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);
+}
diff --git a/ui-plain.h b/ui-plain.h
new file mode 100644
index 0000000..4373118
--- a/dev/null
+++ b/ui-plain.h
@@ -0,0 +1,6 @@
+#ifndef UI_PLAIN_H
+#define UI_PLAIN_H
+
+extern void cgit_print_plain(struct cgit_context *ctx);
+
+#endif /* UI_PLAIN_H */
diff --git a/ui-shared.c b/ui-shared.c
index 197ee37..4408969 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -415,12 +415,14 @@ void cgit_print_http_headers(struct cgit_context *ctx)
{
if (ctx->page.mimetype && ctx->page.charset)
htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
ctx->page.charset);
else if (ctx->page.mimetype)
htmlf("Content-Type: %s\n", ctx->page.mimetype);
+ if (ctx->page.size)
+ htmlf("Content-Length: %ld\n", ctx->page.size);
if (ctx->page.filename)
htmlf("Content-Disposition: inline; filename=\"%s\"\n",
ctx->page.filename);
htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
htmlf("Expires: %s\n", http_date(ctx->page.expires));
html("\n");