0

我正在尝试获取信息CURLINFO_RESPONSE_CODECURLINFO_CONTENT_TYPEwith curl_easy_getinfo,但两次尝试似乎都失败了。

就像在 LuaJit/scanf 示例中一样,我正在分配ffi.new("int[1]")以获取一个元素作为指针并将其用作参数,以便在保存后读取值。

ffi = require "ffi"

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);
    int curl_easy_getinfo(void *curl, int info, ...);
]]

function curl_callback(ptr, size, nmemb, userdata)
    print("Data callback! File-ID: ", userdata, "\n")
    return size*nmemb
end

ffi_curl_callback = ffi.cast("size_t (*)(char *ptr, size_t size, size_t nmemb, void *userdata)", curl_callback)

CURLOPT_URL = 10002
CURLOPT_WRITEFUNCTION = 20011
CURLOPT_VERBOSE = 41
CURLOPT_PROGRESSDATA = 10057
CURLOPT_NOPROGRESS = 43
CURLOPT_WRITEDATA = 10001
CURLE_OK = 0

CURLINFO_RESPONSE_CODE = 2097154
CURLINFO_CONTENT_TYPE = 1048594

libcurl = ffi.load("libcurl.dll")

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

curl = libcurl.curl_easy_init()

if curl then

    file = ffi.new("int", 0xabc) -- not needed

    print("Trying to download: ", url, "\n")
    libcurl.curl_easy_setopt(curl, CURLOPT_VERBOSE, ffi.new("long", 1))
    libcurl.curl_easy_setopt(curl, CURLOPT_URL, url)
    libcurl.curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ffi_curl_callback)
    --libcurl.curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1)
    --libcurl.curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, 123)
    libcurl.curl_easy_setopt(curl, CURLOPT_WRITEDATA, ffi.cast("void *", file))

    -- bug fixed, called curl_easy_getinfo before curl_easy_perform
    result = libcurl.curl_easy_perform(curl)
    if result == CURLE_OK then
        response_code = ffi.new("int[1]")
        content_type = ffi.new("char*[1]")
        libcurl.curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, response_code)
        libcurl.curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, content_type)
        print("RESPONSE CODE: '", tonumber(response_code[0]), "'\n")
        print("CONTENT TYPE: '", ffi.string(content_type[0]), "'\n")
    else
        print("Error! Result: ", result, "\n")
    end

    libcurl.curl_easy_cleanup(curl)
end

输出是(错误的旧输出):

Data callback! File-ID:         cdata<void *>: 0x00000abc
Data callback! File-ID:         cdata<void *>: 0x00000abc
Data callback! File-ID:         cdata<void *>: 0x00000abc
Data callback! File-ID:         cdata<void *>: 0x00000abc
Data callback! File-ID:         cdata<void *>: 0x00000abc
Data callback! File-ID:         cdata<void *>: 0x00000abc
Result:         0
RESPONSE CODE: '        nil     '
CONTENT TYPE: '         '

结果 0 表示 CURLE_OK 并且文件已真正下载(我已经使用我的自定义游戏 FS 函数进行了测试)。虽然我无法获得响应代码或内容类型。有人知道我做错了什么吗?

4

1 回答 1

1

文档

CURLINFO_RESPONSE_CODE

传递一个指向 long的指针以接收最后收到的 HTTP、FTP 或 SMTP 响应代码。

CURLINFO_CONTENT_TYPE

将指针传递给 char 指针以接收下载对象的内容类型。这是从 Content-Type: 字段读取的值。如果你得到 NULL,这意味着服务器没有发送一个有效的 Content-Type 头或者使用的协议不支持这个。

这些选项要求long*char**;你正在传递int*char*。以下应该有效:

local resp = ffi.new("long[1]")  -- long*
local err  = libcurl.curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, resp)
local code = ffi.new("char*[1]") -- char**
local err2 = libcurl.curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, code)
-- get the values
resp = tonumber(resp[0])
code = ffi.string(code[0])

此外,如果那些注释掉的调用curl_easy_setopt不起作用,那是因为您传递的int是 a 而不是 a long

local vb  = ffi.new("long", 1)
local err = libcurl.curl_easy_setopt(curl, CURLOPT_VERBOSE, vb)
于 2014-03-18T17:15:43.463 回答