This repository has been archived on 2025-09-16. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
libcurl-odin/examples/debug.odin
2025-09-16 18:42:43 -04:00

114 lines
2.6 KiB
Odin

package examples
import "base:runtime"
import "core:fmt"
import "core:os"
import "core:testing"
import curl "../"
data :: struct {
trace_ascii: b8,
}
dump :: proc "c" (text: cstring, stream: os.Handle, ptr: [^]u8, size: uint, nohex: b8) {
context = runtime.default_context()
width: uint = 0x80 if nohex else 0x40
fmt.fprintf(stream, "%s, %10.10d bytes (0x%8.8x)\n", text, u64(size), u64(size))
for i: uint = 0; i < size; i += width {
fmt.fprintf(stream, "%4.4x: ", u64(i))
if !nohex {
/* hex not disabled, show it */
for c: uint = 0; c < width; c += 1 {
if i + c < size do fmt.fprintf(stream, "%02x ", ptr[i + c])
else do fmt.fprintf(stream, " ")
}
}
for c: uint = 0; (c < width) && (i + c < size); c += 1 {
/* check for 0D0A; if found, skip past and start a new line of output */
if nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D && ptr[i + c + 1] == 0x0A {
i += (c + 2 - width)
break
}
fmt.fprintf(
stream,
"%c",
(ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.',
)
/* check again for 0D0A, to avoid an extra \n if it's at width */
if nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D && ptr[i + c + 2] == 0x0A {
i += (c + 3 - width)
break
}
}
fmt.fprintf(stream, "\n")
}
fmt.fprintf(stream, "", true)
}
my_trace :: proc "c" (
handle: curl.Handle,
type: curl.Info_Type,
sdata: cstring,
size: uint,
userp: rawptr,
) -> i32 {
context = runtime.default_context()
config := cast(^data)userp
text: cstring
switch type {
case .TEXT:
fmt.fprintf(os.stderr, "== Info: %s", sdata)
return 0
case .HEADER_OUT:
text = "=> Send header"
case .DATA_OUT:
text = "=> Send data"
case .SSL_DATA_OUT:
text = "=> Send SSL data"
case .HEADER_IN:
text = "<= Recv header"
case .DATA_IN:
text = "<= Recv data"
case .SSL_DATA_IN:
text = "<= Recv SSL data"
case:
return 0
}
dump(text, os.stderr, cast([^]u8)sdata, size, config.trace_ascii)
return 0
}
@(test)
debug :: proc(t: ^testing.T) {
config: data
config.trace_ascii = true
hCurl := curl.easy_init()
if hCurl == nil {
testing.fail_now(t, "failed to init curl")
}
defer curl.easy_cleanup(hCurl)
curl.easy_setopt(hCurl, .DEBUGFUNCTION, my_trace)
curl.easy_setopt(hCurl, .DEBUGDATA, &config)
curl.easy_setopt(hCurl, .VERBOSE, 1)
curl.easy_setopt(hCurl, .FOLLOWLOCATION, 1)
curl.easy_setopt(hCurl, .URL, "https://example.com/")
res := curl.easy_perform(hCurl)
out := fmt.tprintf("curl.easy_perform() failed: %s\n", string(curl.easy_strerror(res)))
// test requires a network connection cuz duh
testing.expect(t, res == .OK, out)
}