From f2eb23cd87cf32b8fe545178143b5f49e06a58da Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Tue, 8 Dec 2020 21:16:10 +0100 Subject: emit more specific status codes on video upload (#3423) - reduce http status codes list to potentially useful codes - convert more codes to typed ones - factorize html generator for error responses --- server/controllers/client.ts | 32 +++----------------------------- server/controllers/static.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 29 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/client.ts b/server/controllers/client.ts index 49e6fd661..bd1f19f8c 100644 --- a/server/controllers/client.ts +++ b/server/controllers/client.ts @@ -3,12 +3,11 @@ import { constants, promises as fs } from 'fs' import { join } from 'path' import { CONFIG } from '@server/initializers/config' import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' +import { HttpStatusCode } from '@shared/core-utils' import { root } from '../helpers/core-utils' -import { logger } from '../helpers/logger' -import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers/constants' -import { ClientHtml } from '../lib/client-html' +import { STATIC_MAX_AGE } from '../initializers/constants' +import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html' import { asyncMiddleware, embedCSP } from '../middlewares' -import { HttpStatusCode } from '@shared/core-utils' const clientsRouter = express.Router() @@ -118,31 +117,12 @@ function serveServerTranslations (req: express.Request, res: express.Response) { return res.sendStatus(HttpStatusCode.NOT_FOUND_404) } -async function serveIndexHTML (req: express.Request, res: express.Response) { - if (req.accepts(ACCEPT_HEADERS) === 'html') { - try { - await generateHTMLPage(req, res, req.params.language) - return - } catch (err) { - logger.error('Cannot generate HTML page.', err) - } - } - - return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end() -} - async function generateEmbedHtmlPage (req: express.Request, res: express.Response) { const html = await ClientHtml.getEmbedHTML() return sendHTML(html, res) } -async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { - const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang) - - return sendHTML(html, res) -} - async function generateWatchHtmlPage (req: express.Request, res: express.Response) { const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) @@ -167,12 +147,6 @@ async function generateVideoChannelHtmlPage (req: express.Request, res: express. return sendHTML(html, res) } -function sendHTML (html: string, res: express.Response) { - res.set('Content-Type', 'text/html; charset=UTF-8') - - return res.send(html) -} - async function generateManifest (req: express.Request, res: express.Response) { const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') const manifestJson = await fs.readFile(manifestPhysicalPath, 'utf8') diff --git a/server/controllers/static.ts b/server/controllers/static.ts index ff77452dd..f12f00e1b 100644 --- a/server/controllers/static.ts +++ b/server/controllers/static.ts @@ -27,6 +27,7 @@ import { getTorrentFilePath, getVideoFilePath } from '@server/lib/video-paths' import { getThemeOrDefault } from '../lib/plugins/theme-utils' import { getEnabledResolutions, getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config' import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' +import { serveIndexHTML } from '@server/lib/client-html' const staticRouter = express.Router() @@ -119,6 +120,11 @@ staticRouter.get('/robots.txt', } ) +staticRouter.all('/teapot', + getCup, + asyncMiddleware(serveIndexHTML) +) + // security.txt service staticRouter.get('/security.txt', (_, res: express.Response) => { @@ -391,3 +397,11 @@ function getHLSPlaylist (video: MVideoFullLight) { return Object.assign(playlist, { Video: video }) } + +function getCup (req: express.Request, res: express.Response, next: express.NextFunction) { + res.status(HttpStatusCode.I_AM_A_TEAPOT_418) + res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1') + res.setHeader('Safe', 'if-sepia-awake') + + return next() +} -- cgit v1.2.3