]> git.saurik.com Git - apt.git/blobdiff - test/interactive-helper/aptwebserver.cc
fix three typos in sources & manpages
[apt.git] / test / interactive-helper / aptwebserver.cc
index 644629a33bc33b4ef8d791395644c01c4a2da50d..950a17bc12557e917f4486fe000832e3fb20f0f4 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());
    }
       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(), false));
+      headers.push_back(lastmodified);
+   }
 }
                                                                        /*}}}*/
 static void addDataHeaders(std::list<std::string> &headers, std::string &data)/*{{{*/
 }
                                                                        /*}}}*/
 static void addDataHeaders(std::list<std::string> &headers, std::string &data)/*{{{*/
@@ -119,13 +122,16 @@ static bool sendHead(int const client, int const httpcode, std::list<std::string
    _config->Set("APTWebserver::Last-Status-Code", httpcode);
 
    std::stringstream buffer;
    _config->Set("APTWebserver::Last-Status-Code", httpcode);
 
    std::stringstream buffer;
+   auto const empties = _config->FindVector("aptwebserver::empty-response-header");
+   for (auto && e: empties)
+      buffer << e << ":" << std::endl;
    _config->Dump(buffer, "aptwebserver::response-header", "%t: %v%n", false);
    std::vector<std::string> addheaders = VectorizeString(buffer.str(), '\n');
    for (std::vector<std::string>::const_iterator h = addheaders.begin(); h != addheaders.end(); ++h)
       headers.push_back(*h);
 
    std::string date("Date: ");
    _config->Dump(buffer, "aptwebserver::response-header", "%t: %v%n", false);
    std::vector<std::string> addheaders = VectorizeString(buffer.str(), '\n');
    for (std::vector<std::string>::const_iterator h = addheaders.begin(); h != addheaders.end(); ++h)
       headers.push_back(*h);
 
    std::string date("Date: ");
-   date.append(TimeRFC1123(time(NULL)));
+   date.append(TimeRFC1123(time(NULL), false));
    headers.push_back(date);
 
    if (chunkedTransferEncoding(headers) == true)
    headers.push_back(date);
 
    if (chunkedTransferEncoding(headers) == true)
@@ -206,8 +212,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)
 {
 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>: ");
    response.append("<body><h1>").append(httpcodeToStr(httpcode)).append("</h1>");
    if (httpcode != 200)
       response.append("<p><em>Error</em>: ");
@@ -242,8 +248,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 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>");
    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 +259,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 (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://");
         location.append("https://");
       else
         location.append("http://");
@@ -326,7 +335,7 @@ static void sendDirectoryListing(int const client, std::string const &dir,/*{{{*
       return;
    }
 
       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;}"
           << "<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;}"
@@ -353,7 +362,7 @@ static void sendDirectoryListing(int const client, std::string const &dir,/*{{{*
                 << "<td><a href=\"" << namelist[i]->d_name << "\">" << namelist[i]->d_name << "</a></td>"
                 << "<td>" << SizeToStr(fs.st_size) << "B</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 << "<td>" << TimeRFC1123(fs.st_mtime, true) << "</td></tr>" << std::endl;
    }
    listing << "</table></body></html>" << std::endl;
 
    }
    listing << "</table></body></html>" << std::endl;
 
@@ -598,17 +607,24 @@ static void * handleClient(void * voidclient)                             /*{{{*/
 {
    int client = *((int*)(voidclient));
    std::clog << "ACCEPT client " << client << std::endl;
 {
    int client = *((int*)(voidclient));
    std::clog << "ACCEPT client " << client << std::endl;
-   std::vector<std::string> messages;
    bool closeConnection = false;
    bool closeConnection = false;
-   std::list<std::string> headers;
-   while (closeConnection == false && ReadMessages(client, messages))
+   while (closeConnection == false)
    {
    {
-      // if we announced a closing, do the close
-      if (std::find(headers.begin(), headers.end(), std::string("Connection: close")) != headers.end())
+      std::vector<std::string> messages;
+      if (ReadMessages(client, messages) == false)
         break;
         break;
-      headers.clear();
+
+      std::list<std::string> headers;
       for (std::vector<std::string>::const_iterator m = messages.begin();
            m != messages.end() && closeConnection == false; ++m) {
       for (std::vector<std::string>::const_iterator m = messages.begin();
            m != messages.end() && closeConnection == false; ++m) {
+        // if we announced a closing in previous response, do the close now
+        if (std::find(headers.begin(), headers.end(), std::string("Connection: close")) != headers.end())
+        {
+           closeConnection = true;
+           break;
+        }
+        headers.clear();
+
         std::clog << ">>> REQUEST from " << client << " >>>" << std::endl << *m
            << std::endl << "<<<<<<<<<<<<<<<<" << std::endl;
         std::string filename;
         std::clog << ">>> REQUEST from " << client << " >>>" << std::endl << *m
            << std::endl << "<<<<<<<<<<<<<<<<" << std::endl;
         std::string filename;
@@ -639,7 +655,7 @@ static void * handleClient(void * voidclient)                               /*{{{*/
               redirect.erase(0,1);
            if (redirect != filename)
            {
               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;
            }
         }
               continue;
            }
         }
@@ -671,8 +687,11 @@ static void * handleClient(void * voidclient)                              /*{{{*/
         }
 
         // deal with the request
         }
 
         // 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 &&
         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;
         {
            sendError(client, 400, *m, sendContent, "HTTP disabled, all requests must be HTTPS", headers);
            continue;
@@ -698,6 +717,15 @@ static void * handleClient(void * voidclient)                              /*{{{*/
               condition.clear();
            if (condition.empty() == false && strncmp(condition.c_str(), "bytes=", 6) == 0)
            {
               condition.clear();
            if (condition.empty() == false && strncmp(condition.c_str(), "bytes=", 6) == 0)
            {
+              std::string ranges = ',' + _config->Find("aptwebserver::response-header::Accept-Ranges") + ',';
+              ranges.erase(std::remove(ranges.begin(), ranges.end(), ' '), ranges.end());
+              if (ranges.find(",bytes,") == std::string::npos)
+              {
+                 // we handle it as an error here because we are a test server - a real one should just ignore it
+                 sendError(client, 400, *m, sendContent, "Client does range requests we don't support", headers);
+                 continue;
+              }
+
               time_t cache;
               std::string ifrange;
               if (_config->FindB("aptwebserver::support::if-range", true) == true)
               time_t cache;
               std::string ifrange;
               if (_config->FindB("aptwebserver::support::if-range", true) == true)
@@ -721,13 +749,15 @@ static void * handleClient(void * voidclient)                             /*{{{*/
                     if (filesize > filestart)
                     {
                        data.Skip(filestart);
                     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 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);
                        sendHead(client, 206, headers);
                        if (sendContent == true)
                           sendFile(client, headers, data);
@@ -735,11 +765,14 @@ static void * handleClient(void * voidclient)                             /*{{{*/
                     }
                     else
                     {
                     }
                     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);
                        sendError(client, 416, *m, sendContent, "", headers);
-                       break;
+                       continue;
                     }
                  }
               }
                     }
                  }
               }
@@ -760,9 +793,16 @@ static void * handleClient(void * voidclient)                              /*{{{*/
         else
            sendError(client, 404, *m, sendContent, "", headers);
       }
         else
            sendError(client, 404, *m, sendContent, "", headers);
       }
+
+      // if we announced a closing in the last response, do the close now
+      if (std::find(headers.begin(), headers.end(), std::string("Connection: close")) != headers.end())
+        closeConnection = true;
+
+      if (_error->PendingError() == true)
+        break;
       _error->DumpErrors(std::cerr);
       _error->DumpErrors(std::cerr);
-      messages.clear();
    }
    }
+   _error->DumpErrors(std::cerr);
    close(client);
    std::clog << "CLOSE client " << client << std::endl;
    return NULL;
    close(client);
    std::clog << "CLOSE client " << client << std::endl;
    return NULL;
@@ -802,7 +842,7 @@ int main(int const argc, const char * argv[])
       return 1;
    }
 
       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;
 
    // ensure that we accept all connections: v4 or v6
    int const iponly = 0;
@@ -824,6 +864,26 @@ int main(int const argc, const char * argv[])
       return 2;
    }
 
       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)
    {
    FileFd pidfile;
    if (_config->FindB("aptwebserver::fork", false) == true)
    {
@@ -849,6 +909,7 @@ int main(int const argc, const char * argv[])
         std::string pidcontent;
         strprintf(pidcontent, "%d", child);
         pidfile.Write(pidcontent.c_str(), pidcontent.size());
         std::string pidcontent;
         strprintf(pidcontent, "%d", child);
         pidfile.Write(pidcontent.c_str(), pidcontent.size());
+        pidfile.Sync();
         if (_error->PendingError() == true)
         {
            _error->DumpErrors(std::cerr);
         if (_error->PendingError() == true)
         {
            _error->DumpErrors(std::cerr);