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
3.1 kB 85 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 uint8_t mlang_aturi_parse(const char *source, char **out_body, char **out_error); 11extern void mlang_http_string_free(char *ptr); 12extern lean_obj_res lean_mk_io_user_error(lean_obj_arg msg); 13 14LEAN_EXPORT lean_obj_res mlang_http_get(b_lean_obj_arg url, lean_obj_arg world) { 15 (void)world; 16 char *body = NULL; 17 char *error = NULL; 18 const char *url_cstr = lean_string_cstr(url); 19 uint8_t ok = mlang_http_get_body(url_cstr, &body, &error); 20 if (ok) { 21 lean_object *result = lean_mk_string(body); 22 mlang_http_string_free(body); 23 return lean_io_result_mk_ok(result); 24 } else { 25 if (error == NULL) { 26 error = strdup("unknown HTTP error"); 27 } 28 lean_object *msg = lean_mk_string(error); 29 lean_object *io_error = lean_mk_io_user_error(msg); 30 mlang_http_string_free(error); 31 return lean_io_result_mk_error(io_error); 32 } 33} 34 35static lean_obj_res wrap_string_result( 36 uint8_t ok, 37 char *body, 38 char *error 39) { 40 if (ok) { 41 lean_object *result = lean_mk_string(body); 42 mlang_http_string_free(body); 43 return lean_io_result_mk_ok(result); 44 } else { 45 if (error == NULL) { 46 error = strdup("unknown host error"); 47 } 48 lean_object *msg = lean_mk_string(error); 49 lean_object *io_error = lean_mk_io_user_error(msg); 50 mlang_http_string_free(error); 51 return lean_io_result_mk_error(io_error); 52 } 53} 54 55LEAN_EXPORT lean_obj_res mlang_nickel_eval_string(b_lean_obj_arg source, lean_obj_arg world) { 56 (void)world; 57 char *body = NULL; 58 char *error = NULL; 59 uint8_t ok = mlang_nickel_eval(lean_string_cstr(source), &body, &error); 60 return wrap_string_result(ok, body, error); 61} 62 63LEAN_EXPORT lean_obj_res mlang_nickel_eval_file_string(b_lean_obj_arg path, lean_obj_arg world) { 64 (void)world; 65 char *body = NULL; 66 char *error = NULL; 67 uint8_t ok = mlang_nickel_eval_file(lean_string_cstr(path), &body, &error); 68 return wrap_string_result(ok, body, error); 69} 70 71LEAN_EXPORT lean_obj_res mlang_json_eval_string(b_lean_obj_arg source, lean_obj_arg world) { 72 (void)world; 73 char *body = NULL; 74 char *error = NULL; 75 uint8_t ok = mlang_json_eval(lean_string_cstr(source), &body, &error); 76 return wrap_string_result(ok, body, error); 77} 78 79LEAN_EXPORT lean_obj_res mlang_aturi_parse_string(b_lean_obj_arg source, lean_obj_arg world) { 80 (void)world; 81 char *body = NULL; 82 char *error = NULL; 83 uint8_t ok = mlang_aturi_parse(lean_string_cstr(source), &body, &error); 84 return wrap_string_result(ok, body, error); 85}