diff --git a/libexec/flua/Makefile b/libexec/flua/Makefile --- a/libexec/flua/Makefile +++ b/libexec/flua/Makefile @@ -19,7 +19,7 @@ # FreeBSD Extensions .PATH: ${.CURDIR}/modules SRCS+= linit_flua.c -SRCS+= lfs.c lposix.c +SRCS+= lfetch.c lfs.c lposix.c CFLAGS+= -I${SRCTOP}/lib/liblua -I${.CURDIR}/modules -I${LUASRC} CFLAGS+= -DLUA_PROGNAME="\"${PROG}\"" diff --git a/libexec/flua/linit_flua.c b/libexec/flua/linit_flua.c --- a/libexec/flua/linit_flua.c +++ b/libexec/flua/linit_flua.c @@ -34,6 +34,7 @@ #include "lualib.h" #include "lauxlib.h" +#include "lfetch.h" #include "lfs.h" #include "lposix.h" #include "lua_ucl.h" @@ -57,6 +58,7 @@ {LUA_BITLIBNAME, luaopen_bit32}, #endif /* FreeBSD Extensions */ + {"fetch", luaopen_fetch}, {"lfs", luaopen_lfs}, {"posix.libgen", luaopen_posix_libgen}, {"posix.stdlib", luaopen_posix_stdlib}, diff --git a/libexec/flua/modules/lfetch.h b/libexec/flua/modules/lfetch.h new file mode 100644 --- /dev/null +++ b/libexec/flua/modules/lfetch.h @@ -0,0 +1,11 @@ +/*- + * + * This file is in the public domain. + */ +/* $FreeBSD$ */ + +#pragma once + +#include + +int luaopen_fetch(lua_State *L); diff --git a/libexec/flua/modules/lfetch.c b/libexec/flua/modules/lfetch.c new file mode 100644 --- /dev/null +++ b/libexec/flua/modules/lfetch.c @@ -0,0 +1,82 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2023 Dave Cottlehuber + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include + +#include +#include "lauxlib.h" +#include "lfetch.h" + +/* + * Minimal implementation of libfetch + */ + +static int l_fetchGet(lua_State *L) { + struct url *fetch_url; + // did we get a string on the stack + const char *url = luaL_checkstring(L, 1); + // is it a valid URL + fetch_url = fetchParseURL (url); + if (fetch_url == NULL) { + // stash the goodies + lua_pushnil(L); + lua_pushstring(L, "Failed to parse URL"); + // clean up + fetchFreeURL(fetch_url); + return 2; + } + + // return a fixed body for the moment + const size_t buffer_size = 8192; // big enough for anybody? + char buffer[buffer_size]; + // clean up + fetchFreeURL(fetch_url); + // return the goodies + lua_pushlstring(L, buffer, buffer_size); + lua_pushnil(L); + return 2; +} + +static const struct luaL_Reg fetchlib[] = { + {"get", l_fetchGet}, + {NULL, NULL} +}; + +int +luaopen_fetch(lua_State *L) +{ + luaL_newlib(L, fetchlib); + return (1); +} +