]> git.saurik.com Git - apt.git/blobdiff - test/interactive-helper/aptwebserver.cc
warn if apt-key is used in scripts/its output parsed
[apt.git] / test / interactive-helper / aptwebserver.cc
index 86d5c06f097c6d1e9c639b4257830601246298be..3e91406abdbbc98e9f49a2497a662d48294ccb58 100644 (file)
@@ -96,9 +96,12 @@ static void addFileHeaders(std::list<std::string> &headers, FileFd &data)/*{{{*/
       contentlength << "Content-Length: " << data.FileSize();
       headers.push_back(contentlength.str());
    }
-   std::string lastmodified("Last-Modified: ");
-   lastmodified.append(TimeRFC1123(data.ModificationTime()));
-   headers.push_back(lastmodified);
+   if (_config->FindB("aptwebserver::support::last-modified", true) == true)
+   {
+      std::string lastmodified("Last-Modified: ");
+      lastmodified.append(TimeRFC1123(data.ModificationTime()));
+      headers.push_back(lastmodified);
+   }
 }
                                                                        /*}}}*/
 static void addDataHeaders(std::list<std::string> &headers, std::string &data)/*{{{*/
@@ -206,8 +209,8 @@ static bool sendData(int const client, std::list<std::string> const &headers, st
 static void sendError(int const client, int const httpcode, std::string const &request,/*{{{*/
               bool const content, std::string const &error, std::list<std::string> &headers)
 {
-   std::string response("<html><head><title>");
-   response.append(httpcodeToStr(httpcode)).append("</title></head>");
+   std::string response("<!doctype html><html><head><title>");
+   response.append(httpcodeToStr(httpcode)).append("</title><meta charset=\"utf-8\" /></head>");
    response.append("<body><h1>").append(httpcodeToStr(httpcode)).append("</h1>");
    if (httpcode != 200)
       response.append("<p><em>Error</em>: ");
@@ -242,8 +245,8 @@ static void sendRedirect(int const client, int const httpcode, std::string const
                  std::string const &request, bool content)
 {
    std::list<std::string> headers;
-   std::string response("<html><head><title>");
-   response.append(httpcodeToStr(httpcode)).append("</title></head>");
+   std::string response("<!doctype html><html><head><title>");
+   response.append(httpcodeToStr(httpcode)).append("</title><meta charset=\"utf-8\" /></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>");
@@ -253,7 +256,10 @@ static void sendRedirect(int const client, int const httpcode, std::string const
    if (strncmp(uri.c_str(), "http://", 7) != 0 && strncmp(uri.c_str(), "https://", 8) != 0)
    {
       std::string const host = LookupTag(request, "Host");
-      if (host.find(":4433") != std::string::npos)
+      unsigned int const httpsport = _config->FindI("aptwebserver::port::https", 4433);
+      std::string hosthttpsport;
+      strprintf(hosthttpsport, ":%u", httpsport);
+      if (host.find(hosthttpsport) != std::string::npos)
         location.append("https://");
       else
         location.append("http://");
@@ -326,7 +332,7 @@ static void sendDirectoryListing(int const client, std::string const &dir,/*{{{*
       return;
    }
 
-   listing << "<html><head><title>Index of " << dir << "</title>"
+   listing << "<!doctype html><html><head><title>Index of " << dir << "</title><meta charset=\"utf-8\" />"
           << "<style type=\"text/css\"><!-- td {padding: 0.02em 0.5em 0.02em 0.5em;}"
           << "tr:nth-child(even){background-color:#dfdfdf;}"
           << "h1, td:nth-child(3){text-align:center;}"
@@ -646,7 +652,7 @@ static void * handleClient(void * voidclient)                               /*{{{*/
               redirect.erase(0,1);
            if (redirect != filename)
            {
-              sendRedirect(client, 301, redirect, *m, sendContent);
+              sendRedirect(client, _config->FindI("aptwebserver::redirect::httpcode", 301), redirect, *m, sendContent);
               continue;
            }
         }
@@ -678,8 +684,11 @@ static void * handleClient(void * voidclient)                              /*{{{*/
         }
 
         // deal with the request
+        unsigned int const httpsport = _config->FindI("aptwebserver::port::https", 4433);
+        std::string hosthttpsport;
+        strprintf(hosthttpsport, ":%u", httpsport);
         if (_config->FindB("aptwebserver::support::http", true) == false &&
-              LookupTag(*m, "Host").find(":4433") == std::string::npos)
+              LookupTag(*m, "Host").find(hosthttpsport) == std::string::npos)
         {
            sendError(client, 400, *m, sendContent, "HTTP disabled, all requests must be HTTPS", headers);
            continue;
@@ -728,13 +737,15 @@ static void * handleClient(void * voidclient)                             /*{{{*/
                     if (filesize > filestart)
                     {
                        data.Skip(filestart);
-                       std::ostringstream contentlength;
-                       contentlength << "Content-Length: " << (filesize - filestart);
-                       headers.push_back(contentlength.str());
+                        // make sure to send content-range before conent-length
+                        // as regression test for LP: #1445239
                        std::ostringstream contentrange;
                        contentrange << "Content-Range: bytes " << filestart << "-"
                           << filesize - 1 << "/" << filesize;
                        headers.push_back(contentrange.str());
+                       std::ostringstream contentlength;
+                       contentlength << "Content-Length: " << (filesize - filestart);
+                       headers.push_back(contentlength.str());
                        sendHead(client, 206, headers);
                        if (sendContent == true)
                           sendFile(client, headers, data);
@@ -742,11 +753,14 @@ static void * handleClient(void * voidclient)                             /*{{{*/
                     }
                     else
                     {
-                       std::ostringstream contentrange;
-                       contentrange << "Content-Range: bytes */" << filesize;
-                       headers.push_back(contentrange.str());
+                       if (_config->FindB("aptwebserver::support::content-range", true) == true)
+                       {
+                          std::ostringstream contentrange;
+                          contentrange << "Content-Range: bytes */" << filesize;
+                          headers.push_back(contentrange.str());
+                       }
                        sendError(client, 416, *m, sendContent, "", headers);
-                       break;
+                       continue;
                     }
                  }
               }
@@ -816,7 +830,7 @@ int main(int const argc, const char * argv[])
       return 1;
    }
 
-   int const port = _config->FindI("aptwebserver::port", 8080);
+   int port = _config->FindI("aptwebserver::port", 8080);
 
    // ensure that we accept all connections: v4 or v6
    int const iponly = 0;
@@ -838,6 +852,26 @@ int main(int const argc, const char * argv[])
       return 2;
    }
 
+   if (port == 0)
+   {
+      struct sockaddr_in6 addr;
+      socklen_t addrlen = sizeof(sockaddr_in6);
+      if (getsockname(sock, (struct sockaddr*) &addr, &addrlen) != 0)
+        _error->Errno("getsockname", "Could not get chosen port number");
+      else
+        port = ntohs(addr.sin6_port);
+   }
+   std::string const portfilename = _config->Find("aptwebserver::portfile", "");
+   if (portfilename.empty() == false)
+   {
+      FileFd portfile(portfilename, FileFd::WriteOnly | FileFd::Create | FileFd::Empty);
+      std::string portcontent;
+      strprintf(portcontent, "%d", port);
+      portfile.Write(portcontent.c_str(), portcontent.size());
+      portfile.Sync();
+   }
+   _config->Set("aptwebserver::port::http", port);
+
    FileFd pidfile;
    if (_config->FindB("aptwebserver::fork", false) == true)
    {
@@ -863,6 +897,7 @@ int main(int const argc, const char * argv[])
         std::string pidcontent;
         strprintf(pidcontent, "%d", child);
         pidfile.Write(pidcontent.c_str(), pidcontent.size());
+        pidfile.Sync();
         if (_error->PendingError() == true)
         {
            _error->DumpErrors(std::cerr);