8000 stream api fix · generalstephen1/codebird-php@2de5cf9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2de5cf9

Browse files
committed
stream api fix
1 parent 5f72625 commit 2de5cf9

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/codebird.php

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,7 +1775,9 @@ protected function _callApiStreaming(
17751775
stream_set_timeout($ch, 0);
17761776

17771777
// 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);
17791781
$headers = explode("\r\n", $result);
17801782

17811783
// find HTTP status
@@ -1806,50 +1808,58 @@ protected function _callApiStreaming(
18061808
$signal_function = function_exists('pcntl_signal_dispatch');
18071809
$data = '';
18081810
$last_message = time();
1809-
1811+
$chunk_length = 0;
1812+
$message_length = 0;
1813+
18101814
while (!feof($ch)) {
18111815
// call signal handlers, if any
18121816
if ($signal_function) {
18131817
pcntl_signal_dispatch();
18141818
}
1815-
18161819
$cha = [$ch];
18171820
$write = $except = null;
18181821
if (false === ($num_changed_streams = stream_select($cha, $write, $except, 0, 200000))) {
18191822
break;
18201823
} elseif ($num_changed_streams === 0) {
18211824
if (time() - $last_message >= 1) {
1822-
// deliver empty message, allow callback to cancel stream
1825+
//deliver empty message, allow callback to cancel stream
18231826
$cancel_stream = $this->_deliverStreamingMessage(null);
18241827
if ($cancel_stream) {
18251828
break;
18261829
}
1827-
18281830
$last_message = time();
18291831
}
18301832
continue;
18311833
}
1832-
1833-
$chunk_length = fgets($ch, 10);
1834-
1834+
$chunk_length_raw = $chunk_length = fgets($ch, 10);
18351835
if ($chunk_length === '' || !$chunk_length = hexdec($chunk_length)) {
18361836
continue;
18371837
}
18381838

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+
}
18411852

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;
18471856
}
18481857

1849-
$reply = substr($temp, 0, $object_length);
1850-
$data = substr($temp, $object_length + 2);
1858+
if(strlen($data) < $message_length){
1859+
continue;
1860+
}
18511861

1852-
$reply = $this->_parseApiReply($reply);
1862+
$reply = $this->_parseApiReply($data);
18531863
switch ($this->_return_format) {
18541864
case CODEBIRD_RETURNFORMAT_ARRAY:
18551865
$reply['httpstatus'] = $httpstatus;
@@ -1866,6 +1876,8 @@ protected function _callApiStreaming(
18661876
break;
18671877
}
18681878

1879+
$data = '';
1880+
$message_length = 0;
18691881
$last_message = time();
18701882
}
18711883

@@ -1881,7 +1893,7 @@ protected function _callApiStreaming(
18811893
*/
18821894
protected function _deliverStreamingMessage($message)
18831895
{
1884-
return call_user_func($this->_streaming_callback, $message);
1896+
return call_user_func($this->_streaming_callback, $message);
18851897
}
18861898

18871899
/**

0 commit comments

Comments
 (0)
0