lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

so, I'm trying to make a lua version of php's fopen("http://...";) and
python's urllib.urlopen(). Would be nice reuse the proxy handling,
handling of redirections etc in luasocket's http code.

So I thought I could make use of coroutines to and have a simple sink
that does coroutine.yield() and then make a read() function that
buffers things and give the data requested, read(size), read("*all")
read("*line") and afterward make a lines() functino for loops.

Here is the initial testcode with read() functiton that only gives
back the chunk from sink function:

http = require ("socket.http")

function openhttp(url, proxy)
        local h = {}
        h.co = coroutine.create(function()
                http.request{
                        url = url,
                        proxy = proxy,
                        sink = function(chunk)
                                coroutine.yield(chunk)
                        end
                }
        end)
        h.read = function(self, mode)
                local status, value = coroutine.resume(h.co)
                return value
        end
        return h
end

f = openhttp("http://tools.ietf.org/rfc/rfc2616.txt";)
print(f:read())

However, when i try to execute I get: attempt to yield across
metamethod/C-call boundary

Ideas on how to solve that? I tried to do the yield in the step
function too but got same result.

I suppose I could make a lua binding to netbsd's libfetch too but
would be nice use the existing stuff or extend luasocket to do this
too.

-- 
Natanael Copa