some code cleanup

master
Steffen Pohle 1 month ago
parent 344bbb5093
commit ea36a48ee2

@ -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 = "<html><head><title>Random Big Text</title></head><body>";
html += "<p>just some randdom number <b>";
for (i = 0; i < size; i++) {
html += to_string(rand() % 10);
if ((i % 100) == 0) html += "<br>\n";
}
html += "</b><p>" + to_string(size) + " Bytes of random numbers.<br>";
html += "</b>.<p>Return to <a href=\"index.html\">INDEX.HTML</a></body></html>";
return html;
};

@ -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;

@ -8,6 +8,7 @@
This is only a test for more detail see at <a href="https://steffen.gulpe.de/gitea/steffen/libUDPTCPNetwork">https://steffen.gulpe.de/gitea/steffen/libUDPTCPNetwork</a>
<p>
<a href="test.html">test.html</a> is a hard coded HTML page.
<a href="big.html">big.html</a> is a big hard coded HTML page.
</body>
</html>

Loading…
Cancel
Save