Skip to content

Commit a84e255

Browse files
committed
repo: update from holylib
1 parent e5d688f commit a84e255

File tree

6 files changed

+88
-56
lines changed

6 files changed

+88
-56
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,25 @@ Returns true if the HTTPServer is running.
6464
#### HttpServer:SetTCPnodelay(bool nodelay)
6565
Sets whether a delay should be added to tcp or not.
6666

67-
#### HttpServer:SetReadTimeout(int sec, int usec)
67+
#### HttpServer:SetReadTimeout(number sec, number usec)
6868
Sets the maximum amount of time before a read times out.
6969

70-
#### HttpServer:SetWriteTimeout(int sec, int usec)
70+
#### HttpServer:SetWriteTimeout(number sec, number usec)
7171
Sets the maximum amount of time before a write times out.
7272

73-
#### HttpServer:SetPayloadMaxLength(int maxlength)
73+
#### HttpServer:SetPayloadMaxLength(number maxlength)
7474
Sets the maximum payload length.
7575

76-
#### HttpServer:SetKeepAliveTimeout(int sec)
76+
#### HttpServer:SetKeepAliveTimeout(number sec)
7777
Sets the maximum time a connection is kept alive.
7878

79-
#### HttpServer:SetKeepAliveMaxCount(int amount)
79+
#### HttpServer:SetKeepAliveMaxCount(number amount)
8080
Sets the maximum amount of connections that can be kept alive at the same time.
8181

82+
#### HttpServer:SetThreadSleep(number sleepTime)
83+
The number of ms threads sleep before checking again if a request was handled.
84+
Useful to raise it when you let requests wait for a while.
85+
8286
#### HttpServer:SetMountPoint(string path, string folder)
8387
This mounts the given folder to the given path.
8488
(You can call this multiple times for the same path to mount multiple folders to it.)

source/httpserver.cpp

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ void CallFunc(int func, HttpRequest* request, HttpResponse* response)
1111
Push_HttpRequest(request);
1212
Push_HttpResponse(response);
1313

14-
g_Lua->PCall(2, 0, 0);
14+
if (g_Lua->CallFunctionProtected(2, 1, true))
15+
{
16+
request->bHandled = !g_Lua->GetBool(-1);
17+
g_Lua->Pop(1);
18+
}
1519

1620
Delete_HttpRequest(request);
1721
Delete_HttpResponse(response); // Destroys the Lua reference after we used it
@@ -43,27 +47,32 @@ void HttpServer::Think()
4347
return;
4448

4549
m_bInUpdate = true;
46-
for (auto it = m_pRequests.begin(); it != m_pRequests.end(); ++it) {
50+
for (auto it = m_pRequests.begin(); it != m_pRequests.end(); ++it)
51+
{
4752
auto pEntry = (*it);
48-
if (pEntry->bHandled) { continue; }
49-
if (pEntry->bDelete) {
53+
if (pEntry->bDelete)
54+
{
5055
it = m_pRequests.erase(it);
5156
delete pEntry;
5257
continue;
5358
}
5459

55-
CallFunc(pEntry->iFunction, pEntry, &pEntry->pResponseData);
56-
pEntry->bHandled = true;
60+
if (!pEntry->bHandled)
61+
CallFunc(pEntry->iFunction, pEntry, &pEntry->pResponseData);
5762
}
5863

5964
m_bUpdate = false;
6065
m_bInUpdate = false;
6166
}
6267

63-
httplib::Server::Handler HttpServer::CreateHandler(const char* path, int func, bool ipwhitelist)
68+
static std::string localAddr = "127.0.0.1";
69+
static std::string loopBack = "loopback";
70+
httplib::Server::Handler HttpServer::CreateHandler(const char* path, int func, bool ipWhitelist)
6471
{
65-
return [=](const httplib::Request& req, httplib::Response& res) {
66-
if (ipwhitelist) {
72+
return [=](const httplib::Request& req, httplib::Response& res)
73+
{
74+
if (ipWhitelist)
75+
{
6776
bool found = false;
6877
for (auto& pClient : Util::GetClients())
6978
{
@@ -73,13 +82,15 @@ httplib::Server::Handler HttpServer::CreateHandler(const char* path, int func, b
7382
const netadr_s& addr = pClient->GetNetChannel()->GetRemoteAddress();
7483
std::string address = addr.ToString();
7584
size_t port_pos = address.find(":");
76-
if (address.substr(0, port_pos) == req.remote_addr || (req.remote_addr == "127.0.0.1" && address.substr(0, port_pos) == "loopback")) {
85+
if (address.substr(0, port_pos) == req.remote_addr || (req.remote_addr == localAddr && address.substr(0, port_pos) == loopBack))
86+
{
7787
found = true;
7888
break;
7989
}
8090
}
8191

82-
if (!found) { return; }
92+
if (!found)
93+
return;
8394
}
8495

8596
HttpRequest* request = new HttpRequest;
@@ -89,23 +100,19 @@ httplib::Server::Handler HttpServer::CreateHandler(const char* path, int func, b
89100
request->pResponse = res;
90101
m_pRequests.push_back(request); // We should add a check here since we could write to it from multiple threads?
91102
m_bUpdate = true;
92-
while (!request->bHandled) {
93-
ThreadSleep(1);
94-
}
103+
while (!request->bHandled)
104+
ThreadSleep(m_iThreadSleep);
105+
95106
HttpResponse* rdata = &request->pResponseData;
96-
if (rdata->bSetContent) {
107+
if (rdata->bSetContent)
97108
res.set_content(rdata->strContent, rdata->strContentType);
98-
}
99109

100-
if (rdata->bSetRedirect) {
110+
if (rdata->bSetRedirect)
101111
res.set_redirect(rdata->strRedirect, rdata->iRedirectCode);
102-
}
103112

104-
if (rdata->bSetHeader) {
105-
for (auto& [key, value] : rdata->pHeaders) {
113+
if (rdata->bSetHeader)
114+
for (auto& [key, value] : rdata->pHeaders)
106115
res.set_header(key, value);
107-
}
108-
}
109116

110117
request->bDelete = true;
111118
};

source/httpserver.h

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#include "GarrysMod/Lua/Interface.h"
2-
#include "unordered_map"
31
#include "httplib.h"
2+
#include "unordered_map"
43

54
struct HttpResponse {
65
bool bSetContent = false;
@@ -14,8 +13,10 @@ struct HttpResponse {
1413
};
1514

1615
struct HttpRequest {
16+
~HttpRequest();
17+
1718
bool bHandled = false;
18-
bool bDelete = false;
19+
bool bDelete = false; // We only delete from the main thread.
1920
int iFunction;
2021
std::string strPath;
2122
HttpResponse pResponseData;
@@ -48,47 +49,49 @@ class HttpServer
4849
return 0;
4950
}
5051

51-
void Get(const char* path, int func, bool ipwhitelist)
52+
void Get(const char* path, int func, bool ipWhitelist)
5253
{
53-
m_pServer.Get(path, CreateHandler(path, func, ipwhitelist));
54+
m_pServer.Get(path, CreateHandler(path, func, ipWhitelist));
5455
}
5556

56-
void Post(const char* path, int func, bool ipwhitelist)
57+
void Post(const char* path, int func, bool ipWhitelist)
5758
{
58-
m_pServer.Post(path, CreateHandler(path, func, ipwhitelist));
59+
m_pServer.Post(path, CreateHandler(path, func, ipWhitelist));
5960
}
6061

61-
void Put(const char* path, int func, bool ipwhitelist)
62+
void Put(const char* path, int func, bool ipWhitelist)
6263
{
63-
m_pServer.Put(path, CreateHandler(path, func, ipwhitelist));
64+
m_pServer.Put(path, CreateHandler(path, func, ipWhitelist));
6465
}
6566

66-
void Patch(const char* path, int func, bool ipwhitelist)
67+
void Patch(const char* path, int func, bool ipWhitelist)
6768
{
68-
m_pServer.Patch(path, CreateHandler(path, func, ipwhitelist));
69+
m_pServer.Patch(path, CreateHandler(path, func, ipWhitelist));
6970
}
7071

71-
void Delete(const char* path, int func, bool ipwhitelist)
72+
void Delete(const char* path, int func, bool ipWhitelist)
7273
{
73-
m_pServer.Delete(path, CreateHandler(path, func, ipwhitelist));
74+
m_pServer.Delete(path, CreateHandler(path, func, ipWhitelist));
7475
}
7576

76-
void Options(const char* path, int func, bool ipwhitelist)
77+
void Options(const char* path, int func, bool ipWhitelist)
7778
{
78-
m_pServer.Options(path, CreateHandler(path, func, ipwhitelist));
79+
m_pServer.Options(path, CreateHandler(path, func, ipWhitelist));
7980
}
8081

81-
httplib::Server::Handler CreateHandler(const char*, int, bool);
82+
httplib::Server::Handler CreateHandler(const char* path, int func, bool ipWhitelist);
8283

8384
public:
8485
httplib::Server& GetServer() { return m_pServer; };
8586
unsigned char GetStatus() { return m_iStatus; };
8687
std::string& GetAddress() { return m_strAddress; };
8788
unsigned short GetPort() { return m_iPort; };
89+
void SetThreadSleep(unsigned int threadSleep) { m_iThreadSleep = threadSleep; };
8890

8991
private:
9092
unsigned char m_iStatus = HTTPSERVER_OFFLINE;
9193
unsigned short m_iPort;
94+
unsigned int m_iThreadSleep = 5; // How long the threads sleep / wait for a request to be handled
9295
bool m_bUpdate = false;
9396
bool m_bInUpdate = false;
9497
std::string m_strAddress;

source/lua.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,23 @@ LUA_FUNCTION_STATIC(HttpServer_SetReadTimeout)
165165
{
166166
HttpServer* pServer = Get_HttpServer(1, true);
167167
pServer->GetServer().set_read_timeout((time_t)LUA->CheckNumber(2), (time_t)LUA->CheckNumber(3));
168+
168169
return 0;
169170
}
170171

171172
LUA_FUNCTION_STATIC(HttpServer_SetWriteTimeout)
172173
{
173174
HttpServer* pServer = Get_HttpServer(1, true);
174175
pServer->GetServer().set_write_timeout((time_t)LUA->CheckNumber(1), (time_t)LUA->CheckNumber(2));
176+
175177
return 0;
176178
}
177179

178180
LUA_FUNCTION_STATIC(HttpServer_SetPayloadMaxLength)
179181
{
180182
HttpServer* pServer = Get_HttpServer(1, true);
181183
pServer->GetServer().set_payload_max_length((size_t)LUA->CheckNumber(2));
184+
182185
return 0;
183186
}
184187

@@ -233,18 +236,18 @@ LUA_FUNCTION_STATIC(HttpServer_Stop)
233236
return 0;
234237
}
235238

236-
LUA_FUNCTION_STATIC(httpserver_Create)
239+
LUA_FUNCTION_STATIC(HttpServer_SetThreadSleep)
237240
{
238-
Push_HttpServer(new HttpServer);
239-
return 1;
241+
HttpServer* pServer = Get_HttpServer(1, true);
242+
pServer->SetThreadSleep((unsigned int)LUA->CheckNumber(2));
243+
244+
return 0;
240245
}
241246

242-
LUA_FUNCTION_STATIC(httpserver_Think)
247+
LUA_FUNCTION_STATIC(httpserver_Create)
243248
{
244-
for (auto& [httpserver, _] : g_pPushedHttpServer)
245-
httpserver->Think();
246-
247-
return 0;
249+
Push_HttpServer(new HttpServer);
250+
return 1;
248251
}
249252

250253
LUA_FUNCTION_STATIC(httpserver_Destroy)
@@ -260,6 +263,14 @@ LUA_FUNCTION_STATIC(httpserver_Destroy)
260263
return 0;
261264
}
262265

266+
LUA_FUNCTION_STATIC(httpserver_Think)
267+
{
268+
for (auto& [httpserver, _] : g_pPushedHttpServer)
269+
httpserver->Think();
270+
271+
return 0;
272+
}
273+
263274
void LUA_InitServer(GarrysMod::Lua::ILuaBase* LUA)
264275
{
265276
HttpRequest_LuaInit();
@@ -281,6 +292,7 @@ void LUA_InitServer(GarrysMod::Lua::ILuaBase* LUA)
281292
Util::AddFunc(HttpServer_SetPayloadMaxLength, "SetPayloadMaxLength");
282293
Util::AddFunc(HttpServer_SetKeepAliveTimeout, "SetKeepAliveTimeout");
283294
Util::AddFunc(HttpServer_SetKeepAliveMaxCount, "SetKeepAliveMaxCount");
295+
Util::AddFunc(HttpServer_SetThreadSleep, "SetThreadSleep");
284296

285297
Util::AddFunc(HttpServer_SetMountPoint, "SetMountPoint");
286298
Util::AddFunc(HttpServer_RemoveMountPoint, "RemoveMountPoint");

source/requestdata.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ static int HttpResponse_TypeID = -1;
44
PushReferenced_LuaClass(HttpResponse, HttpResponse_TypeID)
55
Get_LuaClass(HttpResponse, HttpResponse_TypeID, "HttpResponse")
66

7+
static int HttpRequest_TypeID = -1;
8+
PushReferenced_LuaClass(HttpRequest, HttpRequest_TypeID)
9+
Get_LuaClass(HttpRequest, HttpRequest_TypeID, "HttpRequest")
10+
11+
HttpRequest::~HttpRequest()
12+
{
13+
Delete_HttpRequest(this);
14+
Delete_HttpResponse(&this->pResponseData);
15+
}
16+
717
LUA_FUNCTION_STATIC(HttpResponse__tostring)
818
{
919
HttpResponse* pData = Get_HttpResponse(1, false);
@@ -75,10 +85,6 @@ LUA_FUNCTION_STATIC(HttpResponse_SetHeader)
7585
return 0;
7686
}
7787

78-
static int HttpRequest_TypeID = -1;
79-
PushReferenced_LuaClass(HttpRequest, HttpRequest_TypeID)
80-
Get_LuaClass(HttpRequest, HttpRequest_TypeID, "HttpRequest")
81-
8288
LUA_FUNCTION_STATIC(HttpRequest__tostring)
8389
{
8490
HttpRequest* pData = Get_HttpRequest(1, false);

source/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include "LuaInterface.h"
43
#include "httpserver.h"
4+
#include "LuaInterface.h"
55
#include <string>
66
#include "lua.h"
77
#include <vector>

0 commit comments

Comments
 (0)