2008-08-06 01:20:24 +02:00
|
|
|
/* ui-clone.c: functions for http cloning, based on
|
|
|
|
* git's http-backend.c by Shawn O. Pearce
|
|
|
|
*
|
2014-01-08 15:10:49 +01:00
|
|
|
* Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
|
2008-08-06 01:20:24 +02:00
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cgit.h"
|
2013-04-06 12:37:59 +02:00
|
|
|
#include "ui-clone.h"
|
2008-08-06 01:20:24 +02:00
|
|
|
#include "html.h"
|
|
|
|
#include "ui-shared.h"
|
2017-11-29 22:25:42 +01:00
|
|
|
#include "packfile.h"
|
2018-06-04 18:49:28 +02:00
|
|
|
#include "object-store.h"
|
2008-08-06 01:20:24 +02:00
|
|
|
|
2015-07-28 10:42:01 +02:00
|
|
|
static int print_ref_info(const char *refname, const struct object_id *oid,
|
2008-08-06 01:20:24 +02:00
|
|
|
int flags, void *cb_data)
|
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
2017-08-10 02:02:56 +02:00
|
|
|
if (!(obj = parse_object(oid)))
|
2008-08-06 01:20:24 +02:00
|
|
|
return 0;
|
|
|
|
|
2015-07-28 10:42:01 +02:00
|
|
|
htmlf("%s\t%s\n", oid_to_hex(oid), refname);
|
2013-02-01 21:08:51 +01:00
|
|
|
if (obj->type == OBJ_TAG) {
|
2008-08-06 01:20:24 +02:00
|
|
|
if (!(obj = deref_tag(obj, refname, 0)))
|
|
|
|
return 0;
|
2016-01-05 07:38:53 +01:00
|
|
|
htmlf("%s\t%s^{}\n", oid_to_hex(&obj->oid), refname);
|
2008-08-06 01:20:24 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-15 21:53:15 +01:00
|
|
|
static void print_pack_info(void)
|
2008-08-06 01:20:24 +02:00
|
|
|
{
|
|
|
|
struct packed_git *pack;
|
2015-02-09 12:27:44 +01:00
|
|
|
char *offset;
|
2008-08-06 01:20:24 +02:00
|
|
|
|
2014-01-15 21:53:15 +01:00
|
|
|
ctx.page.mimetype = "text/plain";
|
|
|
|
ctx.page.filename = "objects/info/packs";
|
|
|
|
cgit_print_http_headers();
|
2018-06-04 18:49:28 +02:00
|
|
|
reprepare_packed_git(the_repository);
|
|
|
|
for (pack = get_packed_git(the_repository); pack; pack = pack->next) {
|
2015-02-09 12:27:44 +01: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-06 01:20:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-28 10:42:01 +02:00
|
|
|
static void send_file(const char *path)
|
2008-08-06 01:20:24 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path, &st)) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
2015-08-14 13:47:03 +02:00
|
|
|
cgit_print_error_page(404, "Not found", "Not found");
|
2008-08-06 01:20:24 +02:00
|
|
|
break;
|
|
|
|
case EACCES:
|
2015-08-14 13:47:03 +02:00
|
|
|
cgit_print_error_page(403, "Forbidden", "Forbidden");
|
2008-08-06 01:20:24 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-08-14 13:47:03 +02:00
|
|
|
cgit_print_error_page(400, "Bad request", "Bad request");
|
2008-08-06 01:20:24 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2014-01-15 21:53:15 +01:00
|
|
|
ctx.page.mimetype = "application/octet-stream";
|
|
|
|
ctx.page.filename = path;
|
2015-02-09 07:25:00 +01:00
|
|
|
skip_prefix(path, ctx.repo->path, &ctx.page.filename);
|
|
|
|
skip_prefix(ctx.page.filename, "/", &ctx.page.filename);
|
2014-01-15 21:53:15 +01:00
|
|
|
cgit_print_http_headers();
|
2008-08-06 01:20:24 +02:00
|
|
|
html_include(path);
|
|
|
|
}
|
|
|
|
|
2014-01-15 21:53:15 +01:00
|
|
|
void cgit_clone_info(void)
|
2008-08-06 01:20:24 +02:00
|
|
|
{
|
2015-01-15 19:47:42 +01:00
|
|
|
if (!ctx.qry.path || strcmp(ctx.qry.path, "refs")) {
|
2015-08-14 13:47:03 +02:00
|
|
|
cgit_print_error_page(400, "Bad request", "Bad request");
|
2008-08-06 01:20:24 +02:00
|
|
|
return;
|
2015-01-15 19:47:42 +01:00
|
|
|
}
|
2008-08-06 01:20:24 +02:00
|
|
|
|
2014-01-15 21:53:15 +01:00
|
|
|
ctx.page.mimetype = "text/plain";
|
|
|
|
ctx.page.filename = "info/refs";
|
|
|
|
cgit_print_http_headers();
|
|
|
|
for_each_ref(print_ref_info, NULL);
|
2008-08-06 01:20:24 +02:00
|
|
|
}
|
|
|
|
|
2014-01-15 21:53:15 +01:00
|
|
|
void cgit_clone_objects(void)
|
2008-08-06 01:20:24 +02:00
|
|
|
{
|
2018-08-03 15:46:11 +02:00
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!ctx.qry.path)
|
|
|
|
goto err;
|
2008-08-06 01:20:24 +02:00
|
|
|
|
2014-01-15 21:53:15 +01:00
|
|
|
if (!strcmp(ctx.qry.path, "info/packs")) {
|
|
|
|
print_pack_info();
|
2008-08-06 01:20:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-03 15:46:11 +02: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 21:53:15 +01:00
|
|
|
send_file(git_path("objects/%s", ctx.qry.path));
|
2018-08-03 15:46:11 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
cgit_print_error_page(400, "Bad request", "Bad request");
|
2008-08-06 01:20:24 +02:00
|
|
|
}
|
|
|
|
|
2014-01-15 21:53:15 +01:00
|
|
|
void cgit_clone_head(void)
|
2008-08-06 01:20:24 +02:00
|
|
|
{
|
2014-01-15 21:53:15 +01:00
|
|
|
send_file(git_path("%s", "HEAD"));
|
2008-08-06 01:20:24 +02:00
|
|
|
}
|