From 64a28515923aa67a1d109a82aba1892cd227bb15 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 11 Jul 2012 00:46:27 +0200 Subject: ensure that directories are accessed with a slash at the end of the url --- test/interactive-helper/aptwebserver.cc | 40 +++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'test/interactive-helper/aptwebserver.cc') diff --git a/test/interactive-helper/aptwebserver.cc b/test/interactive-helper/aptwebserver.cc index 920ab3bcc..2052fe6d8 100644 --- a/test/interactive-helper/aptwebserver.cc +++ b/test/interactive-helper/aptwebserver.cc @@ -146,6 +146,26 @@ void sendError(int const client, int const httpcode, std::string const &request, sendData(client, response); } /*}}}*/ +void sendRedirect(int const client, int const httpcode, std::string const &uri, std::string const &request, bool content) { /*{{{*/ + std::list headers; + std::string response(""); + response.append(httpcodeToStr(httpcode)).append(""); + response.append("

").append(httpcodeToStr(httpcode)).append("You should be redirected to ").append(uri).append("

"); + response.append("This page is a result of the request:
");
+   response.append(request).append("
"); + addDataHeaders(headers, response); + std::string location("Location: "); + if (strncmp(uri.c_str(), "http://", 7) != 0) + location.append("http://").append(LookupTag(request, "Host")).append("/").append(uri); + else + location.append(uri); + headers.push_back(location); + sendHead(client, httpcode, headers); + if (content == true) + sendData(client, response); +} + /*}}}*/ // sendDirectoryLisiting /*{{{*/ int filter_hidden_files(const struct dirent *a) { if (a->d_name[0] == '.') @@ -209,12 +229,15 @@ void sendDirectoryListing(int const client, std::string const &dir, std::string std::string filename(dir); filename.append("/").append(namelist[i]->d_name); stat(filename.c_str(), &fs); - listing << "" << ((S_ISDIR(fs.st_mode)) ? 'd' : 'f') << "" - << "d_name << "\">" << namelist[i]->d_name << ""; - if (S_ISDIR(fs.st_mode)) - listing << "-"; - else - listing << "" << SizeToStr(fs.st_size) << "B"; + if (S_ISDIR(fs.st_mode)) { + listing << "d" + << "d_name << "/\">" << namelist[i]->d_name << "" + << "-"; + } else { + listing << "f" + << "d_name << "\">" << namelist[i]->d_name << "" + << "" << SizeToStr(fs.st_size) << "B"; + } listing << "" << TimeRFC1123(fs.st_mtime) << "" << std::endl; } listing << "" << std::endl; @@ -337,7 +360,10 @@ int main(int const argc, const char * argv[]) sendFile(client, data); } else if (DirectoryExists(filename) == true) { - sendDirectoryListing(client, filename, *m, sendContent); + if (filename == "." || filename[filename.length()-1] == '/') + sendDirectoryListing(client, filename, *m, sendContent); + else + sendRedirect(client, 301, filename.append("/"), *m, sendContent); } else sendError(client, 404, *m, false); -- cgit v1.2.3