Skip to content

Commit 6791486

Browse files
committed
httpserver: add new things
1 parent d6e1c37 commit 6791486

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2944,12 +2944,15 @@ Destroys the given http server.
29442944
#### table httpserver.GetAll()
29452945
Returns a table containing all existing HttpServer in case you lose a reference.
29462946

2947+
#### HttpServer httpserver.FindByName(string name)
2948+
Returns the HttpServer with the given name set by `HttpServer:SetName()` or returns `nil` on failure.
2949+
29472950
### HttpServer
29482951
This class represents a created HttpServer.
29492952

29502953
#### string HttpServer:\_\_tostring()
29512954
Returns `HttpServer [NULL]` if given invalid list.
2952-
Normally returns `HttpServer [Address:Port]`.
2955+
Normally returns `HttpServer [Address:Port - Name]`.
29532956

29542957
#### HttpServer:\_\_newindex(string key, any value)
29552958
Internally implemented and will set the values into the lua table.
@@ -3013,6 +3016,18 @@ This mounts the given folder to the given path.
30133016
#### HttpServer:RemoveMountPoint(string mountPoint)
30143017
This removes all mounts for the given path.
30153018

3019+
#### number HttpServer:GetPort()
3020+
Returns the port that was originally passed to `HttpServer:Start()`
3021+
3022+
#### string HttpServer:GetAddress()
3023+
Returns the address that was originally passed to `HttpServer:Start()`
3024+
3025+
#### string HttpServer:GetName()
3026+
Returns the name set by `HttpServer:SetName()`, defaults to `NONAME`
3027+
3028+
#### HttpServer:SetName(string name)
3029+
Sets the name of the HttpServer.
3030+
30163031
### Method Functions
30173032
All Method functions add a listener for the given path and the given method, like this:
30183033
```lua

source/modules/httpserver.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

127129
private:
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

139142
static 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+
681716
LUA_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+
713767
void 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

Comments
 (0)