title: JSON string (encoded from large array) got truncated when output to HTTP client
body: <p>I met an issue that the JSON string output to HTTP client was truncated near to close tag. </p>
  
  <p>An example,</p>
  
  <p>a. expect to see - ... ... "last_update":"2014-06-10 20:46:38","garden_id":"1"}],"message":null}</p>
  
  <p>b. actually it is - ... ..."last_update":"2014-06-10 20:46:38","garden_id":"1"}],"message":nul </p>
  
  <p>The last two characaters were truncated for some reason!!!</p>
  
  <p>I tried both Postman on Chrome and curl on a console, all the same output. So looks not a browser specific issue. The JSON string in my application comes from a PHP json_encode on an associative array. The PHP code is using CodeIgniter framework running on Apache. I tried to write the json string into a file before http output, the file content is 100% correct. So it is not a json encoding issue in PHP. </p>
  
  <p>The PHP is pretty straightforward. I have build below array (namely $finaldata) from database query</p>
  
  <pre><code>{
      "success": true,
      "data": [
          {
              "file_id": "1",
              "title": "xxx",
              "create_date": "2014-05-18 21:30:19",
              "auditor": "1",
              "status": "1",
              "last_updater": null,
              "last_update": "2014-06-10 20:43:14",
              "garden_id": "1"
          },
          {
              "file_id": "2",
              "title": "yyy",
              "create_date": "2014-05-18 21:30:19",
              "auditor": "1",
              "status": "1",
              "last_updater": null,
              "last_update": "2014-06-10 20:43:14",
              "garden_id": "1"
          }
      ],
      "message": null
  }
  </code></pre>
  
  <p>the "data" has a sub array and it could be a long array depends on database records. Then the variable $finaldata is passed to a output function that has below general logic:</p>
  
  <pre><code>header('Content-Type: '.$this-&gt;_supported_formats[$this-&gt;response-&gt;format]);
  $output = $this-&gt;format-&gt;factory($finaldata)-&gt;{'to_'.$this-&gt;response-&gt;format}();
  header('Content-Length: ' . strlen($output));
  </code></pre>
  
  <p>The output function is built by a RESTful library from <a href="https://github.com/philsturgeon/codeigniter-restserver" rel="nofollow">https://github.com/philsturgeon/codeigniter-restserver</a>. In this context, it equals to </p>
  
  <pre><code>header('Content-Type: Application/json');
  $output = json_encode($finaldata);
  </code></pre>
  
  <p>But I found an interesting thing that the issue only happened if the string length exceeds 8K. And when I append a random string like "ZZZ" to the JSON string before HTTP response, the issue was gone. I don't know the reason behind. It is not a correct hack because I could not prove 8K is a real threshold though.</p>
  
  <p>Has anyone met such issue before? Any suggestion or comments are appreciated.</p>