diff options
| author | ame <[email protected]> | 2025-06-13 20:31:24 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2025-06-13 20:31:24 -0500 |
| commit | 2efda12fcae7869bc3cc8782dbcaceae65d251f8 (patch) | |
| tree | 9f380005409549d69eb0b6cf1f2a661dbb38c03d /docs | |
| parent | c6714b7212548a55de8ed9e22f4b416f312358b0 (diff) | |
clean net code and made net docs better
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/net/listen.md (renamed from docs/net.md) | 77 | ||||
| -rw-r--r-- | docs/net/websocket.md | 46 |
2 files changed, 74 insertions, 49 deletions
diff --git a/docs/net.md b/docs/net/listen.md index ca17da9..6b7f12c 100644 --- a/docs/net.md +++ b/docs/net/listen.md @@ -1,38 +1,25 @@ -# net
-
## listen (mostly IMPLEMENTED)
-'takes a function with 1 argument and a integer for a port
+net.listen(function, port)
(intentionally styled after expressjs:3)
the function will be ran on initilization, the argument has info on the server and functions to set it up
-**
-right now everything within a server:GET function is partially global, it can read global variables (by making a copy),
-it can not read/copy local variables or modify globals
-**
-
-allows a third optional argument that offers some other options in a table format
-
|name|default value|extra info|
|--|--|--|
-|mime.types.file**|/etc/mime.types|formated similarly to [this](https://wiki.debian.org/MIME/etc/mime.types)|
-|max.connections**|64|maximum number of connections that can be opened at the same time, will respond with error(503)|
-|max.header.size**|1<<20 (1048576)|max header size before refusing connection with error(431)|
-|max.uri.size**|idk yet|maximum uri size before refusing request with error(414)|
-|max.request.timeout**|idk yet|maximum time server will wait for request to be parsed|
+|mimetypes|/etc/mime.types|file used to auto assign content-type when using res:sendfile, nil to skip|
+|max_connections**|64|maximum number of connections that can be opened at the same time, will respond with error(503)|
+|max_header**|1<<20 (1048576)|max header size before refusing connection with error(431)|
+|max_uri**|idk yet|maximum uri size before refusing request with error(414)|
the server will send these codes for these reasons
|code|cause|
|--|--|
-|503|too many current requests, more than max.connections|
+|503|too many current requests, more than max_connections|
|500|anytime a server route crashes|
-|431|header is larger than max header size, more than max.header.size|
-|414|request uri is longer than max.uri.size|
-|408**|request took too long to read all of (recv timeout) than max.request.timeout|
-
-(more to come?**)
+|431|header is larger than max header size, more than header_size|
+|414|request uri is longer than max_uri|
```lua
llib.net.listen(function(server)
@@ -43,19 +30,7 @@ end, 80) ```lua
llib.net.listen(function(server)
...
-end, 80, {["mime.types.file"] = "/etc/mime.types"})
-```
-
-### server:lock server:unlock
-
-continues on the current thread, but pauses all other threads at that point
-
-```lua
-...
-server:lock()
---do something with a global
-server:unlock()
-...
+end, 80)
```
### server:close **
@@ -64,7 +39,7 @@ closes server ### server:GET/POST/...
-'takes a string (the path) and a function to be ran in the background on request
+server:GET(path, function)
the function has 2 arguments, the first (res) contains functions and info about resolving the request,
the second (req) contains info on the request, the path allows for wildcards, multiple get requests per path is allowed
@@ -94,40 +69,44 @@ end) #### res:write
-'takes a string
+res:write(content)
sends the string to the client, constructs a header on first call (whether or not res.header._sent is null)
(the constructed header can not be changed later on in the request*), and sends the string without closing the client
```lua
-...
res:write("<h1>hello world</h1>")
res:write("<h1>good bye world</h1>")
-...
```
*well it can but it wont do anything
#### res:send
-'takes a string
+res:send(content)
sends the string to the client, constructs a header then closes client_fd
```lua
-...
res:send("<h1>hello world</h1>")
-...
```
+functionaly identical to res:write and res:close
+
#### res:close
+res:close()
+
closes connection, sets res.client_fd to -1, any calls that use this value will fail
this will still run any selected functions!
+this is called automatically when there are no more function
+
#### res:stop
+res:stop()
+
prevents all further selected functions from running
#### res.header
@@ -137,9 +116,8 @@ table containing all head information, anything added to it will be used, certai |key|side effect|
|--|--|
|Code|Changes response note, ie: (200: OK)|
-
+|Content-Type|this is changed automatically with res:sendfile|
```lua
-...
res.header["Code"] = 404
res.header["test"] = "wowa"
-- new header will have a code of 404 (at the top duh)
@@ -148,16 +126,15 @@ res.header["test"] = "wowa" -- HTTP/1.1 404 Not Found
-- ...
-- test: wowa
-...
```
### res:sendfile
-'takes one string, which is a path that will be served, must be a file
+res:sendfile(filepath, options?)
res.header["Content-Type"] is set automatically depending on the file extention, using /etc/mime.types, or whatever option was supplied to listen (see listen options)
-option table
+options table
|key|value|effect|
|--|--|--|
@@ -166,9 +143,7 @@ option table ```lua
-...
res:sendfile("./html/index.html")
-...
```
### req.parameters
@@ -193,11 +168,15 @@ these can, of course be used with wildcards however you want ### req:roll
-'takes an integer of bytes to read & parse (optional, otherwise the max amount of bytes will be read)
+req:roll(bytes?)
+
+when bytes is null it will read as much as possible (may not be the full request)
will update req according to how the bytes needed to be parsed, returns the number of bytes read (not necessarily parsed), 0 if there
is no more ready data, -1 if all data has been read, and any other values \< -1 is a recv error (add 1 to the error code)
+> waiting on a rewrite for this, all that will be changed will be how errors are returned
+
```lua
--when a request contains "hello world"
req.Body --"hello"
diff --git a/docs/net/websocket.md b/docs/net/websocket.md new file mode 100644 index 0000000..5a82a70 --- /dev/null +++ b/docs/net/websocket.md @@ -0,0 +1,46 @@ +## ws + +net.wss(url) +net.ws(url)** + +both function identically, wss just uses openssl over socket + +will call each other when the url protocol mismatches the function + +can return an error + +```lua +net.wss("amyy.cc") -- connects to wss://amyy.cc +net.wss("ws://amyy.cc") -- identical to net.wss("amyy.cc" +``` + +```lua +local con = new.ws("amyy.cc") + +while true do + local frame = con:read() + print(frame.content) +end +``` + +### con:read + +con:read() + +reads the oldest unread frame from the server or wait for the next frame + +can return an error + +return table has the frame content (.content) and opcode (.opcode) + +### con:write + +con:write(content) + +sends a frame, returns nil or an error + +### con:close + +con:close() + +calls __gc early |
