2023-08-28 11:36:36 (UTC-03:00)
Marcel Rodrigues <marcelgmr@gmail.com>
tcp: add logs for debugging
diff --git a/lib/ludweb/http.lua b/lib/ludweb/http.lua index c07e1ee..ed07534 100644 --- a/lib/ludweb/http.lua +++ b/lib/ludweb/http.lua @@ -148,8 +148,18 @@ local function new_http() local req = parse_request(datain) local length = req.headers["content-length"] if length == nil then + io.stderr:write("no content-length: ready\n") return true else + local got = #req.payload + local need = tonumber(length) + if got < need then + io.stderr:write("not ready: "..got.." < "..need.."\n") + elseif got > need then + io.stderr:write("super ready: "..got.." > "..need.."\n") + else + io.stderr:write("ready: "..got.." == "..need.."\n") + end return #req.payload >= tonumber(length) end end @@ -158,6 +168,7 @@ local function new_http() return "", true end local req = parse_request(datain) + io.stderr:write("path: "..req.path.."\n") local resp, status, reason, cookies = obj:process(req) if resp == nil then return nil end if type(resp) == "string" then @@ -180,9 +191,9 @@ local function new_http() end local keep_alive = req.headers.connection ~= "close" -- randomly close some connections to save resources - if keep_alive and math.random(32) == 1 then - keep_alive = false - end + --~ if keep_alive and math.random(32) == 1 then + --~ keep_alive = false + --~ end local dataout = build_response(resp, keep_alive) return dataout, keep_alive end diff --git a/lib/ludweb/tcp.lua b/lib/ludweb/tcp.lua index c3aa37a..d939919 100644 --- a/lib/ludweb/tcp.lua +++ b/lib/ludweb/tcp.lua @@ -70,6 +70,11 @@ end local TCP = {} TCP.__index = TCP +function TCP:log(msg) + local ts = os.date("%F %T") + io.stderr:write(ts.." [TCP] "..msg.."\n") +end + function TCP:init(port) local hints = ffi.new("struct addrinfo[1]") hints[0].ai_family = C.AF_UNSPEC @@ -117,14 +122,19 @@ function TCP:run() local buffer = ffi.new("char ["..buflen.."]") local datain = {} local running = true + self:log("loop start") while running do + self:log("epoll_wait()") nfds = C.epoll_wait(efd, evs, curfds, -1) + self:log("returned "..nfds) -- the loop below will run zero times if nfds < 1, ignoring errors for n = 0, nfds-1 do local readyfd = evs[n].data.fd if readyfd == self.sockfd then while true do + self:log("accept("..readyfd..")") local newfd = C.accept(readyfd, nil, nil) + self:log("returned "..newfd) if newfd == -1 then break end set_non_blocking(newfd) ev[0].events = bit.bor(C.EPOLLIN, C.EPOLLET) @@ -134,8 +144,11 @@ function TCP:run() end else local can_close = false + local requests = {} while true do + self:log("recv("..readyfd..")") local size = C.recv(readyfd, buffer, buflen, 0) + self:log("returned "..tostring(size)) if size == -1 then break elseif size == 0 then @@ -143,21 +156,31 @@ function TCP:run() break else datain[readyfd] = (datain[readyfd] or "") .. ffi.string(buffer, size) + local last = datain[readyfd]:sub(-16):gsub("\r?\n", "\\n") + self:log("last 16 bytes: "..last) if self:request_ready(datain[readyfd]) then - local dataout, keep_alive = self:process(datain[readyfd]) + table.insert(requests, datain[readyfd]) datain[readyfd] = "" - if dataout == nil then - running = false - else - C.send(readyfd, dataout, #dataout, 0) - end - if not keep_alive then - can_close = true - end end end end + if #requests > 0 then + self:log("got "..#requests.." requests") + end + for _, request in ipairs(requests) do + local dataout, keep_alive = self:process(request) + if dataout == nil then + running = false + else + self:log("send("..readyfd..", dataout, "..#dataout..", 0)") + C.send(readyfd, dataout, #dataout, 0) + end + if not keep_alive then + can_close = true + end + end if can_close then + self:log("close("..readyfd..")") C.epoll_ctl(efd, C.EPOLL_CTL_DEL, readyfd, ev) C.shutdown(readyfd, C.SHUT_RDWR) C.close(readyfd) @@ -166,6 +189,7 @@ function TCP:run() end end end + self:log("loop end") C.shutdown(self.sockfd, C.SHUT_RDWR) C.close(self.sockfd) end