diff options
Diffstat (limited to 'server/helpers/requests.ts')
-rw-r--r-- | server/helpers/requests.ts | 179 |
1 files changed, 132 insertions, 47 deletions
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index b556c392e..2c9da213c 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts | |||
@@ -1,58 +1,124 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import { createWriteStream, remove } from 'fs-extra' | 1 | import { createWriteStream, remove } from 'fs-extra' |
3 | import * as request from 'request' | 2 | import got, { CancelableRequest, Options as GotOptions } from 'got' |
3 | import { join } from 'path' | ||
4 | import { CONFIG } from '../initializers/config' | ||
4 | import { ACTIVITY_PUB, PEERTUBE_VERSION, WEBSERVER } from '../initializers/constants' | 5 | import { ACTIVITY_PUB, PEERTUBE_VERSION, WEBSERVER } from '../initializers/constants' |
6 | import { pipelinePromise } from './core-utils' | ||
5 | import { processImage } from './image-utils' | 7 | import { processImage } from './image-utils' |
6 | import { join } from 'path' | ||
7 | import { logger } from './logger' | 8 | import { logger } from './logger' |
8 | import { CONFIG } from '../initializers/config' | ||
9 | 9 | ||
10 | function doRequest <T> ( | 10 | const httpSignature = require('http-signature') |
11 | requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean }, | 11 | |
12 | bodyKBLimit = 1000 // 1MB | 12 | type PeerTubeRequestOptions = { |
13 | ): Bluebird<{ response: request.RequestResponse, body: T }> { | 13 | activityPub?: boolean |
14 | if (!(requestOptions.headers)) requestOptions.headers = {} | 14 | bodyKBLimit?: number // 1MB |
15 | requestOptions.headers['User-Agent'] = getUserAgent() | 15 | httpSignature?: { |
16 | algorithm: string | ||
17 | authorizationHeaderName: string | ||
18 | keyId: string | ||
19 | key: string | ||
20 | headers: string[] | ||
21 | } | ||
22 | jsonResponse?: boolean | ||
23 | } & Pick<GotOptions, 'headers' | 'json' | 'method' | 'searchParams'> | ||
24 | |||
25 | const peertubeGot = got.extend({ | ||
26 | headers: { | ||
27 | 'user-agent': getUserAgent() | ||
28 | }, | ||
29 | |||
30 | handlers: [ | ||
31 | (options, next) => { | ||
32 | const promiseOrStream = next(options) as CancelableRequest<any> | ||
33 | const bodyKBLimit = options.context?.bodyKBLimit | ||
34 | if (!bodyKBLimit) throw new Error('No KB limit for this request') | ||
35 | |||
36 | /* eslint-disable @typescript-eslint/no-floating-promises */ | ||
37 | promiseOrStream.on('downloadProgress', progress => { | ||
38 | if (progress.transferred * 1000 > bodyKBLimit && progress.percent !== 1) { | ||
39 | promiseOrStream.cancel(`Exceeded the download limit of ${bodyKBLimit} bytes`) | ||
40 | } | ||
41 | }) | ||
16 | 42 | ||
17 | if (requestOptions.activityPub === true) { | 43 | return promiseOrStream |
18 | requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER | 44 | } |
45 | ], | ||
46 | |||
47 | hooks: { | ||
48 | beforeRequest: [ | ||
49 | options => { | ||
50 | const headers = options.headers || {} | ||
51 | headers['host'] = options.url.host | ||
52 | }, | ||
53 | |||
54 | options => { | ||
55 | const httpSignatureOptions = options.context?.httpSignature | ||
56 | |||
57 | if (httpSignatureOptions) { | ||
58 | const method = options.method ?? 'GET' | ||
59 | const path = options.path ?? options.url.pathname | ||
60 | |||
61 | if (!method || !path) { | ||
62 | throw new Error(`Cannot sign request without method (${method}) or path (${path}) ${options}`) | ||
63 | } | ||
64 | |||
65 | httpSignature.signRequest({ | ||
66 | getHeader: function (header) { | ||
67 | return options.headers[header] | ||
68 | }, | ||
69 | |||
70 | setHeader: function (header, value) { | ||
71 | options.headers[header] = value | ||
72 | }, | ||
73 | |||
74 | method, | ||
75 | path | ||
76 | }, httpSignatureOptions) | ||
77 | } | ||
78 | } | ||
79 | ] | ||
19 | } | 80 | } |
81 | }) | ||
20 | 82 | ||
21 | return new Bluebird<{ response: request.RequestResponse, body: T }>((res, rej) => { | 83 | function doRequest (url: string, options: PeerTubeRequestOptions = {}) { |
22 | request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body })) | 84 | const gotOptions = buildGotOptions(options) |
23 | .on('data', onRequestDataLengthCheck(bodyKBLimit)) | 85 | |
24 | }) | 86 | return peertubeGot(url, gotOptions) |
87 | .catch(err => { throw buildRequestError(err) }) | ||
88 | } | ||
89 | |||
90 | function doJSONRequest <T> (url: string, options: PeerTubeRequestOptions = {}) { | ||
91 | const gotOptions = buildGotOptions(options) | ||
92 | |||
93 | return peertubeGot<T>(url, { ...gotOptions, responseType: 'json' }) | ||
94 | .catch(err => { throw buildRequestError(err) }) | ||
25 | } | 95 | } |
26 | 96 | ||
27 | function doRequestAndSaveToFile ( | 97 | async function doRequestAndSaveToFile ( |
28 | requestOptions: request.CoreOptions & request.UriOptions, | 98 | url: string, |
29 | destPath: string, | 99 | destPath: string, |
30 | bodyKBLimit = 10000 // 10MB | 100 | options: PeerTubeRequestOptions = {} |
31 | ) { | 101 | ) { |
32 | if (!requestOptions.headers) requestOptions.headers = {} | 102 | const gotOptions = buildGotOptions(options) |
33 | requestOptions.headers['User-Agent'] = getUserAgent() | ||
34 | |||
35 | return new Bluebird<void>((res, rej) => { | ||
36 | const file = createWriteStream(destPath) | ||
37 | file.on('finish', () => res()) | ||
38 | 103 | ||
39 | request(requestOptions) | 104 | const outFile = createWriteStream(destPath) |
40 | .on('data', onRequestDataLengthCheck(bodyKBLimit)) | ||
41 | .on('error', err => { | ||
42 | file.close() | ||
43 | 105 | ||
44 | remove(destPath) | 106 | try { |
45 | .catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err })) | 107 | await pipelinePromise( |
108 | peertubeGot.stream(url, gotOptions), | ||
109 | outFile | ||
110 | ) | ||
111 | } catch (err) { | ||
112 | remove(destPath) | ||
113 | .catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err })) | ||
46 | 114 | ||
47 | return rej(err) | 115 | throw buildRequestError(err) |
48 | }) | 116 | } |
49 | .pipe(file) | ||
50 | }) | ||
51 | } | 117 | } |
52 | 118 | ||
53 | async function downloadImage (url: string, destDir: string, destName: string, size: { width: number, height: number }) { | 119 | async function downloadImage (url: string, destDir: string, destName: string, size: { width: number, height: number }) { |
54 | const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName) | 120 | const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName) |
55 | await doRequestAndSaveToFile({ method: 'GET', uri: url }, tmpPath) | 121 | await doRequestAndSaveToFile(url, tmpPath) |
56 | 122 | ||
57 | const destPath = join(destDir, destName) | 123 | const destPath = join(destDir, destName) |
58 | 124 | ||
@@ -73,24 +139,43 @@ function getUserAgent () { | |||
73 | 139 | ||
74 | export { | 140 | export { |
75 | doRequest, | 141 | doRequest, |
142 | doJSONRequest, | ||
76 | doRequestAndSaveToFile, | 143 | doRequestAndSaveToFile, |
77 | downloadImage | 144 | downloadImage |
78 | } | 145 | } |
79 | 146 | ||
80 | // --------------------------------------------------------------------------- | 147 | // --------------------------------------------------------------------------- |
81 | 148 | ||
82 | // Thanks to https://github.com/request/request/issues/2470#issuecomment-268929907 <3 | 149 | function buildGotOptions (options: PeerTubeRequestOptions) { |
83 | function onRequestDataLengthCheck (bodyKBLimit: number) { | 150 | const { activityPub, bodyKBLimit = 1000 } = options |
84 | let bufferLength = 0 | ||
85 | const bytesLimit = bodyKBLimit * 1000 | ||
86 | 151 | ||
87 | return function (chunk) { | 152 | const context = { bodyKBLimit, httpSignature: options.httpSignature } |
88 | bufferLength += chunk.length | ||
89 | if (bufferLength > bytesLimit) { | ||
90 | this.abort() | ||
91 | 153 | ||
92 | const error = new Error(`Response was too large - aborted after ${bytesLimit} bytes.`) | 154 | let headers = options.headers || {} |
93 | this.emit('error', error) | 155 | |
94 | } | 156 | headers = { ...headers, date: new Date().toUTCString() } |
157 | |||
158 | if (activityPub) { | ||
159 | headers = { ...headers, accept: ACTIVITY_PUB.ACCEPT_HEADER } | ||
95 | } | 160 | } |
161 | |||
162 | return { | ||
163 | method: options.method, | ||
164 | json: options.json, | ||
165 | searchParams: options.searchParams, | ||
166 | headers, | ||
167 | context | ||
168 | } | ||
169 | } | ||
170 | |||
171 | function buildRequestError (error: any) { | ||
172 | const newError = new Error(error.message) | ||
173 | newError.name = error.name | ||
174 | newError.stack = error.stack | ||
175 | |||
176 | if (error.response?.body) { | ||
177 | error.responseBody = error.response.body | ||
178 | } | ||
179 | |||
180 | return newError | ||
96 | } | 181 | } |