filter: don't use dlsym unnecessarily
[cgit.git] / ui-clone.c
blobe4ddd34932f8182033267d2a439f6b76950a730f
1 /* ui-clone.c: functions for http cloning, based on
2 * git's http-backend.c by Shawn O. Pearce
4 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
10 #include "cgit.h"
11 #include "ui-clone.h"
12 #include "html.h"
13 #include "ui-shared.h"
15 static int print_ref_info(const char *refname, const struct object_id *oid,
16 int flags, void *cb_data)
18 struct object *obj;
20 if (!(obj = parse_object(oid->hash)))
21 return 0;
23 htmlf("%s\t%s\n", oid_to_hex(oid), refname);
24 if (obj->type == OBJ_TAG) {
25 if (!(obj = deref_tag(obj, refname, 0)))
26 return 0;
27 htmlf("%s\t%s^{}\n", sha1_to_hex(obj->sha1), refname);
29 return 0;
32 static void print_pack_info(void)
34 struct packed_git *pack;
35 char *offset;
37 ctx.page.mimetype = "text/plain";
38 ctx.page.filename = "objects/info/packs";
39 cgit_print_http_headers();
40 prepare_packed_git();
41 for (pack = packed_git; pack; pack = pack->next) {
42 if (pack->pack_local) {
43 offset = strrchr(pack->pack_name, '/');
44 if (offset && offset[1] != '\0')
45 ++offset;
46 else
47 offset = pack->pack_name;
48 htmlf("P %s\n", offset);
53 static void send_file(const char *path)
55 struct stat st;
57 if (stat(path, &st)) {
58 switch (errno) {
59 case ENOENT:
60 html_status(404, "Not found", 0);
61 break;
62 case EACCES:
63 html_status(403, "Forbidden", 0);
64 break;
65 default:
66 html_status(400, "Bad request", 0);
68 return;
70 ctx.page.mimetype = "application/octet-stream";
71 ctx.page.filename = path;
72 skip_prefix(path, ctx.repo->path, &ctx.page.filename);
73 skip_prefix(ctx.page.filename, "/", &ctx.page.filename);
74 cgit_print_http_headers();
75 html_include(path);
78 void cgit_clone_info(void)
80 if (!ctx.qry.path || strcmp(ctx.qry.path, "refs")) {
81 html_status(400, "Bad request", 0);
82 return;
85 ctx.page.mimetype = "text/plain";
86 ctx.page.filename = "info/refs";
87 cgit_print_http_headers();
88 for_each_ref(print_ref_info, NULL);
91 void cgit_clone_objects(void)
93 if (!ctx.qry.path) {
94 html_status(400, "Bad request", 0);
95 return;
98 if (!strcmp(ctx.qry.path, "info/packs")) {
99 print_pack_info();
100 return;
103 send_file(git_path("objects/%s", ctx.qry.path));
106 void cgit_clone_head(void)
108 send_file(git_path("%s", "HEAD"));