Skip to content
Snippets Groups Projects
Commit aa3b73a9 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

test links http and https

parent bba8be47
No related branches found
No related tags found
1 merge request!6OP#7728 Redirect Tool anpassen https://projects.iml.unibe.ch/work_packages/77287728 handle http and https redirects
......@@ -44,6 +44,12 @@ async function showInOverlay(oLink){
if (response.ok) {
show(body);
// colorize clicked link
var myspan=document.getElementsByClassName("status")[0];
var myclass=myspan.className.replace("status ","");
oLink.className=myclass;
oLink.title=myspan.innerText;
} else {
show("HTTP-Error: " + response.status);
}
......
......@@ -14,6 +14,7 @@
* 2022-02-03 v0.1 <axel.hahn@iml.unibe.ch> initial version
* 2022-05-31 v0.2 <axel.hahn@iml.unibe.ch> optical changes; use debugredirect=1 if url is a local domain
* 2023-08-28 v1.0 <axel.hahn@unibe.ch> Welcome message if there is no config yet
* 2025-01-13 v1.1 <axel.hahn@unibe.ch> test links http and https
* ----------------------------------------------------------------------
*/
......@@ -23,9 +24,6 @@ $sHtml='';
$sErrors = '';
$aIco = [
'h1'=>'⏩',
'h2_head'=>'✔️',
'h2_err' => '⚠️',
'h2_config' => '🛠️',
'h2_file' => '📄',
......@@ -35,7 +33,7 @@ $aIco=[
'ip_err' => '❗',
'type_config' => '🔷',
'type_alias'=>'◻️',
'type_alias' => '▪️',
'url' => '🌐',
'welcome' => '🪄',
......@@ -45,7 +43,8 @@ $aIco=[
// FUNCTIONS
// ----------------------------------------------------------------------
function getId($sDomain){
function getId($sDomain)
{
return 'id_' . md5($sDomain);
}
......@@ -69,9 +68,9 @@ if (!$oR->isEnabled()){
$sUrl = (isset($_GET['url']) && $_GET['url']) ? $_GET['url'] : '';
if ($sUrl) {
$sResult=$oR->httpGet($sUrl,1);
$aResult = $oR->httpGet($sUrl, 1);
echo '<h2>Response of a http HEAD request to ' . $aIco['url'] . ' <a href="' . $sUrl . '">' . $sUrl . '</a></h2>'
. $oR->renderHttpResponseHeader($sResult)
. $oR->renderHttpResponseHeader($aResult)
;
return true;
}
......@@ -116,11 +115,11 @@ if (!$oR->isEnabled()){
$sTdFirst = '<tr class="cfgtype-' . $aCfg['type'] . '">'
. '<td>'
. '<span style="display: none">' . $sHost . '</span>'
.$aIco['type_'.$aCfg['type']] . " $sHost"
. '<span style="float: right;">'
. ' <a href="?url=http' . $sUrlpart . '" title="click to test http://' . $sHost . '/" onclick="showInOverlay(this); return false;">http</a> '
. ($bHttpOnly ? '' : '<a href="?url=https' . $sUrlpart . '" title="click to test https://' . $sHost . '/" onclick="showInOverlay(this); return false;">https</a>')
. '</span>'
. $aIco['type_' . $aCfg['type']] . " $sHost"
. '</td>'
. '<td>'
. ($aCfg['ip']
......@@ -137,11 +136,6 @@ if (!$oR->isEnabled()){
)
. '</td>'
;
/*
if(!$aCfg['ip']){
$sErrors.='<li>Host was not found in DNS: '.$sHost.'</li>';
}
*/
if (isset($aCfg['redirects'])) {
$iCount = 0;
foreach (['direct', 'regex'] as $sType) {
......@@ -151,7 +145,6 @@ if (!$oR->isEnabled()){
$sTable .= $sTdFirst
. '<td class="type-' . $sType . '">' . $sType . '</td>'
. '<td class="type-' . $sType . '">'
. $sFrom
. ($sType == 'direct'
? '<span style="float: right;">'
. '<a href="?url=http://' . $sHost . $sFrom . '" title="click to test http://' . $sHost . '/' . $sFrom . '" onclick="showInOverlay(this); return false;">http</a> '
......@@ -159,6 +152,7 @@ if (!$oR->isEnabled()){
. '</span>'
: ''
)
. $sFrom
. '</td>'
. '<td class="http-' . $aTo['code'] . '">' . $aTo['code'] . '</td>'
. '<td>' . $aIco['url'] . ' <a href="?url=' . $aTo['target'] . '" title="click to test ' . $aTo['target'] . '" onclick="showInOverlay(this); return false;">' . $aTo['target'] . '</a></td>'
......@@ -168,9 +162,9 @@ if (!$oR->isEnabled()){
}
} else {
// type = alias
// $sHtml.='<tr>'.$sTdFirst.'<td></td><td></td><td></td><td>'.(isset($aCfg['target']) ? 'see config for <a href="#'.getId($aCfg['target']).'">'.$aCfg['target'].'</a>' : '').'</td></tr>';
$sTable.=$sTdFirst.'<td></td><td></td><td></td><td>'.(isset($aCfg['target']) ? 'see config for <em>'.$aCfg['target'].'</em>' : '').'</td></tr>';
$sTable .= $sTdFirst . '<td></td><td></td><td></td><td>'
. (isset($aCfg['target']) ? 'see config for <em>' . $aCfg['target'] . '</em>' : '')
. '</td></tr>';
}
}
......@@ -183,7 +177,7 @@ if (!$oR->isEnabled()){
<th>Setup</th>
<th>Type</th>
<th>From</th>
<th>Code</th>
<th class="th-code">Code</th>
<th>Target</th>
</tr>
</thead><tbody>' . $sTable . '</tbody></table></div>'
......@@ -235,33 +229,17 @@ if (!$oR->isEnabled()){
;
$sHtml .= '
<!--
<h2>'.$aIco['h2_head'].' Http head tester</h2>
<div class="content">
<form>
'.$aIco['url'].' <input type="text" name="url" size="100" value="'.$sUrl.'" placeholder="Enter url or click a link in the table below."/>
<button>Http HEAD</button>
</form>
</div>
<br>
-->
<h2>' . $aIco['h2_config'] . ' Domains and their redirects</h2>
<div class="content">'
/*
.'<h2>Config array</h2>
<pre>'.print_r($aHosts, 1).'</pre>'
*/
. $sTable
. '</div>'
;
$sErrors = $sErrors
? '<!-- <h2>' . $aIco['h2_err'] . ' Found errors</h2> -->'
// .'<div class="content">'
. '<ol class="error">'
. $sErrors
. '</ol>'
// .'</div>'
: ''
;
......@@ -273,17 +251,27 @@ if (!$oR->isEnabled()){
?><!doctype html>
<html>
<head>
<title>Redirects</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.11.4/datatables.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.11.4/datatables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"
integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/2.2.1/css/dataTables.dataTables.min.css" />
<script type="text/javascript" src="//cdn.datatables.net/2.2.1/js/dataTables.min.js"></script>
<link rel="stylesheet" href="main.css">
</head>
<body>
<h1><a href="?"> Redirects :: admin</a></h1>
<main>
<?php echo $sErrors . $sHtml; ?>
</main>
<div id="divoverlay" class="overlay" onclick="this.style.display='none';"></div>
<script type="text/javascript" src="functions.js"></script>
......@@ -291,4 +279,5 @@ if (!$oR->isEnabled()){
</script>
</body>
</html>
\ No newline at end of file
a{color:royalblue;}
:root{
--col-border-1: #6aa;
--body-bg: linear-gradient(-10deg, #ddd, #fff, #ddd, #e5e5e5) fixed;
--body-color: #335;
--top-bg: linear-gradient(0deg, #000, #234, #222);
--top-color: #e55;
--top-bottom: var(--col-border-1);
--link-color: #46d;
--h2-bg: #c8e0e0;
--h2-color: #458;
--h2-border: var(--col-border-1);
--h3-color: #ccc;
--footer-bg: rgba(0,0,0,0.05);
--box-content-bg: #fff;
--box-legend-bg: #fff4e8;
--txt-error-bg: #ebb;
--txt-error-color: #800;
--txt-ok-bg: #aeb;
--txt-ok-color: #080;
--txt-warning-bg: #fda;
--txt-warning-color: #651;
--txt-alias-color: #89a;
--http-301-color: #a55;
--http-307-color: #488;
--http-308-color: #a95;
--type-direct-color:#383;
--type-regex-color:#838;
--txt-statuscode-bg: rgba(255,255,255,0.8);
--txt-location-bg: #cde;
--txt-location-color: #236;
--txt-location-before-bg: #fff;
--txt-debug-color: #197;
--overlay-bg: rgba(0,0,0,0.3);
--overlay-box-bg: #f8f8f8;
--overlay-box-shadow: 0 0 3em #000;
--spin-color1: #ccc;
--spin-color2: var(--col-border-1);
}
a{color: var(--link-color);}
body {
background: linear-gradient(-10deg, #ddd, #fff, #ddd, #e5e5e5) fixed;
color: #335;
background: var(--body-bg);
color: var(--body-color);
font-size: 1.2em;
font-family: arial;
margin: 0;
......@@ -10,49 +68,54 @@ body {
height: 100%,;
}
h1{margin: 0 0 1em;; padding: 0;}
h1 a{background: linear-gradient(0deg, #000, #234, #222); color: #e55; text-decoration: none;border-bottom: 5px solid #6aa; display: block; padding: 0.5em;}
h1{margin: 0 0 0.5em;; padding: 0;}
h1 a{background: var(--top-bg); color: var(--top-color); text-decoration: none;border-bottom: 5px solid var(--top-bottom); display: block; padding: 0.5em;}
h2{background: var(--h2-bg); color: var('--h2-color'); margin: 1em 0 0.5em; border-top: 2px solid var(--h2-border); border-left: 5px solid var(--h2-border); border-top-left-radius: 0.5em; padding: 0.5em; margin: 0;}
h3{color:var('--h3-color'); font-size: 250%}
pre{background: rgba(0,0,0,0.02);padding: 0.3em 1em; border: 1px solid rgba(0,0,0,0.1); margin: 2em 0 3em;; border-bottom: 2px solid rgba(0,0,0,0.2); border-bottom-right-radius: 1em;}
h2{background: #dee; color:#458; margin: 1em 0 0.5em; border-top: 2px solid #fff; border-left: 5px solid #fff; border-top-left-radius: 0.5em; padding: 0.5em; margin: 0 0 1em;}
h3{color:#ccc; font-size: 250%}
.dataTable tr:hover{background: #f4f0f8 !important;}
footer{background: var(--footer-bg); margin-top: 4em; text-align: right; padding: 1em;}
main{margin: 0 2em; padding: 0em;}
pre{background: rgba(0,0,0,0.02);padding: 0.3em 1em; border: 1px solid rgba(0,0,0,0.1); margin: 2em 0 3em;; border-bottom: 2px solid rgba(0,0,0,0.2);}
.content{background: var(--box-content-bg); padding: 1em;}
.legend{background: var(--box-legend-bg); border-top: 1px dashed; padding: 1em;}
.error{background: var(--txt-error-bg); color: var(--txt-error-color); padding: 0.2em 0.5em;}
.warning{background: var(--txt-warning-bg); color: var(--txt-warning-color); }
tr:hover{background: #f4f0f8 !important;}
footer{background:rgba(0,0,0,0.03); margin-top: 4em; text-align: right;padding: 1em;}
ol.error{padding-left: 2em;}
.content{margin: 0 1em; background: #fff; margin: 0 3em; padding: 1em;}
.legend{background: none; border: 1px dashed; padding: 1em;}
.error{background: #fbb; padding: 0.2em 1.5em;}
.warning{color:#651; background:#fec; padding: 0.2em 1em;}
.th-code{min-width: 12em;}
.cfgtype-alias{color:#89a;}
.http-301::after{color:#a55; content: ' (Moved Permanently)'}
.http-307::after{color:#488; content: ' (Temporary Redirect)'}
.http-308::after{color:#a95; content: ' (Permanent Redirect)'}
.type-direct{color:#383; }
.type-regex{color:#838; }
.cfgtype-alias{color:var(--txt-alias-color);}
.http-301::after{color: var(--http-301-color); content: ' (Moved Permanently)'}
.http-307::after{color:var(--http-307-color); content: ' (Temporary Redirect)'}
.http-308::after{color:var(--http-308-color); content: ' (Permanent Redirect)'}
.type-direct{color: var(--type-direct-color); }
.type-regex{color: var(--type-regex-color); }
.status{padding: 0.5em 1em; position: relative;top: -0.8em; border-left: 2px solid; border-left_: 1.5em solid;font-size: 125%;}
.status-ok{color:#080; background:#cec; }
.status-redirect{color:#651; background:#fec;}
.status-error{color:#800; background:#ecc;}
.statuscode{background: rgba(255,255,255,0.8); font-size: 150%;}
.status{padding: 0.5em 1em; position: relative; top: -1.4em; left: -0.9em; border-left: 5px solid; font-size: 125%; border-bottom-right-radius: 0.7em;}
.status-ok{background: var(--txt-ok-bg); color: var(--txt-ok-color);}
.status-redirect{background: var(--txt-warning-bg); color: var(--txt-warning-color);}
.status-error{background: var(--txt-error-bg); color: var(--txt-error-color);}
.statuscode{background: var(--txt-statuscode-bg); font-size: 150%;}
.location{background:#cde; border: 1px solid rgba(0,0,0,0.2); font-size: 125%; color:#236;}
.location::before{content:' > '; color:#800; background-color: #fff;}
.debug{color:#197;}
.location{background:var(--txt-location-bg); font-size: 125%; color:var(--txt-location-color);}
.location::before{content:' 🌐 '; background-color: var(--txt-location-before-bg);}
.debug{color:var(--txt-debug-color);}
.overlay{position: fixed; margin: 0; width: 100%; height: 100%; top: 0; left: 0; background: rgba(0,0,0,0.3);overflow: scroll; display: none;}
.overlay>div{margin: 3% 10%; background: #f8f8f8; padding: 1em;box-shadow: 0 0 3em #000; }
.overlay{position: fixed; margin: 0; width: 100%; height: 100%; top: 0; left: 0; background: var(--overlay-bg); overflow: scroll; display: none;}
.overlay>div{margin: 3% 10%; background: var(--overlay-box-bg); padding: 1em;box-shadow: var(--overlay-box-shadow); }
.spin {
display: inline-block;
width: 50px;
height: 50px;
border: 3px solid #ccc;
border: 3px solid var(--spin-color1);
border-radius: 50%;
border-top-color: #1c87c9;
border-top-color: var(--spin-color2);
animation: spin 1s ease-in-out infinite;
-webkit-animation: spin 1s ease-in-out infinite;
}
......
......@@ -57,13 +57,15 @@ class redirectadmin extends redirect
}
/**
* Make a single http(s) get request and return the response body
* Make a single http(s) get request and return an array with response header, body, curl error info
* @param string $url url to fetch
* @param boolean $bHeaderOnly optional: true=make HEAD request; default: false (=GET)
* @return string
* @return array
*/
public function httpGet(string $url, bool $bHeaderOnly = false): bool|string
public function httpGet(string $url, bool $bHeaderOnly = false): array
{
$aResult = [];
$ch = curl_init($url);
foreach ($this->_getCurlOptions() as $sCurlOption => $sCurlValue) {
curl_setopt($ch, $sCurlOption, $sCurlValue);
......@@ -73,29 +75,46 @@ class redirectadmin extends redirect
curl_setopt($ch, CURLOPT_NOBODY, 1);
}
$res = curl_exec($ch);
$sHeader = '';
$sBody = '';
$aResponse = explode("\r\n\r\n", $res, 2);
list($sHeader, $sBody) = count($aResponse) > 1
? $aResponse
: [$aResponse[0], ''];
$aResult = [
'url' => $url,
'response_header' => $sHeader,
'response_body' => $sBody,
'curlinfo' => curl_getinfo($ch),
'curlerrorcode' => curl_errno($ch),
'curlerrormsg' => curl_error($ch),
];
curl_close($ch);
return ($res);
return $aResult;
}
/**
* Get html code for a response header of a request
*
* @param string $sHeader
* @param array $aResponse response array from httpGet() method
* @return string
*/
public function renderHttpResponseHeader(string $sHeader): string
public function renderHttpResponseHeader(array $aResponse): string
{
$sHeader=$aResponse['response_header'];
$sReturn = $sHeader;
if (!$sReturn) {
$sReturn = '<pre><span class="status status-error">Request failed. </span><br>'
. 'No data... no response.<br>'
. 'Maybe ... '
. '<ul>'
. '<li>the nostname does not exist ... </li>'
. '<li>or there is a network problem ... or </li>'
. '<li>the webservice on the target system does not run.</li>'
. '</ul>'
// $sReturn.="<pre>".print_r($aResponse, 1)."</pre>";
if ($aResponse['curlerrorcode']) {
$sReturn = '<pre><br>'
.'<span class="status status-error">Request failed.<br></span>'
.'Curl error #'.$aResponse['curlerrorcode'] .':<br>'
. '<strong>'.$aResponse['curlerrormsg'].'</strong><br><br>'
.'</pre>'
.'🌐 <a href="https://curl.se/libcurl/c/libcurl-errors.html" target="_blank">Curl error codes</a>'
;
} else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment