]> git.saurik.com Git - apt.git/blobdiff - test/interactive-helper/aptwebserver.cc
do not hardcode /dev/null in changetowebserver so it can be changed
[apt.git] / test / interactive-helper / aptwebserver.cc
index 0780288a420e6f241f20066c0a04a084909d44b5..2052fe6d865884df7276587e7fc6b8cb33aba16c 100644 (file)
@@ -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<std::string> headers;
+   std::string response("<html><head><title>");
+   response.append(httpcodeToStr(httpcode)).append("</title></head>");
+   response.append("<body><h1>").append(httpcodeToStr(httpcode)).append("</h1");
+   response.append("<p>You should be redirected to <em>").append(uri).append("</em></p>");
+   response.append("This page is a result of the request: <pre>");
+   response.append(request).append("</pre></body></html>");
+   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 << "<tr><td>" << ((S_ISDIR(fs.st_mode)) ? 'd' : 'f') << "</td>"
-             << "<td><a href=\"" << namelist[i]->d_name << "\">" << namelist[i]->d_name << "</a></td>";
-      if (S_ISDIR(fs.st_mode))
-        listing << "<td>-</td>";
-      else
-        listing << "<td>" << SizeToStr(fs.st_size) << "B</td>";
+      if (S_ISDIR(fs.st_mode)) {
+        listing << "<tr><td>d</td>"
+                << "<td><a href=\"" << namelist[i]->d_name << "/\">" << namelist[i]->d_name << "</a></td>"
+                << "<td>-</td>";
+      } else {
+        listing << "<tr><td>f</td>"
+                << "<td><a href=\"" << namelist[i]->d_name << "\">" << namelist[i]->d_name << "</a></td>"
+                << "<td>" << SizeToStr(fs.st_size) << "B</td>";
+      }
       listing << "<td>" << TimeRFC1123(fs.st_mtime) << "</td></tr>" << std::endl;
    }
    listing << "</table></body></html>" << std::endl;
@@ -311,6 +334,8 @@ int main(int const argc, const char * argv[])
            std::string filename = m->substr(5, filestart - 5);
            if (filename.empty() == true)
               filename = ".";
+           else
+              filename = DeQuoteString(filename);
 
            if (simulate_broken_server == true) {
               std::string data("ni ni ni\n");
@@ -335,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);