Lua(LuaJit)cURL curl_easy_perform始终返回CURLE_URL_MALFORMAT(3)错误。

我正在尝试使用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, ...); // 这里是错误!
    int curl_easy_perform(void *curl);
    void curl_easy_cleanup(void *curl);
]]

function cb(ptr, size, nmemb, stream)
        print("Data callback!\n") -- 甚至不会被调用一次
        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版本:", libcurl.curl_version(),"\n")

curl = libcurl.curl_easy_init()

if curl then
    print("尝试下载:", url, "\n")
    libcurl.curl_easy_setopt(curl, CURLOPT_VERBOSE, 1)
    --libcurl.curl_easy_setopt(curl, CURLOPT_URL, ffi.cast("char *", url)) -- 或者这个?两个都不起作用
    libcurl.curl_easy_setopt(curl, CURLOPT_URL, url)
    libcurl.curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fptr)

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

使用两个.dll版本的脚本输出:

> dofile("curl.lua")
cURL版本:1887658112

尝试下载:http://static02.mediaite.com/geekosystem/uploads/2013/12/doge.jpg

结果:3

>
>
> dofile("curl.lua")
cURL版本:1757089944

尝试下载:http://static02.mediaite.com/geekosystem/uploads/2013/12/doge.jpg

结果:3

> 

我尝试了两个.dll,两者的表现都一样。

我从这里下载了第二个.dll:http://www.confusedbycode.com/curl/curl-7.35.0-win32-fix1.zip

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

点赞
用户1688185
用户1688185

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

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

你应该使用这个:

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

因为 CURLoption 是一个 enum 类型。

2014-03-17 09:47:30