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, 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 then c = 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
- Lua-cURL

I ended up with the following code in my utility module

-- handle luacurl and Lua-cURL
pcall(require, 'curl')
pcall(require, 'luacurl')
assert(curl, "curl library not found (luacurl or Lua-cURL)")
	local and or curl.easy_init()
	function get(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
		local ret = table.concat(t)
		return ret