diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-12-08 21:16:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-08 21:16:10 +0100 |
commit | f2eb23cd87cf32b8fe545178143b5f49e06a58da (patch) | |
tree | af7d59945af70e28fd85047e2c688c59a908f548 /server/controllers | |
parent | c977fd3ec931c059111ddb2b8d6ddbb20b6b99a1 (diff) | |
download | PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.tar.gz PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.tar.zst PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.zip |
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
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/client.ts | 32 | ||||
-rw-r--r-- | server/controllers/static.ts | 14 |
2 files changed, 17 insertions, 29 deletions
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' | |||
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { CONFIG } from '@server/initializers/config' | 4 | import { CONFIG } from '@server/initializers/config' |
5 | import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' | 5 | import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' |
6 | import { HttpStatusCode } from '@shared/core-utils' | ||
6 | import { root } from '../helpers/core-utils' | 7 | import { root } from '../helpers/core-utils' |
7 | import { logger } from '../helpers/logger' | 8 | import { STATIC_MAX_AGE } from '../initializers/constants' |
8 | import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers/constants' | 9 | import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html' |
9 | import { ClientHtml } from '../lib/client-html' | ||
10 | import { asyncMiddleware, embedCSP } from '../middlewares' | 10 | import { asyncMiddleware, embedCSP } from '../middlewares' |
11 | import { HttpStatusCode } from '@shared/core-utils' | ||
12 | 11 | ||
13 | const clientsRouter = express.Router() | 12 | const clientsRouter = express.Router() |
14 | 13 | ||
@@ -118,31 +117,12 @@ function serveServerTranslations (req: express.Request, res: express.Response) { | |||
118 | return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | 117 | return res.sendStatus(HttpStatusCode.NOT_FOUND_404) |
119 | } | 118 | } |
120 | 119 | ||
121 | async function serveIndexHTML (req: express.Request, res: express.Response) { | ||
122 | if (req.accepts(ACCEPT_HEADERS) === 'html') { | ||
123 | try { | ||
124 | await generateHTMLPage(req, res, req.params.language) | ||
125 | return | ||
126 | } catch (err) { | ||
127 | logger.error('Cannot generate HTML page.', err) | ||
128 | } | ||
129 | } | ||
130 | |||
131 | return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end() | ||
132 | } | ||
133 | |||
134 | async function generateEmbedHtmlPage (req: express.Request, res: express.Response) { | 120 | async function generateEmbedHtmlPage (req: express.Request, res: express.Response) { |
135 | const html = await ClientHtml.getEmbedHTML() | 121 | const html = await ClientHtml.getEmbedHTML() |
136 | 122 | ||
137 | return sendHTML(html, res) | 123 | return sendHTML(html, res) |
138 | } | 124 | } |
139 | 125 | ||
140 | async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { | ||
141 | const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang) | ||
142 | |||
143 | return sendHTML(html, res) | ||
144 | } | ||
145 | |||
146 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { | 126 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { |
147 | const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) | 127 | const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) |
148 | 128 | ||
@@ -167,12 +147,6 @@ async function generateVideoChannelHtmlPage (req: express.Request, res: express. | |||
167 | return sendHTML(html, res) | 147 | return sendHTML(html, res) |
168 | } | 148 | } |
169 | 149 | ||
170 | function sendHTML (html: string, res: express.Response) { | ||
171 | res.set('Content-Type', 'text/html; charset=UTF-8') | ||
172 | |||
173 | return res.send(html) | ||
174 | } | ||
175 | |||
176 | async function generateManifest (req: express.Request, res: express.Response) { | 150 | async function generateManifest (req: express.Request, res: express.Response) { |
177 | const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') | 151 | const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') |
178 | const manifestJson = await fs.readFile(manifestPhysicalPath, 'utf8') | 152 | 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' | |||
27 | import { getThemeOrDefault } from '../lib/plugins/theme-utils' | 27 | import { getThemeOrDefault } from '../lib/plugins/theme-utils' |
28 | import { getEnabledResolutions, getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config' | 28 | import { getEnabledResolutions, getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config' |
29 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | 29 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
30 | import { serveIndexHTML } from '@server/lib/client-html' | ||
30 | 31 | ||
31 | const staticRouter = express.Router() | 32 | const staticRouter = express.Router() |
32 | 33 | ||
@@ -119,6 +120,11 @@ staticRouter.get('/robots.txt', | |||
119 | } | 120 | } |
120 | ) | 121 | ) |
121 | 122 | ||
123 | staticRouter.all('/teapot', | ||
124 | getCup, | ||
125 | asyncMiddleware(serveIndexHTML) | ||
126 | ) | ||
127 | |||
122 | // security.txt service | 128 | // security.txt service |
123 | staticRouter.get('/security.txt', | 129 | staticRouter.get('/security.txt', |
124 | (_, res: express.Response) => { | 130 | (_, res: express.Response) => { |
@@ -391,3 +397,11 @@ function getHLSPlaylist (video: MVideoFullLight) { | |||
391 | 397 | ||
392 | return Object.assign(playlist, { Video: video }) | 398 | return Object.assign(playlist, { Video: video }) |
393 | } | 399 | } |
400 | |||
401 | function getCup (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
402 | res.status(HttpStatusCode.I_AM_A_TEAPOT_418) | ||
403 | res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1') | ||
404 | res.setHeader('Safe', 'if-sepia-awake') | ||
405 | |||
406 | return next() | ||
407 | } | ||