httpclient : allow 3 modes : Chunked, Content-Length and connection closed modes

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4807 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2018-12-26 17:23:18 +00:00
parent 2a1977630c
commit a46300881d

View File

@@ -378,6 +378,7 @@ class Httpclient
return $this; return $this;
} }
// }}} // }}}
////////////////////////////////// //////////////////////////////////
//// THE ACTIVE METHODS //// //// THE ACTIVE METHODS ////
////////////////////////////////// //////////////////////////////////
@@ -523,7 +524,7 @@ class Httpclient
$cookies = $this->cookieToSend ($this->url); $cookies = $this->cookieToSend ($this->url);
if (! empty ($cookies)) if (! empty ($cookies))
{ {
$this->headersSent[] = "Cookie: ".implode (";", $cookies); $this->headersSent[] = "Cookie: ".implode ("; ", $cookies);
} }
if ($this->method !== "GET" && ! empty ($this->formData)) if ($this->method !== "GET" && ! empty ($this->formData))
{ {
@@ -597,7 +598,7 @@ class Httpclient
// }}} // }}}
// Get the result header from the server // Get the result header from the server
// {{{ // {{{
$headers = array (); $headers = array ();
while (($header = $this->tcpclient->read (4095)) !== "") while (($header = $this->tcpclient->read (4095)) !== "")
{ {
@@ -666,15 +667,19 @@ class Httpclient
} }
if ($this->contentMethod === "chunked") if ($this->contentMethod === "chunked")
$this->log (1, "URL $this->method $this->url $this->httpCode Chunked"); $this->log (1, "URL $this->method $this->url $this->httpCode Chunked");
elseif (is_int ($this->bodySize))
$this->log (1, "URL $this->method $this->url $this->httpCode ".
$this->bodySize);
else else
$this->log (1, "URL $this->method $this->url $this->httpCode $this->bodySize"); $this->log (1, "URL $this->method $this->url $this->httpCode 0");
// }}} // }}}
} }
// }}} // }}}
/** Read max MAXSIZE bytes /** Read max MAXSIZE bytes
* Return false if all the file is received * Return "" if all the file is received
* 3 methods are supported : Chunked mode, Content-Length defined and all
* until the connection will be closed by the server
* @param integer $maxsize The maxsize to get in this read * @param integer $maxsize The maxsize to get in this read
*/ */
public function read ($maxsize = 4096) public function read ($maxsize = 4096)
@@ -683,12 +688,12 @@ class Httpclient
if ($this->tcpclient === null) if ($this->tcpclient === null)
throw new \Exception ("HTTPClient : can not read non connected URL", 406); throw new \Exception ("HTTPClient : can not read non connected URL", 406);
$this->tcpclient->timeout ($this->timeout); $this->tcpclient->timeout ($this->timeout);
if ($this->contentMethod === false) /* if ($this->contentMethod === false)
{ {
// If the server will never send anything, code 204 by example, do not try // If the server will never send anything, code 204 by example, do not try
// to get data // to get data
return ""; return "";
} }*/
$content = ""; $content = "";
if ($this->contentMethod === "chunked" && $this->bodySize === 0) if ($this->contentMethod === "chunked" && $this->bodySize === 0)
{ {
@@ -699,13 +704,17 @@ class Httpclient
} }
if ($this->bodySize === 0) if ($this->bodySize === 0)
return ""; return "";
if ($this->bodySize === null)
$toBeRead = $this->bodySize; $toBeRead = $maxsize;
else
$toBeRead = $this->bodySize;
if ($toBeRead > $maxsize) if ($toBeRead > $maxsize)
$toBeRead = $maxsize; $toBeRead = $maxsize;
$this->tcpclient->readMode ("binary"); $this->tcpclient->readMode ("binary");
$content = $this->tcpclient->read ($toBeRead); $content = $this->tcpclient->read ($toBeRead);
$this->bodySize = $this->bodySize - strlen ($content); // In close mode, the bodySize is not set and should not be updated
if ($this->bodySize !== null)
$this->bodySize = $this->bodySize - strlen ($content);
if ($this->contentMethod === "chunked" && $this->bodySize === 0) if ($this->contentMethod === "chunked" && $this->bodySize === 0)
{ {
// Get the Carriage return before the next chunk size // Get the Carriage return before the next chunk size