Add: luci.http.splice to allow direct copying of data from a file
authorSteven Barth <steven@midlink.org>
Sat, 25 Jul 2009 07:27:05 +0000 (07:27 +0000)
committerSteven Barth <steven@midlink.org>
Sat, 25 Jul 2009 07:27:05 +0000 (07:27 +0000)
descriptor

libs/lucid-http/luasrc/lucid/http/server.lua
libs/sgi-cgi/luasrc/sgi/cgi.lua
libs/web/luasrc/http.lua

index 00c6b841610683843486aafd381ed275d63454c2..0fe947316635251c6f207c6c793514bd92324335 100644 (file)
@@ -510,7 +510,7 @@ function Server.process(self, client, env)
                                        headers["Content-Length"] = sourceout.len
                                end
                        end
-                       if not headers["Content-Length"] then
+                       if not headers["Content-Length"] and not close then
                                if message.env.SERVER_PROTOCOL == "HTTP/1.1" then
                                        headers["Transfer-Encoding"] = "chunked"
                                        sinkout = chunksink(client)
@@ -554,8 +554,15 @@ function Server.process(self, client, env)
 
                if sourceout and stat then
                        if util.instanceof(sourceout, IOResource) then
-                               stat, code, msg = sourceout.fd:copyz(client, sourceout.len)
-                       else
+                               if not headers["Transfer-Encoding"] then
+                                       stat, code, msg = sourceout.fd:copyz(client, sourceout.len)
+                                       sourceout = nil
+                               else
+                                       sourceout = sourceout.fd:blocksource(nil, sourceout.len)
+                               end
+                       end
+
+                       if sourceout then
                                stat, msg = ltn12.pump.all(sourceout, sinkout)
                        end
                end
index 15363a1a15340a9a015190424e5a30a594815c9e..f2c6f69574ed4e612c8196f89a72b16448071fac 100644 (file)
@@ -25,6 +25,7 @@ limitations under the License.
 ]]--
 module("luci.sgi.cgi", package.seeall)
 local ltn12 = require("luci.ltn12")
+require("nixio.util")
 require("luci.http")
 require("luci.sys")
 require("luci.dispatcher")
@@ -84,6 +85,8 @@ function run()
                                io.flush()
                                io.close()
                                active = false
+                       elseif id == 6 then
+                               data1:copyz(nixio.stdout, data2)
                        end
                end
        end
index bac9979522d6bfaa89291d636d90d15584531c1b..d34e253a53a8c9a550304270633b59e3770e81e0 100644 (file)
@@ -258,6 +258,13 @@ function write(content, src_err)
        end
 end
 
+--- Splice data from a filedescriptor to the client.
+-- @param fp   File descriptor
+-- @param size Bytes to splice (optional)
+function splice(fd, size)
+       coroutine.yield(6, fd, size)
+end
+
 --- Redirects the client to a new URL and closes the connection.
 -- @param url  Target URL
 function redirect(url)