note 110064 added to features.commandline.webserver
| From: | matthewkastor at gmail dot com | Date: | Sun, 16 Sep 2012 00:58:28 +0000 |
| Subject: | note 110064 added to features.commandline.webserver | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190799@lists.php.net to get a copy of this message | ||
The commandline webserver kept giving me 404's when I would request a directory. It would only
automatically find index.php. Since I wanted links to subdirectories and index.html to be processed
automatically, I made a router that does it. I left three handlers dangling out there to make it
easy to process files, directories, or 404's. I was thinking this might be useful and that
someone else might not like the way my auto index looks, so I tried to make this code easy to alter.
<?php
function atropa_router() {
function add_single_trailing_fslash($to) {
if(!$to) return '/';
if(substr($to, -1) !== '/') {
$to = $to . '/';
}
return $to;
}
function get_absolute_path_to_request() {
$p = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
if( is_null($p) ) {
$req = $_SERVER['DOCUMENT_ROOT'];
} else {
$req = $_SERVER['DOCUMENT_ROOT'] . $p;
}
return $req;
}
function path_has_filename($req) {
$req = pathinfo($req, PATHINFO_BASENAME);
if($req) {
return true;
} else {
return false;
}
}
function web_index_exists($dir) {
if(is_file($dir . "/index.php") ) {
return true;
} else {
return false;
}
}
function process_request() {
$req = get_absolute_path_to_request();
if(path_has_filename($req)) {
if (is_file($req)) {
if(!file_handler($req)) {
return false; // serve the requested resource as-is.
}
}
}
if(is_dir($req)) {
if (web_index_exists($req)) {
return false;
} elseif ( is_file($req . "/index.html")) {
$loc = add_single_trailing_fslash($_SERVER['REQUEST_URI']);
header('Location: ' . $loc . 'index.html');
exit;
} else {
directory_handler($req);
}
} else {
not_found_handler($req);
}
}
if(process_request() === false) {
return false;
}
}
function file_handler($req) {
return false;
}
function directory_handler($req) {
$d = dir($req);
$p = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$separator = '/';
$p = add_single_trailing_fslash($p);
echo '<!Doctype
html><html><head><title>Directory</title></head><body>';
echo "Location: " . $d->path . '<br>';
echo
'<table><tr><th>Name</th><th>Type</th></tr>';
while (false !== ($entry = $d->read())) {
if($entry === '.') continue;
if(is_file($req . '/' . $entry)) {
$separator = '';
$type = 'File';
} else {
$separator = '/';
$type = 'Directory';
}
echo '<tr><td><a href="' . $p . $entry . $separator .
'">' . $entry . '</a></td><td>' . $type .
'</td></tr>';
}
$d->close();
echo '</table></body></html>';
}
function not_found_handler($req) {
header('HTTP/1.1 404 Not Found');
echo '<!Doctype
html><html><head><title>404</title></head><body><p>Requested
resource could not be found</p></body></html>';
}
return atropa_router();
?>
----
Server IP: 69.147.83.199
Probable Submitter: 71.205.34.219
----
Manual Page -- http://www.php.net/manual/en/features.commandline.webserver.php
Edit -- https://master.php.net/note/edit/110064
Del: integrated -- https://master.php.net/note/delete/110064/integrated
Del: useless -- https://master.php.net/note/delete/110064/useless
Del: bad code -- https://master.php.net/note/delete/110064/bad+code
Del: spam -- https://master.php.net/note/delete/110064/spam
Del: non-english -- https://master.php.net/note/delete/110064/non-english
Del: in docs -- https://master.php.net/note/delete/110064/in+docs
Del: other reasons-- https://master.php.net/note/delete/110064
Reject -- https://master.php.net/note/reject/110064
Search -- https://master.php.net/manage/user-notes.php