implement basic redirection support (status codes 30, 31) change default --match rules

This commit is contained in:
yggverse 2025-11-11 13:05:48 +02:00
parent 090c930c7b
commit 3395d4fa17
3 changed files with 141 additions and 37 deletions

View file

@ -16,6 +16,7 @@ use \Yggverse\Net\Address;
class Cli
{
// Init totals
public int $redirects = 0;
public int $save = 0;
public int $size = 0;
public float $time = 0;
@ -122,25 +123,120 @@ class Cli
// Calculate response time
$this->time += $time = microtime(true) - $time; // @TODO to API
// Check response code success
if (20 === $response->getCode())
// Route
switch ($response->getCode())
{
case 20: // success
// Reset redirection counter
$this->redirects = 0;
print(
Message::magenta(
sprintf(
_("\tcode: %d"),
_("\tcode: %d (success)"),
$response->getCode()
)
)
);
}
break;
case 30: // redirection
case 31:
// Increase redirection counter
$this->redirects++;
// Print event debug
print(
Message::yellow(
sprintf(
_("\tcode: %d (redirection #%d)"),
$response->getCode(),
$this->redirects
)
)
);
print(
Message::yellow(
sprintf(
_("\tmeta: %s"),
$response->getMeta()
)
)
);
print(
Message::magenta(
sprintf(
_("\ttime: %f -d %f"),
$time,
$this->option->delay + $time
)
)
);
// Crawl next address...
if ($this->option->crawl)
{
if ($this->redirects <= 5) // @TODO optional (currently protocol spec)
{
// Validate redirection target location
if (filter_var($response->getMeta(), FILTER_VALIDATE_URL)) // @TODO resolve relative locations
{
// Apply redirection target to the current destination
$this->source[$offset] = $response->getMeta();
// Rescan current destination using updated location
$this->start(
$offset
);
}
else
{
print(
Message::red(
sprintf(
_("\tcode: %d"),
_("\tskip invalid redirection URL: `%s`"),
$response->getMeta()
)
)
);
// Continue next location
$this->start(
$offset + 1
);
}
}
else
{
print(
Message::red(
sprintf(
_("\tredirection count reached, continue next address in queue"),
)
)
);
// Continue next location
$this->start(
$offset + 1
);
}
}
return; // panic @TODO
break;
default: // failure
print(
Message::red(
sprintf(
_("\tcode: %d (unsupported)"),
intval(
$response->getCode()
)
@ -148,6 +244,9 @@ class Cli
)
);
// Reset redirection counter
$this->redirects = 0;
// Crawl next address...
if ($this->option->crawl)
{
@ -156,7 +255,7 @@ class Cli
);
}
return; // stop next operations in queue
return; // panic @TODO
}
// Calculate document size
@ -218,8 +317,8 @@ class Cli
$source
);
// Check link match common source rules, @TODO --external links
if (!$this->_source($address->get()) || $address->getHost() != $source->getHost())
// Check link match common source rules
if (!$this->_source($address->get()))
{
continue;
}

View file

@ -40,6 +40,18 @@ class Message
);
}
public static function yellow(
string $message,
bool $bold = false
): string
{
return self::plain(
$message,
$bold ? Color::LIGHT_YELLOW
: Color::YELLOW
);
}
public static function green(
string $message
): string

View file

@ -110,13 +110,6 @@ class Option
);
}
if (!preg_match($this->match, $this->source))
{
throw new \Exception(
_('--source does not --match condition!')
);
}
// Validate target
if (empty($this->target))
{