Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F163578126
D40274.id122498.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
5 KB
Referenced Files
None
Subscribers
None
D40274.id122498.diff
View Options
diff --git a/libexec/flua/Makefile b/libexec/flua/Makefile
--- a/libexec/flua/Makefile
+++ b/libexec/flua/Makefile
@@ -11,7 +11,7 @@
CWARNFLAGS.gcc+= -Wno-format-nonliteral
-LIBADD= lua
+LIBADD= lua fetch
# Entry point
SRCS+= lua.c
@@ -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 <lua.h>
+
+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,138 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Dave Cottlehuber <dch@FreeBSD.org>
+ *
+ * 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 <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <errno.h>
+#include <sys/param.h>
+#include <stdio.h>
+#include <string.h>
+#include <fetch.h>
+
+#include <lua.h>
+#include "lauxlib.h"
+#include "lfetch.h"
+
+/*
+ * Minimal implementation of libfetch
+ */
+
+static int
+lfetch_parse_url(lua_State *L)
+{
+ const char *url = luaL_checkstring(L, 1);
+ struct url *u;
+
+ u = fetchParseURL(url);
+ if (u == NULL) {
+ lua_pushnil(L);
+ lua_pushstring(L, "Failed to parse URL");
+ return (2);
+ }
+
+ lua_newtable(L);
+ lua_pushstring(L, u->scheme);
+ lua_setfield(L, -2, "scheme");
+ lua_pushstring(L, u->user);
+ lua_setfield(L, -2, "user");
+ lua_pushstring(L, u->pwd);
+ lua_setfield(L, -2, "password");
+ lua_pushstring(L, u->host);
+ lua_setfield(L, -2, "host");
+ /* if port is not explicitly set, it defaults to 0, not scheme */
+ lua_pushinteger(L, u->port);
+ lua_setfield(L, -2, "port");
+ /* doc is also known as the query string for http(s) */
+ lua_pushstring(L, u->doc);
+ lua_setfield(L, -2, "doc");
+ fetchFreeURL(u);
+ return (1);
+}
+
+static int
+lfetch_get_url(lua_State *L) {
+ const char *url = luaL_checkstring(L, 1);
+ const char *out = luaL_checkstring(L, 2);
+
+ struct url *u;
+ u = fetchParseURL(url);
+ if (u == NULL) {
+ lua_pushnil(L);
+ lua_pushstring(L, "Failed to parse URL");
+ return (2);
+ }
+
+ FILE *file = fopen(out, "wb");
+ if (file == NULL) {
+ lua_pushnil(L);
+ lua_pushfstring(L, "Failed to open output file: %s", strerror(errno));
+ return 2;
+ }
+
+ FILE *fetch = fetchGet(u, "");
+ if (fetch == NULL) {
+ lua_pushnil(L);
+ lua_pushfstring(L, "Failed to read from URL: %s", strerror(errno));
+ return 2;
+ }
+
+ int chunk_size = 4096;
+ char buf[chunk_size];
+ size_t bytes;
+
+ while ((bytes = fread(buf, 1, chunk_size, fetch)) > 0) {
+ if (fwrite(buf, 1, bytes, file) != bytes) {
+ lua_pushnil(L);
+ lua_pushfstring(L, "Failed to write to file: %s", strerror(errno));
+ fclose(file);
+ return 2;
+ }
+ }
+
+ fclose(fetch);
+ fclose(file);
+ fetchFreeURL(u);
+ lua_pushboolean(L, 1);
+ return 1;
+}
+
+static const struct
+luaL_Reg fetchlib[] = {
+ {"parse_url", lfetch_parse_url},
+ {"get_url", lfetch_get_url},
+ {NULL, NULL}
+};
+
+int
+luaopen_fetch(lua_State *L)
+{
+ luaL_newlib(L, fetchlib);
+ return (1);
+}
+
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Jul 25, 4:20 PM (18 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
35479446
Default Alt Text
D40274.id122498.diff (5 KB)
Attached To
Mode
D40274: flua: add libfetch functionality
Attached
Detach File
Event Timeline
Log In to Comment