/** * Output whole contents of file * @param string $path Path * @param callable $cb Callback * @param integer $pri Priority * @return boolean Success */ public function sendfile($path, $cb, $pri = EIO_PRI_DEFAULT) { if ($this->state === self::STATE_FINISHED) { return false; } try { $this->header('Content-Type: ' . MIME::get($path)); } catch (RequestHeadersAlreadySent $e) { } if ($this->upstream->checkSendfileCap()) { FileSystem::sendfile($this->upstream, $path, $cb, function ($file, $length, $handler) { try { $this->header('Content-Length: ' . $length); } catch (RequestHeadersAlreadySent $e) { } $this->ensureSentHeaders(); $this->upstream->onWriteOnce(function ($conn) use($handler, $file) { $handler($file); }); return true; }, 0, null, $pri); return true; } $first = true; FileSystem::readfileChunked($path, $cb, function ($file, $chunk) use(&$first) { // readed chunk if ($this->upstream->isFreed()) { return false; } if ($first) { try { $this->header('Content-Length: ' . $file->stat['size']); } catch (RequestHeadersAlreadySent $e) { } $first = false; } $this->out($chunk); return true; }); return true; }
/** * @param $path * @param $dir */ public function autoindex($path, $dir) { $this->onWakeup(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Index of /</title> <style type="text/css"> a, a:active { text-decoration: none; color: blue; } a:visited { color: #48468F; } a:hover, a:focus { text-decoration: underline; color: red; } body { background-color: #F5F5F5; } h2 { margin-bottom: 12px; } table { margin-left: 12px; } th, td { font: 90% monospace; text-align: left; } th { font-weight: bold; padding-right: 14px; padding-bottom: 3px; } td { padding-right: 14px; } td.s, th.s { text-align: right; } div.list { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px; } div.foot { font: 90% monospace; color: #787878; padding-top: 4px; } </style> </head> <body> <pre class="header">Welcome!</pre> <h2>Index of /</h2> <div class="list"> <table summary="Directory Listing" cellpadding="0" cellspacing="0"> <thead> <tr> <th class="n">Name</th> <th class="t">Type</th> </tr> </thead> <tbody> <tr> <td class="n"><a href="../../">Parent Directory</a>/</td> <td class="t">Directory</td> </tr> <?php foreach ($dir['dents'] as $item) { $type = $item['type'] === EIO_DT_DIR ? 'Directory' : \PHPDaemon\Utils\MIME::get($path . $item['name']); ?> <tr> <td class="n"><a href="<?php echo htmlspecialchars($item['name']) . ($type == 'Directory' ? '/' : ''); ?> "><?php echo htmlspecialchars($item['name']); ?> </a></td> <td class="t"><?php echo $type; ?> </td> </tr> <?php } ?> </tbody> </table> </div> <?php if ($this->upstream->pool->config->expose->value) { ?> <div class="foot">phpDaemon/<?php echo \PHPDaemon\Core\Daemon::$version; ?> </div><?php } ?> </body> </html><?php }