@@ -131,8 +131,8 @@ private function fetchReference(Reference $reference): void {
131131 }
132132
133133 $ linkContentLength = $ headResponse ->getHeader ('Content-Length ' );
134- if (is_numeric ($ linkContentLength ) && (int ) $ linkContentLength > self ::MAX_CONTENT_LENGTH ) {
135- $ this ->logger ->debug ('Skip resolving links pointing to content length > 5 MiB ' );
134+ if (is_numeric ($ linkContentLength ) && (int )$ linkContentLength > self ::MAX_CONTENT_LENGTH ) {
135+ $ this ->logger ->debug ('[Head] Skip resolving links pointing to content length > 5 MiB ' );
136136 return ;
137137 }
138138
@@ -146,18 +146,28 @@ private function fetchReference(Reference $reference): void {
146146 }
147147
148148 try {
149- $ response = $ client ->get ($ reference ->getId (), [ 'timeout ' => 10 ]);
149+ $ response = $ client ->get ($ reference ->getId (), [ 'timeout ' => 10 , ' stream ' => true ]);
150150 } catch (\Exception $ e ) {
151151 $ this ->logger ->debug ('Failed to fetch link for obtaining open graph data ' , ['exception ' => $ e ]);
152152 return ;
153153 }
154154
155- $ responseBody = (string )$ response ->getBody ();
155+ $ body = $ response ->getBody ();
156+ if (is_resource ($ body )) {
157+ $ responseContent = fread ($ body , self ::MAX_CONTENT_LENGTH );
158+ if (!feof ($ body )) {
159+ $ this ->logger ->debug ('[Get] Skip resolving links pointing to content length > 5 MiB ' );
160+ return ;
161+ }
162+ } else {
163+ $ this ->logger ->error ('[Get] Impossible to check content length ' );
164+ return ;
165+ }
156166
157167 // OpenGraph handling
158168 $ consumer = new Consumer ();
159169 $ consumer ->useFallbackMode = true ;
160- $ object = $ consumer ->loadHtml ($ responseBody );
170+ $ object = $ consumer ->loadHtml ($ responseContent );
161171
162172 $ reference ->setUrl ($ reference ->getId ());
163173
0 commit comments