blob: 6ba8f361e6ae63bf13de716a16957c3bbe73f58a [file] [log] [blame]
Lars Hjemli02a545e2008-08-06 01:20:24 +02001/* ui-clone.c: functions for http cloning, based on
2 * git's http-backend.c by Shawn O. Pearce
3 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01004 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli02a545e2008-08-06 01:20:24 +02005 *
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
9
10#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010011#include "ui-clone.h"
Lars Hjemli02a545e2008-08-06 01:20:24 +020012#include "html.h"
13#include "ui-shared.h"
Christian Hesse5d947ba2017-11-29 22:25:42 +010014#include "packfile.h"
Christian Hesse255b78f2018-06-04 18:49:28 +020015#include "object-store.h"
Lars Hjemli02a545e2008-08-06 01:20:24 +020016
Christian Hessede83de22015-07-28 10:42:01 +020017static int print_ref_info(const char *refname, const struct object_id *oid,
Lars Hjemli02a545e2008-08-06 01:20:24 +020018 int flags, void *cb_data)
19{
20 struct object *obj;
21
Jeff Smith86a6d352017-08-09 19:02:56 -050022 if (!(obj = parse_object(oid)))
Lars Hjemli02a545e2008-08-06 01:20:24 +020023 return 0;
24
Christian Hessede83de22015-07-28 10:42:01 +020025 htmlf("%s\t%s\n", oid_to_hex(oid), refname);
Jason A. Donenfeld05da4c22013-02-01 21:08:51 +010026 if (obj->type == OBJ_TAG) {
Lars Hjemli02a545e2008-08-06 01:20:24 +020027 if (!(obj = deref_tag(obj, refname, 0)))
28 return 0;
Christian Hesse559ab5e2016-01-05 07:38:53 +010029 htmlf("%s\t%s^{}\n", oid_to_hex(&obj->oid), refname);
Lars Hjemli02a545e2008-08-06 01:20:24 +020030 }
31 return 0;
32}
33
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010034static void print_pack_info(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +020035{
36 struct packed_git *pack;
Jason A. Donenfeld6e498de2015-02-09 12:27:44 +010037 char *offset;
Lars Hjemli02a545e2008-08-06 01:20:24 +020038
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010039 ctx.page.mimetype = "text/plain";
40 ctx.page.filename = "objects/info/packs";
41 cgit_print_http_headers();
Christian Hesse255b78f2018-06-04 18:49:28 +020042 reprepare_packed_git(the_repository);
43 for (pack = get_packed_git(the_repository); pack; pack = pack->next) {
Jason A. Donenfeld6e498de2015-02-09 12:27:44 +010044 if (pack->pack_local) {
45 offset = strrchr(pack->pack_name, '/');
46 if (offset && offset[1] != '\0')
47 ++offset;
48 else
49 offset = pack->pack_name;
50 htmlf("P %s\n", offset);
51 }
52 }
Lars Hjemli02a545e2008-08-06 01:20:24 +020053}
54
Christian Hessede83de22015-07-28 10:42:01 +020055static void send_file(const char *path)
Lars Hjemli02a545e2008-08-06 01:20:24 +020056{
57 struct stat st;
Lars Hjemli02a545e2008-08-06 01:20:24 +020058
59 if (stat(path, &st)) {
60 switch (errno) {
61 case ENOENT:
John Keeping329381d2015-08-14 12:47:03 +010062 cgit_print_error_page(404, "Not found", "Not found");
Lars Hjemli02a545e2008-08-06 01:20:24 +020063 break;
64 case EACCES:
John Keeping329381d2015-08-14 12:47:03 +010065 cgit_print_error_page(403, "Forbidden", "Forbidden");
Lars Hjemli02a545e2008-08-06 01:20:24 +020066 break;
67 default:
John Keeping329381d2015-08-14 12:47:03 +010068 cgit_print_error_page(400, "Bad request", "Bad request");
Lars Hjemli02a545e2008-08-06 01:20:24 +020069 }
Lars Hjemli02a545e2008-08-06 01:20:24 +020070 return;
71 }
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010072 ctx.page.mimetype = "application/octet-stream";
73 ctx.page.filename = path;
Lukas Fleischerb66b16e2015-02-09 07:25:00 +010074 skip_prefix(path, ctx.repo->path, &ctx.page.filename);
75 skip_prefix(ctx.page.filename, "/", &ctx.page.filename);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010076 cgit_print_http_headers();
Lars Hjemli02a545e2008-08-06 01:20:24 +020077 html_include(path);
78}
79
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010080void cgit_clone_info(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +020081{
Lukas Fleischered7e3bc2015-01-15 19:47:42 +010082 if (!ctx.qry.path || strcmp(ctx.qry.path, "refs")) {
John Keeping329381d2015-08-14 12:47:03 +010083 cgit_print_error_page(400, "Bad request", "Bad request");
Lars Hjemli02a545e2008-08-06 01:20:24 +020084 return;
Lukas Fleischered7e3bc2015-01-15 19:47:42 +010085 }
Lars Hjemli02a545e2008-08-06 01:20:24 +020086
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010087 ctx.page.mimetype = "text/plain";
88 ctx.page.filename = "info/refs";
89 cgit_print_http_headers();
90 for_each_ref(print_ref_info, NULL);
Lars Hjemli02a545e2008-08-06 01:20:24 +020091}
92
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010093void cgit_clone_objects(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +020094{
Jason A. Donenfeld53efaf32018-08-03 15:46:11 +020095 char *p;
96
97 if (!ctx.qry.path)
98 goto err;
Lars Hjemli02a545e2008-08-06 01:20:24 +020099
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100100 if (!strcmp(ctx.qry.path, "info/packs")) {
101 print_pack_info();
Lars Hjemli02a545e2008-08-06 01:20:24 +0200102 return;
103 }
104
Jason A. Donenfeld53efaf32018-08-03 15:46:11 +0200105 /* Avoid directory traversal by forbidding "..", but also work around
106 * other funny business by just specifying a fairly strict format. For
107 * example, now we don't have to stress out about the Cygwin port.
108 */
109 for (p = ctx.qry.path; *p; ++p) {
110 if (*p == '.' && *(p + 1) == '.')
111 goto err;
112 if (!isalnum(*p) && *p != '/' && *p != '.' && *p != '-')
113 goto err;
114 }
115
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100116 send_file(git_path("objects/%s", ctx.qry.path));
Jason A. Donenfeld53efaf32018-08-03 15:46:11 +0200117 return;
118
119err:
120 cgit_print_error_page(400, "Bad request", "Bad request");
Lars Hjemli02a545e2008-08-06 01:20:24 +0200121}
122
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100123void cgit_clone_head(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +0200124{
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100125 send_file(git_path("%s", "HEAD"));
Lars Hjemli02a545e2008-08-06 01:20:24 +0200126}