Fun API Documentation 0.42.1
The programming language that makes you have fun!
Loading...
Searching...
No Matches
curl.c File Reference

libcurl helpers and buffers used by HTTP-related VM opcodes. More...

#include <curl/curl.h>
Include dependency graph for curl.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  FunCurlBuf
 Simple growable buffer for libcurl write callbacks. More...

Functions

static size_t fun_curl_write_cb (void *ptr, size_t sz, size_t nm, void *ud)
 libcurl write callback that appends data to a FunCurlBuf.
static size_t fun_curl_file_write_cb (void *ptr, size_t sz, size_t nm, void *ud)
 libcurl write callback that writes directly to a FILE*.

Detailed Description

libcurl helpers and buffers used by HTTP-related VM opcodes.

This module centralizes small, concrete libcurl utilities used by VM opcodes under src/vm/http/*.c (or similar). The opcodes themselves perform VM stack marshalling and call these helpers for I/O glue. Keeping the concrete logic here mirrors other extensions (e.g., PCRE2, SQLite) and allows the opcode code to remain minimal.

Build-time feature flag:

  • The code in this file is compiled only when FUN_WITH_CURL is enabled. When disabled, curl-related opcodes should be compiled with no-op fallbacks in their respective files.

Buffering and ownership model:

  • FunCurlBuf is a small growable buffer intended for use with libcurl's CURLOPT_WRITEFUNCTION callback. The buffer expands with realloc() as data arrives and is maintained NUL-terminated for convenience when the content is treated as a C-string. The caller is responsible for allocating and freeing the FunCurlBuf fields (i.e., initialize { .d=NULL, .n=0 } before first use and free(b.d) afterwards).

Callbacks provided:

  • fun_curl_write_cb(): appends incoming data to a FunCurlBuf, keeping it NUL-terminated. Returns the number of bytes handled (sz*nm) on success or 0 on allocation failure to signal an error to libcurl.
  • fun_curl_file_write_cb(): writes incoming data directly to a FILE* passed via CURLOPT_WRITEDATA. Returns the number of elements written as per fwrite().

Error handling:

  • The write-to-buffer callback returns 0 on realloc failure, causing libcurl to abort the transfer and report CURLE_WRITE_ERROR. The file writer relies on fwrite()'s return value; callers should check the CURLcode after performing the transfer for final status.

Thread-safety:

  • These helpers themselves are stateless and thread-safe as long as each CURL easy handle and associated buffers/FILE* are not shared concurrently across threads without external synchronization.

Definition in file curl.c.

Function Documentation

◆ fun_curl_file_write_cb()

size_t fun_curl_file_write_cb ( void * ptr,
size_t sz,
size_t nm,
void * ud )
static

libcurl write callback that writes directly to a FILE*.

Suitable for large downloads or when incremental flushing to disk is desired. The FILE* should be opened in binary mode (e.g., "wb").

Parameters
ptrPointer to incoming data.
szSize of each element.
nmNumber of elements.
udUser data; must be a FILE* opened for writing in binary mode (passed via CURLOPT_WRITEDATA).
Returns
Number of elements written (as returned by fwrite). Returning a value smaller than nm will signal an error to libcurl.

Definition at line 114 of file curl.c.

◆ fun_curl_write_cb()

size_t fun_curl_write_cb ( void * ptr,
size_t sz,
size_t nm,
void * ud )
static

libcurl write callback that appends data to a FunCurlBuf.

Reallocates the destination buffer as needed and keeps it NUL-terminated. The buffer must be initialized by the caller with { .d=NULL, .n=0 } prior to first use.

Parameters
ptrPointer to incoming data block provided by libcurl.
szSize of each data element (as provided by libcurl).
nmNumber of elements in this block (as provided by libcurl).
udUser data; must be a FunCurlBuf* (passed via CURLOPT_WRITEDATA).
Returns
Number of bytes actually handled (sz*nm) on success; 0 on failure to signal an error to libcurl (which will abort with CURLE_WRITE_ERROR).

Definition at line 88 of file curl.c.