Skip to content

Commit aee78a5

Browse files
authored
Update it. (#1)
* Update main.cpp * Update compile.yml * Update main.cpp * Ooops * Update main.cpp * Update main.cpp * Update main.cpp * Update main.cpp * Update main.cpp * Update main.cpp * No windows compile for now. * Update main.cpp * Update main.cpp * Restructuring * Basic Lua stuff * Fixed compile errors. * Make make happy * More Compile errors. * Oops * Call it. * Small changes * Forgot to set the status. * Trying to find the cuase of a crash * Update util.h * Update httpserver.cpp * Update httpserver.cpp * Update httpserver.cpp * Update httpserver.cpp * Update httpserver.cpp * Update httpserver.cpp * Update httpserver.cpp * Update httpserver.cpp * Update httpserver.cpp * Update httpserver.cpp * Revert "Update httpserver.cpp" This reverts commit 7f2f14a. * Update httpserver.cpp * Found out how it works. * Update httpserver.cpp * Update httpserver.cpp * Fixed some things. * Update httpserver.cpp * Update httpserver.cpp * Testing * Added a check * Added some stuff + Bug fixes * Bug fixes + Added more functions + updated Readme * Reactivated Windoes + 64x Build + Small change to the Readme * Small changes * Update README.md * Make 64x happy * Update compile.yml * Update httpnet.lua * Update httpnet.lua
1 parent e5e43c3 commit aee78a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+687
-42027
lines changed

.github/workflows/compile.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Build
33
on: [push, pull_request, workflow_dispatch]
44

55
env:
6-
PROJECT_NAME: "httpnet"
6+
PROJECT_NAME: "httpserver"
77
BUILD_64x: "true"
88
REALM: "sv"
99
FINAL_REALM: "sv"
@@ -91,7 +91,7 @@ jobs:
9191
9292
- uses: actions/upload-artifact@v2
9393
with:
94-
name: gm${{env.FINAL_REALM}}_${{env.PROJECT_NAME}}_windows.dll
94+
name: gm${{env.FINAL_REALM}}_${{env.PROJECT_NAME}}_win32.dll
9595
path: projects/windows/vs2019/x86/ReleaseWithSymbols/gm${{env.REALM}}_${{env.PROJECT_NAME}}_win32.dll
9696

9797
- uses: actions/upload-artifact@v2

README.md

+81-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,82 @@
11
This Project aims to add a HTTPServer to Gmod.
2-
Currently, I'm testing around with it and it's not nearly finished.
2+
3+
This project uses the [cpp-httplib](https://github.com/yhirose/cpp-httplib) as the HTTPServer.
4+
# Functions
5+
## Basic Functions
6+
#### httpserver.Start(String IP, Number Port)
7+
This will start or restart the HTTP Server, and it will listen on the given address + port.
8+
NOTE: If a Method function was called like httpserver.Get after httpserver.Start was called, you need to call httpserver.Start again!
9+
#### httpserver.Stop()
10+
This stops the HTTP Server.
11+
#### (internal function!) httpserver.Think()
12+
This is internally used to manage all requests and to call all functions needed. (Never call this yourself.)
13+
14+
## Method Functions
15+
All Method functions add a listener for the given path and the given method, like this:
16+
```lua
17+
httpserver.Get("/", function(_, response
18+
print("GET request"
19+
response.SetContent("You sent a GET request.", "text/plain")
20+
end)
21+
```
22+
#### httpserver.Get(String path, function (Request, Response))
23+
#### httpserver.Put(String path, function (Request, Response))
24+
#### httpserver.Post(String path, function (Request, Response))
25+
#### httpserver.Patch(String path, function (Request, Response))
26+
#### httpserver.Delete(String path, function (Request, Response))
27+
#### httpserver.Options(String path, function (Request, Response))
28+
29+
## Additional Functions
30+
#### httpserver.IsRunning()
31+
Returns true if the HTTPServer is running.
32+
#### httpserver.SetTCPnodelay(bool nodelay)
33+
Sets whether a delay should be added to tcp or not.
34+
#### httpserver.SetReadTimeout(int sec, int usec)
35+
Sets the maximum amount of time before a read times out.
36+
#### httpserver.SetWriteTimeout(int sec, int usec)
37+
Sets the maximum amount of time before a write times out.
38+
#### httpserver.SetPayloadMaxLength(int maxlength)
39+
Sets the maximum payload length.
40+
#### httpserver.SetKeepAliveTimeout(int sec)
41+
Sets the maximum time a connection is kept alive.
42+
#### httpserver.SetKeepAliveMaxCount(int amount)
43+
Sets the maximum amount of connections that can be kept alive at the same time.
44+
#### httpserver.SetMountPoint(string path, string folder)
45+
This mounts the given folder to the given path.
46+
(You can call this multiple times for the same path to mount multiple folders to it.)
47+
#### httpserver.RemoveMountPoint(string path)
48+
This removes all mounts for the given path.
49+
50+
## Request
51+
#### Request.body
52+
The body of the HTTP Request.
53+
#### Request.remote_addr
54+
the IP Address of the Person who sent the HTTP Request
55+
#### Request.remote_port
56+
The Port the HTTP request was received from.
57+
#### Request.local_addr
58+
#### Request.local_port
59+
#### Request.method
60+
The HTTP Method that was used like GET or PUT.
61+
#### Request.authorization_count
62+
#### Request.content_length
63+
The length of the HTTP Request content.
64+
#### Request.HasHeader(key)
65+
returns true if the client has the given key in the header.
66+
#### Request.HasParam(key)
67+
returns true if the client has the given key in the parameters.
68+
#### Request.GetHeader(key)
69+
returns the value of the given key from the header.
70+
#### Request.GetParam(key)
71+
returns the value of the given key from the parameters.
72+
73+
## Response
74+
#### Response.SetContent(content, content-type)
75+
Sets the content like this:
76+
```lua
77+
Response.SetContent("Hello World", "text/plain")
78+
```
79+
#### Response.SetRedirect(url, code)
80+
Redirects one to the given URL and returns the given code.
81+
#### Response.SetHeader(key, value)
82+
Sets the given value for the given key in the header.

examples/httpnet.lua

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
--[[
2+
This Example shows an alternative way to network huge amounts of data, and it provides a small library to manage the data.
3+
This adds:
4+
httpnet.SetData(ply, key, data) -- Allow the client to download the given data by requesting the key.
5+
httpnet.GetData(ply, key) -- returns the data set for the given key.
6+
7+
(Clientside)
8+
httpnet.Download(key, callback) -- Make a GET request to download the given data.
9+
]]
10+
11+
if CLIENT then -- Put this code into a seperate file only the client executes.
12+
httpnet = {}
13+
httpnet.Port = 32039
14+
httpnet.Url = "http://" .. string.Split(game.GetIPAddress(), ":")[1] .. ":" .. httpnet.Port .. "/httpnet"
15+
function httpnet.Download(key, callback)
16+
if !key or !callback then return end
17+
HTTP({
18+
success = function(code, body)
19+
callback(body)
20+
end,
21+
failure = function(reason)
22+
print("HTTPNet Failed!", reason)
23+
end,
24+
method = "GET",
25+
url = httpnet.Url,
26+
parameters = {
27+
["httpnet"] = key
28+
}
29+
})
30+
end
31+
32+
net.Receive("HTTPNet_Example", function()
33+
httpnet.Download(net.ReadString(), function(body)
34+
PrintTable(util.JSONToTable(body))
35+
end)
36+
end)
37+
end
38+
39+
require("httpserver")
40+
41+
local ply_data = {}
42+
httpserver.Get("/httpnet", function(request, response)
43+
if request.HasParam("httpnet") then
44+
local tbl = ply_data[request.remote_addr]
45+
if tbl then
46+
response.SetContent(tbl[request.GetParam("httpnet")] or "", "text/plain")
47+
return
48+
end
49+
end
50+
51+
response.SetContent("", "text/plain")
52+
end)
53+
54+
httpserver.Start("0.0.0.0", 32039) -- Change the Port to one you have open & unused.
55+
56+
httpnet = {}
57+
function httpnet.SetData(ply, key, data)
58+
if !ply or ply == NULL then return end
59+
if istable(data) then
60+
data = util.TableToJSON(data)
61+
end
62+
63+
local ip = string.Split(ply:IPAddress(), ":")[1]
64+
if !ply_data[ip] then
65+
ply_data[ip] = {}
66+
end
67+
68+
ply_data[ip][key] = data
69+
end
70+
71+
function httpnet.GetData(ply, key)
72+
if !ply or ply == NULL or !key then return end
73+
return ply_data[string.Split(ply:IPAddress(), ":")[1]][key]
74+
end
75+
76+
util.AddNetworkString("HTTPNet_Example")
77+
concommand.Add("httpnet_example", function(ply)
78+
if !ply then return end -- Console. No.
79+
httpnet.SetData(ply, "Example", {
80+
["Hello"] = "World",
81+
["This"] = "is a Example"
82+
})
83+
net.Start("HTTPNet_Example")
84+
net.WriteString("Example")
85+
net.Send(ply)
86+
end)

examples/player_connected.lua

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require("httpserver")
2+
3+
httpserver.Get("/", function(request, response)
4+
for k, v in ipairs(player.GetAll()) do
5+
if string.Split(v:IPAddress(), ":")[1] == request.remote_addr then
6+
response.SetContent("You are connected to the Server.", "text/plain")
7+
return
8+
end
9+
end
10+
11+
response.SetContent("You are NOT connected to the Server.", "text/plain")
12+
end)
13+
14+
httpserver.Start("0.0.0.0", 32039) -- Change the Port to one you have open & unused.

premake5.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local gmcommon = assert(_OPTIONS.gmcommon or os.getenv("GARRYSMOD_COMMON"),
1212
"you didn't provide a path to your garrysmod_common (https://github.com/danielga/garrysmod_common) directory")
1313
include(gmcommon)
1414

15-
CreateWorkspace({name = "httpnet", abi_compatible = false})
15+
CreateWorkspace({name = "httpserver", abi_compatible = false})
1616
-- Serverside module (gmsv prefix)
1717
-- Can define "source_path", where the source files are located
1818
-- Can define "manual_files", which allows you to manually add files to the project,

0 commit comments

Comments
 (0)