Lua(LuaJit)cURL curl_easy_perform总是返回CURLE_URL_MALFORMAT(3)

时间:2014-03-16 22:21:59

标签: curl lua libcurl luajit

我尝试将libcurl.dll与LuaJit一起使用,但curl_easy_perform始终返回CURLE_URL_MALFORMAT(3)

这是我的实际代码(代码已修复):

url = [[http://static02.mediaite.com/geekosystem/uploads/2013/12/doge.jpg]]

ffi.cdef [[
    int curl_version();
    void *curl_easy_init();
    int curl_easy_setopt(void *curl, int option, ...); // here was the error!
    int curl_easy_perform(void *curl);
    void curl_easy_cleanup(void *curl);
]]

function cb(ptr, size, nmemb, stream)
        print("Data callback!\n") -- not even called once
        local bytes = size*nmemb
        local buf = ffi.new('char[?]', bytes+1)
        ffi.copy(buf, ptr, bytes)
        buf[bytes] = 0
        data = ffi.string(buf)
        return bytes
end

fptr = ffi.cast("size_t (*)(char *, size_t, size_t, void *)", cb)

data = ""

CURLOPT_URL = 10002
CURLOPT_WRITEFUNCTION = 20011
CURLOPT_VERBOSE = 41
libcurl = ffi.load("libcurl.dll")

print("cURL Version: ", libcurl.curl_version(), "\n")

curl = libcurl.curl_easy_init()

if curl then
    print("Trying to download: ", url, "\n")
    libcurl.curl_easy_setopt(curl, CURLOPT_VERBOSE, 1)
    --libcurl.curl_easy_setopt(curl, CURLOPT_URL, ffi.cast("char *", url)) -- or this? both doesn't work
    libcurl.curl_easy_setopt(curl, CURLOPT_URL, url)
    libcurl.curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fptr)

    print("Result: ", libcurl.curl_easy_perform(curl), "\n")
    libcurl.curl_easy_cleanup(curl)
end

使用.dll版本输出脚本:

> dofile("curl.lua")
cURL Version:   1887658112

Trying to download:     http://static02.mediaite.com/geekosystem/uploads/2013/12/doge.jpg

Result:         3

>
>
> dofile("curl.lua")
cURL Version:   1757089944

Trying to download:     http://static02.mediaite.com/geekosystem/uploads/2013/12/doge.jpg

Result:         3

>

我对两个.dll进行了尝试,两者都是一样的。

我从http://www.confusedbycode.com/curl/curl-7.35.0-win32-fix1.zip

下载的第二个.dll

有人知道如何让LuaJit / cURL一起工作吗?

1 个答案:

答案 0 :(得分:1)

问题是你没有将任何选项传递给libcurl,因为这个错误的声明:

int curl_easy_setopt(void *curl, char option, ...);

你应该改用:

int curl_easy_setopt(void *curl, int option, ...);

由于CURLoptionenum类型。