Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
post.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
38
40#ifdef FUN_WITH_CURL
41 Value vbody = pop_value(vm);
42 Value vurl = pop_value(vm);
43 char *url = value_to_string_alloc(&vurl);
44 char *body = value_to_string_alloc(&vbody);
45 free_value(vurl);
46 free_value(vbody);
47 if (!url) {
48 if (body) free(body);
49 push_value(vm, make_string(""));
50 break;
51 }
52 if (!body) body = strdup("");
53 FunCurlBuf buf = {NULL, 0};
54 CURL *h = curl_easy_init();
55 if (!h) {
56 free(url);
57 free(body);
58 push_value(vm, make_string(""));
59 break;
60 }
61 curl_easy_setopt(h, CURLOPT_URL, url);
62 curl_easy_setopt(h, CURLOPT_FOLLOWLOCATION, 1L);
63 curl_easy_setopt(h, CURLOPT_POST, 1L);
64 curl_easy_setopt(h, CURLOPT_POSTFIELDS, body);
65 curl_easy_setopt(h, CURLOPT_WRITEFUNCTION, fun_curl_write_cb);
66 curl_easy_setopt(h, CURLOPT_WRITEDATA, &buf);
67 CURLcode rc = curl_easy_perform(h);
68 curl_easy_cleanup(h);
69 free(url);
70 free(body);
71 if (rc != CURLE_OK) {
72 if (buf.d) free(buf.d);
73 push_value(vm, make_string(""));
74 break;
75 }
76 Value s = make_string(buf.d ? buf.d : "");
77 if (buf.d) free(buf.d);
78 push_value(vm, s);
79#else
80 Value a = pop_value(vm);
82 Value b = pop_value(vm);
85#endif
86 break;
87}
Value a
Definition add.c:37
uint32_t b
Definition band.c:32
@ OP_CURL_POST
Definition bytecode.h:174
int rc
char * buf
Definition input_line.c:103
free(vals)
push_value(vm, make_string(""))
free_value(a)
uint32_t s
Definition rol.c:31
Tagged union representing a Fun value.
Definition value.h:68
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641