@@ -123,6 +123,8 @@ class HttpServer
123123 std::string& GetAddress () { return m_strAddress; };
124124 unsigned short GetPort () { return m_iPort; };
125125 void SetThreadSleep (unsigned int threadSleep) { m_iThreadSleep = threadSleep; };
126+ std::string& GetName () { return m_strName; };
127+ void SetName (std::string strName) { m_strName = strName; };
126128
127129private:
128130 unsigned char m_iStatus = HTTPSERVER_OFFLINE;
@@ -134,6 +136,7 @@ class HttpServer
134136 std::vector<HttpRequest*> m_pRequests;
135137 std::vector<int > m_pHandlerReferences; // Contains the Lua references to the handler functions.
136138 httplib::Server m_pServer;
139+ std::string m_strName = " NONAME" ;
137140};
138141
139142static int HttpResponse_TypeID = -1 ;
@@ -492,7 +495,7 @@ LUA_FUNCTION_STATIC(HttpServer__tostring)
492495 }
493496
494497 char szBuf[64 ] = {};
495- V_snprintf (szBuf, sizeof (szBuf)," HttpServer [%s]" , (pServer->GetAddress () + std::to_string (pServer->GetPort ())).c_str ());
498+ V_snprintf (szBuf, sizeof (szBuf)," HttpServer [%s - %s ]" , (pServer->GetAddress () + std::to_string (pServer->GetPort ())).c_str (), pServer-> GetName ());
496499 LUA->PushString (szBuf);
497500 return 1 ;
498501}
@@ -678,6 +681,38 @@ LUA_FUNCTION_STATIC(HttpServer_SetThreadSleep)
678681 return 0 ;
679682}
680683
684+ LUA_FUNCTION_STATIC (HttpServer_GetPort)
685+ {
686+ HttpServer* pServer = Get_HttpServer (1 , true );
687+
688+ LUA->PushNumber (pServer->GetPort ());
689+ return 1 ;
690+ }
691+
692+ LUA_FUNCTION_STATIC (HttpServer_GetAddress)
693+ {
694+ HttpServer* pServer = Get_HttpServer (1 , true );
695+
696+ LUA->PushString (pServer->GetAddress ().c_str ());
697+ return 1 ;
698+ }
699+
700+ LUA_FUNCTION_STATIC (HttpServer_GetName)
701+ {
702+ HttpServer* pServer = Get_HttpServer (1 , true );
703+
704+ LUA->PushString (pServer->GetName ().c_str ());
705+ return 1 ;
706+ }
707+
708+ LUA_FUNCTION_STATIC (HttpServer_SetName)
709+ {
710+ HttpServer* pServer = Get_HttpServer (1 , true );
711+
712+ pServer->SetName (LUA->CheckString (2 ));
713+ return 0 ;
714+ }
715+
681716LUA_FUNCTION_STATIC (httpserver_Create)
682717{
683718 Push_HttpServer (new HttpServer);
@@ -710,6 +745,25 @@ LUA_FUNCTION_STATIC(httpserver_GetAll)
710745 return 1 ;
711746}
712747
748+ LUA_FUNCTION_STATIC (httpserver_FindByName)
749+ {
750+ std::string strName = LUA->CheckString (1 );
751+ bool bPushed = false ;
752+ for (auto & [_, value] : g_pPushedHttpServer)
753+ {
754+ if (((HttpServer*)value->GetData ())->GetName () == strName)
755+ {
756+ value->Push ();
757+ bPushed = true ;
758+ }
759+ }
760+
761+ if (!bPushed)
762+ LUA->PushNil ();
763+
764+ return 1 ;
765+ }
766+
713767void CHTTPServerModule::LuaInit (bool bServerInit)
714768{
715769 if (bServerInit)
@@ -744,6 +798,11 @@ void CHTTPServerModule::LuaInit(bool bServerInit)
744798 Util::AddFunc (HttpServer_Patch, " Patch" );
745799 Util::AddFunc (HttpServer_Delete, " Delete" );
746800 Util::AddFunc (HttpServer_Options, " Options" );
801+
802+ Util::AddFunc (HttpServer_GetPort, " GetPort" );
803+ Util::AddFunc (HttpServer_GetAddress, " GetAddress" );
804+ Util::AddFunc (HttpServer_GetName, " GetName" );
805+ Util::AddFunc (HttpServer_SetName, " SetName" );
747806 g_Lua->Pop (1 );
748807
749808 HttpResponse_TypeID = g_Lua->CreateMetaTable (" HttpResponse" );
@@ -786,6 +845,7 @@ void CHTTPServerModule::LuaInit(bool bServerInit)
786845 Util::AddFunc (httpserver_Create, " Create" );
787846 Util::AddFunc (httpserver_Destroy, " Destroy" );
788847 Util::AddFunc (httpserver_GetAll, " GetAll" );
848+ Util::AddFunc (httpserver_FindByName, " FindByName" );
789849 Util::FinishTable (" httpserver" );
790850}
791851
0 commit comments