aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/routes/repo.ts
blob: 32ac9c4dc10f5a6c35c07fddb55772c941b396a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { Repository } from "../git/repository";
import { CoolFastifyRequest, Route } from "../types/fastify";
import { Tag } from "../git/tag";
import { FastifyInstance, FastifyPluginOptions } from "fastify";
import { verifyRepoName } from "../routes/api/util";
import { ServerError } from "../git/error";

export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void {
	fastify.addHook("onRequest", async(req: CoolFastifyRequest, reply) => {
		if(!verifyRepoName(req.params.repo)) {
			reply.code(400).send("Bad request");
		}
	});

	fastify.route<Route>({
		method: "GET",
		url: "/info/refs",
		handler: async(req, reply) => {
			reply.header("Content-Type", "application/x-git-upload-pack-advertisement");

			if(!req.query.service) {
				reply.header("Content-Type", "text/plain");
				reply.code(403).send("Missing service query parameter\n");
				return;
			}

			if(req.query.service !== "git-upload-pack") {
				reply.header("Content-Type", "text/plain");
				reply.code(403).send("Access denied!\n");
				return;
			}

			if(Object.keys(req.query).length !== 1) {
				reply.code(403).send("Too many query parameters!\n");
				return;
			}

			const repository = await Repository.open(opts.config.settings.git_dir, req.params.repo);
			repository.HTTPconnect(req, reply);
		}
	});

	fastify.route<Route>({
		method: "POST",
		url: "/git-upload-pack",
		handler: async(req, reply) => {
			const repository = await Repository.open(opts.config.settings.git_dir, req.params.repo);
			repository.HTTPconnect(req, reply);
		}
	});

	fastify.route({
		method: "POST",
		url: "/git-receive-pack",
		handler: (req, reply) => {
			reply.header("Content-Type", "application/x-git-receive-pack-result");
			reply.code(403).send("Access denied!");
		}
	});

	fastify.route<Route>({
		method: "GET",
		url: "/refs/tags/:tag",
		handler: async(req, reply) => {
			const repository = await Repository.open(opts.config.settings.git_dir, req.params.repo).catch((err: ServerError) => err);

			if(repository instanceof ServerError) {
				reply.code(repository.code).send(repository.message);
				return;
			}

			const tag = await Tag.lookup(repository, req.params.tag).catch((err: ServerError) => err);

			if(tag instanceof ServerError) {
				reply.code(tag.code).send(tag.message);
				return;
			}

			tag.downloadTarball(reply);
		}
	});

	done();
}