diff options
author | Chocobozzz <me@florianbigard.com> | 2021-03-09 09:58:08 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-03-24 18:18:40 +0100 |
commit | b329abc2f00628f8c6814d304df6faad301d216c (patch) | |
tree | 733baa9746c0cc8497f66f71e14e7478a773ae88 | |
parent | 18b24b2dc5a21007f62b2e625d50b1f4b7dec784 (diff) | |
download | PeerTube-b329abc2f00628f8c6814d304df6faad301d216c.tar.gz PeerTube-b329abc2f00628f8c6814d304df6faad301d216c.tar.zst PeerTube-b329abc2f00628f8c6814d304df6faad301d216c.zip |
Fix request body limit
-rw-r--r-- | server/controllers/api/plugins.ts | 1 | ||||
-rw-r--r-- | server/helpers/requests.ts | 20 |
2 files changed, 16 insertions, 5 deletions
diff --git a/server/controllers/api/plugins.ts b/server/controllers/api/plugins.ts index 1c0b5edb1..bb69f25a1 100644 --- a/server/controllers/api/plugins.ts +++ b/server/controllers/api/plugins.ts | |||
@@ -205,7 +205,6 @@ async function listAvailablePlugins (req: express.Request, res: express.Response | |||
205 | if (!resultList) { | 205 | if (!resultList) { |
206 | return res.status(HttpStatusCode.SERVICE_UNAVAILABLE_503) | 206 | return res.status(HttpStatusCode.SERVICE_UNAVAILABLE_503) |
207 | .json({ error: 'Plugin index unavailable. Please retry later' }) | 207 | .json({ error: 'Plugin index unavailable. Please retry later' }) |
208 | .end() | ||
209 | } | 208 | } |
210 | 209 | ||
211 | return res.json(resultList) | 210 | return res.json(resultList) |
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index 2c9da213c..aee8f6673 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts | |||
@@ -30,13 +30,25 @@ const peertubeGot = got.extend({ | |||
30 | handlers: [ | 30 | handlers: [ |
31 | (options, next) => { | 31 | (options, next) => { |
32 | const promiseOrStream = next(options) as CancelableRequest<any> | 32 | const promiseOrStream = next(options) as CancelableRequest<any> |
33 | const bodyKBLimit = options.context?.bodyKBLimit | 33 | const bodyKBLimit = options.context?.bodyKBLimit as number |
34 | if (!bodyKBLimit) throw new Error('No KB limit for this request') | 34 | if (!bodyKBLimit) throw new Error('No KB limit for this request') |
35 | 35 | ||
36 | const bodyLimit = bodyKBLimit * 1000 | ||
37 | |||
36 | /* eslint-disable @typescript-eslint/no-floating-promises */ | 38 | /* eslint-disable @typescript-eslint/no-floating-promises */ |
37 | promiseOrStream.on('downloadProgress', progress => { | 39 | promiseOrStream.on('downloadProgress', progress => { |
38 | if (progress.transferred * 1000 > bodyKBLimit && progress.percent !== 1) { | 40 | if (progress.transferred > bodyLimit && progress.percent !== 1) { |
39 | promiseOrStream.cancel(`Exceeded the download limit of ${bodyKBLimit} bytes`) | 41 | const message = `Exceeded the download limit of ${bodyLimit} B` |
42 | logger.warn(message) | ||
43 | |||
44 | // CancelableRequest | ||
45 | if (promiseOrStream.cancel) { | ||
46 | promiseOrStream.cancel() | ||
47 | return | ||
48 | } | ||
49 | |||
50 | // Stream | ||
51 | (promiseOrStream as any).destroy() | ||
40 | } | 52 | } |
41 | }) | 53 | }) |
42 | 54 | ||
@@ -177,5 +189,5 @@ function buildRequestError (error: any) { | |||
177 | error.responseBody = error.response.body | 189 | error.responseBody = error.response.body |
178 | } | 190 | } |
179 | 191 | ||
180 | return newError | 192 | return error |
181 | } | 193 | } |