mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-05 10:48:29 +00:00
Support any HTTP method
It's now possible to use redbean Fetch() with arbitrary HTTP methods, e.g. LIST which is used by Hashicorp. There's an eight char limit and uppercase canonicalization still happens. This change also includes a better function for launching a browser tab, that won't deadlock on a headless workstation running Debian. Closes #1107
This commit is contained in:
parent
29eac8e2a2
commit
ad3944a3b6
14 changed files with 249 additions and 412 deletions
|
@ -162,7 +162,7 @@ int _curl(int argc, char *argv[]) {
|
|||
size_t n;
|
||||
char **p;
|
||||
} headers = {0};
|
||||
int method = 0;
|
||||
uint64_t method = 0;
|
||||
int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
|
||||
int ciphersuite = MBEDTLS_SSL_PRESET_SUITEC;
|
||||
bool includeheaders = false;
|
||||
|
@ -193,7 +193,7 @@ int _curl(int argc, char *argv[]) {
|
|||
postdata = optarg;
|
||||
break;
|
||||
case 'X':
|
||||
if (!(method = GetHttpMethod(optarg, strlen(optarg)))) {
|
||||
if (!(method = ParseHttpMethod(optarg, -1))) {
|
||||
tinyprint(2, prog, ": bad http method: ", optarg, "\n", NULL);
|
||||
exit(1);
|
||||
}
|
||||
|
@ -280,11 +280,13 @@ int _curl(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
char *request = 0;
|
||||
char methodstr[9] = {0};
|
||||
WRITE64LE(methodstr, method);
|
||||
appendf(&request,
|
||||
"%s %s HTTP/1.1\r\n"
|
||||
"Connection: close\r\n"
|
||||
"User-Agent: %s\r\n",
|
||||
kHttpMethod[method], gc(EncodeUrl(&url, 0)), agent);
|
||||
methodstr, gc(EncodeUrl(&url, 0)), agent);
|
||||
|
||||
bool senthost = false;
|
||||
bool sentcontenttype = false;
|
||||
|
|
|
@ -99,7 +99,8 @@ TOOL_NET_REDBEAN_LUA_MODULES = \
|
|||
o/$(MODE)/tool/net/ljson.o \
|
||||
o/$(MODE)/tool/net/lmaxmind.o \
|
||||
o/$(MODE)/tool/net/lsqlite3.o \
|
||||
o/$(MODE)/tool/net/largon2.o
|
||||
o/$(MODE)/tool/net/largon2.o \
|
||||
o/$(MODE)/tool/net/launch.o
|
||||
|
||||
o/$(MODE)/tool/net/redbean.com.dbg: \
|
||||
$(TOOL_NET_DEPS) \
|
||||
|
|
|
@ -15,7 +15,7 @@ static int LuaFetch(lua_State *L) {
|
|||
bool usingssl;
|
||||
uint32_t ip;
|
||||
struct Url url;
|
||||
int t, ret, sock = -1, methodidx, hdridx;
|
||||
int t, ret, sock = -1, hdridx;
|
||||
const char *host, *port;
|
||||
char *request;
|
||||
struct TlsBio *bio;
|
||||
|
@ -34,7 +34,9 @@ static int LuaFetch(lua_State *L) {
|
|||
size_t urlarglen, requestlen, paylen, bodylen;
|
||||
size_t i, g, hdrsize;
|
||||
int keepalive = kaNONE;
|
||||
int imethod, numredirects = 0, maxredirects = 5;
|
||||
char canmethod[9] = {0};
|
||||
uint64_t imethod;
|
||||
int numredirects = 0, maxredirects = 5;
|
||||
bool followredirect = true;
|
||||
struct addrinfo hints = {.ai_family = AF_INET,
|
||||
.ai_socktype = SOCK_STREAM,
|
||||
|
@ -54,9 +56,10 @@ static int LuaFetch(lua_State *L) {
|
|||
body = luaL_optlstring(L, -1, "", &bodylen);
|
||||
lua_getfield(L, 2, "method");
|
||||
// use GET by default if no method is provided
|
||||
method = luaL_optstring(L, -1, kHttpMethod[kHttpGet]);
|
||||
if ((imethod = GetHttpMethod(method, -1))) {
|
||||
method = kHttpMethod[imethod];
|
||||
method = luaL_optstring(L, -1, "GET");
|
||||
if ((imethod = ParseHttpMethod(method, -1))) {
|
||||
WRITE64LE(canmethod, imethod);
|
||||
method = canmethod;
|
||||
} else {
|
||||
return LuaNilError(L, "bad method");
|
||||
}
|
||||
|
@ -123,16 +126,16 @@ static int LuaFetch(lua_State *L) {
|
|||
} else if (lua_isnoneornil(L, 2)) {
|
||||
body = "";
|
||||
bodylen = 0;
|
||||
method = kHttpMethod[kHttpGet];
|
||||
method = "GET";
|
||||
} else {
|
||||
body = luaL_checklstring(L, 2, &bodylen);
|
||||
method = kHttpMethod[kHttpPost];
|
||||
method = "POST";
|
||||
}
|
||||
// provide Content-Length header unless it's zero and not expected
|
||||
methodidx = GetHttpMethod(method, -1);
|
||||
if (bodylen > 0 || !(methodidx == kHttpGet || methodidx == kHttpHead ||
|
||||
methodidx == kHttpTrace || methodidx == kHttpDelete ||
|
||||
methodidx == kHttpConnect)) {
|
||||
imethod = ParseHttpMethod(method, -1);
|
||||
if (bodylen > 0 ||
|
||||
!(imethod == kHttpGet || imethod == kHttpHead || imethod == kHttpTrace ||
|
||||
imethod == kHttpDelete || imethod == kHttpConnect)) {
|
||||
conlenhdr = gc(xasprintf("Content-Length: %zu\r\n", bodylen));
|
||||
}
|
||||
|
||||
|
@ -142,8 +145,8 @@ static int LuaFetch(lua_State *L) {
|
|||
gc(ParseUrl(urlarg, urlarglen, &url, true));
|
||||
gc(url.params.p);
|
||||
DEBUGF("(ftch) client fetching %`'s (host=%`'.*s, port=%.*s, path=%`'.*s)",
|
||||
urlarg, url.host.n, url.host.p, url.port.n, url.port.p,
|
||||
url.path.n, url.path.p);
|
||||
urlarg, url.host.n, url.host.p, url.port.n, url.port.p, url.path.n,
|
||||
url.path.p);
|
||||
|
||||
usingssl = false;
|
||||
if (url.scheme.n) {
|
||||
|
@ -488,7 +491,7 @@ Finished:
|
|||
if (msg.status == 303) {
|
||||
body = "";
|
||||
bodylen = 0;
|
||||
method = kHttpMethod[kHttpGet];
|
||||
method = "GET";
|
||||
}
|
||||
// create table if needed
|
||||
if (!lua_istable(L, 2)) {
|
||||
|
@ -512,8 +515,8 @@ Finished:
|
|||
VERBOSEF("(ftch) client redirecting %`'.*s "
|
||||
"(scheme=%`'.*s, host=%`'.*s, port=%.*s, path=%`'.*s)",
|
||||
FetchHeaderLength(kHttpLocation), FetchHeaderData(kHttpLocation),
|
||||
url.scheme.n, url.scheme.p, url.host.n, url.host.p,
|
||||
url.port.n, url.port.p, url.path.n, url.path.p);
|
||||
url.scheme.n, url.scheme.p, url.host.n, url.host.p, url.port.n,
|
||||
url.port.p, url.path.n, url.path.p);
|
||||
// while it's possible to check for IsAcceptableHost/IsAcceptablePort
|
||||
// it's not clear what to do if they are not;
|
||||
// if they are invalid, redirect returns "invalid host" message
|
||||
|
@ -530,7 +533,7 @@ Finished:
|
|||
if (FetchHeaderData(kHttpLocation)[0] == '/') {
|
||||
// if the path is absolute, then use it
|
||||
// so `/redir/more` -> `/less` becomes `/less`
|
||||
url.path.n = 0; // replace the path
|
||||
url.path.n = 0; // replace the path
|
||||
} else {
|
||||
// if the path is relative, then merge it,
|
||||
// so `/redir/more` -> `less` becomes `/redir/less`
|
||||
|
@ -539,8 +542,8 @@ Finished:
|
|||
}
|
||||
}
|
||||
url.path.p = gc(xasprintf("%.*s%.*s", url.path.n, url.path.p,
|
||||
FetchHeaderLength(kHttpLocation),
|
||||
FetchHeaderData(kHttpLocation)));
|
||||
FetchHeaderLength(kHttpLocation),
|
||||
FetchHeaderData(kHttpLocation)));
|
||||
url.path.n = strlen(url.path.p);
|
||||
lua_pushstring(L, gc(EncodeUrl(&url, 0)));
|
||||
}
|
||||
|
|
120
tool/net/launch.c
Normal file
120
tool/net/launch.c
Normal file
|
@ -0,0 +1,120 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2024 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/proc/posix_spawn.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
|
||||
static volatile bool g_timed_out;
|
||||
|
||||
static void finish(void) {
|
||||
if (!IsWindows()) {
|
||||
_exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
static void timeout(int sig) {
|
||||
g_timed_out = true;
|
||||
}
|
||||
|
||||
static void failure(const char *url, const char *cmd, const char *reason) {
|
||||
WARNF("(srvr) failed to open %s in a browser tab using %s: %s", url, cmd,
|
||||
reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens browser tab on host system.
|
||||
*/
|
||||
void launch_browser(const char *url) {
|
||||
|
||||
// perform this task from a subprocess so it doesn't block server
|
||||
if (!IsWindows()) {
|
||||
switch (fork()) {
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
case -1:
|
||||
perror("fork");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// determine which command opens browser tab
|
||||
const char *cmd;
|
||||
if (IsWindows()) {
|
||||
cmd = "/c/windows/explorer.exe";
|
||||
} else if (IsXnu()) {
|
||||
cmd = "open";
|
||||
} else {
|
||||
cmd = "xdg-open";
|
||||
}
|
||||
|
||||
// spawn process
|
||||
// set process group so ctrl-c won't kill browser
|
||||
int pid, err;
|
||||
posix_spawnattr_t sa;
|
||||
char *args[] = {(char *)cmd, (char *)url, 0};
|
||||
posix_spawnattr_init(&sa);
|
||||
posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETPGROUP);
|
||||
err = posix_spawnp(&pid, cmd, 0, &sa, args, environ);
|
||||
posix_spawnattr_destroy(&sa);
|
||||
if (err) {
|
||||
failure(url, cmd, strerror(err));
|
||||
return finish();
|
||||
}
|
||||
|
||||
// kill command if it takes more than three seconds
|
||||
// we need it because xdg-open acts weird on headless systems
|
||||
struct sigaction hand;
|
||||
hand.sa_flags = 0;
|
||||
sigemptyset(&hand.sa_mask);
|
||||
hand.sa_handler = timeout;
|
||||
sigaction(SIGALRM, &hand, 0);
|
||||
alarm(3);
|
||||
|
||||
// wait for tab to return finish opening
|
||||
// the browser will still be running after this completes
|
||||
int ws;
|
||||
while (waitpid(pid, &ws, 0) == -1) {
|
||||
if (errno != EINTR) {
|
||||
failure(url, cmd, strerror(errno));
|
||||
kill(pid, SIGKILL);
|
||||
return finish();
|
||||
}
|
||||
if (g_timed_out) {
|
||||
failure(url, cmd, "process timed out");
|
||||
kill(pid, SIGKILL);
|
||||
return finish();
|
||||
}
|
||||
}
|
||||
if (ws) {
|
||||
failure(url, cmd, "process exited with non-zero status");
|
||||
}
|
||||
|
||||
// we're done
|
||||
return finish();
|
||||
}
|
|
@ -95,5 +95,7 @@ int LuaVisualizeControlCodes(lua_State *);
|
|||
void LuaPushUrlView(lua_State *, struct UrlView *);
|
||||
char *FormatUnixHttpDateTime(char *, int64_t);
|
||||
|
||||
void launch_browser(const char *);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* COSMOPOLITAN_TOOL_NET_LFUNCS_H_ */
|
||||
|
|
|
@ -2534,7 +2534,7 @@ img { vertical-align: middle; }\r\n\
|
|||
}
|
||||
|
||||
static char *ServeErrorImplDefault(unsigned code, const char *reason,
|
||||
const char *details) {
|
||||
const char *details) {
|
||||
size_t n;
|
||||
char *p, *s;
|
||||
struct Asset *a;
|
||||
|
@ -2590,7 +2590,6 @@ static char *ServeErrorImpl(unsigned code, const char *reason,
|
|||
} else {
|
||||
return ServeErrorImplDefault(code, reason, details);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static char *ServeErrorWithPath(unsigned code, const char *reason,
|
||||
|
@ -2610,9 +2609,10 @@ static char *ServeError(unsigned code, const char *reason) {
|
|||
}
|
||||
|
||||
static char *ServeFailure(unsigned code, const char *reason) {
|
||||
ERRORF("(srvr) failure: %d %s %s HTTP%02d %.*s %`'.*s %`'.*s %`'.*s %`'.*s",
|
||||
code, reason, DescribeClient(), cpm.msg.version,
|
||||
cpm.msg.xmethod.b - cpm.msg.xmethod.a, inbuf.p + cpm.msg.xmethod.a,
|
||||
char method[9] = {0};
|
||||
WRITE64LE(method, cpm.msg.method);
|
||||
ERRORF("(srvr) failure: %d %s %s HTTP%02d %s %`'.*s %`'.*s %`'.*s %`'.*s",
|
||||
code, reason, DescribeClient(), cpm.msg.version, method,
|
||||
HeaderLength(kHttpHost), HeaderData(kHttpHost),
|
||||
cpm.msg.uri.b - cpm.msg.uri.a, inbuf.p + cpm.msg.uri.a,
|
||||
HeaderLength(kHttpReferer), HeaderData(kHttpReferer),
|
||||
|
@ -2921,12 +2921,8 @@ static const char *GetSystemUrlLauncherCommand(void) {
|
|||
}
|
||||
|
||||
static void LaunchBrowser(const char *path) {
|
||||
int pid, ws;
|
||||
struct in_addr addr;
|
||||
const char *u, *prog;
|
||||
sigset_t chldmask, savemask;
|
||||
struct sigaction ignore, saveint, savequit;
|
||||
uint16_t port = 80;
|
||||
struct in_addr addr;
|
||||
path = firstnonnull(path, "/");
|
||||
// use the first server address if there is at least one server
|
||||
if (servers.n) {
|
||||
|
@ -2936,42 +2932,7 @@ static void LaunchBrowser(const char *path) {
|
|||
// assign a loopback address if no server or unknown server address
|
||||
if (!servers.n || !addr.s_addr) addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
if (*path != '/') path = gc(xasprintf("/%s", path));
|
||||
if ((prog = commandv(GetSystemUrlLauncherCommand(), gc(malloc(PATH_MAX)),
|
||||
PATH_MAX))) {
|
||||
u = gc(xasprintf("http://%s:%d%s", inet_ntoa(addr), port, path));
|
||||
DEBUGF("(srvr) opening browser with command %`'s %s", prog, u);
|
||||
ignore.sa_flags = 0;
|
||||
ignore.sa_handler = SIG_IGN;
|
||||
sigemptyset(&ignore.sa_mask);
|
||||
sigaction(SIGINT, &ignore, &saveint);
|
||||
sigaction(SIGQUIT, &ignore, &savequit);
|
||||
sigemptyset(&chldmask);
|
||||
sigaddset(&chldmask, SIGCHLD);
|
||||
sigprocmask(SIG_BLOCK, &chldmask, &savemask);
|
||||
CHECK_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
setpgrp(); // ctrl-c'ing redbean shouldn't kill browser
|
||||
sigaction(SIGINT, &saveint, 0);
|
||||
sigaction(SIGQUIT, &savequit, 0);
|
||||
sigprocmask(SIG_SETMASK, &savemask, 0);
|
||||
execv(prog, (char *const[]){(char *)prog, (char *)u, 0});
|
||||
_Exit(127);
|
||||
}
|
||||
while (wait4(pid, &ws, 0, 0) == -1) {
|
||||
CHECK_EQ(EINTR, errno);
|
||||
errno = 0;
|
||||
}
|
||||
sigaction(SIGINT, &saveint, 0);
|
||||
sigaction(SIGQUIT, &savequit, 0);
|
||||
sigprocmask(SIG_SETMASK, &savemask, 0);
|
||||
if (!(WIFEXITED(ws) && WEXITSTATUS(ws) == 0)) {
|
||||
WARNF("(srvr) command %`'s exited with %d", GetSystemUrlLauncherCommand(),
|
||||
WIFEXITED(ws) ? WEXITSTATUS(ws) : 128 + WEXITSTATUS(ws));
|
||||
}
|
||||
} else {
|
||||
WARNF("(srvr) can't launch browser because %`'s isn't installed",
|
||||
GetSystemUrlLauncherCommand());
|
||||
}
|
||||
launch_browser(gc(xasprintf("http://%s:%d%s", inet_ntoa(addr), port, path)));
|
||||
}
|
||||
|
||||
static char *BadMethod(void) {
|
||||
|
@ -3971,12 +3932,9 @@ static int LuaGetRedbeanVersion(lua_State *L) {
|
|||
|
||||
static int LuaGetMethod(lua_State *L) {
|
||||
OnlyCallDuringRequest(L, "GetMethod");
|
||||
if (cpm.msg.method) {
|
||||
lua_pushstring(L, kHttpMethod[cpm.msg.method]);
|
||||
} else {
|
||||
lua_pushlstring(L, inbuf.p + cpm.msg.xmethod.a,
|
||||
cpm.msg.xmethod.b - cpm.msg.xmethod.a);
|
||||
}
|
||||
char method[9] = {0};
|
||||
WRITE64LE(method, cpm.msg.method);
|
||||
lua_pushstring(L, method);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -4870,6 +4828,9 @@ static int LuaBlackhole(lua_State *L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
static void BlockSignals(void) {
|
||||
}
|
||||
|
||||
wontreturn static void Replenisher(void) {
|
||||
struct timespec ts;
|
||||
VERBOSEF("(token) replenish worker started");
|
||||
|
@ -6052,9 +6013,10 @@ static char *HandleRequest(void) {
|
|||
LockInc(&shared->c.urisrefused);
|
||||
return ServeFailure(400, "Bad URI");
|
||||
}
|
||||
INFOF("(req) received %s HTTP%02d %.*s %s %`'.*s %`'.*s", DescribeClient(),
|
||||
cpm.msg.version, cpm.msg.xmethod.b - cpm.msg.xmethod.a,
|
||||
inbuf.p + cpm.msg.xmethod.a, FreeLater(EncodeUrl(&url, 0)),
|
||||
char method[9] = {0};
|
||||
WRITE64LE(method, cpm.msg.method);
|
||||
INFOF("(req) received %s HTTP%02d %s %s %`'.*s %`'.*s", DescribeClient(),
|
||||
cpm.msg.version, method, FreeLater(EncodeUrl(&url, 0)),
|
||||
HeaderLength(kHttpReferer), HeaderData(kHttpReferer),
|
||||
HeaderLength(kHttpUserAgent), HeaderData(kHttpUserAgent));
|
||||
if (HasHeader(kHttpContentType) &&
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue