File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 1515use GuzzleHttp \Exception \RequestException ;
1616use GuzzleHttp \Psr7 \Request ;
1717use GuzzleHttp \Psr7 \Response ;
18+ use GuzzleHttp \Psr7 \Utils ;
1819use Illuminate \Support \Facades \App ;
1920use Psr \Http \Message \RequestInterface ;
2021
@@ -121,12 +122,33 @@ protected function performHttpRequest(
121122 /** @var Client $httpClient */
122123 $ httpClient = App::make (Client::class);
123124
125+ // Send a streamed response to be able to validate the size
126+ $ options ['stream ' ] = true ;
127+ $ options ['progress ' ] = function (
128+ $ downloadTotal ,
129+ $ downloadedBytes
130+ ) use ($ request ) {
131+ if ($ downloadedBytes > 1024000 ) {
132+ throw new \RuntimeException ("Unplausible response size while fetching from " . $ request ->getUri ());
133+ }
134+ };
135+
124136 /** @var Response $response */
125137 $ response = $ httpClient ->send (
126138 $ request ,
127139 $ options
128140 );
129141
142+ // Convert the streamed response into a "normal" one
143+ $ buffer = '' ;
144+
145+ while (!$ response ->getBody ()->eof ()) {
146+ $ buffer .= $ response ->getBody ()->read (8192 );
147+ }
148+
149+ // Overwrite streamed body
150+ $ response = $ response ->withBody (Utils::streamFor ($ buffer ));
151+
130152 // Validate response
131153 if (!json_validate ((string ) $ response ->getBody ())) {
132154 throw new RequestException (
You can’t perform that action at this time.
0 commit comments