2008-08-05 23:20:24 +00:00
|
|
|
/* ui-clone.c: functions for http cloning, based on
|
|
|
|
* git's http-backend.c by Shawn O. Pearce
|
|
|
|
*
|
2014-01-08 14:10:49 +00:00
|
|
|
* Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
|
2008-08-05 23:20:24 +00:00
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
2024-07-16 07:45:13 +00:00
|
|
|
#define USE_THE_REPOSITORY_VARIABLE
|
|
|
|
|
2008-08-05 23:20:24 +00:00
|
|
|
#include "cgit.h"
|
2013-04-06 10:37:59 +00:00
|
|
|
#include "ui-clone.h"
|
2008-08-05 23:20:24 +00:00
|
|
|
#include "html.h"
|
|
|
|
#include "ui-shared.h"
|
2017-11-29 21:25:42 +00:00
|
|
|
#include "packfile.h"
|
2018-06-04 16:49:28 +00:00
|
|
|
#include "object-store.h"
|
2008-08-05 23:20:24 +00:00
|
|
|
|
2015-07-28 08:42:01 +00:00
|
|
|
static int print_ref_info(const char *refname, const struct object_id *oid,
|
2008-08-05 23:20:24 +00:00
|
|
|
int flags, void *cb_data)
|
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
2018-08-28 16:27:00 +00:00
|
|
|
if (!(obj = parse_object(the_repository, oid)))
|
2008-08-05 23:20:24 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-07-28 08:42:01 +00:00
|
|
|
htmlf("%s\t%s\n", oid_to_hex(oid), refname);
|
2013-02-01 20:08:51 +00:00
|
|
|
if (obj->type == OBJ_TAG) {
|
2018-08-28 16:27:00 +00:00
|
|
|
if (!(obj = deref_tag(the_repository, obj, refname, 0)))
|
2008-08-05 23:20:24 +00:00
|
|
|
return 0;
|
2016-01-05 06:38:53 +00:00
|
|
|
htmlf("%s\t%s^{}\n", oid_to_hex(&obj->oid), refname);
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
static void print_pack_info(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
|
|
|
struct packed_git *pack;
|
2015-02-09 11:27:44 +00:00
|
|
|
char *offset;
|
2008-08-05 23:20:24 +00:00
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
ctx.page.mimetype = "text/plain";
|
|
|
|
ctx.page.filename = "objects/info/packs";
|
|
|
|
cgit_print_http_headers();
|
2018-06-04 16:49:28 +00:00
|
|
|
reprepare_packed_git(the_repository);
|
|
|
|
for (pack = get_packed_git(the_repository); pack; pack = pack->next) {
|
2015-02-09 11:27:44 +00:00
|
|
|
if (pack->pack_local) {
|
|
|
|
offset = strrchr(pack->pack_name, '/');
|
|
|
|
if (offset && offset[1] != '\0')
|
|
|
|
++offset;
|
|
|
|
else
|
|
|
|
offset = pack->pack_name;
|
|
|
|
htmlf("P %s\n", offset);
|
|
|
|
}
|
|
|
|
}
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 08:42:01 +00:00
|
|
|
static void send_file(const char *path)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path, &st)) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
2015-08-14 11:47:03 +00:00
|
|
|
cgit_print_error_page(404, "Not found", "Not found");
|
2008-08-05 23:20:24 +00:00
|
|
|
break;
|
|
|
|
case EACCES:
|
2015-08-14 11:47:03 +00:00
|
|
|
cgit_print_error_page(403, "Forbidden", "Forbidden");
|
2008-08-05 23:20:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-08-14 11:47:03 +00:00
|
|
|
cgit_print_error_page(400, "Bad request", "Bad request");
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2014-01-15 20:53:15 +00:00
|
|
|
ctx.page.mimetype = "application/octet-stream";
|
|
|
|
ctx.page.filename = path;
|
2015-02-09 06:25:00 +00:00
|
|
|
skip_prefix(path, ctx.repo->path, &ctx.page.filename);
|
|
|
|
skip_prefix(ctx.page.filename, "/", &ctx.page.filename);
|
2014-01-15 20:53:15 +00:00
|
|
|
cgit_print_http_headers();
|
2008-08-05 23:20:24 +00:00
|
|
|
html_include(path);
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
void cgit_clone_info(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
2015-01-15 18:47:42 +00:00
|
|
|
if (!ctx.qry.path || strcmp(ctx.qry.path, "refs")) {
|
2015-08-14 11:47:03 +00:00
|
|
|
cgit_print_error_page(400, "Bad request", "Bad request");
|
2008-08-05 23:20:24 +00:00
|
|
|
return;
|
2015-01-15 18:47:42 +00:00
|
|
|
}
|
2008-08-05 23:20:24 +00:00
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
ctx.page.mimetype = "text/plain";
|
|
|
|
ctx.page.filename = "info/refs";
|
|
|
|
cgit_print_http_headers();
|
2024-07-16 07:45:13 +00:00
|
|
|
refs_for_each_ref(get_main_ref_store(the_repository),
|
|
|
|
print_ref_info, NULL);
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
void cgit_clone_objects(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
2018-08-03 13:46:11 +00:00
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!ctx.qry.path)
|
|
|
|
goto err;
|
2008-08-05 23:20:24 +00:00
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
if (!strcmp(ctx.qry.path, "info/packs")) {
|
|
|
|
print_pack_info();
|
2008-08-05 23:20:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-03 13:46:11 +00:00
|
|
|
/* Avoid directory traversal by forbidding "..", but also work around
|
|
|
|
* other funny business by just specifying a fairly strict format. For
|
|
|
|
* example, now we don't have to stress out about the Cygwin port.
|
|
|
|
*/
|
|
|
|
for (p = ctx.qry.path; *p; ++p) {
|
|
|
|
if (*p == '.' && *(p + 1) == '.')
|
|
|
|
goto err;
|
|
|
|
if (!isalnum(*p) && *p != '/' && *p != '.' && *p != '-')
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
send_file(git_path("objects/%s", ctx.qry.path));
|
2018-08-03 13:46:11 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
cgit_print_error_page(400, "Bad request", "Bad request");
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
void cgit_clone_head(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
2014-01-15 20:53:15 +00:00
|
|
|
send_file(git_path("%s", "HEAD"));
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|