|
| 1 | +-- |
| 2 | +-- convert `debug.c` example from libcurl examples |
| 3 | +-- |
| 4 | + |
| 5 | +local curl = require "lcurl" |
| 6 | + |
| 7 | +local function printf(...) |
| 8 | + io.stderr:write(string.format(...)) |
| 9 | +end |
| 10 | + |
| 11 | +local function dumb(title, data, n) |
| 12 | + n = n or 16 |
| 13 | + printf("%s, %10.10d bytes (0x%8.8x)\n", title, #data, #data) |
| 14 | + for i = 1, #data do |
| 15 | + if (i - 1) % n == 0 then printf("%4.4x: ", i-1) end |
| 16 | + printf("%02x ", string.byte(data, i, i)) |
| 17 | + if i % n == 0 then printf("\n") end |
| 18 | + end |
| 19 | + if #data % n ~= 0 then printf("\n") end |
| 20 | +end |
| 21 | + |
| 22 | +local function my_trace(type, data) |
| 23 | + local text |
| 24 | + if type == curl.INFO_TEXT then printf("== Info: %s", data) end |
| 25 | + if type == curl.INFO_HEADER_OUT then text = "=> Send header" end |
| 26 | + if type == curl.INFO_DATA_OUT then text = "=> Send data" end |
| 27 | + if type == curl.INFO_SSL_DATA_OUT then text = "=> Send SSL data" end |
| 28 | + if type == curl.INFO_HEADER_IN then text = "<= Recv header" end |
| 29 | + if type == curl.INFO_DATA_IN then text = "<= Recv data" end |
| 30 | + if type == curl.INFO_SSL_DATA_IN then text = "<= Recv SSL data" end |
| 31 | + if text then dumb(text, data) end |
| 32 | +end |
| 33 | + |
| 34 | +local easy = curl.easy{ |
| 35 | + url = "http://google.com", |
| 36 | + verbose = true, |
| 37 | + debugfunction = my_trace, |
| 38 | + followlocation = true, |
| 39 | + writefunction = function()end, |
| 40 | +} |
| 41 | + |
| 42 | +easy:perform() |
0 commit comments