Go to file
Vadim A. Misbakh-Soloviov 673152f9e6 Makefile improvements
Signed-off-by: Vadim A. Misbakh-Soloviov <mva@mva.name>
2014-09-10 00:35:34 +07:00
.travis Add. Travis files 2014-08-26 12:40:18 +05:00
doc Update doc [ci skip] 2014-09-05 16:23:32 +05:00
examples Add. pause examples. 2014-09-05 13:15:59 +05:00
msvc Implement `setopt_proxytype` in Lua-cURL interface. 2014-08-29 15:06:36 +05:00
rockspecs Add. Install `lcurl.cURL` module in rockspec 2014-08-29 12:29:23 +05:00
src Merge branch 'master' of https://github.com/moteus/lua-lcurl 2014-09-08 14:25:37 +05:00
test Add. Note about test fail. [ci skip] 2014-09-08 13:28:47 +04:00
.config Makefile improvements 2014-09-10 00:35:34 +07:00
.gitignore Update gitignore 2014-08-29 14:02:45 +05:00
.travis.yml Update test 2014-09-08 10:22:04 +05:00
LICENSE Initial commit 2014-08-25 14:00:15 +05:00
Makefile Makefile improvements 2014-09-10 00:35:34 +07:00
README.md Update README.md 2014-09-08 15:58:24 +05:00
lakeconfig.lua Add. lakefile 2014-08-28 14:52:46 +05:00
lakefile Add. `easy:pause()` method 2014-09-05 11:29:08 +05:00

README.md

Lua binding to libcurl

Build Status Coverage Status Licence

Documentation

API
Also library provide lcurl.cURL compatibility module for Lua-cURL binding.

Why one more curl binding

Existing Lua-cURL binding has several problems:

  • it can not return error codes but just raise Lua errors
  • it raise Lua error from callback that may result resource leak in libcurl
  • it does not provide building multipart/formdata explicitly
  • it has memory leak when send multipart/formdata
  • it does not save string for curl options that may result crush in libcurl
  • there no way to get result for operations in multi interface (e.g. if one of easy operation fail you can not get result code/error message)
  • you can not use multi interface for upload operation
  • you can not use your own callback function to perform operation with multi interface
  • you can not pass your context to callback functions

Installation

luarocks install lcurl --server=https://rocks.moonscript.org/dev

Usage

-- HTTP Get
curl.easy()
  :setopt_url('http://httpbin.org/get')
  :setopt_httpheader{
    "X-Test-Header1: Header-Data1",
    "X-Test-Header2: Header-Data2",
  }
  :setopt_writefunction(io.stderr) -- use io.stderr:write()
  :perform()
:close()
-- HTTP Post
curl.easy()
  :setopt_url('http://posttestserver.com/post.php')
  :setopt_writefunction(io.write)
  :setopt_httppost(curl.form() -- lcurl guarantee that form will be alive
    :add_content("test_content", "some data", {
      "MyHeader: SomeValue"
    })
    :add_buffer("test_file", "filename", "text data", "text/plain", {
      "Description: my file description"
    })
    :add_file("test_file2", "BuildLog.htm", "application/octet-stream", {
      "Description: my file description"
    })
  )
  :perform()
:close()
-- FTP Upload
local function get_bin_by(str,n)
  local pos = 1 - n
  return function()
    pos = pos + n
    return (str:sub(pos,pos+n-1))
  end
end

curl.easy()
  :setopt_url("ftp://moteus:123456@127.0.0.1/test.dat")
  :setopt_upload(true)
  :setopt_readfunction(
    get_bin_by(("0123456789"):rep(4), 9)
  )
  :perform()
:close()
-- Multi FTP Upload

-- We get error E_LOGIN_DENIED for this operation
e1 = curl.easy()
  :setopt_url("ftp://moteus:999999@127.0.0.1/test1.dat")
  :setopt_upload(true)
  :setopt_readfunction(
    function(t) return table.remove(t) end, {"1111", "2222"}
  )

e2 = curl.easy()
  :setopt_url("ftp://moteus:123456@127.0.0.1/test2.dat")
  :setopt_upload(true)
  :setopt_readfunction(get_bin_by(("e"):rep(1000), 5))

m = curl.multi()
m:add_handle(e1)
m:add_handle(e2)

while m:perform() > 0 do m:wait() end

while true do
  h, ok, err = m:info_read()
  if h == 0 then break end

  if h == e1 then 
    assert(ok == nil)
    assert(err:name() == "LOGIN_DENIED")
    assert(err:no() == curl.E_LOGIN_DENIED)
  end

  if h == e2 then 
    assert(ok == true)
  end
end