X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/905fba60a046646a26a56b4c5d4a5dc7d5906f0d..bd4a8f51649ee37291c6e07310104a94f4f5fbed:/test/interactive-helper/aptwebserver.cc?ds=sidebyside diff --git a/test/interactive-helper/aptwebserver.cc b/test/interactive-helper/aptwebserver.cc index 644629a33..849323cc0 100644 --- a/test/interactive-helper/aptwebserver.cc +++ b/test/interactive-helper/aptwebserver.cc @@ -96,9 +96,12 @@ static void addFileHeaders(std::list &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 &headers, std::string &data)/*{{{*/ @@ -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://"); @@ -598,17 +604,24 @@ static void * handleClient(void * voidclient) /*{{{*/ { int client = *((int*)(voidclient)); std::clog << "ACCEPT client " << client << std::endl; - std::vector messages; bool closeConnection = false; - std::list 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 messages; + if (ReadMessages(client, messages) == false) break; - headers.clear(); + + std::list headers; for (std::vector::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; @@ -671,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; @@ -721,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); @@ -735,9 +753,12 @@ 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; } @@ -760,9 +781,16 @@ static void * handleClient(void * voidclient) /*{{{*/ 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); - messages.clear(); } + _error->DumpErrors(std::cerr); close(client); std::clog << "CLOSE client " << client << std::endl; return NULL; @@ -802,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; @@ -824,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) { @@ -849,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);