title: Curl: transfer closed with outstanding read data remaining
body: <p>I'm having an issue with a large curl call. </p>
  
  <p>I get </p>
  
  <blockquote>
    <ul>
    <li>nread &lt;= 0, server closed connection, bailing</li>
    <li>transfer closed with outstanding read data remaining</li>
    </ul>
  </blockquote>
  
  <p>and the content is partial delivered</p>
  
  <pre><code>    GET /stats/?stats_breakdown=track__track&amp;campaign=&amp;search_criteria=2&amp;period=0&amp;date_month=11&amp;date_day=03&amp;date_year=2015&amp;start_date_month=11&amp;start_date_day=03&amp;start_date_year=2015&amp;end_date_month=12&amp;end_date_day=31&amp;end_date_year=2014 HTTP/1.1
  User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13
  Host: domain.com
  Accept: */*
  Cookie: sessionid=xxg4gglsm7o3b224wihqz8od19wl31h1; csrftoken=JBpLxNtgAVvDEw2wNqvBnRmzDJIjxL6C
  Cache-Control: no-cache
  Connection: Keep-Alive
  Keep-Alive: 600
  Accept-Language: en-us
  X-CSRFToken: SeN9bHryRK8FWLTLJIs5c6u9AZ47a8pR
  Content-Type: application/x-www-form-urlencoded
  Origin: https://domain.com
  Referer: https://domain.com
  
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * additional stuff not fine transfer.c:1037: 0 0
  * HTTP 1.1 or later with persistent connection, pipelining supported
  &lt; HTTP/1.1 200 OK
  &lt; Server: nginx/1.8.0
  &lt; Date: Wed, 04 Nov 2015 12:54:05 GMT
  &lt; Content-Type: text/html; charset=utf-8
  &lt; Transfer-Encoding: chunked
  &lt; Connection: keep-alive
  &lt; Vary: Accept-Encoding
  &lt; Vary: Cookie, Accept-Language
  &lt; P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
  &lt; Content-Language: en
  * Replaced cookie csrftoken="JBpLxNtgAVvDEw2wNqvBnRmzDJIjxL6C" for domain domain.com, path /, expire 1478091245
  &lt; Set-Cookie: csrftoken=JBpLxNtgAVvDEw2wNqvBnRmzDJIjxL6C; expires=Wed, 02-Nov-2016 12:54:05 GMT; Max-Age=31449600; Path=/; secure
  &lt;
  * nread &lt;= 0, server closed connection, bailing
  * transfer closed with outstanding read data remaining
  * Closing connection #0
  </code></pre>
  
  <p>This is the php configuration I use</p>
  
  <pre><code>function getHeaders()
  {
      $headers = array();
      $headers[] = 'Cache-Control: no-cache';
      $headers[] = 'Connection: Keep-Alive';
      $headers[] = 'Keep-Alive: 600';
      $headers[] = 'Accept-Language: en-us';
      $headers[] = 'X-CSRFToken: SeN9bHryRK8FWLTLJIs5c6u9AZ47a8pR';
  
      $headers[] = 'Content-Type: application/x-www-form-urlencoded';
      $headers[] = 'Origin: https://domain.com';
      $headers[] = 'Referer: https://domain.com';
  
      return $headers;
  
  }
  
  curl_setopt($connection, CURLOPT_URL, $url);
      curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($connection, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
      curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($connection, CURLOPT_COOKIEFILE, 'cookie.txt');
      curl_setopt($connection, CURLOPT_COOKIEJAR, 'cookie.txt');
      curl_setopt($connection, CURLOPT_CONNECTTIMEOUT ,550000000);
      curl_setopt($connection, CURLOPT_TIMEOUT, 5500000000); //timeout in seconds
      curl_setopt($connection, CURLOPT_HTTPHEADER, getHeaders());
      curl_setopt($connection, CURLOPT_VERBOSE, 1);
  </code></pre>