summaryrefslogtreecommitdiffstats
path: root/WebServer
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--WebServer/Socket.cpp1
-rw-r--r--WebServer/WebServer.cpp74
-rw-r--r--WebServer/WebServer.h10
3 files changed, 69 insertions, 16 deletions
diff --git a/WebServer/Socket.cpp b/WebServer/Socket.cpp
index 59efc8127..430461fe8 100644
--- a/WebServer/Socket.cpp
+++ b/WebServer/Socket.cpp
@@ -37,6 +37,7 @@
#ifndef _WIN32
#include <cstring>
#include <sys/time.h>
+ #include <unistd.h>
#define SD_SEND 0x01
#else
#define MSG_NOSIGNAL (0)
diff --git a/WebServer/WebServer.cpp b/WebServer/WebServer.cpp
index aca585a88..8be690bd1 100644
--- a/WebServer/WebServer.cpp
+++ b/WebServer/WebServer.cpp
@@ -47,7 +47,15 @@
#include "UrlHelper.h"
#include "base64.h"
-webserver::request_func webserver::request_func_=0;
+
+
+
+
+webserver::request_func webserver::request_func_ = NULL;
+
+
+
+
static std::string EatLine( std::string& a_String )
{
@@ -76,6 +84,10 @@ static std::string EatLine( std::string& a_String )
return RetVal;
}
+
+
+
+
// Turns
// "blabla my string with \"quotes\"!"
// into
@@ -116,6 +128,10 @@ static std::string GetQuotedString( const std::string& a_String )
return RetVal;
}
+
+
+
+
void ParseMultipartFormData( webserver::http_request& req, Socket* s)
{
static const std::string multipart_form_data = "multipart/form-data";
@@ -229,6 +245,10 @@ void ParseMultipartFormData( webserver::http_request& req, Socket* s)
}
}
+
+
+
+
#ifdef _WIN32
unsigned webserver::Request(void* ptr_s)
#else
@@ -395,6 +415,10 @@ void* webserver::Request(void* ptr_s)
return 0;
}
+
+
+
+
void webserver::Stop()
{
m_bStop = true;
@@ -402,46 +426,72 @@ void webserver::Stop()
m_Events->Wait();
}
-void webserver::Begin()
+
+
+
+
+bool webserver::Begin()
{
+ if (!m_Socket->IsValid())
+ {
+ LOGINFO("WebAdmin: The server socket is invalid. Terminating WebAdmin.");
+ return false;
+ }
m_bStop = false;
while ( !m_bStop )
{
- Socket* ptr_s=m_Socket->Accept();
- if( m_bStop )
+ Socket * ptr_s = m_Socket->Accept();
+ if (m_bStop)
{
- if( ptr_s != 0 )
+ if (ptr_s != 0)
{
ptr_s->Close();
delete ptr_s;
}
break;
}
+ if (ptr_s == NULL)
+ {
+ LOGINFO("WebAdmin: Accepted socket is NULL. Terminating WebAdmin to avoid busywait.");
+ return false;
+ }
- // unused variable 'ret'
- //_beginthreadex(0,0,Request,(void*) ptr_s,0,&ret);
- // Thanks to Frank M. Hoffmann for fixing a HANDLE leak
#ifdef _WIN32
unsigned ret;
- HANDLE hHandle = reinterpret_cast<HANDLE>(_beginthreadex(0,0,Request,(void*) ptr_s,0,&ret));
+ HANDLE hHandle = reinterpret_cast<HANDLE>(_beginthreadex(NULL, 0, Request, (void *)ptr_s, 0, &ret));
CloseHandle(hHandle);
#else
- pthread_t* hHandle = new pthread_t;
+ // Mattes: TODO: this handle probably leaks!
+ pthread_t * hHandle = new pthread_t;
pthread_create( hHandle, NULL, Request, ptr_s);
#endif
}
m_Events->Set();
+ return true;
}
-webserver::webserver(unsigned int port_to_listen, request_func r) {
- m_Socket = new SocketServer(port_to_listen,1);
+
+
+
+
+webserver::webserver(unsigned int port_to_listen, request_func r)
+{
+ m_Socket = new SocketServer(port_to_listen, 1);
request_func_ = r;
m_Events = new cEvents();
}
+
+
+
+
webserver::~webserver()
{
delete m_Socket;
delete m_Events;
}
+
+
+
+
diff --git a/WebServer/WebServer.h b/WebServer/WebServer.h
index 90b38d407..d9d4b2fd1 100644
--- a/WebServer/WebServer.h
+++ b/WebServer/WebServer.h
@@ -89,16 +89,18 @@ public:
webserver(unsigned int port_to_listen, request_func);
~webserver();
- void Begin();
+ bool Begin();
void Stop();
private:
bool m_bStop;
-#ifdef _WIN32
+
+ #ifdef _WIN32
static unsigned __stdcall Request(void*);
-#else
+ #else
static void* Request(void*);
-#endif
+ #endif
+
static request_func request_func_;
cEvents * m_Events;