Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
download.c File Reference

Fun VM opcode snippet: HTTP download to file via libcurl (OP_CURL_DOWNLOAD). More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

 free_value (vurl)
 free_value (vpath)
 if (!url||!path)
 if (!fp)
 curl_easy_setopt (h, CURLOPT_URL, url)
 curl_easy_setopt (h, CURLOPT_FOLLOWLOCATION, 1L)
 curl_easy_setopt (h, CURLOPT_WRITEFUNCTION, fun_curl_file_write_cb)
 curl_easy_setopt (h, CURLOPT_WRITEDATA, fp)
 curl_easy_cleanup (h)
 fclose (fp)
 free (url)
 free (path)
 if (rc !=CURLE_OK)
 push_value (vm, make_int(1))

Variables

case OP_CURL_DOWNLOAD
Value vurl = pop_value(vm)
char * url = value_to_string_alloc(&vurl)
char * path = value_to_string_alloc(&vpath)
FILE * fp = fopen(path, "wb")
CURL * h = curl_easy_init()
CURLcode rc = curl_easy_perform(h)
 break

Detailed Description

Fun VM opcode snippet: HTTP download to file via libcurl (OP_CURL_DOWNLOAD).

This snippet is included by vm.c and implements the OP_CURL_DOWNLOAD instruction. When FUN_WITH_CURL is enabled, it downloads the content at the given URL and writes it to the specified filesystem path.

Stack behavior:

  • Pops: path:string, url:string (values are converted via value_to_string_alloc)
  • Pushes: int (1 on success, 0 on error or when CURL is disabled)

Pops a destination path and URL, streams the HTTP response body into the file, and pushes 1 on success or 0 on any error. Without FUN_WITH_CURL, behaves as a no-op that consumes two values and pushes 0.

Error handling:

  • Returns 0 if URL/path conversion fails, file open fails, CURL init or perform fails.
  • Follows redirects (CURLOPT_FOLLOWLOCATION = 1L).
  • Writes via fun_curl_file_write_cb directly into the opened FILE*.

Notes:

  • All temporary allocations (URL, path) are freed; FILE* is closed.
  • On builds without FUN_WITH_CURL, consumes two values and pushes 0.

Definition in file download.c.

Function Documentation

◆ curl_easy_cleanup()

curl_easy_cleanup ( h )

◆ curl_easy_setopt() [1/4]

curl_easy_setopt ( h ,
CURLOPT_FOLLOWLOCATION ,
1L  )

◆ curl_easy_setopt() [2/4]

curl_easy_setopt ( h ,
CURLOPT_URL ,
url  )

◆ curl_easy_setopt() [3/4]

curl_easy_setopt ( h ,
CURLOPT_WRITEDATA ,
fp  )

◆ curl_easy_setopt() [4/4]

curl_easy_setopt ( h ,
CURLOPT_WRITEFUNCTION ,
fun_curl_file_write_cb  )

◆ fclose()

fclose ( fp )

◆ free() [1/2]

free ( path )

◆ free() [2/2]

free ( url )

◆ free_value() [1/2]

free_value ( vpath )

◆ free_value() [2/2]

free_value ( vurl )

◆ if() [1/3]

if ( ! fp)

Definition at line 53 of file download.c.

◆ if() [2/3]

if ( !url||! path)

Definition at line 46 of file download.c.

◆ if() [3/3]

if ( rc ! = CURLE_OK)

Definition at line 76 of file download.c.

◆ push_value()

push_value ( vm ,
make_int(1)  )

Variable Documentation

◆ break

break

Definition at line 88 of file download.c.

◆ fp

FILE* fp = fopen(path, "wb")

Definition at line 52 of file download.c.

◆ h

CURL* h = curl_easy_init()

Definition at line 59 of file download.c.

◆ OP_CURL_DOWNLOAD

case OP_CURL_DOWNLOAD

Definition at line 38 of file download.c.

◆ path

char* path = value_to_string_alloc(&vpath)

Definition at line 43 of file download.c.

◆ rc

void rc = curl_easy_perform(h)

Definition at line 71 of file download.c.

◆ url

char* url = value_to_string_alloc(&vurl)

Definition at line 42 of file download.c.

◆ vurl

Value vurl = pop_value(vm)

Definition at line 41 of file download.c.