|
1 | 1 | 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. |
0 commit comments