@@ -35,11 +35,12 @@ struct HttpRequest {
3535
3636 bool bHandled = false ;
3737 bool bDelete = false ; // We only delete from the main thread.
38- int iFunction;
38+ int iFunction = - 1 ;
3939 std::string strPath;
4040 HttpResponse pResponseData;
4141 httplib::Response pResponse;
4242 httplib::Request pRequest;
43+ int pClientUserID = -1 ;
4344};
4445
4546enum
5152class HttpServer
5253{
5354public:
55+ ~HttpServer ()
56+ {
57+ if (!ThreadInMainThread ())
58+ {
59+ Warning (" holylib: Something deleted a HttpServer from another thread!\n " ); // Spooky leaking references.
60+ return ;
61+ }
62+
63+ if (!g_Lua)
64+ return ;
65+
66+ for (auto & ref : m_pHandlerReferences)
67+ g_Lua->ReferenceFree (ref);
68+
69+ m_pHandlerReferences.clear ();
70+ }
71+
5472 void Start (const char * address, unsigned short port);
5573 void Stop ();
5674 void Think ();
@@ -114,6 +132,7 @@ class HttpServer
114132 bool m_bInUpdate = false ;
115133 std::string m_strAddress;
116134 std::vector<HttpRequest*> m_pRequests;
135+ std::vector<int > m_pHandlerReferences; // Contains the Lua references to the handler functions.
117136 httplib::Server m_pServer;
118137};
119138
@@ -298,10 +317,30 @@ LUA_FUNCTION_STATIC(HttpRequest_GetContentLength)
298317 return 1 ;
299318}
300319
320+ LUA_FUNCTION_STATIC (HttpRequest_GetClient)
321+ {
322+ HttpRequest* pData = Get_HttpRequest (1 , false );
323+
324+ Push_CBaseClient (Util::GetClientByUserID (pData->pClientUserID ));
325+ return 1 ;
326+ }
327+
328+ LUA_FUNCTION_STATIC (HttpRequest_GetPlayer)
329+ {
330+ HttpRequest* pData = Get_HttpRequest (1 , false );
331+ CBaseClient* pClient = Util::GetClientByUserID (pData->pClientUserID );
332+
333+ Util::Push_Entity ((CBaseEntity*)(pClient ? Util::GetPlayerByClient (pClient) : NULL ));
334+ return 1 ;
335+ }
336+
301337void CallFunc (int func, HttpRequest* request, HttpResponse* response)
302338{
303339 Util::ReferencePush (g_Lua, func);
304340
341+ if (g_pHttpServerModule.InDebug ())
342+ Msg (" holylib: pushed handler function %i with type %i\n " , func, g_Lua->GetType (-1 ));
343+
305344 Push_HttpRequest (request);
306345 Push_HttpResponse (response);
307346
@@ -341,7 +380,7 @@ void HttpServer::Think()
341380 return ;
342381
343382 m_bInUpdate = true ;
344- for (auto it = m_pRequests.begin (); it != m_pRequests.end (); ++it )
383+ for (auto it = m_pRequests.begin (); it != m_pRequests.end ();)
345384 {
346385 auto pEntry = (*it);
347386 if (pEntry->bDelete )
@@ -353,6 +392,8 @@ void HttpServer::Think()
353392
354393 if (!pEntry->bHandled )
355394 CallFunc (pEntry->iFunction , pEntry, &pEntry->pResponseData );
395+
396+ ++it;
356397 }
357398
358399 m_bUpdate = false ;
@@ -363,35 +404,35 @@ static std::string localAddr = "127.0.0.1";
363404static std::string loopBack = " loopback" ;
364405httplib::Server::Handler HttpServer::CreateHandler (const char * path, int func, bool ipWhitelist)
365406{
407+ m_pHandlerReferences.push_back (func);
408+
366409 return [=](const httplib::Request& req, httplib::Response& res)
367410 {
368- if (ipWhitelist)
411+ int userID = -1 ;
412+ for (auto & pClient : Util::GetClients ())
369413 {
370- bool found = false ;
371- for (auto & pClient : Util::GetClients ())
414+ if (!pClient->IsActive ())
415+ continue ;
416+
417+ const netadr_s& addr = pClient->GetNetChannel ()->GetRemoteAddress ();
418+ std::string address = addr.ToString ();
419+ size_t port_pos = address.find (" :" );
420+ if (address.substr (0 , port_pos) == req.remote_addr || (req.remote_addr == localAddr && address.substr (0 , port_pos) == loopBack))
372421 {
373- if (!pClient->IsActive ())
374- continue ;
375-
376- const netadr_s& addr = pClient->GetNetChannel ()->GetRemoteAddress ();
377- std::string address = addr.ToString ();
378- size_t port_pos = address.find (" :" );
379- if (address.substr (0 , port_pos) == req.remote_addr || (req.remote_addr == localAddr && address.substr (0 , port_pos) == loopBack))
380- {
381- found = true ;
382- break ;
383- }
422+ userID = pClient->GetUserID ();
423+ break ;
384424 }
385-
386- if (!found)
387- return ;
388425 }
389426
427+ if (ipWhitelist && userID == -1 )
428+ return ;
429+
390430 HttpRequest* request = new HttpRequest;
391431 request->strPath = path;
392432 request->pRequest = req;
393433 request->iFunction = func;
394434 request->pResponse = res;
435+ request->pClientUserID = userID;
395436 m_pRequests.push_back (request); // We should add a check here since we could write to it from multiple threads?
396437 m_bUpdate = true ;
397438 while (!request->bHandled )
@@ -732,6 +773,9 @@ void CHTTPServerModule::LuaInit(bool bServerInit)
732773 Util::AddFunc (HttpRequest_GetMethod, " GetMethod" );
733774 Util::AddFunc (HttpRequest_GetAuthorizationCount, " GetAuthorizationCount" );
734775 Util::AddFunc (HttpRequest_GetContentLength, " GetContentLength" );
776+
777+ Util::AddFunc (HttpRequest_GetClient, " GetClient" );
778+ Util::AddFunc (HttpRequest_GetPlayer, " GetPlayer" );
735779 g_Lua->Pop (1 );
736780
737781 Util::StartTable ();
0 commit comments