Adds response headers for each step in the results table

This commit is contained in:
Andrew Gioia 2023-12-12 12:40:14 -05:00
parent 7f6c4a6d77
commit efc54383bd
Signed by: andrew
GPG Key ID: FC09694A000800C8
4 changed files with 159 additions and 22 deletions

View File

@ -8,13 +8,14 @@ class Follow
const ERROR_CURL_REDIRECT = "Could not curl_getinfo the redirect URL";
// properties
public string $url;
public string $next;
public int $code;
public int $step;
public bool $redirect;
public array $path;
public array $error;
public array $headers;
public string $url;
public string $next;
// constructor
public function __construct(string $url)
@ -25,10 +26,12 @@ class Follow
$this->step = 1;
$this->redirect = false;
$this->error = [];
$this->headers = [];
$this->path[$this->step] = [
'step' => $this->step,
'url' => $this->url,
'code' => null,
'headers' => [],
'next' => null ];
}
@ -45,7 +48,7 @@ class Follow
}
// update a path entry
public function updatePath(int $step, string $key, string $value): void
public function updatePath(int $step, string $key, $value): void
{
$this->path[$step][$key] = $value;
}
@ -60,7 +63,7 @@ class Follow
return false;
}
// set request headers and execute
// set request header options
$response = curl_setopt($ch, CURLOPT_URL, $this->url);
$response = curl_setopt($ch, CURLOPT_HEADER, true); // enable this for debugging
$response = curl_setopt($ch, CURLOPT_HTTPGET, true); // redundant but making sure it's a GET
@ -69,6 +72,31 @@ class Follow
$response = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output instead of going to screen
$response = curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$response = curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT);
// save response header to $header variable
$headers = [];
curl_setopt(
$ch,
CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$headers)
{
$len = strlen($header);
$header = explode(':', $header, 2);
// ignore invalid headers
if (count($header) < 2) {
return $len;
}
// save the header formatted
$headers[strtolower(trim($header[0]))][] = trim($header[1]);
// return the header length
return $len;
}
);
// execute the curl handle
$response = curl_exec($ch);
// check for a response
@ -79,6 +107,10 @@ class Follow
}
else
{
// save the response headers to the path entry
$this->headers = $headers;
$this->updatePath($this->step, 'headers', $headers);
// get the http status code
if (curl_getinfo($ch, CURLINFO_HTTP_CODE))
{
@ -132,6 +164,7 @@ class Follow
'step' => $this->step,
'url' => $this->next,
'code' => null,
'header' => '',
'next' => null
]);

View File

@ -34,7 +34,7 @@ if (isset($_POST['url']))
// update our code
$code = ($request->code) ? $request->code : false;
} while (substr($code, 0, 1) != 2);
} while (substr($code, 0, 1) == 3); // continue while we have a 3XX code
}
else
{

View File

@ -47,9 +47,20 @@ a:hover {
color: #000;
text-decoration-color: #000;
}
nav {
align-items: center;
display: flex;
gap: 1.5rem;
}
nav a {
font-size: 1.1rem;
line-height: 1.6rem;
align-items: center;
display: inline-flex;
font-size: 1.1rem;
line-height: 1.6rem;
}
nav svg {
height: 1.5rem;
width: 1.5rem;
}
h1 {
@ -208,7 +219,7 @@ details[open] + details {
}
code {
font-family: Menlo, monospace;
font-family: Menlo, 'Berkeley Mono', Hack, monospace;
font-size: 0.9rem;
background: #fff;
padding: 0.1rem 0.2rem;
@ -255,6 +266,12 @@ th {
text-transform: uppercase;
text-align: left;
}
th:first-child {
padding-left: 3rem;
}
tr {
position: relative;
}
td {
background-color: #fafafa;
border-bottom: 1px solid #1a1a1a;
@ -263,17 +280,20 @@ td {
word-wrap: anywhere;
}
td:first-child {
text-align: center;
width: 0;
}
td:nth-child(2) {
text-align: center;
}
td:nth-child(3) {
line-height: 1.5rem;
padding: 0.8rem 1rem;
width: 100%;
}
td:nth-child(2) a:hover {
td:nth-child(3) a:hover {
text-decoration-color: transparent;
}
td:nth-child(4) {
td:nth-child(5) {
text-align: center;
padding-right: 1.5rem;
}
@ -293,11 +313,11 @@ td b i {
display: inline-block;
transition: transform 150ms ease-in-out;
}
td:nth-child(2) a:hover b {
td:nth-child(3) a:hover b {
border-color: #1a1a1a;
color: #1a1a1a;
}
td:nth-child(2) a:hover b i {
td:nth-child(3) a:hover b i {
transform: rotate(45deg);
}
tbody tr:nth-child(odd) td {
@ -307,6 +327,59 @@ tbody td:last-child {
padding-left: 0;
width: 1rem;
}
td label {
cursor: pointer;
left: 1.25rem;
position: absolute;
top: 0.85rem;
transition: transform 150ms ease-in-out;
}
td label i {
font-size: 1.25rem;
font-style: normal;
line-height: 1rem;
}
section {
display: flex;
max-height: 0;
overflow: hidden;
flex-direction: column;
transition: max-height 200ms ease;
}
hgroup {
display: flex;
flex-direction: column;
margin-bottom: 0.5rem;
}
section hgroup:first-of-type {
padding-top: 1rem;
}
td input[type="checkbox"] {
display: none;
}
td input[type="checkbox"]:checked + section {
max-height: 100rem;
}
td input[type="checkbox"]:checked + section > label {
transform: rotate(90deg);
}
q {
color: #666;
font-size: 0.8rem;
font-weight: bold;
text-transform: uppercase;
white-space: nowrap;
}
q::before,
q::after {
content: '';
}
mark {
background: none;
font-family: Menlo, 'Berkeley Mono', Hack, monospace;
font-size: 0.9rem;
}
caption {
caption-side: bottom;
line-height: 1.5rem;

View File

@ -16,10 +16,20 @@ include('inc/codes.php');
<h1>Follow <span>URL Redirect Checker</span></h1>
<nav>
<a href="/"><span>Check a new URL</span></a>
<a href="https://git.gioia.cloud/andrew/follow" title="Gitea source code respository">
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" fill-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2" clip-rule="evenodd" viewBox="0 0 24 24" fill="currentColor">
<path d="M5.211 4.639c.762.06 3.164.29 7.56.391 3.118.071 5.918.069 10.15-.326a.922.922 0 0 1 1.002.811c.29 2.448-.295 4.456-.849 6.571-.393 1.499-1.148 3.433-2.093 4.91-.714 1.116-1.557 1.967-2.409 2.32a.93.93 0 0 1-.353.07h-7.307a.906.906 0 0 1-.326-.06c-1.7-.642-2.913-2.487-3.747-4.404-.771-.029-2.098-.158-3.35-.658-1.404-.562-2.697-1.574-3.19-3.31-.555-1.953-.311-3.579.511-4.685.839-1.129 2.315-1.794 4.401-1.63Zm-.466 1.823c-1.133-.032-1.976.266-2.453.908-.539.725-.58 1.801-.217 3.08.318 1.121 1.193 1.737 2.1 2.099.628.252 1.28.387 1.833.458-.672-1.892-1.118-4.479-1.263-6.545Zm3.571 7.203c.343.866.757 1.758 1.281 2.503.419.596.902 1.101 1.498 1.371h6.92c.511-.276.974-.856 1.41-1.538.844-1.318 1.512-3.046 1.863-4.383.428-1.633.906-3.19.865-4.992-2.563.215-4.61.285-6.572.286l-.026 3.061 2.092 1.012c.476.23.676.804.445 1.28l-1.977 4.088a.957.957 0 0 1-1.279.445l-4.089-1.977a.958.958 0 0 1-.445-1.279l1.977-4.089a.959.959 0 0 1 1.28-.445l.157.076c.006-.678.014-1.573.018-2.188l-1.006-.02a125.176 125.176 0 0 1-6.121-.275c.189 2.369.753 5.395 1.648 6.938.024.04.044.081.061.124v.002Zm7.733-1.407-2.495-1.199-1.2 2.495 2.496 1.199 1.199-2.495Z"/>
</svg>
</a>
<a href="https://andrewgioia.com" title="Andrew Gioia's homepage">
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" fill-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2" clip-rule="evenodd" viewBox="0 0 24 24" fill="currentColor">
<path d="M3.7353 17.3608H.4998L5.43 3.4998h3.891l4.9229 13.861h-3.2356l-1.06-3.1605h-5.153l-1.06 3.1605Zm1.8271-5.448h3.6262L7.431 6.6675H7.32L5.5624 11.913Zm12.7008 9.5627c-2.8871 0-4.5397-1.2586-4.8948-2.8963l2.7474-.359c.244.6297.9062 1.1847 2.217 1.1847 1.29 0 2.2317-.555 2.2317-1.983v-1.902h-.1257c-.3906.8595-1.3456 1.6923-3.0544 1.6923-2.4122 0-4.3443-1.6111-4.3443-5.0696 0-3.5396 1.9876-5.313 4.3377-5.313 1.7923 0 2.6637 1.0354 3.061 1.8818h.1116v-1.746h2.9494V17.462c0 2.66-2.1756 4.0134-5.2366 4.0134Zm.0629-6.4294c1.4292 0 2.2524-1.0827 2.2524-2.9171 0-1.8201-.8092-3.0047-2.2524-3.0047-1.4713 0-2.2525 1.2385-2.2525 3.0047 0 1.7935.7952 2.9171 2.2525 2.9171Z"/>
</svg>
</a>
</nav>
</menu>
<form method="post" action="/" type="application/x-www-form-urlencoded">
<input type="url" value="<?php echo $url; ?>" placeholder="URL to check..." name="url">
<input type="url" value="<?php echo (isset($url)) ? $url : ''; ?>" placeholder="URL to check..." name="url">
<button type="submit">Check</button>
</form>
</header>
@ -55,7 +65,7 @@ if (isset($request) && count($request->path) > 0)
<table>
<thead>
<tr>
<th>Step</th>
<th colspan=\"2\">Step</th>
<th>Request</th>
<th colspan=\"2\">Code</th>
</tr>
@ -63,14 +73,35 @@ if (isset($request) && count($request->path) > 0)
<tbody>";
foreach ($request->path as $step)
{
$item = $step['step'];
$url = $step['url'];
$code = $step['code'];
$next = ($step['next']) ? '↩︎' : ((isset($error)) ? 'x' : '✓');
echo "
$item = $step['step'];
$url = $step['url'];
$code = $step['code'];
$headers = $step['headers'];
$next = ($step['next']) ? '↩︎' : ((isset($error)) ? 'x' : '✓');
echo "
<tr>
<td>".$item."</td>
<td>".$url." <a href=\"".$url."\" target=\"blank\"><b><i>↗︎</i></b></a></td>
<td>&nbsp;</td>
<td><span>".$item."</span></td>
<td>
<div>".$url." <a href=\"".$url."\" target=\"blank\"><b><i>↗︎</i></b></a></div>";
if (is_array($headers) && count($headers) > 0)
{
echo "
<input type=\"checkbox\" id=\"step-".$item."\" />
<section>
<label for=\"step-".$item."\"><i></i></label>";
foreach ($headers as $hkey => $hval)
{
echo "
<hgroup>
<q>".$hkey."</q><mark>".$hval[0]."</mark>
</hgroup>";
}
echo "
</section>";
}
echo "
</td>
<td><code>".$code."</code></td>
<td>".$next."</td>
</tr>";