summaryrefslogtreecommitdiffstatshomepage
path: root/shared.c
diff options
context:
space:
mode:
Diffstat (limited to 'shared.c')
-rw-r--r--shared.c43
1 files changed, 14 insertions, 29 deletions
diff --git a/shared.c b/shared.c
index f063894..6c1a762 100644
--- a/shared.c
+++ b/shared.c
@@ -10,6 +10,7 @@
struct repolist cgit_repolist;
struct repoinfo *cgit_repo;
+struct cgit_context ctx;
int cgit_cmd;
const char *cgit_version = CGIT_VERSION;
@@ -49,24 +50,8 @@ int cgit_max_msg_len = 60;
int cgit_max_repodesc_len = 60;
int cgit_max_commit_count = 50;
-int cgit_query_has_symref = 0;
-int cgit_query_has_sha1 = 0;
-
-char *cgit_querystring = NULL;
-char *cgit_query_repo = NULL;
-char *cgit_query_page = NULL;
-char *cgit_query_head = NULL;
-char *cgit_query_search = NULL;
-char *cgit_query_grep = NULL;
-char *cgit_query_sha1 = NULL;
-char *cgit_query_sha2 = NULL;
-char *cgit_query_path = NULL;
-char *cgit_query_name = NULL;
-int cgit_query_ofs = 0;
-
int htmlfd = 0;
-
int cgit_get_cmd_index(const char *cmd)
{
static char *cmds[] = {"log", "commit", "diff", "tree", "blob",
@@ -239,32 +224,32 @@ void cgit_global_config_cb(const char *name, const char *value)
void cgit_querystring_cb(const char *name, const char *value)
{
if (!strcmp(name,"r")) {
- cgit_query_repo = xstrdup(value);
+ ctx.qry.repo = xstrdup(value);
cgit_repo = cgit_get_repoinfo(value);
} else if (!strcmp(name, "p")) {
- cgit_query_page = xstrdup(value);
+ ctx.qry.page = xstrdup(value);
cgit_cmd = cgit_get_cmd_index(value);
} else if (!strcmp(name, "url")) {
cgit_parse_url(value);
} else if (!strcmp(name, "qt")) {
- cgit_query_grep = xstrdup(value);
+ ctx.qry.grep = xstrdup(value);
} else if (!strcmp(name, "q")) {
- cgit_query_search = xstrdup(value);
+ ctx.qry.search = xstrdup(value);
} else if (!strcmp(name, "h")) {
- cgit_query_head = xstrdup(value);
- cgit_query_has_symref = 1;
+ ctx.qry.head = xstrdup(value);
+ ctx.qry.has_symref = 1;
} else if (!strcmp(name, "id")) {
- cgit_query_sha1 = xstrdup(value);
- cgit_query_has_sha1 = 1;
+ ctx.qry.sha1 = xstrdup(value);
+ ctx.qry.has_sha1 = 1;
} else if (!strcmp(name, "id2")) {
- cgit_query_sha2 = xstrdup(value);
- cgit_query_has_sha1 = 1;
+ ctx.qry.sha2 = xstrdup(value);
+ ctx.qry.has_sha1 = 1;
} else if (!strcmp(name, "ofs")) {
- cgit_query_ofs = atoi(value);
+ ctx.qry.ofs = atoi(value);
} else if (!strcmp(name, "path")) {
- cgit_query_path = trim_end(value, '/');
+ ctx.qry.path = trim_end(value, '/');
} else if (!strcmp(name, "name")) {
- cgit_query_name = xstrdup(value);
+ ctx.qry.name = xstrdup(value);
}
}
#fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/* 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, *ext;
	unsigned long size;
	struct string_list_item *mime;

	type = sha1_object_info(sha1, &size);
	if (type == OBJ_BAD) {
		html_status(404, "Not found", 0);
		return;
	}

	buf = read_sha1_file(sha1, &type, &size);
	if (!buf) {
		html_status(404, "Not found", 0);
		return;
	}
	ctx.page.mimetype = NULL;
	ext = strrchr(path, '.');
	if (ext && *(++ext)) {
		mime = string_list_lookup(ext, &ctx.cfg.mimetypes);
		if (mime)
			ctx.page.mimetype = (char *)mime->util;
	}
	if (!ctx.page.mimetype) {
		if (buffer_is_binary(buf, size))
			ctx.page.mimetype = "application/octet-stream";
		else
			ctx.page.mimetype = "text/plain";
	}
	ctx.page.filename = fmt("%s", path);
	ctx.page.size = size;
	ctx.page.etag = sha1_to_hex(sha1);
	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)
{
	if (S_ISDIR(mode))
		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, "Not found", 0);
		return;
	}
	commit = lookup_commit_reference(sha1);
	if (!commit || parse_commit(commit)) {
		html_status(404, "Not found", 0);
		return;
	}
	match_path = ctx->qry.path;
	read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);
	if (!match)
		html_status(404, "Not found", 0);
}