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
This commit is part of merge request !6. Comments created here will be created in the context of that merge request.
...@@ -44,6 +44,12 @@ async function showInOverlay(oLink){ ...@@ -44,6 +44,12 @@ async function showInOverlay(oLink){
if (response.ok) { if (response.ok) {
show(body); 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 { } else {
show("HTTP-Error: " + response.status); show("HTTP-Error: " + response.status);
} }
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
* 2022-02-03 v0.1 <axel.hahn@iml.unibe.ch> initial version * 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 * 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 * 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=''; ...@@ -23,9 +24,6 @@ $sHtml='';
$sErrors = ''; $sErrors = '';
$aIco = [ $aIco = [
'h1'=>'⏩',
'h2_head'=>'✔️',
'h2_err' => '⚠️', 'h2_err' => '⚠️',
'h2_config' => '🛠️', 'h2_config' => '🛠️',
'h2_file' => '📄', 'h2_file' => '📄',
...@@ -35,7 +33,7 @@ $aIco=[ ...@@ -35,7 +33,7 @@ $aIco=[
'ip_err' => '❗', 'ip_err' => '❗',
'type_config' => '🔷', 'type_config' => '🔷',
'type_alias'=>'◻️', 'type_alias' => '▪️',
'url' => '🌐', 'url' => '🌐',
'welcome' => '🪄', 'welcome' => '🪄',
...@@ -45,7 +43,8 @@ $aIco=[ ...@@ -45,7 +43,8 @@ $aIco=[
// FUNCTIONS // FUNCTIONS
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
function getId($sDomain){ function getId($sDomain)
{
return 'id_' . md5($sDomain); return 'id_' . md5($sDomain);
} }
...@@ -69,9 +68,9 @@ if (!$oR->isEnabled()){ ...@@ -69,9 +68,9 @@ if (!$oR->isEnabled()){
$sUrl = (isset($_GET['url']) && $_GET['url']) ? $_GET['url'] : ''; $sUrl = (isset($_GET['url']) && $_GET['url']) ? $_GET['url'] : '';
if ($sUrl) { 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>' 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; return true;
} }
...@@ -116,11 +115,11 @@ if (!$oR->isEnabled()){ ...@@ -116,11 +115,11 @@ if (!$oR->isEnabled()){
$sTdFirst = '<tr class="cfgtype-' . $aCfg['type'] . '">' $sTdFirst = '<tr class="cfgtype-' . $aCfg['type'] . '">'
. '<td>' . '<td>'
. '<span style="display: none">' . $sHost . '</span>' . '<span style="display: none">' . $sHost . '</span>'
.$aIco['type_'.$aCfg['type']] . " $sHost"
. '<span style="float: right;">' . '<span style="float: right;">'
. ' <a href="?url=http' . $sUrlpart . '" title="click to test http://' . $sHost . '/" onclick="showInOverlay(this); return false;">http</a> ' . ' <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>') . ($bHttpOnly ? '' : '<a href="?url=https' . $sUrlpart . '" title="click to test https://' . $sHost . '/" onclick="showInOverlay(this); return false;">https</a>')
. '</span>' . '</span>'
. $aIco['type_' . $aCfg['type']] . " $sHost"
. '</td>' . '</td>'
. '<td>' . '<td>'
. ($aCfg['ip'] . ($aCfg['ip']
...@@ -137,11 +136,6 @@ if (!$oR->isEnabled()){ ...@@ -137,11 +136,6 @@ if (!$oR->isEnabled()){
) )
. '</td>' . '</td>'
; ;
/*
if(!$aCfg['ip']){
$sErrors.='<li>Host was not found in DNS: '.$sHost.'</li>';
}
*/
if (isset($aCfg['redirects'])) { if (isset($aCfg['redirects'])) {
$iCount = 0; $iCount = 0;
foreach (['direct', 'regex'] as $sType) { foreach (['direct', 'regex'] as $sType) {
...@@ -151,7 +145,6 @@ if (!$oR->isEnabled()){ ...@@ -151,7 +145,6 @@ if (!$oR->isEnabled()){
$sTable .= $sTdFirst $sTable .= $sTdFirst
. '<td class="type-' . $sType . '">' . $sType . '</td>' . '<td class="type-' . $sType . '">' . $sType . '</td>'
. '<td class="type-' . $sType . '">' . '<td class="type-' . $sType . '">'
. $sFrom
. ($sType == 'direct' . ($sType == 'direct'
? '<span style="float: right;">' ? '<span style="float: right;">'
. '<a href="?url=http://' . $sHost . $sFrom . '" title="click to test http://' . $sHost . '/' . $sFrom . '" onclick="showInOverlay(this); return false;">http</a> ' . '<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()){ ...@@ -159,6 +152,7 @@ if (!$oR->isEnabled()){
. '</span>' . '</span>'
: '' : ''
) )
. $sFrom
. '</td>' . '</td>'
. '<td class="http-' . $aTo['code'] . '">' . $aTo['code'] . '</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>' . '<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()){ ...@@ -168,9 +162,9 @@ if (!$oR->isEnabled()){
} }
} else { } else {
// type = alias $sTable .= $sTdFirst . '<td></td><td></td><td></td><td>'
// $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>'; . (isset($aCfg['target']) ? 'see config for <em>' . $aCfg['target'] . '</em>' : '')
$sTable.=$sTdFirst.'<td></td><td></td><td></td><td>'.(isset($aCfg['target']) ? 'see config for <em>'.$aCfg['target'].'</em>' : '').'</td></tr>'; . '</td></tr>';
} }
} }
...@@ -183,7 +177,7 @@ if (!$oR->isEnabled()){ ...@@ -183,7 +177,7 @@ if (!$oR->isEnabled()){
<th>Setup</th> <th>Setup</th>
<th>Type</th> <th>Type</th>
<th>From</th> <th>From</th>
<th>Code</th> <th class="th-code">Code</th>
<th>Target</th> <th>Target</th>
</tr> </tr>
</thead><tbody>' . $sTable . '</tbody></table></div>' </thead><tbody>' . $sTable . '</tbody></table></div>'
...@@ -235,33 +229,17 @@ if (!$oR->isEnabled()){ ...@@ -235,33 +229,17 @@ if (!$oR->isEnabled()){
; ;
$sHtml .= ' $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> <h2>' . $aIco['h2_config'] . ' Domains and their redirects</h2>
<div class="content">' <div class="content">'
/*
.'<h2>Config array</h2>
<pre>'.print_r($aHosts, 1).'</pre>'
*/
. $sTable . $sTable
. '</div>'
; ;
$sErrors = $sErrors $sErrors = $sErrors
? '<!-- <h2>' . $aIco['h2_err'] . ' Found errors</h2> -->' ? '<!-- <h2>' . $aIco['h2_err'] . ' Found errors</h2> -->'
// .'<div class="content">'
. '<ol class="error">' . '<ol class="error">'
. $sErrors . $sErrors
. '</ol>' . '</ol>'
// .'</div>'
: '' : ''
; ;
...@@ -273,17 +251,27 @@ if (!$oR->isEnabled()){ ...@@ -273,17 +251,27 @@ if (!$oR->isEnabled()){
?><!doctype html> ?><!doctype html>
<html> <html>
<head> <head>
<title>Redirects</title> <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 src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.11.4/datatables.min.js"></script> 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"> <link rel="stylesheet" href="main.css">
</head> </head>
<body> <body>
<h1><a href="?"> Redirects :: admin</a></h1> <h1><a href="?"> Redirects :: admin</a></h1>
<main>
<?php echo $sErrors . $sHtml; ?> <?php echo $sErrors . $sHtml; ?>
</main>
<div id="divoverlay" class="overlay" onclick="this.style.display='none';"></div> <div id="divoverlay" class="overlay" onclick="this.style.display='none';"></div>
<script type="text/javascript" src="functions.js"></script> <script type="text/javascript" src="functions.js"></script>
...@@ -291,4 +279,5 @@ if (!$oR->isEnabled()){ ...@@ -291,4 +279,5 @@ if (!$oR->isEnabled()){
</script> </script>
</body> </body>
</html> </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 { body {
background: linear-gradient(-10deg, #ddd, #fff, #ddd, #e5e5e5) fixed; background: var(--body-bg);
color: #335; color: var(--body-color);
font-size: 1.2em; font-size: 1.2em;
font-family: arial; font-family: arial;
margin: 0; margin: 0;
...@@ -10,49 +68,54 @@ body { ...@@ -10,49 +68,54 @@ body {
height: 100%,; height: 100%,;
} }
h1{margin: 0 0 1em;; padding: 0;} h1{margin: 0 0 0.5em;; 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 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;} .dataTable tr:hover{background: #f4f0f8 !important;}
h3{color:#ccc; font-size: 250%} 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;} ol.error{padding-left: 2em;}
footer{background:rgba(0,0,0,0.03); margin-top: 4em; text-align: right;padding: 1em;}
.content{margin: 0 1em; background: #fff; margin: 0 3em; padding: 1em;} .th-code{min-width: 12em;}
.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;}
.cfgtype-alias{color:#89a;} .cfgtype-alias{color:var(--txt-alias-color);}
.http-301::after{color:#a55; content: ' (Moved Permanently)'} .http-301::after{color: var(--http-301-color); content: ' (Moved Permanently)'}
.http-307::after{color:#488; content: ' (Temporary Redirect)'} .http-307::after{color:var(--http-307-color); content: ' (Temporary Redirect)'}
.http-308::after{color:#a95; content: ' (Permanent Redirect)'} .http-308::after{color:var(--http-308-color); content: ' (Permanent Redirect)'}
.type-direct{color:#383; } .type-direct{color: var(--type-direct-color); }
.type-regex{color:#838; } .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{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{color:#080; background:#cec; } .status-ok{background: var(--txt-ok-bg); color: var(--txt-ok-color);}
.status-redirect{color:#651; background:#fec;} .status-redirect{background: var(--txt-warning-bg); color: var(--txt-warning-color);}
.status-error{color:#800; background:#ecc;} .status-error{background: var(--txt-error-bg); color: var(--txt-error-color);}
.statuscode{background: rgba(255,255,255,0.8); font-size: 150%;} .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{background:var(--txt-location-bg); font-size: 125%; color:var(--txt-location-color);}
.location::before{content:' > '; color:#800; background-color: #fff;} .location::before{content:' 🌐 '; background-color: var(--txt-location-before-bg);}
.debug{color:#197;} .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{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: #f8f8f8; padding: 1em;box-shadow: 0 0 3em #000; } .overlay>div{margin: 3% 10%; background: var(--overlay-box-bg); padding: 1em;box-shadow: var(--overlay-box-shadow); }
.spin { .spin {
display: inline-block; display: inline-block;
width: 50px; width: 50px;
height: 50px; height: 50px;
border: 3px solid #ccc; border: 3px solid var(--spin-color1);
border-radius: 50%; border-radius: 50%;
border-top-color: #1c87c9; border-top-color: var(--spin-color2);
animation: spin 1s ease-in-out infinite; animation: spin 1s ease-in-out infinite;
-webkit-animation: spin 1s ease-in-out infinite; -webkit-animation: spin 1s ease-in-out infinite;
} }
......
...@@ -57,13 +57,15 @@ class redirectadmin extends redirect ...@@ -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 string $url url to fetch
* @param boolean $bHeaderOnly optional: true=make HEAD request; default: false (=GET) * @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); $ch = curl_init($url);
foreach ($this->_getCurlOptions() as $sCurlOption => $sCurlValue) { foreach ($this->_getCurlOptions() as $sCurlOption => $sCurlValue) {
curl_setopt($ch, $sCurlOption, $sCurlValue); curl_setopt($ch, $sCurlOption, $sCurlValue);
...@@ -73,29 +75,46 @@ class redirectadmin extends redirect ...@@ -73,29 +75,46 @@ class redirectadmin extends redirect
curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_NOBODY, 1);
} }
$res = curl_exec($ch); $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); curl_close($ch);
return ($res); return $aResult;
} }
/** /**
* Get html code for a response header of a request * Get html code for a response header of a request
* *
* @param string $sHeader * @param array $aResponse response array from httpGet() method
* @return string * @return string
*/ */
public function renderHttpResponseHeader(string $sHeader): string public function renderHttpResponseHeader(array $aResponse): string
{ {
$sHeader=$aResponse['response_header'];
$sReturn = $sHeader; $sReturn = $sHeader;
if (!$sReturn) { // $sReturn.="<pre>".print_r($aResponse, 1)."</pre>";
$sReturn = '<pre><span class="status status-error">Request failed. </span><br>'
. 'No data... no response.<br>' if ($aResponse['curlerrorcode']) {
. 'Maybe ... ' $sReturn = '<pre><br>'
. '<ul>' .'<span class="status status-error">Request failed.<br></span>'
. '<li>the nostname does not exist ... </li>' .'Curl error #'.$aResponse['curlerrorcode'] .':<br>'
. '<li>or there is a network problem ... or </li>' . '<strong>'.$aResponse['curlerrormsg'].'</strong><br><br>'
. '<li>the webservice on the target system does not run.</li>'
. '</ul>'
.'</pre>' .'</pre>'
.'🌐 <a href="https://curl.se/libcurl/c/libcurl-errors.html" target="_blank">Curl error codes</a>'
; ;
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment