diff --git a/microservices/download/index.js b/microservices/download/index.js index 4f4fc6b..a6da31c 100644 --- a/microservices/download/index.js +++ b/microservices/download/index.js @@ -3,26 +3,55 @@ const utils = require("./utils.js"); const port = parseInt(process.env.PORT) || 8080; const server = http.createServer(async (req, res) => { - // Since req.url is our URL pluse query params, lets split them first. - const path = req.url.split("?"); + if (typeof req?.url !== "string"){ + // Not sure if this error condition is even possible, but handling it anyway + await utils.displayError(req, res, { + code: 500, + msg: "Internal Server Error: Misformed URL" + }); + console.log("Download Returned 500 due to the requested URL not being received as a string internally"); + return; + } + + // Since req.url is our URL plus query params, lets split them first. + let [urlPath, urlParams] = req.url.split("?"); // Set our Request Route - if (path[0] === "/" && req.method === "GET") { + if (urlPath === "/" && req.method === "GET") { + + if (typeof urlParams !== "string"){ + await utils.displayError(req, res, { + code: 400, + msg: "Missing Query Parameters" + }); + console.log("Download Returned 400 due to the requested URL having no query parameters"); + return; + } + + if (urlParams.length > 36){ + // Longest valid combo is 36 characters ("os=silicon_mac&type=mac_zip_blockmap") + await utils.displayError(req, res, { + code: 414, + msg: "Requested Parameters are Too Long" + }); + console.log("Download Returned 414 due to the provided parameters being too long"); + return; + } - let params = { - os: utils.query_os(path[1]), - type: utils.query_type(path[1]) + let validatedParams = { + os: utils.query_os(urlParams), + type: utils.query_type(urlParams) }; - if (!params.os || !params.type) { + if (!validatedParams.os || !validatedParams.type) { await utils.displayError(req, res, { code: 503, - msg: "Missing Required Download Parameters" + msg: "Required Download Parameters Missing or Invalid" }); - console.log("Download Returned 503 due to missing os or type"); + console.log("Download Returned 503 due to missing or invalid os or type"); return; } - let redirLink = await utils.findLink(params.os, params.type); + let redirLink = await utils.findLink(validatedParams.os, validatedParams.type); if (!redirLink.ok) { await utils.displayError(req, res, redirLink); @@ -34,7 +63,7 @@ const server = http.createServer(async (req, res) => { Location: redirLink.content }).end(); - console.log(`Download Returned: OS: ${params.os} - TYPE: ${params.type} - URL: ${redirLink.content}`); + console.log(`Download Returned: OS: ${validatedParams.os} - TYPE: ${validatedParams.type} - URL: ${redirLink.content}`); } else {