* luci/httpd: Handle timeouts on socket writes correctly
authorSteven Barth <steven@midlink.org>
Sun, 29 Jun 2008 14:56:33 +0000 (14:56 +0000)
committerSteven Barth <steven@midlink.org>
Sun, 29 Jun 2008 14:56:33 +0000 (14:56 +0000)
libs/httpd/luasrc/httpd.lua
libs/httpd/luasrc/httpd/server.lua

index 201ece616bdebaa96ee901a34a5a6f2385ee5b84..19610195027afa0309fd1901073d977388059f5b 100644 (file)
@@ -55,6 +55,24 @@ function corecv(socket, ...)
        end
 end
 
+function cosend(socket, chunk, i, ...)
+       threadi[socket] = true
+       i = i or 1
+
+       while true do
+               local stat, err, sent = socket:send(chunk, i, ...)
+
+               if err ~= "timeout" then
+                       threadi[socket] = false
+                       return stat, err, sent
+               else
+                       i = sent and (sent + 1) or 1 
+               end
+               coroutine.yield()
+       end
+end
+
 function register(socket, s_clhandler, s_errhandler)
        table.insert(reading, socket)
        clhandler[socket] = s_clhandler
index 7bfac68629bbe0ac7a4e69c695ad68bed6d9b6bd..9ec8961b87f21d81b24e2ecf32b0f4fb1bedaeaf 100644 (file)
@@ -111,7 +111,8 @@ function Server.process( self, client )
 
        -- Setup sockets and sources
        local thread = {
-               receive = function(self, ...) return luci.httpd.corecv(client, ...) end
+               receive = function(self, ...) return luci.httpd.corecv(client, ...) end,
+               send = function(self, ...) return luci.httpd.cosend(client, ...) end
        }
        
        client:settimeout( 0 )
@@ -206,7 +207,7 @@ function Server.process( self, client )
                end
                
                
-               local sinkout = socket.sink(sinkmode, client)
+               local sinkout = socket.sink(sinkmode, thread)
                
                local header =
                        message.env.SERVER_PROTOCOL .. " " ..