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

Fun VM opcode snippet: HTTP POST via libcurl (OP_CURL_POST). 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 (vbody)
 if (!url)
 curl_easy_setopt (h, CURLOPT_URL, url)
 curl_easy_setopt (h, CURLOPT_FOLLOWLOCATION, 1L)
 curl_easy_setopt (h, CURLOPT_POST, 1L)
 curl_easy_setopt (h, CURLOPT_POSTFIELDS, body)
 curl_easy_setopt (h, CURLOPT_WRITEFUNCTION, fun_curl_write_cb)
 curl_easy_setopt (h, CURLOPT_WRITEDATA, &buf)
 curl_easy_cleanup (h)
 free (url)
 free (body)
 if (rc !=CURLE_OK)
 if (buf.d) free(buf.d)
 push_value (vm, s)

Variables

case OP_CURL_POST
Value vurl = pop_value(vm)
char * url = value_to_string_alloc(&vurl)
char * body = value_to_string_alloc(&vbody)
FunCurlBuf buf = {NULL, 0}
CURL * h = curl_easy_init()
CURLcode rc = curl_easy_perform(h)
Value s = make_string(buf.d ? buf.d : "")
 break

Detailed Description

Fun VM opcode snippet: HTTP POST via libcurl (OP_CURL_POST).

This snippet is included by vm.c and implements the OP_CURL_POST instruction. When FUN_WITH_CURL is enabled, it performs an HTTP POST to the given URL with the provided request body and pushes the response body as a string. If libcurl support is not built in, or an error occurs, an empty string is pushed instead.

Stack behavior:

  • Pops: body:string, url:string (values are converted via value_to_string_alloc)
  • Pushes: body:string (server response; "" on error or when CURL is disabled)

Pops the POST body and URL, performs an HTTP POST, and pushes the response as a string. Without FUN_WITH_CURL, consumes two values and pushes an empty string.

Error handling:

  • If URL conversion fails, the opcode pushes an empty string and discards any converted body.
  • Follows redirects (CURLOPT_FOLLOWLOCATION = 1L).
  • Sets CURLOPT_POST=1L and CURLOPT_POSTFIELDS to submit the body.

Notes:

  • Uses FunCurlBuf and fun_curl_write_cb from the curl extension helpers.
  • All temporary allocations (URL, body, response buffer) are freed.

Definition in file post.c.

Function Documentation

◆ curl_easy_cleanup()

curl_easy_cleanup ( h )

◆ curl_easy_setopt() [1/6]

curl_easy_setopt ( h ,
CURLOPT_FOLLOWLOCATION ,
1L  )

◆ curl_easy_setopt() [2/6]

curl_easy_setopt ( h ,
CURLOPT_POST ,
1L  )

◆ curl_easy_setopt() [3/6]

curl_easy_setopt ( h ,
CURLOPT_POSTFIELDS ,
body  )

◆ curl_easy_setopt() [4/6]

curl_easy_setopt ( h ,
CURLOPT_URL ,
url  )

◆ curl_easy_setopt() [5/6]

curl_easy_setopt ( h ,
CURLOPT_WRITEDATA ,
& buf )

◆ curl_easy_setopt() [6/6]

curl_easy_setopt ( h ,
CURLOPT_WRITEFUNCTION ,
fun_curl_write_cb  )

◆ free() [1/2]

free ( body )

◆ free() [2/2]

free ( url )

◆ free_value() [1/2]

free_value ( vbody )

◆ free_value() [2/2]

free_value ( vurl )

◆ if() [1/3]

if ( ! url)

Definition at line 47 of file post.c.

◆ if() [2/3]

if ( buf. d)

◆ if() [3/3]

if ( rc ! = CURLE_OK)

Definition at line 71 of file post.c.

◆ push_value()

push_value ( vm ,
s  )

Variable Documentation

◆ body

char* body = value_to_string_alloc(&vbody)

Definition at line 44 of file post.c.

◆ break

break

Definition at line 86 of file post.c.

◆ buf

FunCurlBuf buf = {NULL, 0}

Definition at line 53 of file post.c.

◆ h

CURL* h = curl_easy_init()

Definition at line 54 of file post.c.

◆ OP_CURL_POST

case OP_CURL_POST

Definition at line 39 of file post.c.

◆ rc

CURLcode rc = curl_easy_perform(h)

Definition at line 67 of file post.c.

◆ s

Definition at line 76 of file post.c.

◆ url

char* url = value_to_string_alloc(&vurl)

Definition at line 43 of file post.c.

◆ vurl

Value vurl = pop_value(vm)

Definition at line 42 of file post.c.