@@ -168,20 +168,34 @@ protected function writeRecord($requestId, $type, $content = null)
168168 }
169169
170170 /**
171- * Write a stream to the connection as FCGI_STDOUT records.
171+ * Write a response to the connection as FCGI_STDOUT records.
172172 *
173- * @param int $requestId The request id to write to
174- * @param StreamInterface $stream The stream to write
173+ * @param int $requestId The request id to write to
174+ * @param string $headerData The header data to write (including terminating CRLFCRLF)
175+ * @param StreamInterface $stream The stream to write
175176 */
176- protected function writeStream ($ requestId , StreamInterface $ stream )
177+ protected function writeResponse ($ requestId, $ headerData , StreamInterface $ stream )
177178 {
178- while (! $ stream -> eof ()) {
179- $ data = $ stream -> read ( 65535 ) ;
179+ $ data = $ headerData ;
180+ $ eof = false ;
180181
181- if (false !== $ data ) {
182- $ this ->writeRecord ($ requestId , DaemonInterface::FCGI_STDOUT , $ data );
182+ $ stream ->rewind ();
183+
184+ do {
185+ $ dataLength = strlen ($ data );
186+
187+ if ($ dataLength < 65535 && !$ eof && !($ eof = $ stream ->eof ())) {
188+ $ readLength = 65535 - $ dataLength ;
189+ $ data .= $ stream ->read ($ readLength );
190+ $ dataLength = strlen ($ data );
183191 }
184- }
192+
193+ $ writeSize = min ($ dataLength , 65535 );
194+ $ writeData = substr ($ data , 0 , $ writeSize );
195+ $ data = substr ($ data , $ writeSize );
196+
197+ $ this ->writeRecord ($ requestId , DaemonInterface::FCGI_STDOUT , $ writeData );
198+ } while ($ writeSize === 65535 );
185199 }
186200
187201 /**
@@ -404,21 +418,15 @@ protected function dispatchRequest($requestId)
404418 */
405419 protected function sendResponse ($ requestId , ResponseInterface $ response )
406420 {
407- $ outputData = "Status: {$ response ->getStatusCode ()} {$ response ->getReasonPhrase ()}\r\n" ;
421+ $ headerData = "Status: {$ response ->getStatusCode ()} {$ response ->getReasonPhrase ()}\r\n" ;
408422
409423 foreach ($ response ->getHeaders () as $ name => $ values ) {
410- $ outputData .= $ name . ': ' . implode (', ' , $ values ) ."\r\n" ;
424+ $ headerData .= $ name . ': ' . implode (', ' , $ values ) ."\r\n" ;
411425 }
412426
413- $ outputData .= "\r\n" ;
414-
415- $ outputChunks = str_split ($ outputData , 65535 );
416-
417- foreach ($ outputChunks as $ chunk ) {
418- $ this ->writeRecord ($ requestId , DaemonInterface::FCGI_STDOUT , $ chunk );
419- }
427+ $ headerData .= "\r\n" ;
420428
421- $ this ->writeStream ($ requestId , $ response ->getBody ());
429+ $ this ->writeResponse ($ requestId, $ headerData , $ response ->getBody ());
422430
423431 $ this ->writeRecord ($ requestId , DaemonInterface::FCGI_STDOUT );
424432 $ this ->endRequest ($ requestId );
0 commit comments