httpclient : add referer support and Cookie keywords can now be received in capital or lowercase

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4799 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2018-12-23 21:14:32 +00:00
parent d8860ca0a9
commit 4cea28e534

View File

@@ -4,6 +4,8 @@
* @author Dominique Fournier <dominique@fournier38.fr>
*/
namespace vendor\domframework;
/** This programe allow to get a HTTP page from a site, and examine the content.
* It will store the Cookies, allow to do the redirects, follow links and
* get form / input and send the values.
@@ -35,6 +37,10 @@ class Httpclient
*/
private $headersReceived = array ();
/** The headersSent to the server
*/
private $headersSent = array ();
/** The Method used to communicate : GET, POST, HEAD, PUT, DELETE
*/
private $method = "GET";
@@ -84,6 +90,11 @@ class Httpclient
/** Store the user agent. If it is empty, it will not be sent to the server
*/
private $useragent = "HTTPClient";
/** The last valid page known. Used as referer
*/
private $referer = "";
// }}}
/** The constructor
@@ -193,6 +204,8 @@ class Httpclient
// }}}
/** Set / Get the debug mode
* 0: Nothing is displayed, 1: Only URL are displayed,
* 2: headers only send and received, 3: all the content
* @param boolean|null $debug The debug value to set or get
*/
public function debug ($debug = null)
@@ -291,6 +304,20 @@ class Httpclient
}
// }}}
/** Get/Set the referer page
* @param string|null $referer The new referer that will be used on next
* request
*/
public function referer ($referer)
// {{{
{
if ($referer === null)
return $this->referer;
$this->referer = $referer;
return $this;
}
// }}}
//////////////////////////////////
//// THE ACTIVE METHODS ////
//////////////////////////////////
@@ -320,6 +347,7 @@ class Httpclient
throw new \Exception ("File to get exceeded maxsize", 500);
}
$this->disconnect ();
$this->referer = $url;
if ($this->httpCode === 301 || $this->httpCode === 302)
{
if (! key_exists ("Location", $this->headersReceived))
@@ -420,9 +448,11 @@ class Httpclient
$this->headersSent = array ();
$this->headersSent[] = "$this->method $path HTTP/1.1";
$this->headersSent[] = "Host: ".$parseURL["host"];
$this->headersSent[] = "Accept: text/html";
$this->headersSent[] = "Accept: text/html;q=0.9, */*;q=0.8";
if ($this->useragent !== "")
$this->headersSent[] = "User-Agent: $this->useragent";
if ($this->referer !== "")
$this->headersSent[] = "Referer: $this->referer";
$this->headersSent[] = "Connection: keep-alive";
$cookies = $this->cookieToSend ($this->url);
if (! empty ($cookies))
@@ -558,9 +588,16 @@ class Httpclient
$this->contentMethod = "Content-Length";
$this->bodySize = $headers["Content-Length"];
}
elseif (key_exists ("Connection", $headers) &&
$headers["Connection"] === "close")
{
// Connection closed by server. Nothing to get
}
elseif ($this->httpCode !== 204 && $this->httpCode !== 301 &&
$this->httpCode !== 302)
{
throw new \Exception ("No transfert content provided", 500);
}
// }}}
}
// }}}
@@ -685,8 +722,8 @@ class Httpclient
{
@list ($key, $val) = explode ("=", $part, 2);
$key = trim ($key);
if ($key === "path") $path = $val;
elseif ($key === "domain")
if (strtolower ($key) === "path") $path = $val;
elseif (strtolower ($key) === "domain")
{
// Check if $domain is compatible with $key before storing the cookie
if (substr ($domain, -1 * strlen ($val)) === $val)
@@ -699,7 +736,7 @@ class Httpclient
return "Invalid domain provided";
}
}
elseif ($key === "expires")
elseif (strtolower ($key) === "expires")
{
try
{