From dc57a59b1e7a904ad7263245fa2b1d6a5a55655e Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 10 Jul 2012 18:00:11 +0200 Subject: reformat the aptwebserver code to look more like the rest of APT --- test/interactive-helper/aptwebserver.cc | 89 ++++++++++++++++----------------- 1 file changed, 44 insertions(+), 45 deletions(-) (limited to 'test') diff --git a/test/interactive-helper/aptwebserver.cc b/test/interactive-helper/aptwebserver.cc index 8fbb9eab9..97a298c70 100644 --- a/test/interactive-helper/aptwebserver.cc +++ b/test/interactive-helper/aptwebserver.cc @@ -18,7 +18,7 @@ #include #include -char const * const httpcodeToStr(int httpcode) { /*{{{*/ +char const * const httpcodeToStr(int const httpcode) { /*{{{*/ switch (httpcode) { // Informational 1xx case 100: return "100 Continue"; @@ -67,9 +67,9 @@ char const * const httpcodeToStr(int httpcode) { /*{{{*/ case 505: return "HTTP Version not supported"; } return NULL; -} /*}}}*/ - -void addFileHeaders(std::list &headers, FileFd &data) { /*{{{*/ +} + /*}}}*/ +void addFileHeaders(std::list &headers, FileFd &data) { /*{{{*/ std::ostringstream contentlength; contentlength << "Content-Length: " << data.FileSize(); headers.push_back(contentlength.str()); @@ -77,15 +77,15 @@ void addFileHeaders(std::list &headers, FileFd &data) { /*{{{*/ std::string lastmodified("Last-Modified: "); lastmodified.append(TimeRFC1123(data.ModificationTime())); headers.push_back(lastmodified); -} /*}}}*/ - +} + /*}}}*/ void addDataHeaders(std::list &headers, std::string &data) {/*{{{*/ std::ostringstream contentlength; contentlength << "Content-Length: " << data.size(); headers.push_back(contentlength.str()); -} /*}}}*/ - -bool sendHead(int client, int httpcode, std::list &headers) { /*{{{*/ +} + /*}}}*/ +bool sendHead(int const client, int const httpcode, std::list &headers) { /*{{{*/ string response("HTTP/1.1 "); response.append(httpcodeToStr(httpcode)); headers.push_front(response); @@ -107,9 +107,9 @@ bool sendHead(int client, int httpcode, std::list &headers) { /*{{{ Success &= FileFd::Write(client, "\r\n", 2); std::clog << "<<<<<<<<<<<<<<<<" << std::endl; return Success; -} /*}}}*/ - -bool sendFile(int client, FileFd &data) { /*{{{*/ +} + /*}}}*/ +bool sendFile(int const client, FileFd &data) { /*{{{*/ bool Success = true; char buffer[500]; unsigned long long actual = 0; @@ -120,34 +120,32 @@ bool sendFile(int client, FileFd &data) { /*{{{*/ } Success &= FileFd::Write(client, "\r\n", 2); return Success; -} /*}}}*/ - -bool sendData(int client, std::string &data) { /*{{{*/ +} + /*}}}*/ +bool sendData(int const client, std::string const &data) { /*{{{*/ bool Success = true; Success &= FileFd::Write(client, data.c_str(), data.size()); Success &= FileFd::Write(client, "\r\n", 2); return Success; -} /*}}}*/ - -void sendError(int client, int httpcode, string request, bool content) { /*{{{*/ +} + /*}}}*/ +void sendError(int const client, int const httpcode, string const &request, bool content) { /*{{{*/ std::list headers; - string response; - if (content == true) { - response.append(""); - response.append(httpcodeToStr(httpcode)).append(""); - response.append("

").append(httpcodeToStr(httpcode)).append(""); - response.append(request).append(""); - addDataHeaders(headers, response); - } + string response(""); + response.append(httpcodeToStr(httpcode)).append(""); + response.append("

").append(httpcodeToStr(httpcode)).append(""); + response.append(request).append(""); + addDataHeaders(headers, response); sendHead(client, httpcode, headers); - sendData(client, response); -} /*}}}*/ - -int main(int argc, const char *argv[]) + if (content == true) + sendData(client, response); +} + /*}}}*/ +int main(int const argc, const char * argv[]) { CommandLine::Args Args[] = { - {0, "simulate-paywall", "aptwebserver::Simulate-Paywall", + {0, "simulate-paywall", "aptwebserver::Simulate-Paywall", CommandLine::Boolean}, {0, "port", "aptwebserver::port", CommandLine::HasArg}, {0,0,0,0} @@ -198,18 +196,19 @@ int main(int argc, const char *argv[]) } listen(sock, 1); + /*}}}*/ std::vector messages; int client; while ((client = accept(sock, NULL, NULL)) != -1) { std::clog << "ACCEPT client " << client - << " on socket " << sock << std::endl; + << " on socket " << sock << std::endl; while (ReadMessages(client, messages)) { for (std::vector::const_iterator m = messages.begin(); m != messages.end(); ++m) { - std::clog << ">>> REQUEST >>>>" << std::endl << *m - << std::endl << "<<<<<<<<<<<<<<<<" << std::endl; + std::clog << ">>> REQUEST >>>>" << std::endl << *m + << std::endl << "<<<<<<<<<<<<<<<<" << std::endl; std::list headers; bool sendContent = true; if (strncmp(m->c_str(), "HEAD ", 5) == 0) @@ -227,12 +226,12 @@ int main(int argc, const char *argv[]) size_t const filestart = m->find(' ', 5); string filename = m->substr(5, filestart - 5); - if (simulate_broken_server == true) { - string data("ni ni ni\n"); - addDataHeaders(headers, data); - sendHead(client, 200, headers); - sendData(client, data); - } + if (simulate_broken_server == true) { + string data("ni ni ni\n"); + addDataHeaders(headers, data); + sendHead(client, 200, headers); + sendData(client, data); + } else if (RealFileExists(filename) == false) sendError(client, 404, *m, sendContent); else { @@ -240,8 +239,8 @@ int main(int argc, const char *argv[]) std::string condition = LookupTag(*m, "If-Modified-Since", ""); if (condition.empty() == false) { time_t cache; - if (RFC1123StrToTime(condition.c_str(), cache) == true && - cache >= data.ModificationTime()) { + if (RFC1123StrToTime(condition.c_str(), cache) == true && + cache >= data.ModificationTime()) { sendError(client, 304, *m, false); continue; } @@ -256,8 +255,8 @@ int main(int argc, const char *argv[]) messages.clear(); } - std::clog << "CLOSE client " << client - << " on socket " << sock << std::endl; + std::clog << "CLOSE client " << client + << " on socket " << sock << std::endl; close(client); } return 0; -- cgit v1.2.3