Skip to content

Commit a5779d3

Browse files
authored
feat: add max file size to http responses (#3140)
1 parent c2c88e9 commit a5779d3

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

config.default.ini.php

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
timeout = 60
2020
useragent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0"
2121

22+
; Max http response size in MB
23+
max_filesize = 20
24+
2225
[cache]
2326

2427
; Defines the cache type used by RSS-Bridge

lib/contents.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ function getContents(
128128
'headers' => array_merge($defaultHttpHeaders, $httpHeadersNormalized),
129129
'curl_options' => $curlOptions,
130130
];
131+
132+
$maxFileSize = Configuration::getConfig('http', 'max_filesize');
133+
if ($maxFileSize) {
134+
// Multiply with 2^20 (1M) to the value in bytes
135+
$config['max_filesize'] = $maxFileSize * 2 ** 20;
136+
}
137+
131138
if (Configuration::getConfig('proxy', 'url') && !defined('NOPROXY')) {
132139
$config['proxy'] = Configuration::getConfig('proxy', 'url');
133140
}
@@ -200,10 +207,9 @@ function getContents(
200207
}
201208

202209
/**
203-
* Private function used internally
204-
*
205210
* Fetch content from url
206211
*
212+
* @internal Private function used internally
207213
* @throws HttpException
208214
*/
209215
function _http_request(string $url, array $config = []): array
@@ -216,6 +222,7 @@ function _http_request(string $url, array $config = []): array
216222
'curl_options' => [],
217223
'if_not_modified_since' => null,
218224
'retries' => 3,
225+
'max_filesize' => null,
219226
];
220227
$config = array_merge($defaults, $config);
221228

@@ -235,6 +242,21 @@ function _http_request(string $url, array $config = []): array
235242
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
236243
// Force HTTP 1.1 because newer versions of libcurl defaults to HTTP/2
237244
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
245+
246+
if ($config['max_filesize']) {
247+
// This option inspects the Content-Length header
248+
curl_setopt($ch, CURLOPT_MAXFILESIZE, $config['max_filesize']);
249+
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
250+
// This progress function will monitor responses who omit the Content-Length header
251+
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($ch, $downloadSize, $downloaded, $uploadSize, $uploaded) use ($config) {
252+
if ($downloaded > $config['max_filesize']) {
253+
// Return a non-zero value to abort the transfer
254+
return -1;
255+
}
256+
return 0;
257+
});
258+
}
259+
238260
if ($config['proxy']) {
239261
curl_setopt($ch, CURLOPT_PROXY, $config['proxy']);
240262
}

0 commit comments

Comments
 (0)