Skip to content

Instantly share code, notes, and snippets.

@ziogaschr
Last active December 19, 2015 00:09
Show Gist options
  • Save ziogaschr/5866532 to your computer and use it in GitHub Desktop.
Save ziogaschr/5866532 to your computer and use it in GitHub Desktop.
LUA: luacurl, fetch a file in memory
require("luacurl")
local c = curl.new()
function GET(url)
-- c:setopt(curl.OPT_VERBOSE, true) -- run curl in verbose mode
c:setopt(curl.OPT_URL, url)
c:setopt(curl.OPT_HEADER, true)
c:setopt(curl.OPT_CONNECTTIMEOUT, 3)
c:setopt(curl.OPT_TIMEOUT, 20)
c:setopt(curl.OPT_USERAGENT, "luacurl-agent/1.0")
local headerT = {} -- this will collect resulting chunks
c:setopt(curl.OPT_HEADERFUNCTION, function (param, buf)
table.insert(headerT, buf) -- store a chunk of data received
return #buf
end)
local bodyT = {} -- this will collect resulting chunks
c:setopt(curl.OPT_WRITEFUNCTION, function (param, buf)
table.insert(bodyT, buf) -- store a chunk of data received
return #buf
end)
local ok = c:perform() -- execute curl
c:close() -- close curl
local headers = table.concat( headerT, "")
local http, status, msg = string.match(headers, "(.-) (.-) (.-)\n")
local body = table.concat( bodyT, "")
return body, tonumber(status)
end
local res, status = GET("https://www.google.gr/images/srpr/logo4w.png")
return res, status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment