File tree Expand file tree Collapse file tree 1 file changed +18
-1
lines changed
src/Recorders/ExternalHttpRecorder Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change 66use Illuminate \Http \Client \Events \ConnectionFailed ;
77use Illuminate \Http \Client \Events \RequestSending ;
88use Illuminate \Http \Client \Events \ResponseReceived ;
9+ use Illuminate \Http \Client \Response ;
910use Spatie \FlareClient \Recorders \ExternalHttpRecorder \ExternalHttpRecorder as BaseExternalHttpRecorder ;
1011use Spatie \FlareClient \Support \BackTracer ;
1112use Spatie \FlareClient \Support \Redactor ;
@@ -34,12 +35,28 @@ public function boot(): void
3435
3536 $ this ->dispatcher ->listen (ResponseReceived::class, fn (ResponseReceived $ event ) => $ this ->recordReceived (
3637 $ event ->response ->status (),
37- strlen ($ event ->response -> body () ),
38+ $ this -> getResponseLength ($ event ->response ),
3839 $ event ->response ->headers (),
3940 ));
4041
4142 $ this ->dispatcher ->listen (ConnectionFailed::class, fn (ConnectionFailed $ event ) => $ this ->recordConnectionFailed (
4243 $ event ->exception ::class
4344 ));
4445 }
46+
47+ /**
48+ * Determine the content length of the response without reading the stream.
49+ */
50+ protected function getResponseLength (Response $ response ): ?int
51+ {
52+ if ($ response ->getHeaderLine ('Transfer-Encoding ' ) === 'chunked ' ) {
53+ return null ;
54+ }
55+
56+ if ($ response ->hasHeader ('Content-Length ' )) {
57+ return (int ) $ response ->getHeaderLine ('Content-Length ' );
58+ }
59+
60+ return $ response ->toPsrResponse ()->getBody ()->getSize () ?: null ;
61+ }
4562}
You can’t perform that action at this time.
0 commit comments