This commit is contained in:
2025-09-16 18:42:43 -04:00
commit ed50dc81dc
10 changed files with 2346 additions and 0 deletions

1975
curl.odin Normal file

File diff suppressed because it is too large Load Diff

2
curl_darwin.odin Normal file
View File

@@ -0,0 +1,2 @@
package libcurl
fd_set :: struct {}

2
curl_linux.odin Normal file
View File

@@ -0,0 +1,2 @@
package libcurl
fd_set :: struct {}

4
curl_windows.odin Normal file
View File

@@ -0,0 +1,4 @@
package libcurl
import "core:sys/windows"
fd_set :: windows.fd_set
socket_t :: windows.SOCKET

17
examples/basic.odin Normal file
View File

@@ -0,0 +1,17 @@
package examples
import "core:fmt"
import curl "../"
main :: proc() {
hCurl := curl.easy_init()
defer curl.easy_cleanup(hCurl)
if hCurl != nil {
curl.easy_setopt(hCurl, .URL, "https://example.com")
curl.easy_setopt(hCurl, .FOLLOWLOCATION, true)
res := curl.easy_perform(hCurl)
if res != .OK {
fmt.eprintln(curl.easy_strerror(res))
}
}
}

155
examples/debug.c Normal file
View File

@@ -0,0 +1,155 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Show how CURLOPT_DEBUGFUNCTION can be used.
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
struct data {
char trace_ascii; /* 1 or 0 */
};
static
void dump(const char *text,
FILE *stream, unsigned char *ptr, size_t size,
char nohex)
{
size_t i;
size_t c;
unsigned int width = 0x40;
if(nohex)
/* without the hex output, we can fit more on screen */
width = 0x80;
fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n",
text, (unsigned long)size, (unsigned long)size);
for(i = 0; i < size; i += width) {
fprintf(stream, "%4.4lx: ", (unsigned long)i);
if(!nohex) {
/* hex not disabled, show it */
for(c = 0; c < width; c++)
if(i + c < size)
fprintf(stream, "%02x ", ptr[i + c]);
else
fputs(" ", stream);
}
for(c = 0; (c < width) && (i + c < size); c++) {
/* 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;
}
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;
}
}
fputc('\n', stream); /* newline */
}
fflush(stream);
}
static
int my_trace(CURL *handle, curl_infotype type,
char *data, size_t size,
void *userp)
{
struct data *config = (struct data *)userp;
const char *text;
(void)handle; /* prevent compiler warning */
switch(type) {
case CURLINFO_TEXT:
fprintf(stderr, "== Info: %s", data);
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
default: /* in case a new one is introduced to shock us */
return 0;
}
dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
return 0;
}
int main(void)
{
CURL *curl;
CURLcode res;
struct data config;
config.trace_ascii = 1; /* enable ASCII tracing */
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
/* the DEBUGFUNCTION has no effect until we enable VERBOSE */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

113
examples/debug.odin Normal file
View File

@@ -0,0 +1,113 @@
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)
}

54
examples/getinfo.c Normal file
View File

@@ -0,0 +1,54 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Use getinfo to get content-type after completed transfer.
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
res = curl_easy_perform(curl);
if(CURLE_OK == res) {
char *ct;
/* ask for the content-type */
res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
if((CURLE_OK == res) && ct)
printf("We received Content-Type: %s\n", ct);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

24
examples/getinfo.odin Normal file
View File

@@ -0,0 +1,24 @@
package examples
import "core:fmt"
import "core:testing"
import curl "../"
@(test)
getinfo :: proc(t: ^testing.T) {
res: curl.Code
hCurl := curl.easy_init()
if hCurl == nil do testing.fail_now(t, "failed to init curl")
defer curl.easy_cleanup(hCurl);
curl.easy_setopt(hCurl, .URL, "https://www.example.com/")
res = curl.easy_perform(hCurl);
if res != .OK do testing.fail_now(t, fmt.tprintf("got non-OK response for URL: %v", res))
ct: cstring
res = curl.easy_getinfo(hCurl, .CONTENT_TYPE, &ct);
if res != .OK || ct == nil do testing.fail_now(t, "could not read Content-Type")
testing.expectf(t, true, "Content-Type: %s\n", ct)
}

BIN
lib/libcurl_a.lib Normal file

Binary file not shown.