Downloading a file from the web
Luacurl is based on the libcurl library, which is extremely versatile and you can download and upload files in nearly every way possible.
init script
nothing of interest here, the same as usual
main script
The code defines two callback functions, which get called for chunks of incoming (downloaded) data. In this case, the data is just appended to a buffer. You could also write it to a file, if you want.
The curl stuff is set up then, by first creating a curl object (curl.new()). Curl objects do all the nasty stuff, and do only understand one command - "perform()". You tell them what exactly to do by using the setopt function. In this case, we want to download via http. The two callback functions get installed. The proxy options are there, too, if you need them.
If perform is successfull, it returns something different from nil. If an error occurs, it returns nil as the code and an error description as string in the err variable.
The rest is just concatenation of strings.
Documentation of the luacurl module can be found here.
The scripts need two of the standard libraries, the basic and the string library. The basic library for the "require" command, as always, and the string library contains the string.len function.
The scripts above are included with the plugin installer.
init script
nothing of interest here, the same as usual
require "luacurl"
main script
result_data = ""
result_header = ""
function data(stream,buf)
result_data = result_data .. buf
return string.len(buf)
end
function header(stream,buf)
result_header = result_header .. buf
return string.len(buf)
end
---- HTTP GET test ------------------------------------------------------------------
c = curl.new()
c:setopt(curl.OPT_URL,'http://www.lua.org')
c:setopt(curl.OPT_WRITEFUNCTION,data)
c:setopt(curl.OPT_HEADERFUNCTION,header)
--c:setopt(curl.OPT_PROXY,"localhost:3000")
--c:setopt(curl.OPT_PROXYUSERPWD,"ciccio:ciccio")
c:setopt(curl.OPT_HTTPHEADER,"User-Agent: curl (;;lua;;) luacurl")
code,err = c:perform()
if not code then return err end
result = "HEADER ("..string.len(result_header).." bytes):\n"..result_header
result = result.."DATA ("..string.len(result_data).." bytes):\n"..result_data
return resultThe code defines two callback functions, which get called for chunks of incoming (downloaded) data. In this case, the data is just appended to a buffer. You could also write it to a file, if you want.
The curl stuff is set up then, by first creating a curl object (curl.new()). Curl objects do all the nasty stuff, and do only understand one command - "perform()". You tell them what exactly to do by using the setopt function. In this case, we want to download via http. The two callback functions get installed. The proxy options are there, too, if you need them.
If perform is successfull, it returns something different from nil. If an error occurs, it returns nil as the code and an error description as string in the err variable.
The rest is just concatenation of strings.
Documentation of the luacurl module can be found here.
The scripts need two of the standard libraries, the basic and the string library. The basic library for the "require" command, as always, and the string library contains the string.len function.
The scripts above are included with the plugin installer.
