This commit is contained in:
Laurent Destailleur 2021-11-01 03:18:12 +01:00
parent 6acc8a6582
commit 2f7e246ab3

View File

@ -105,15 +105,12 @@ function error2string($value)
$level_names[E_STRICT]='E_STRICT';
}
$levels=array();
if(($value&E_ALL)==E_ALL)
{
if (($value&E_ALL)==E_ALL) {
$levels[]='E_ALL';
$value&=~E_ALL;
}
foreach($level_names as $level=>$name)
{
if(($value&$level)==$level)
{
foreach ($level_names as $level=>$name) {
if (($value&$level)==$level) {
$levels[]=$name;
}
}
@ -184,12 +181,11 @@ class http_class
$this->connected = false;
$url = $arguments["URL"];
$port = $this->default_port;
#$url = split (':', $url, 2);
// $url = split (':', $url, 2);
$url = preg_split('#:#', $url, 2);
$transport_type = $url[0];
$unix = false;
switch ($transport_type)
{
switch ($transport_type) {
case 'http':
$transport_type = 'tcp://';
break;
@ -209,23 +205,19 @@ class http_class
break;
}
$url = $url[1];
if (!$unix)
{
#$url = split ("/", preg_replace ("#^/{1,}#", '', $url), 2);
if (!$unix) {
// $url = split ("/", preg_replace ("#^/{1,}#", '', $url), 2);
$url = preg_split("#/#", preg_replace("#^/{1,}#", '', $url), 2);
$url = $url[0];
$port = $this->port;
$error = sprintf(_("Cannot resolve url: %s"), $url);
$ip = gethostbyname($url);
$ip = @gethostbyaddr($ip);
if (!$ip)
{
if (!$ip) {
return $this->_HttpError($error, E_USER_WARNING);
}
if (strstr ($url, ":")) // we got an ipv6 address
{
if (!strstr ($url, "[")) // it is not escaped
{
if (strstr($url, ":")) { // we got an ipv6 address
if (!strstr($url, "[")) { // it is not escaped
$url = sprintf("[%s]", $url);
}
}
@ -234,8 +226,7 @@ class http_class
$error =
sprintf(_('Unable to connect to "%s%s port %s": %s'), $transport_type,
$url, $port, $errstr);
if (!$this->connection)
{
if (!$this->connection) {
return $this->_HttpError($error, E_USER_WARNING);
}
$this->connected = true;
@ -247,26 +238,22 @@ class http_class
$error =
sprintf(_('Streaming request failed to %s'), $arguments['RequestURI']);
$result = self::_StreamRequest($arguments);
if (!$result[0])
{
if (!$result[0]) {
return $this->_HttpError($error." ".$result[1], E_USER_WARNING);
}
self::_ReadReply();
if (!preg_match ('#http/1.1 401 unauthorized#', $this->status))
{
if (!preg_match('#http/1.1 401 unauthorized#', $this->status)) {
return array (true, "success");
}
$headers = array_keys($this->reply_headers);
$error = _("need authentication but no mechanism provided");
if (!in_array ("www-authenticate", $headers))
{
if (!in_array("www-authenticate", $headers)) {
return $this->_HttpError($error, E_USER_WARNING);
}
#$authtype = split (' ', $this->reply_headers["www-authenticate"]);
// $authtype = split (' ', $this->reply_headers["www-authenticate"]);
$authtype = preg_split('# #', $this->reply_headers["www-authenticate"]);
$authtype = strtolower($authtype[0]);
switch ($authtype)
{
switch ($authtype) {
case 'basic':
$pass = base64_encode($this->user.":".$this->password);
$arguments["Headers"]["Authorization"] = "Basic ".$pass;
@ -288,8 +275,7 @@ class http_class
$error = sprintf(_('Streaming request failed to %s after a try to authenticate'), $arguments['RequestURI']);
$result = self::_StreamRequest($arguments);
if (!$result[0])
{
if (!$result[0]) {
return $this->_HttpError($error.": ".$result[1], E_USER_WARNING);
}
self::_ReadReply();
@ -309,8 +295,7 @@ class http_class
public function Close()
{
if (!$this->connected)
{
if (!$this->connected) {
return;
}
fclose($this->connection);
@ -326,18 +311,14 @@ class http_class
{
$trace = '';
$backtrace = debug_backtrace();
foreach ($backtrace as $trace)
{
foreach ($backtrace as $trace) {
$trace .= sprintf("in [file: '%s'][function: '%s'][line: %s];\n", $trace['file'], $trace['function'], $trace['line']);
}
$msg = sprintf( '%s\n%s: [errno: %s]: %s',
$trace, error2string($level), $errno, $msg);
if ($this->with_exceptions)
{
if ($this->with_exceptions) {
throw new httpException($msg, $errno);
}
else
{
} else {
trigger_error($msg, $level);
return array (false, $msg);
}
@ -346,8 +327,7 @@ class http_class
private function _streamString($string)
{
$success = fwrite($this->connection, $string);
if (!$success)
{
if (!$success) {
return false;
}
return true;
@ -358,34 +338,26 @@ class http_class
$this->status = false;
$this->reply_headers = array ();
$this->reply_body = "";
if (!$this->connected)
{
if (!$this->connected) {
return $this->_HttpError(_("not connected"), E_USER_WARNING);
}
$this->arguments = $arguments;
$content_length = 0;
foreach ($this->arguments["BodyStream"] as $argument)
{
list ($type, $value) = each ($argument);
foreach ($this->arguments["BodyStream"] as $argument) {
// list ($type, $value) = each ($argument);
$type = key($argument);
$value = current($argument);
reset($argument);
if ($type == "Data")
{
if ($type == "Data") {
$length = strlen($value);
}
elseif ($type == "File")
{
if (is_readable ($value))
{
} elseif ($type == "File") {
if (is_readable($value)) {
$length = filesize($value);
}
else
{
} else {
$length = 0;
return $this->_HttpError(sprintf(_("%s: file is not readable"), $value), E_USER_WARNING);
}
}
else
{
} else {
$length = 0;
return $this->_HttpError(sprintf(_("%s: not a valid argument for content"), $type), E_USER_WARNING);
}
@ -394,60 +366,47 @@ class http_class
$this->request_body = sprintf(_("%s Bytes"), $content_length);
$this->headers["Content-Length"] = $content_length;
$this->arguments["Headers"] = array_merge($this->headers, $this->arguments["Headers"]);
if ($this->arguments["RequestMethod"] != "POST")
{
if ($this->arguments["RequestMethod"] != "POST") {
return $this->_HttpError(sprintf(_("%s: method not implemented"), $arguments["RequestMethod"]), E_USER_WARNING);
}
$string = sprintf("POST %s HTTP/1.1\r\n", $this->arguments["RequestURI"]);
$this->request_headers[$string] = '';
if (!$this->_streamString ($string))
{
if (!$this->_streamString($string)) {
return $this->_HttpError(_("Error while puts POST operation"), E_USER_WARNING);
}
foreach ($this->arguments["Headers"] as $header => $value)
{
foreach ($this->arguments["Headers"] as $header => $value) {
$string = sprintf("%s: %s\r\n", $header, $value);
$this->request_headers[$header] = $value;
if (!$this->_streamString ($string))
{
if (!$this->_streamString($string)) {
return $this->_HttpError(_("Error while puts HTTP headers"), E_USER_WARNING);
}
}
$string = "\r\n";
if (!$this->_streamString ($string))
{
if (!$this->_streamString($string)) {
return $this->_HttpError(_("Error while ends HTTP headers"), E_USER_WARNING);
}
foreach ($this->arguments["BodyStream"] as $argument)
{
list ($type, $value) = each ($argument);
foreach ($this->arguments["BodyStream"] as $argument) {
// list ($type, $value) = each ($argument);
$type = key($argument);
$value = current($argument);
reset($argument);
if ($type == "Data")
{
if ($type == "Data") {
$streamed_length = 0;
while ($streamed_length < strlen ($value))
{
while ($streamed_length < strlen($value)) {
$string = substr($value, $streamed_length, $this->window_size);
if (!$this->_streamString ($string))
{
if (!$this->_streamString($string)) {
return $this->_HttpError(_("error while sending body data"), E_USER_WARNING);
}
$streamed_length += $this->window_size;
}
}
elseif ($type == "File")
{
if (is_readable ($value))
{
} elseif ($type == "File") {
if (is_readable($value)) {
$file = fopen($value, 'rb');
while (!feof ($file))
{
if (gettype ($block = @fread ($file, $this->window_size)) != "string")
{
while (!feof($file)) {
if (gettype($block = @fread($file, $this->window_size)) != "string") {
return $this->_HttpError(_("cannot read file to upload"), E_USER_WARNING);
}
if (!$this->_streamString ($block))
{
if (!$this->_streamString($block)) {
return $this->_HttpError(_("error while sending body data"), E_USER_WARNING);
}
}
@ -459,39 +418,32 @@ class http_class
private function _ReadReply()
{
if (!$this->connected)
{
if (!$this->connected) {
return array (false, _("not connected"));
}
$this->reply_headers = array ();
$this->reply_body = "";
$headers = array ();
$body = "";
while (!feof ($this->connection))
{
while (!feof($this->connection)) {
$line = fgets($this->connection, 1024);
if (strlen (trim($line)) == 0)
{
if (strlen(trim($line)) == 0) {
break;
} // \r\n => end of headers
if (preg_match ('#^[[:space:]]#', $line))
{
if (preg_match('#^[[:space:]]#', $line)) {
$headers[-1] .= sprintf(' %s', trim($line));
continue;
}
$headers[] = trim($line);
}
$this->status = isset($headers[0]) ? strtolower($headers[0]) : false;
foreach ($headers as $header)
{
foreach ($headers as $header) {
$header = preg_split("#: #", $header);
$header[0] = strtolower($header[0]);
if ($header[0] !== "www-authenticate")
{
if ($header[0] !== "www-authenticate") {
$header[1] = isset($header[1]) ? strtolower($header[1]) : "";
}
if (!isset ($this->reply_headers[$header[0]]))
{
if (!isset($this->reply_headers[$header[0]])) {
$this->reply_headers[$header[0]] = $header[1];
}
}
@ -501,8 +453,7 @@ class http_class
private function _ReadStream()
{
if (! array_key_exists ("content-length", $this->reply_headers))
{
if (! array_key_exists("content-length", $this->reply_headers)) {
stream_set_blocking($this->connection, 0);
$this->reply_body = stream_get_contents($this->connection);
return true;
@ -516,13 +467,12 @@ class http_class
private function _BuildDigest()
{
$auth = $this->reply_headers["www-authenticate"];
#list ($head, $auth) = split (" ", $auth, 2);
// list ($head, $auth) = split (" ", $auth, 2);
list ($head, $auth) = preg_split("# #", $auth, 2);
#$auth = split (", ", $auth);
// $auth = split (", ", $auth);
$auth = preg_split("#, #", $auth);
foreach ($auth as $sheme)
{
#list ($sheme, $value) = split ('=', $sheme);
foreach ($auth as $sheme) {
// list ($sheme, $value) = split ('=', $sheme);
list ($sheme, $value) = preg_split('#=#', $sheme);
$fields[$sheme] = trim(trim($value), '"');
}
@ -535,11 +485,9 @@ class http_class
$username = $this->user;
$password = $this->password;
$A1 = $username.":".$fields["realm"].":".$password;
if (array_key_exists ("algorithm", $fields))
{
if (array_key_exists("algorithm", $fields)) {
$algorithm = strtolower($fields["algorithm"]);
switch ($algorithm)
{
switch ($algorithm) {
case "md5":
break;
@ -559,17 +507,13 @@ class http_class
}
}
$A2 = "POST:".$this->arguments["RequestURI"];
if (array_key_exists ("qop", $fields))
{
if (array_key_exists("qop", $fields)) {
$qop = strtolower($fields["qop"]);
#$qop = split (" ", $qop);
// $qop = split (" ", $qop);
$qop = preg_split("# #", $qop);
if (in_array ("auth", $qop))
{
if (in_array("auth", $qop)) {
$qop = "auth";
}
else
{
} else {
self::_HttpError(
sprintf(_("digest Authorization: algorithm '%s' not implemented"),
$qop),
@ -578,31 +522,25 @@ class http_class
}
}
$response = md5(md5($A1).":".$fields["nonce"].":".md5($A2));
if (isset ($qop) && ($qop == "auth"))
{
if (isset($qop) && ($qop == "auth")) {
$response =
md5(md5($A1).":".$fields["nonce"].":".$nc.":".$cnonce.":".$qop.
":".$A2);
}
$auth_scheme =
sprintf
('Digest username="%s", realm="%s", nonce="%s", uri="%s", response="%s"',
sprintf('Digest username="%s", realm="%s", nonce="%s", uri="%s", response="%s"',
$username, $fields["realm"], $fields['nonce'],
$this->arguments["RequestURI"], $response);
if (isset ($algorithm))
{
if (isset($algorithm)) {
$auth_scheme .= sprintf(', algorithm="%s"', $algorithm);
}
if (isset ($qop))
{
if (isset($qop)) {
$auth_scheme .= sprintf(', cnonce="%s"', $cnonce);
}
if (array_key_exists ("opaque", $fields))
{
if (array_key_exists("opaque", $fields)) {
$auth_scheme .= sprintf(', opaque="%s"', $fields['opaque']);
}
if (isset ($qop))
{
if (isset($qop)) {
$auth_scheme .= sprintf(', qop="%s"', $qop);
}
$auth_scheme .= sprintf(', nc=%s', $nc);