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

Fun VM opcode snippet: HTTP GET via libcurl (OP_CURL_GET). More...

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

Go to the source code of this file.

Functions

 free_value (vurl)
 if (!url)
 curl_easy_setopt (h, CURLOPT_URL, url)
 curl_easy_setopt (h, CURLOPT_FOLLOWLOCATION, 1L)
 curl_easy_setopt (h, CURLOPT_WRITEFUNCTION, fun_curl_write_cb)
 curl_easy_setopt (h, CURLOPT_WRITEDATA, &buf)
 curl_easy_cleanup (h)
 free (url)
 if (rc !=CURLE_OK)
 if (buf.d) free(buf.d)
 push_value (vm, s)

Variables

case OP_CURL_GET
char * url = value_to_string_alloc(&vurl)
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 GET via libcurl (OP_CURL_GET).

This snippet is included by vm.c and implements the OP_CURL_GET instruction. When FUN_WITH_CURL is enabled, it performs an HTTP GET request for the provided URL 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: url:string (any value is converted via value_to_string_alloc)
  • Pushes: body:string ("" on error or when CURL is disabled)

Converts the top stack value to a URL string, issues a GET request using libcurl, and pushes the response body as a string. When compiled without FUN_WITH_CURL, the opcode becomes a no-op that consumes one value and pushes an empty string.

Error handling:

  • If URL conversion fails or CURL initialization/performance fails, the opcode pushes an empty string.
  • Follows HTTP redirects (CURLOPT_FOLLOWLOCATION = 1L).

Notes:

  • Uses FunCurlBuf and fun_curl_write_cb from the curl extension helpers.
  • Memory allocated for temporary strings and buffers is freed before exit.

Definition in file get.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 ,
& buf )

◆ curl_easy_setopt() [4/4]

curl_easy_setopt ( h ,
CURLOPT_WRITEFUNCTION ,
fun_curl_write_cb  )

◆ free()

free ( url )

◆ free_value()

free_value ( vurl )

◆ if() [1/3]

if ( ! url)

Definition at line 44 of file get.c.

◆ if() [2/3]

if ( buf. d)

◆ if() [3/3]

if ( rc ! = CURLE_OK)

Definition at line 62 of file get.c.

◆ push_value()

push_value ( vm ,
s  )

Variable Documentation

◆ break

break

Definition at line 75 of file get.c.

◆ buf

FunCurlBuf buf = {NULL, 0}

Definition at line 48 of file get.c.

◆ h

CURL* h = curl_easy_init()

Definition at line 49 of file get.c.

◆ OP_CURL_GET

case OP_CURL_GET

Definition at line 39 of file get.c.

◆ rc

CURLcode rc = curl_easy_perform(h)

Definition at line 59 of file get.c.

◆ s

Definition at line 67 of file get.c.

◆ url

char* url = value_to_string_alloc(&vurl)

Definition at line 42 of file get.c.