@@ -1775,7 +1775,9 @@ protected function _callApiStreaming(
1775
1775
stream_set_timeout ($ ch , 0 );
1776
1776
1777
1777
// collect headers
1778
- $ result = stream_get_line ($ ch , 1048576 , "\r\n\r\n" );
1778
+ do {
1779
+ $ result = stream_get_line ($ ch , 1048576 , "\r\n\r\n" );
1780
+ }while (!$ result );
1779
1781
$ headers = explode ("\r\n" , $ result );
1780
1782
1781
1783
// find HTTP status
@@ -1806,50 +1808,58 @@ protected function _callApiStreaming(
1806
1808
$ signal_function = function_exists ('pcntl_signal_dispatch ' );
1807
1809
$ data = '' ;
1808
1810
$ last_message = time ();
1809
-
1811
+ $ chunk_length = 0 ;
1812
+ $ message_length = 0 ;
1813
+
1810
1814
while (!feof ($ ch )) {
1811
1815
// call signal handlers, if any
1812
1816
if ($ signal_function ) {
1813
1817
pcntl_signal_dispatch ();
1814
1818
}
1815
-
1816
1819
$ cha = [$ ch ];
1817
1820
$ write = $ except = null ;
1818
1821
if (false === ($ num_changed_streams = stream_select ($ cha , $ write , $ except , 0 , 200000 ))) {
1819
1822
break ;
1820
1823
} elseif ($ num_changed_streams === 0 ) {
1821
1824
if (time () - $ last_message >= 1 ) {
1822
- // deliver empty message, allow callback to cancel stream
1825
+ //deliver empty message, allow callback to cancel stream
1823
1826
$ cancel_stream = $ this ->_deliverStreamingMessage (null );
1824
1827
if ($ cancel_stream ) {
1825
1828
break ;
1826
1829
}
1827
-
1828
1830
$ last_message = time ();
1829
1831
}
1830
1832
continue ;
1831
1833
}
1832
-
1833
- $ chunk_length = fgets ($ ch , 10 );
1834
-
1834
+ $ chunk_length_raw = $ chunk_length = fgets ($ ch , 10 );
1835
1835
if ($ chunk_length === '' || !$ chunk_length = hexdec ($ chunk_length )) {
1836
1836
continue ;
1837
1837
}
1838
1838
1839
- $ chunk = fread ($ ch , $ chunk_length + 4 );
1840
- $ data .= $ chunk ;
1839
+ $ chunk = '' ;
1840
+ do {
1841
+ $ chunk .= fread ($ ch , $ chunk_length );
1842
+ $ chunk_length -= strlen ($ chunk );
1843
+ } while ( $ chunk_length > 0 );
1844
+
1845
+ if (0 === $ message_length ){
1846
+ $ message_length = (int ) strstr ($ chunk , "\r\n" , true );
1847
+ if ($ message_length ){
1848
+ $ chunk = substr ($ chunk , strpos ( $ chunk , "\r\n" ) + 2 );
1849
+ }else {
1850
+ continue ;
1851
+ }
1841
1852
1842
- // extract object to parse
1843
- list ($ object_length , $ temp ) = explode ("\r\n" , $ data , 2 );
1844
- if ($ object_length < 1
1845
- || strlen ($ temp ) < $ object_length ) {
1846
- continue ;
1853
+ $ data = $ chunk ;
1854
+ }else {
1855
+ $ data .= $ chunk ;
1847
1856
}
1848
1857
1849
- $ reply = substr ($ temp , 0 , $ object_length );
1850
- $ data = substr ($ temp , $ object_length + 2 );
1858
+ if (strlen ($ data ) < $ message_length ){
1859
+ continue ;
1860
+ }
1851
1861
1852
- $ reply = $ this ->_parseApiReply ($ reply );
1862
+ $ reply = $ this ->_parseApiReply ($ data );
1853
1863
switch ($ this ->_return_format ) {
1854
1864
case CODEBIRD_RETURNFORMAT_ARRAY :
1855
1865
$ reply ['httpstatus ' ] = $ httpstatus ;
@@ -1866,6 +1876,8 @@ protected function _callApiStreaming(
1866
1876
break ;
1867
1877
}
1868
1878
1879
+ $ data = '' ;
1880
+ $ message_length = 0 ;
1869
1881
$ last_message = time ();
1870
1882
}
1871
1883
@@ -1881,7 +1893,7 @@ protected function _callApiStreaming(
1881
1893
*/
1882
1894
protected function _deliverStreamingMessage ($ message )
1883
1895
{
1884
- return call_user_func ($ this ->_streaming_callback , $ message );
1896
+ return call_user_func ($ this ->_streaming_callback , $ message );
1885
1897
}
1886
1898
1887
1899
/**
0 commit comments