This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

mlang / native / mlang_http_ffi.c
2.7 kB 76 lines
1#include <lean/lean.h> 2#include <stdint.h> 3#include <stdlib.h> 4#include <string.h> 5 6extern uint8_t mlang_http_get_body(const char *url, char **out_body, char **out_error); 7extern uint8_t mlang_nickel_eval(const char *source, char **out_body, char **out_error); 8extern uint8_t mlang_nickel_eval_file(const char *path, char **out_body, char **out_error); 9extern uint8_t mlang_json_eval(const char *source, char **out_body, char **out_error); 10extern void mlang_http_string_free(char *ptr); 11extern lean_obj_res lean_mk_io_user_error(lean_obj_arg msg); 12 13LEAN_EXPORT lean_obj_res mlang_http_get(b_lean_obj_arg url, lean_obj_arg world) { 14 (void)world; 15 char *body = NULL; 16 char *error = NULL; 17 const char *url_cstr = lean_string_cstr(url); 18 uint8_t ok = mlang_http_get_body(url_cstr, &body, &error); 19 if (ok) { 20 lean_object *result = lean_mk_string(body); 21 mlang_http_string_free(body); 22 return lean_io_result_mk_ok(result); 23 } else { 24 if (error == NULL) { 25 error = strdup("unknown HTTP error"); 26 } 27 lean_object *msg = lean_mk_string(error); 28 lean_object *io_error = lean_mk_io_user_error(msg); 29 mlang_http_string_free(error); 30 return lean_io_result_mk_error(io_error); 31 } 32} 33 34static lean_obj_res wrap_string_result( 35 uint8_t ok, 36 char *body, 37 char *error 38) { 39 if (ok) { 40 lean_object *result = lean_mk_string(body); 41 mlang_http_string_free(body); 42 return lean_io_result_mk_ok(result); 43 } else { 44 if (error == NULL) { 45 error = strdup("unknown host error"); 46 } 47 lean_object *msg = lean_mk_string(error); 48 lean_object *io_error = lean_mk_io_user_error(msg); 49 mlang_http_string_free(error); 50 return lean_io_result_mk_error(io_error); 51 } 52} 53 54LEAN_EXPORT lean_obj_res mlang_nickel_eval_string(b_lean_obj_arg source, lean_obj_arg world) { 55 (void)world; 56 char *body = NULL; 57 char *error = NULL; 58 uint8_t ok = mlang_nickel_eval(lean_string_cstr(source), &body, &error); 59 return wrap_string_result(ok, body, error); 60} 61 62LEAN_EXPORT lean_obj_res mlang_nickel_eval_file_string(b_lean_obj_arg path, lean_obj_arg world) { 63 (void)world; 64 char *body = NULL; 65 char *error = NULL; 66 uint8_t ok = mlang_nickel_eval_file(lean_string_cstr(path), &body, &error); 67 return wrap_string_result(ok, body, error); 68} 69 70LEAN_EXPORT lean_obj_res mlang_json_eval_string(b_lean_obj_arg source, lean_obj_arg world) { 71 (void)world; 72 char *body = NULL; 73 char *error = NULL; 74 uint8_t ok = mlang_json_eval(lean_string_cstr(source), &body, &error); 75 return wrap_string_result(ok, body, error); 76}