curl.lua
function build_w_cb(t)
return function(s,len)
stores the received data in the table t
table.insert(t,s)
return number_of_byte_served, error_message
number_of_byte_served ~= len is an error
return len,nil
end
end
This cb factory stores all the chunks received in table t. you can have the
whole file back using table.concat(t). The callback for
curl.OPT_READFUNCTION takes a number, the number of the maximum amount of
data that can return. Here a simple example of an infinite stream of '#':
function build_r_cb()
return function(n)
local s = string.rep("#",n)
return size_of_data,data
size_of_data = 0 means end of data
size_of_data must be <= n
return string.len(s),s
end
end
curl_slist are mapped to lua tables {"item1","item2",...}. For example
curl.OPT_HTTPHEADER may be called in this way:
c:setopt(curl.OPT_HTTPHEADER,{"Referer: my_home!","Dummy-field: hello"})
Last important case is the curl.OPT_HTTPPOST for which a special syntax is
supported:
c:setopt(curl.OPT_HTTPPOST,{
{curl.FORM_COPYNAME,"name1",
curl.FORM_COPYCONTENTS,"data1",
curl.FORM_CONTENTTYPE,"Content-type: text/plain",
curl.FORM_END }
,
{curl.FORM_COPYNAME,"name2",
curl.FORM_COPYCONTENTS,"data2",
curl.FORM_CONTENTTYPE,"Content-type: text/plain",
curl.FORM_END}
})
And now a simple example of an HTTP GET with a proxy:
gl_b = {}
gl_h = {}
c = curl.easy_init()
c:setopt(curl.OPT_URL,'http://tassi.web.cs.unibo.it/cgi-bin/stats.cgi')
c:setopt(curl.OPT_WRITEFUNCTION,build_w_cb(gl_b))
c:setopt(curl.OPT_HEADERFUNCTION,build_w_cb(gl_h))
c:setopt(curl.OPT_PROXY,"localhost:3000")
c:setopt(curl.OPT_PROXYUSERPWD,"ciccio:ciccio")
print("\n\t$$ performing... returns code,error $$\n")
print(c:perform())
print("\n\t$$ here you see the header, line per line $$\n")
table.foreach(gl_h,function(i,s) print(i.."= "..string.gsub(s,"\n","")) end)
print("\n\t$$ and here you see the data, after concatenation of "..
table.getn(gl_b).." chunks $$\n")
print(table.concat(gl_b))
Create a curl handler.
curl.escape
(s)
URLencodes the string.
curl.unescape
(s)
URLdecodes the string.
curl.version
()
version.
version informations.
perform
()
Starts curl.
setopt
(opt,data)
The setopt function.
curl.easy_init
()
curl.escape
(s)
curl.unescape
(s)
curl.version
()
perform
()
setopt
(opt,data)
opt | number one of curl.OPT_* |