lundi 27 juin 2016

Debug Streaming PHP-jQuery Lost Data


I'm streaming data from a PHP script back to jQuery using this PHP script and it's working normally on on Mac / iPhone / Windows 10 laptop but it's not working on a desktop device. I had a look using wireshark and the data seems to be received by the computer (can see it in TCP packets no dropping occuring) but it never seems to get to the jQuery?

I've tried alert(...)ing out the this_response variable but nothing's happening. How can I debug this further?

$.ajax( 
    {
        xhrFields: {
            onprogress: function(e)
            {
                var this_response, response = e.currentTarget.response;

                if(last_response_len === false)
                {
                        this_response = response;
                        last_response_len = response.length;
                }
                else
                {
                        this_response = response.substring(last_response_len);
                        last_response_len = response.length;
                }

                /* Put output on page */
                $('#tool-output-area').append(this_response + '<br>');

            }
        },
  method: 'POST',
    url: '../../../../../../../../page.php',
    data:
  { 
    data-key: data-variable...
  }
    })
    .done(function(data)
    {
      // stuff
    })
    .fail(function()
    {
        // stuff
    });
});

Server Side:

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

/* Set Maximum Execution Time By Plan */
ini_set('max_execution_time', getMaxExeTime());

ini_set('output_buffering', 'off');
ini_set('zlib.output_compression', false);
ini_set('implicit_flush', true);

ob_implicit_flush(true);

while (ob_get_level() > 0) 
{
  $level = ob_get_level();
  ob_end_clean();
  if (ob_get_level() == $level) break;
}

if (function_exists('apache_setenv')) 
{
  apache_setenv('no-gzip', '1');
  apache_setenv('dont-vary', '1');
}

// Checks

$descriptorspec = array(
    0 => array("pipe", "r"), 
    1 => array("pipe", "w"), 
    2 => array("file", "proc.txt", "a")
);

$pipes = array();
$process = proc_open('exec ' . escapeshellcmd($tool_command), $descriptorspec, $pipes, '/path-here', null);
$proc_details = proc_get_status($process);
$pid = $proc_details['pid'];

$proc_row = logProcess($pid, $tool_command, $output_file, $tool);

$str = '';

session_write_close();

if(is_resource($process)) 
{
    while ($curStr = fgets($pipes[1])) 
    {
        echo nl2br(htmlentities($curStr)); // Send the data back
        flush();
        $str .= nl2br(htmlentities($curStr));
        $arr = proc_get_status($process);
        sleep(1);
    }
}

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);

Aucun commentaire:

Enregistrer un commentaire