lua-users home
lua-l archive

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


On Fri, 2010-10-22 at 16:16 +1000, pj@pjb.com.au wrote:
> P.S.  my function so far, but untested with the non-freepops curl:
>   function wget(url)
>     local text = {}
>     local function WriteMemoryCallback(s)
>         text[#text+1] = s
>         return string.len(s)
>     end
>     local c
>     if curl.new then c = curl.new() else c = curl.easy_init() end
>     c:setopt(curl.OPT_URL, url)
>     c:setopt(curl.OPT_WRITEFUNCTION, WriteMemoryCallback)
>     c:setopt(curl.OPT_USERAGENT, "luacurl-agent/1.0")
>     c:perform()
>     if curl.close then c:close() end
>     return table.concat(text,'')
>   end

I was also bitten by the use of several curl bindings:
- luacurl http://luaforge.net/projects/luacurl/
- Lua-cURL http://luaforge.net/projects/lua-curl/

I ended up with the following code in my utility module
http://github.com/mkottman/wdm



-- handle luacurl and Lua-cURL
pcall(require, 'curl')
pcall(require, 'luacurl')
assert(curl, "curl library not found (luacurl or Lua-cURL)")
...
do
	local c=curl.new and curl.new() or curl.easy_init()
	function get(url)
		c:setopt(curl.OPT_URL,url)
		local t = {}
		c:setopt(curl.OPT_WRITEFUNCTION, function (a, b)
			local s
			-- luacurl and Lua-cURL friendly
			if type(a) == "string" then s = a else s = b end
			table.insert(t, s)
			return #s
		end)
		assert(c:perform())
		local ret = table.concat(t)
		return ret
	end
end