diff --git a/src/core/api.js b/src/core/api.js index 8c790023..ab9c1787 100644 --- a/src/core/api.js +++ b/src/core/api.js @@ -95,37 +95,42 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) { } }); + const acceptRegex = /^application\/json(; charset=utf-8)?$/; app.post('/api/json', async (req, res) => { - try { - let lang = languageCode(req); - let j = apiJSON(0, { t: "bad request" }); - try { - let contentCon = String(req.header('Content-Type')) === "application/json"; - let request = req.body; - if (contentCon && request.url) { - request.dubLang = request.dubLang ? lang : false; - - let chck = checkJSONPost(request); - if (!chck) throw new Error(); + const request = req.body; + const lang = languageCode(req); + const fail = (t) => { + const { status, body } = apiJSON(0, { t: loc(lang, t) }); + res.status(status).json(body); + } - const parsed = extract(chck.url); - if (parsed === null) { - return apiJSON(0, { t: errorUnsupported(lang) }) - } - - j = await match(parsed.host, parsed.patternMatch, lang, chck) - .catch(() => apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') })) - } else { - j = apiJSON(0, { - t: !contentCon ? "invalid content type header" : loc(lang, 'ErrorNoLink') - }); - } - } catch (e) { - j = apiJSON(0, { t: loc(lang, 'ErrorCantProcess') }); - } - return res.status(j.status).json(j.body); - } catch (e) { - return res.destroy(); + if (!acceptRegex.test(req.header('Content-Type'))) { + return fail('ErrorInvalidContentType'); + } + + if (!request.url) { + return fail('ErrorNoLink'); + } + + request.dubLang = request.dubLang ? lang : false; + const normalizedRequest = checkJSONPost(request); + if (!normalizedRequest) { + return fail('ErrorCantProcess'); + } + + const parsed = extract(normalizedRequest.url); + if (parsed === null) { + return fail('ErrorUnsupported'); + } + + try { + const result = await match( + parsed.host, parsed.patternMatch, lang, normalizedRequest + ); + + res.status(result.status).json(result.body); + } catch { + fail('ErrorSomethingWentWrong'); } }); diff --git a/src/localization/languages/en.json b/src/localization/languages/en.json index b0987337..6e9c9677 100644 --- a/src/localization/languages/en.json +++ b/src/localization/languages/en.json @@ -155,6 +155,7 @@ "SettingsTikTokH265Description": "download 1080p videos from tiktok in h265/hevc format when available.", "SettingsYoutubeDub": "use browser language", "SettingsYoutubeDubDescription": "uses your browser's default language for youtube dubbed audio tracks. works even if cobalt ui isn't translated to your language.", - "UpdateIstream": "better service support and ux" + "UpdateIstream": "better service support and ux", + "ErrorInvalidContentType": "invalid content type header" } }