| 49 | } |
| 50 | |
| 51 | void FileHandler::processDirectory(stefanfrings::HttpRequest &request, stefanfrings::HttpResponse &response, const QString &path) |
| 52 | { |
| 53 | Q_UNUSED(request) |
| 54 | // Add entries for each of the files |
| 55 | QString listing; |
| 56 | for(QFileInfo info : QDir(path).entryInfoList(QDir::NoFilter, QDir::Name | QDir::DirsFirst | QDir::IgnoreCase)) |
| 57 | { |
| 58 | listing.append(QString("<li><a href=\"%1%2\">%1%2</a></li>") |
| 59 | .arg(info.fileName().toHtmlEscaped()) |
| 60 | .arg(info.isDir() ? "/" : "")); |
| 61 | } |
| 62 | |
| 63 | static const QString ListTemplate = |
| 64 | "<!DOCTYPE html>" |
| 65 | "<html>" |
| 66 | "<head>" |
| 67 | "<meta charset=\"utf-8\">" |
| 68 | "<title>%1</title>" |
| 69 | "</head>" |
| 70 | "<body>" |
| 71 | "<h1>%1</h1>" |
| 72 | "<p>Directory listing:</p>" |
| 73 | "<ul>%2</ul>" |
| 74 | "<hr>" |
| 75 | "<p><em>KikoPlay</em></p>" |
| 76 | "</body>" |
| 77 | "</html>"; |
| 78 | |
| 79 | // Build the response and convert the string to UTF-8 |
| 80 | QByteArray data = ListTemplate |
| 81 | .arg("/" + path.toHtmlEscaped()) |
| 82 | .arg(listing) |
| 83 | .toUtf8(); |
| 84 | |
| 85 | // Set the headers and write the content |
| 86 | response.setHeader("Content-Type", "text/html"); |
| 87 | response.write(data, true); |
| 88 | } |
| 89 | |
| 90 | void FileHandler::processFile(stefanfrings::HttpRequest &request, stefanfrings::HttpResponse &response, const QString &path) |
| 91 | { |