Question

I'm trying to use libcurl.dll with LuaJit, but curl_easy_perform always returns CURLE_URL_MALFORMAT (3)

This is my actual code (code fixed):

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

Output of the script with both .dll versions:

> 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

>

I tried it against two .dll's, both act the same.

The 2nd .dll I downloaded from: http://www.confusedbycode.com/curl/curl-7.35.0-win32-fix1.zip

Does anybody know how to get LuaJit/cURL to work together?

Was it helpful?

Solution

The problem is you do not pass any option to libcurl because of this erroneous declaration:

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

You should use this instead:

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

Since CURLoption is an enum type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top