Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
download.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file download.c
12 * @brief Fun VM opcode snippet: HTTP download to file via libcurl (OP_CURL_DOWNLOAD).
13 *
14 * This snippet is included by vm.c and implements the OP_CURL_DOWNLOAD
15 * instruction. When FUN_WITH_CURL is enabled, it downloads the content at
16 * the given URL and writes it to the specified filesystem path.
17 *
18 * Stack behavior:
19 * - Pops: path:string, url:string (values are converted via value_to_string_alloc)
20 * - Pushes: int (1 on success, 0 on error or when CURL is disabled)
21 *
22 * Pops a destination path and URL, streams the HTTP response body
23 * into the file, and pushes 1 on success or 0 on any error. Without
24 * FUN_WITH_CURL, behaves as a no-op that consumes two values and
25 * pushes 0.
26 *
27 * Error handling:
28 * - Returns 0 if URL/path conversion fails, file open fails, CURL init
29 * or perform fails.
30 * - Follows redirects (CURLOPT_FOLLOWLOCATION = 1L).
31 * - Writes via fun_curl_file_write_cb directly into the opened FILE*.
32 *
33 * Notes:
34 * - All temporary allocations (URL, path) are freed; FILE* is closed.
35 * - On builds without FUN_WITH_CURL, consumes two values and pushes 0.
36 */
37
39#ifdef FUN_WITH_CURL
40 Value vpath = pop_value(vm);
46 if (!url || !path) {
47 if (url) free(url);
48 if (path) free(path);
49 push_value(vm, make_int(0));
50 break;
51 }
52 FILE *fp = fopen(path, "wb");
53 if (!fp) {
54 free(url);
55 free(path);
56 push_value(vm, make_int(0));
57 break;
58 }
59 CURL *h = curl_easy_init();
60 if (!h) {
61 fclose(fp);
62 free(url);
63 free(path);
64 push_value(vm, make_int(0));
65 break;
66 }
67 curl_easy_setopt(h, CURLOPT_URL, url);
68 curl_easy_setopt(h, CURLOPT_FOLLOWLOCATION, 1L);
69 curl_easy_setopt(h, CURLOPT_WRITEFUNCTION, fun_curl_file_write_cb);
70 curl_easy_setopt(h, CURLOPT_WRITEDATA, fp);
71 CURLcode rc = curl_easy_perform(h);
76 if (rc != CURLE_OK) {
77 push_value(vm, make_int(0));
78 break;
79 }
81#else
82 Value a = pop_value(vm);
84 Value b = pop_value(vm);
86 push_value(vm, make_int(0));
87#endif
88 break;
89}
Value a
Definition add.c:37
uint32_t b
Definition band.c:32
@ OP_CURL_DOWNLOAD
Definition bytecode.h:175
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*.
Definition curl.c:66
CURLcode rc
Definition download.c:71
CURL * h
Definition download.c:59
curl_easy_cleanup(h)
fclose(fp)
free_value(vurl)
char * path
Definition download.c:43
Value vurl
Definition download.c:41
char * url
Definition download.c:42
curl_easy_setopt(h, CURLOPT_URL, url)
free(url)
push_value(vm, make_int(1))
Tagged union representing a Fun value.
Definition value.h:68
void vpath
Definition stubs.c:23
vm fp
Definition throw.c:57
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580