login

<     >

2023-07-14 18:47:43 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

don't shutdown socket on keep-alive mode

diff --git a/lib/ludweb/http.lua b/lib/ludweb/http.lua
index 2afa5f4..1c7327c 100644
--- a/lib/ludweb/http.lua
+++ b/lib/ludweb/http.lua
@@ -112,7 +112,9 @@ local function new_http()
         if dataout == nil then
             return nil
         end
-        return build_response(dataout, status, reason, cookies)
+        local resp = build_response(dataout, status, reason, cookies)
+        local keep_alive = req.headers.connection ~= "close"
+        return resp, keep_alive
     end
     return obj
 end

diff --git a/lib/ludweb/tcp.lua b/lib/ludweb/tcp.lua
index 5b60b57..7f48cfe 100644
--- a/lib/ludweb/tcp.lua
+++ b/lib/ludweb/tcp.lua
@@ -134,16 +134,18 @@ function TCP:run()
                 curfds = curfds + 1
             else
                 C.recv(evs[n].data.fd, buffer, buflen, 0)
-                local data = self:process(ffi.string(buffer))
+                local data, keep_alive = self:process(ffi.string(buffer))
                 if data == nil then
                     running = false
                 else
                     C.send(evs[n].data.fd, data, #data, 0)
                 end
-                C.epoll_ctl(efd, C.EPOLL_CTL_DEL, evs[n].data.fd, ev)
-                C.shutdown(evs[n].data.fd, C.SHUT_RDWR)
-                curfds = curfds - 1
-                C.close(evs[n].data.fd)
+                if not keep_alive then
+                    C.epoll_ctl(efd, C.EPOLL_CTL_DEL, evs[n].data.fd, ev)
+                    C.shutdown(evs[n].data.fd, C.SHUT_RDWR)
+                    curfds = curfds - 1
+                    C.close(evs[n].data.fd)
+                end
             end
         end
     end