From c63e558f402cfad914031a58fdcf3d8e0f3d125d Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 25 Jul 2021 12:32:59 +0200 Subject: Moved backend routes to a dedicated directory --- packages/server/src/routes/repo.ts | 84 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 packages/server/src/routes/repo.ts (limited to 'packages/server/src/routes/repo.ts') diff --git a/packages/server/src/routes/repo.ts b/packages/server/src/routes/repo.ts new file mode 100644 index 0000000..ce81dcd --- /dev/null +++ b/packages/server/src/routes/repo.ts @@ -0,0 +1,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 { BaseError } 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({ + 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.base_dir, req.params.repo); + repository.HTTPconnect(req, reply); + } + }); + + fastify.route({ + method: "POST", + url: "/git-upload-pack", + handler: async(req, reply) => { + const repository = await Repository.open(opts.config.settings.base_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({ + method: "GET", + url: "/refs/tags/:tag", + handler: async(req, reply) => { + const repository: Repository | BaseError = await Repository.open(opts.settings.base_dir, req.params.repo).catch(err => err); + + if(repository instanceof BaseError) { + reply.code(repository.code).send(repository.message); + return; + } + + const tag = await Tag.lookup(repository, req.params.tag).catch(err => err); + + if(tag instanceof BaseError) { + reply.code(tag.code).send(tag.message); + return; + } + + tag.downloadTarball(reply); + } + }); + + done(); +} \ No newline at end of file -- cgit v1.2.3-18-g5258