X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/8c9c50f94768a13359b108e2cd0da9832f9cf6ad..fdba4d53d6b9b594531c34792798f4044a25157e:/test/interactive-helper/aptwebserver.cc

diff --git a/test/interactive-helper/aptwebserver.cc b/test/interactive-helper/aptwebserver.cc
index b7663a76a..34476e1af 100644
--- a/test/interactive-helper/aptwebserver.cc
+++ b/test/interactive-helper/aptwebserver.cc
@@ -1,29 +1,31 @@
 #include <config.h>
 
-#include <apt-pkg/strutl.h>
-#include <apt-pkg/fileutl.h>
-#include <apt-pkg/error.h>
 #include <apt-pkg/cmndline.h>
 #include <apt-pkg/configuration.h>
-#include <apt-pkg/init.h>
-
-#include <vector>
-#include <string>
-#include <list>
-#include <sstream>
+#include <apt-pkg/error.h>
+#include <apt-pkg/fileutl.h>
+#include <apt-pkg/strutl.h>
 
+#include <dirent.h>
+#include <errno.h>
+#include <netinet/in.h>
+#include <pthread.h>
+#include <regex.h>
+#include <signal.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/socket.h>
-#include <sys/types.h>
 #include <sys/stat.h>
-#include <netinet/in.h>
-#include <unistd.h>
-#include <errno.h>
 #include <time.h>
-#include <stdlib.h>
-#include <dirent.h>
-#include <signal.h>
+#include <unistd.h>
+#include <iostream>
+#include <sstream>
+#include <list>
+#include <string>
+#include <vector>
 
-char const * const httpcodeToStr(int const httpcode)			/*{{{*/
+static char const * httpcodeToStr(int const httpcode)		/*{{{*/
 {
    switch (httpcode)
    {
@@ -77,7 +79,7 @@ char const * const httpcodeToStr(int const httpcode)			/*{{{*/
    return NULL;
 }
 									/*}}}*/
-void addFileHeaders(std::list<std::string> &headers, FileFd &data)	/*{{{*/
+static void addFileHeaders(std::list<std::string> &headers, FileFd &data)/*{{{*/
 {
    std::ostringstream contentlength;
    contentlength << "Content-Length: " << data.FileSize();
@@ -88,14 +90,14 @@ void addFileHeaders(std::list<std::string> &headers, FileFd &data)	/*{{{*/
    headers.push_back(lastmodified);
 }
 									/*}}}*/
-void addDataHeaders(std::list<std::string> &headers, std::string &data)	/*{{{*/
+static void addDataHeaders(std::list<std::string> &headers, std::string &data)/*{{{*/
 {
    std::ostringstream contentlength;
    contentlength << "Content-Length: " << data.size();
    headers.push_back(contentlength.str());
 }
 									/*}}}*/
-bool sendHead(int const client, int const httpcode, std::list<std::string> &headers)/*{{{*/
+static bool sendHead(int const client, int const httpcode, std::list<std::string> &headers)/*{{{*/
 {
    std::string response("HTTP/1.1 ");
    response.append(httpcodeToStr(httpcode));
@@ -128,7 +130,7 @@ bool sendHead(int const client, int const httpcode, std::list<std::string> &head
    return Success;
 }
 									/*}}}*/
-bool sendFile(int const client, FileFd &data)				/*{{{*/
+static bool sendFile(int const client, FileFd &data)			/*{{{*/
 {
    bool Success = true;
    char buffer[500];
@@ -144,7 +146,7 @@ bool sendFile(int const client, FileFd &data)				/*{{{*/
    return Success;
 }
 									/*}}}*/
-bool sendData(int const client, std::string const &data)		/*{{{*/
+static bool sendData(int const client, std::string const &data)		/*{{{*/
 {
    if (FileFd::Write(client, data.c_str(), data.size()) == false)
    {
@@ -154,7 +156,7 @@ bool sendData(int const client, std::string const &data)		/*{{{*/
    return true;
 }
 									/*}}}*/
-void sendError(int const client, int const httpcode, std::string const &request,/*{{{*/
+static void sendError(int const client, int const httpcode, std::string const &request,/*{{{*/
 	       bool content, std::string const &error = "")
 {
    std::list<std::string> headers;
@@ -179,13 +181,13 @@ void sendError(int const client, int const httpcode, std::string const &request,
    if (content == true)
       sendData(client, response);
 }
-void sendSuccess(int const client, std::string const &request,
+static void sendSuccess(int const client, std::string const &request,
 	       bool content, std::string const &error = "")
 {
    sendError(client, 200, request, content, error);
 }
 									/*}}}*/
-void sendRedirect(int const client, int const httpcode, std::string const &uri,/*{{{*/
+static void sendRedirect(int const client, int const httpcode, std::string const &uri,/*{{{*/
 		  std::string const &request, bool content)
 {
    std::list<std::string> headers;
@@ -197,9 +199,14 @@ void sendRedirect(int const client, int const httpcode, std::string const &uri,/
    response.append(request).append("</pre></body></html>");
    addDataHeaders(headers, response);
    std::string location("Location: ");
-   if (strncmp(uri.c_str(), "http://", 7) != 0)
+   if (strncmp(uri.c_str(), "http://", 7) != 0 && strncmp(uri.c_str(), "https://", 8) != 0)
    {
-      location.append("http://").append(LookupTag(request, "Host")).append("/");
+      std::string const host = LookupTag(request, "Host");
+      if (host.find(":4433") != std::string::npos)
+	 location.append("https://");
+      else
+	 location.append("http://");
+      location.append(host).append("/");
       if (strncmp("/home/", uri.c_str(), strlen("/home/")) == 0 && uri.find("/public_html/") != std::string::npos)
       {
 	 std::string homeuri = SubstVar(uri, "/home/", "~");
@@ -217,7 +224,7 @@ void sendRedirect(int const client, int const httpcode, std::string const &uri,/
       sendData(client, response);
 }
 									/*}}}*/
-int filter_hidden_files(const struct dirent *a)				/*{{{*/
+static int filter_hidden_files(const struct dirent *a)			/*{{{*/
 {
    if (a->d_name[0] == '.')
       return 0;
@@ -231,7 +238,7 @@ int filter_hidden_files(const struct dirent *a)				/*{{{*/
 #endif
    return 1;
 }
-int grouped_alpha_case_sort(const struct dirent **a, const struct dirent **b) {
+static int grouped_alpha_case_sort(const struct dirent **a, const struct dirent **b) {
 #ifdef _DIRENT_HAVE_D_TYPE
    if ((*a)->d_type == DT_DIR && (*b)->d_type == DT_DIR);
    else if ((*a)->d_type == DT_DIR && (*b)->d_type == DT_REG)
@@ -255,7 +262,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, std::string const &dir,	/*{{{*/
+static void sendDirectoryListing(int const client, std::string const &dir,/*{{{*/
 			  std::string const &request, bool content)
 {
    std::list<std::string> headers;
@@ -307,7 +314,7 @@ void sendDirectoryListing(int const client, std::string const &dir,	/*{{{*/
       sendData(client, response);
 }
 									/*}}}*/
-bool parseFirstLine(int const client, std::string const &request,	/*{{{*/
+static bool parseFirstLine(int const client, std::string const &request,/*{{{*/
 		    std::string &filename, std::string &params, bool &sendContent,
 		    bool &closeConnection)
 {
@@ -427,7 +434,7 @@ bool parseFirstLine(int const client, std::string const &request,	/*{{{*/
    return true;
 }
 									/*}}}*/
-bool handleOnTheFlyReconfiguration(int const client, std::string const &request, std::vector<std::string> const &parts)/*{{{*/
+static bool handleOnTheFlyReconfiguration(int const client, std::string const &request, std::vector<std::string> const &parts)/*{{{*/
 {
    size_t const pcount = parts.size();
    if (pcount == 4 && parts[1] == "set")
@@ -470,7 +477,7 @@ bool handleOnTheFlyReconfiguration(int const client, std::string const &request,
    return false;
 }
 									/*}}}*/
-void * handleClient(void * voidclient)					/*{{{*/
+static void * handleClient(void * voidclient)				/*{{{*/
 {
    int client = *((int*)(voidclient));
    std::clog << "ACCEPT client " << client << std::endl;
@@ -507,7 +514,8 @@ void * handleClient(void * voidclient)					/*{{{*/
 	    std::string redirect = "/" + filename;
 	    for (::Configuration::Item *I = Replaces->Child; I != NULL; I = I->Next)
 	       redirect = SubstVar(redirect, I->Tag, I->Value);
-	    redirect.erase(0,1);
+	    if (redirect.empty() == false && redirect[0] == '/')
+	       redirect.erase(0,1);
 	    if (redirect != filename)
 	    {
 	       sendRedirect(client, 301, redirect, *m, sendContent);
@@ -542,7 +550,13 @@ void * handleClient(void * voidclient)					/*{{{*/
 	 }
 
 	 // deal with the request
-	 if (RealFileExists(filename) == true)
+	 if (_config->FindB("aptwebserver::support::http", true) == false &&
+	       LookupTag(*m, "Host").find(":4433") == std::string::npos)
+	 {
+	    sendError(client, 400, *m, sendContent, "HTTP disabled, all requests must be HTTPS");
+	    continue;
+	 }
+	 else if (RealFileExists(filename) == true)
 	 {
 	    FileFd data(filename, FileFd::ReadOnly);
 	    std::string condition = LookupTag(*m, "If-Modified-Since", "");