From ea36a48ee2aa259734666aff7b81575299a31259 Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Wed, 7 Jan 2026 00:00:01 +0100 Subject: [PATCH] some code cleanup --- test-webserver.cc | 28 +++++++++++++++++++++++++++- webserver.cc | 9 +-------- www/index.html | 1 + 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/test-webserver.cc b/test-webserver.cc index cc560d4..2609e21 100644 --- a/test-webserver.cc +++ b/test-webserver.cc @@ -15,6 +15,7 @@ int running = 1; static void sig_int(int); int SetupSignals(); std::string GenerateHtmlFile(); +std::string GenerateBigHtmlFile(int size); class SimpleWebServer : public WebServer { private: @@ -31,7 +32,13 @@ int SimpleWebServer::HandleRequest (WebRequestBuffer *requestbuffer, WebServerCl printf ("SimpleWebServerClient::HandleRequest() Request:%s\n", request.c_str()); if (request.compare ("/") == 0) request = "/index.html"; - if (request.find("/test.html") != std::string::npos) { + if (request.find("/big.html") != std::string::npos) { + std::string htmlfile = GenerateBigHtmlFile(8*1024*1024); + + if (webclient->SendResponseFileFromMemory(requestbuffer, request, "", + (void*) htmlfile.c_str(), strlen(htmlfile.c_str())) != 1) return 0; + } + else if (request.find("/test.html") != std::string::npos) { std::string htmlfile = GenerateHtmlFile(); if (webclient->SendResponseFileFromMemory(requestbuffer, request, "", @@ -55,6 +62,8 @@ int main (int argc, char **argv) { webserver.SetupSSL(DEFAULT_KEY, DEFAULT_CERT); webserver.Start(); + debug ("http server is running on http://localhost:%d", DEFAULT_HTTP_PORT); + debug ("https server is running on https://localhost:%d", DEFAULT_HTTPS_PORT); running = 1; while (running == 1) { @@ -102,3 +111,20 @@ std::string GenerateHtmlFile() { return html; }; + + + +std::string GenerateBigHtmlFile(int size) { + std::string html; + int i; + html = "Random Big Text"; + html += "

just some randdom number "; + for (i = 0; i < size; i++) { + html += to_string(rand() % 10); + if ((i % 100) == 0) html += "
\n"; + } + html += "

" + to_string(size) + " Bytes of random numbers.
"; + html += ".

Return to INDEX.HTML"; + + return html; +}; diff --git a/webserver.cc b/webserver.cc index 9583b9c..cf052b1 100644 --- a/webserver.cc +++ b/webserver.cc @@ -224,7 +224,7 @@ int WebServerClient::SendResponseFileFromMemory (WebRequestBuffer *request, std: // debug ("send file %s with %ld bytes\n", fname.c_str(), filereadbytes); if (l != bytestosend) { - debug ("could not send all data. Error:%s\n", strerror(errno)); + debug ("could not send all data. bytestosend:%d l:%d Error:%s\n", bytestosend, l, strerror(errno)); free (outbuffer); return 0; } @@ -396,13 +396,11 @@ int WebServer::Start() { printf ("error on listen\n"); return 0; } - printf ("test server is running on port: %d\n", conf_httpport); if (https.Listen(conf_httpsport) != 1) { printf ("error on listen\n"); return 0; } - printf ("test server is running on port: %d\n", conf_httpsport); running = 1; @@ -443,7 +441,6 @@ int WebServer::Loop() { } if (tcpclient) { - printf ("new %s connection from %s\n", ssl_enabled ? "HTTPS" : "HTTP", tcpclient->GetRemoteAddr().c_str()); if (webclients.size() > conf_maxclients) { printf ("max connections reached. closing connection.\n"); @@ -464,7 +461,6 @@ int WebServer::Loop() { } webclient->SetDecoumentRoot("./www"); webclients.push_back(webclient); - printf ("add new connection to client list\n"); } // @@ -476,7 +472,6 @@ int WebServer::Loop() { reqbuffer = webclient->Loop(); if (reqbuffer == NULL) { // error on loop, remove and delete webclient. - printf ("remove connection\n"); webclients.remove(webclient); delete webclient; wci = webclients.begin(); @@ -485,7 +480,6 @@ int WebServer::Loop() { else if (reqbuffer->RequestComplete()) { if (HandleRequest(reqbuffer, *wci) == 1) continue; // error handling request, remove and delete webclient. - printf ("remove connection\n"); webclients.remove(webclient); delete webclient; wci = webclients.begin(); @@ -501,7 +495,6 @@ int WebServer::HandleRequest (WebRequestBuffer *requestbuffer, WebServerClient * if (requestbuffer == NULL || webclient == NULL) return 0; std::string request = requestbuffer->GetRequest(); - printf ("WebServerClient::HandleRequest() Request:%s\n", request.c_str()); if (request.compare ("/") == 0) request = "/index.html"; if (webclient->SendResponseFile(requestbuffer, request, "") != 1) return 0; diff --git a/www/index.html b/www/index.html index 754c61d..3618f4b 100644 --- a/www/index.html +++ b/www/index.html @@ -8,6 +8,7 @@ This is only a test for more detail see at https://steffen.gulpe.de/gitea/steffen/libUDPTCPNetwork

test.html is a hard coded HTML page. + big.html is a big hard coded HTML page.