1111
1212use Psr \Http \Message \ResponseInterface ;
1313use RuntimeException ;
14+ use Zend \Diactoros \RelativeStream ;
1415
1516class SapiStreamEmitter implements EmitterInterface
1617{
@@ -56,14 +57,15 @@ public function emit(ResponseInterface $response, $maxBufferLength = 8192)
5657 private function emitBody (ResponseInterface $ response , $ maxBufferLength )
5758 {
5859 $ body = $ response ->getBody ();
59- if ($ body ->isSeekable ()) {
60- $ body ->rewind ();
6160
62- while (! $ body ->eof ()) {
63- echo $ body ->read ($ maxBufferLength );
64- }
65- } else {
61+ if (! $ body ->isSeekable ()) {
6662 echo $ body ;
63+ return ;
64+ }
65+
66+ $ body ->rewind ();
67+ while (! $ body ->eof ()) {
68+ echo $ body ->read ($ maxBufferLength );
6769 }
6870 }
6971
@@ -78,21 +80,21 @@ private function emitBodyRange(array $range, ResponseInterface $response, $maxBu
7880 {
7981 list ($ unit , $ first , $ last , $ length ) = $ range ;
8082
81- ++$ last ; //zero-based position
8283 $ body = $ response ->getBody ();
8384
84- if (!$ body ->isSeekable ()) {
85+ if (! $ body ->isSeekable ()) {
8586 $ contents = $ body ->getContents ();
86- echo substr ($ contents , $ first , $ last - $ first );
87+ echo substr ($ contents , $ first , $ last - $ first + 1 );
8788 return ;
8889 }
8990
90- $ body ->seek ($ first );
91- $ pos = $ first ;
92-
93- while (! $ body ->eof () && $ pos < $ last ) {
94- if (($ pos + $ maxBufferLength ) > $ last ) {
95- echo $ body ->read ($ last - $ pos );
91+ $ body = new RelativeStream ($ body , $ first );
92+ $ body ->rewind ();
93+ $ pos = 0 ;
94+ $ length = $ last - $ first + 1 ;
95+ while (! $ body ->eof () && $ pos < $ length ) {
96+ if (($ pos + $ maxBufferLength ) > $ length ) {
97+ echo $ body ->read ($ length - $ pos );
9698 break ;
9799 }
98100
0 commit comments