Changeset View
Changeset View
Standalone View
Standalone View
libexec/flua/modules/lfetch.c
- This file was added.
/*- | |||||
* 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 <fetch.h> | |||||
#include <lua.h> | |||||
#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; | |||||
} | |||||
kevans: My gut reaction to this is that we probably shouldn't expose the port at all if it's not… | |||||
static const struct luaL_Reg fetchlib[] = { | |||||
{"get", l_fetchGet}, | |||||
{NULL, NULL} | |||||
}; | |||||
int | |||||
luaopen_fetch(lua_State *L) | |||||
{ | |||||
luaL_newlib(L, fetchlib); | |||||
return (1); | |||||
Done Inline ActionsBrace on its own line kevans: Brace on its own line | |||||
} | |||||
Done Inline ActionsThis should move up above the blank line kevans: This should move up above the blank line | |||||
Done Inline ActionsIIRC style(9) still doesn't allow declarations mid-block, it would need to be at the top of this function. kevans: IIRC style(9) still doesn't allow declarations mid-block, it would need to be at the top of… | |||||
Done Inline ActionsDitto for these declarations; chunk_size I'd probably just make a #define since we're not going to be altering it kevans: Ditto for these declarations; chunk_size I'd probably just make a `#define` since we're not… | |||||
Done Inline ActionsLeaks fetch kevans: Leaks `fetch` | |||||
Done Inline ActionsIf I fclose(fetch); here, this segfaults: Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
fish: Job 1, '/usr/libexec/flua $argv' terminated by signal SIGSEGV (Address boundary error) (lldb) thr backtrace all * thread #1, name = 'flua', stop reason = signal SIGSEGV * frame #0: 0x00001252697ad0f9 frame #1: 0xffadb0abacff9a89 dch: If I `fclose(fetch);` here, this segfaults:
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC… | |||||
Not Done Inline ActionsThe context for this one has changed, the comment was originally down in the error path in the loop. This branch looks fine kevans: The context for this one has changed, the comment was originally down in the error path in the… | |||||
Done Inline ActionsLeaks file kevans: Leaks `file` |
My gut reaction to this is that we probably shouldn't expose the port at all if it's not explicitly set, just leave it nil in this table to allow for more idiomatic usage where you can simply test the port as truthy before using it.f