@@ -128,6 +128,13 @@ function getContents(
128
128
'headers ' => array_merge ($ defaultHttpHeaders , $ httpHeadersNormalized ),
129
129
'curl_options ' => $ curlOptions ,
130
130
];
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
+
131
138
if (Configuration::getConfig ('proxy ' , 'url ' ) && !defined ('NOPROXY ' )) {
132
139
$ config ['proxy ' ] = Configuration::getConfig ('proxy ' , 'url ' );
133
140
}
@@ -200,10 +207,9 @@ function getContents(
200
207
}
201
208
202
209
/**
203
- * Private function used internally
204
- *
205
210
* Fetch content from url
206
211
*
212
+ * @internal Private function used internally
207
213
* @throws HttpException
208
214
*/
209
215
function _http_request (string $ url , array $ config = []): array
@@ -216,6 +222,7 @@ function _http_request(string $url, array $config = []): array
216
222
'curl_options ' => [],
217
223
'if_not_modified_since ' => null ,
218
224
'retries ' => 3 ,
225
+ 'max_filesize ' => null ,
219
226
];
220
227
$ config = array_merge ($ defaults , $ config );
221
228
@@ -235,6 +242,21 @@ function _http_request(string $url, array $config = []): array
235
242
curl_setopt ($ ch , CURLOPT_PROTOCOLS , CURLPROTO_HTTP | CURLPROTO_HTTPS );
236
243
// Force HTTP 1.1 because newer versions of libcurl defaults to HTTP/2
237
244
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
+
238
260
if ($ config ['proxy ' ]) {
239
261
curl_setopt ($ ch , CURLOPT_PROXY , $ config ['proxy ' ]);
240
262
}
0 commit comments