#include #include #include #include extern uint8_t mlang_http_get_body(const char *url, char **out_body, char **out_error); extern uint8_t mlang_nickel_eval(const char *source, char **out_body, char **out_error); extern uint8_t mlang_nickel_eval_file(const char *path, char **out_body, char **out_error); extern uint8_t mlang_json_eval(const char *source, char **out_body, char **out_error); extern uint8_t mlang_aturi_parse(const char *source, char **out_body, char **out_error); extern void mlang_http_string_free(char *ptr); extern lean_obj_res lean_mk_io_user_error(lean_obj_arg msg); LEAN_EXPORT lean_obj_res mlang_http_get(b_lean_obj_arg url, lean_obj_arg world) { (void)world; char *body = NULL; char *error = NULL; const char *url_cstr = lean_string_cstr(url); uint8_t ok = mlang_http_get_body(url_cstr, &body, &error); if (ok) { lean_object *result = lean_mk_string(body); mlang_http_string_free(body); return lean_io_result_mk_ok(result); } else { if (error == NULL) { error = strdup("unknown HTTP error"); } lean_object *msg = lean_mk_string(error); lean_object *io_error = lean_mk_io_user_error(msg); mlang_http_string_free(error); return lean_io_result_mk_error(io_error); } } static lean_obj_res wrap_string_result( uint8_t ok, char *body, char *error ) { if (ok) { lean_object *result = lean_mk_string(body); mlang_http_string_free(body); return lean_io_result_mk_ok(result); } else { if (error == NULL) { error = strdup("unknown host error"); } lean_object *msg = lean_mk_string(error); lean_object *io_error = lean_mk_io_user_error(msg); mlang_http_string_free(error); return lean_io_result_mk_error(io_error); } } LEAN_EXPORT lean_obj_res mlang_nickel_eval_string(b_lean_obj_arg source, lean_obj_arg world) { (void)world; char *body = NULL; char *error = NULL; uint8_t ok = mlang_nickel_eval(lean_string_cstr(source), &body, &error); return wrap_string_result(ok, body, error); } LEAN_EXPORT lean_obj_res mlang_nickel_eval_file_string(b_lean_obj_arg path, lean_obj_arg world) { (void)world; char *body = NULL; char *error = NULL; uint8_t ok = mlang_nickel_eval_file(lean_string_cstr(path), &body, &error); return wrap_string_result(ok, body, error); } LEAN_EXPORT lean_obj_res mlang_json_eval_string(b_lean_obj_arg source, lean_obj_arg world) { (void)world; char *body = NULL; char *error = NULL; uint8_t ok = mlang_json_eval(lean_string_cstr(source), &body, &error); return wrap_string_result(ok, body, error); } LEAN_EXPORT lean_obj_res mlang_aturi_parse_string(b_lean_obj_arg source, lean_obj_arg world) { (void)world; char *body = NULL; char *error = NULL; uint8_t ok = mlang_aturi_parse(lean_string_cstr(source), &body, &error); return wrap_string_result(ok, body, error); }