55#include < baseclient.h>
66#include < inetchannel.h>
77#include < netadr.h>
8+ #include " unordered_set"
89
910class CHTTPServerModule : public IModule
1011{
@@ -16,7 +17,7 @@ class CHTTPServerModule : public IModule
1617 virtual int Compatibility () { return LINUX32 | LINUX64 | WINDOWS32 | WINDOWS64; };
1718};
1819
19- CHTTPServerModule g_pHttpServerModule;
20+ static CHTTPServerModule g_pHttpServerModule;
2021IModule* pHttpServerModule = &g_pHttpServerModule;
2122
2223struct HttpResponse {
4950 HTTPSERVER_OFFLINE
5051};
5152
53+ class HttpServer ;
54+ static std::unordered_set<HttpServer*> g_pHttpServers;
5255class HttpServer
5356{
5457public:
58+ HttpServer () {
59+ g_pHttpServers.insert (this );
60+ }
61+
5562 ~HttpServer ()
5663 {
5764 if (!ThreadInMainThread ())
@@ -67,6 +74,7 @@ class HttpServer
6774 Util::ReferenceFree (ref, " HttpServer::~HttpServer - Handler references" );
6875
6976 m_pHandlerReferences.clear ();
77+ g_pHttpServers.erase (this );
7078 }
7179
7280 void Start (const char * address, unsigned short port);
@@ -128,11 +136,11 @@ class HttpServer
128136
129137private:
130138 unsigned char m_iStatus = HTTPSERVER_OFFLINE;
131- unsigned short m_iPort;
139+ unsigned short m_iPort = 0 ;
132140 unsigned int m_iThreadSleep = 5 ; // How long the threads sleep / wait for a request to be handled
133141 bool m_bUpdate = false ;
134142 bool m_bInUpdate = false ;
135- std::string m_strAddress;
143+ std::string m_strAddress = " " ;
136144 std::vector<HttpRequest*> m_pRequests;
137145 std::vector<int > m_pHandlerReferences; // Contains the Lua references to the handler functions.
138146 httplib::Server m_pServer;
@@ -743,11 +751,11 @@ LUA_FUNCTION_STATIC(httpserver_Destroy)
743751
744752LUA_FUNCTION_STATIC (httpserver_GetAll)
745753{
746- LUA->PreCreateTable (g_pPushedHttpServer .size (), 0 );
754+ LUA->PreCreateTable (g_pHttpServers .size (), 0 );
747755 int idx = 0 ;
748- for (auto & [_, value] : g_pPushedHttpServer )
756+ for (auto & server : g_pHttpServers )
749757 {
750- value-> Push ( );
758+ Push_HttpServer (server );
751759 Util::RawSetI (LUA, -2 , ++idx);
752760 }
753761
@@ -758,11 +766,11 @@ LUA_FUNCTION_STATIC(httpserver_FindByName)
758766{
759767 std::string strName = LUA->CheckString (1 );
760768 bool bPushed = false ;
761- for (auto & [_, value] : g_pPushedHttpServer )
769+ for (auto & server : g_pHttpServers )
762770 {
763- if (((HttpServer*)value-> GetData ()) ->GetName () == strName)
771+ if (server ->GetName () == strName)
764772 {
765- value-> Push ( );
773+ Push_HttpServer (server );
766774 bPushed = true ;
767775 }
768776 }
@@ -862,10 +870,17 @@ void CHTTPServerModule::LuaShutdown()
862870{
863871 Util::NukeTable (" httpserver" );
864872
865- // Shouls we allow HttpServers to persist across map changes? Maybe later, but if anyone wants this request it .
873+ // HttpServers WILL persist across map changes.
866874 DeleteAll_HttpResponse ();
867875 DeleteAll_HttpRequest ();
868876 DeleteAll_HttpServer ();
877+
878+ std::vector<HttpServer*> httpServers; // Copy of g_pHttpServers as when deleting we can't iterate over it.
879+ for (auto server : g_pHttpServers)
880+ httpServers.push_back (server);
881+
882+ for (auto server : httpServers)
883+ delete server;
869884}
870885
871886void CHTTPServerModule::Think (bool simulating)
0 commit comments