]> 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 0b2720dad8ea1b027ef7db34ee5d28e2074283e8..2052fe6d865884df7276587e7fc6b8cb33aba16c 100644 (file)
@@ -1,3 +1,5 @@
+#include <config.h>
+
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/fileutl.h>
 #include <apt-pkg/error.h>
@@ -88,7 +90,7 @@ void addDataHeaders(std::list<std::string> &headers, std::string &data) {/*{{{*/
 }
                                                                        /*}}}*/
 bool sendHead(int const client, int const httpcode, std::list<std::string> &headers) { /*{{{*/
-   string response("HTTP/1.1 ");
+   std::string response("HTTP/1.1 ");
    response.append(httpcodeToStr(httpcode));
    headers.push_front(response);
 
@@ -131,9 +133,9 @@ bool sendData(int const client, std::string const &data) {          /*{{{*/
    return Success;
 }
                                                                        /*}}}*/
-void sendError(int const client, int const httpcode, string const &request, bool content) { /*{{{*/
+void sendError(int const client, int const httpcode, std::string const &request, bool content) { /*{{{*/
    std::list<std::string> headers;
-   string response("<html><head><title>");
+   std::string response("<html><head><title>");
    response.append(httpcodeToStr(httpcode)).append("</title></head>");
    response.append("<body><h1>").append(httpcodeToStr(httpcode)).append("</h1");
    response.append("This error is a result of the request: <pre>");
@@ -144,6 +146,26 @@ void sendError(int const client, int const httpcode, string const &request, bool
       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] == '.')
@@ -181,7 +203,7 @@ int grouped_alpha_case_sort(const struct dirent **a, const struct dirent **b) {
    }
    return strcasecmp((*a)->d_name, (*b)->d_name);
 }
-void sendDirectoryListing(int const client, string const &dir, string const &request, bool content) {
+void sendDirectoryListing(int const client, std::string const &dir, std::string const &request, bool content) {
    std::list<std::string> headers;
    std::ostringstream listing;
 
@@ -207,12 +229,15 @@ void sendDirectoryListing(int const client, string const &dir, string const &req
       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;
@@ -306,12 +331,14 @@ int main(int const argc, const char * argv[])
            }
 
            size_t const filestart = m->find(' ', 5);
-           string filename = m->substr(5, filestart - 5);
+           std::string filename = m->substr(5, filestart - 5);
            if (filename.empty() == true)
               filename = ".";
+           else
+              filename = DeQuoteString(filename);
 
            if (simulate_broken_server == true) {
-              string data("ni ni ni\n");
+              std::string data("ni ni ni\n");
               addDataHeaders(headers, data);
               sendHead(client, 200, headers);
               sendData(client, data);
@@ -333,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);