Improve redbean fetch() (#460)

* Updated Fetch to return `nil,error` on errors
* Fix localhost connect when only IP address is specified
This commit is contained in:
Paul Kulchenko 2022-07-08 07:13:51 -07:00 committed by GitHub
parent 853b6c3864
commit 059fe22ea3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 42 deletions

View file

@ -722,7 +722,8 @@ FUNCTIONS
This function is not available in unsecure mode.
Fetch(url:str[,body:str|{method=value:str,body=value:str,headers=table,...}])
→ status:int,{header:str=value:str,...},body:str
├─→ status:int, {header:str=value:str,...}, body:str
└─→ nil, error:str
Sends an HTTP/HTTPS request to the specified URL. If only the URL is
provided, then a GET request is sent. If both URL and body parameters
are specified, then a POST request is sent. If any other method needs

View file

@ -3641,17 +3641,6 @@ static void ReseedRng(mbedtls_ctr_drbg_context *r, const char *s) {
#endif
}
static wontreturn void LuaThrowTlsError(lua_State *L, const char *s, int r) {
const char *code;
code = gc(xasprintf("-0x%04x", -r));
if (!IsTiny()) {
luaL_error(L, "tls %s failed (%s %s)", s, code, GetTlsError(r));
} else {
luaL_error(L, "tls %s failed (grep %s)", s, code);
}
unreachable;
}
static void LogMessage(const char *d, const char *s, size_t n) {
size_t n2, n3;
char *s2, *s3;
@ -3681,6 +3670,21 @@ static void LogBody(const char *d, const char *s, size_t n) {
}
}
static int LuaNilError(lua_State *L, const char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
lua_pushnil(L);
lua_pushvfstring(L, fmt, argp);
va_end(argp);
return 2;
}
static int LuaNilTlsError(lua_State *L, const char *s, int r) {
return LuaNilError(L, "tls %s failed (%s %s)", s,
IsTiny() ? "grep" : GetTlsError(r),
gc(xasprintf("-0x%04x", -r)));
}
static int LuaFetch(lua_State *L) {
#define ssl nope // TODO(jart): make this file less huge
char *p;
@ -3738,11 +3742,11 @@ static int LuaFetch(lua_State *L) {
if (lua_type(L, -2) == LUA_TSTRING) { // skip any non-string keys
key = lua_tolstring(L, -2, &keylen);
if (!IsValidHttpToken(key, keylen))
return luaL_argerror(L, 2, "invalid header name");
return LuaNilError(L, "invalid header name: %s", key);
val = lua_tolstring(L, -1, &vallen);
if (!(hdr = gc(EncodeHttpHeaderValue(val, vallen, 0))))
return luaL_argerror(L, 2, "invalid header value encoding");
return LuaNilError(L, "invalid header %s value encoding", key);
// Content-Length and Connection will be overwritten;
// skip them to avoid duplicates;
@ -3795,8 +3799,7 @@ static int LuaFetch(lua_State *L) {
} else
#endif
if (!(url.scheme.n == 4 && !memcasecmp(url.scheme.p, "http", 4))) {
luaL_argerror(L, 1, "bad scheme");
unreachable;
return LuaNilError(L, "bad scheme");
}
}
@ -3815,16 +3818,14 @@ static int LuaFetch(lua_State *L) {
} else {
port = "80";
}
} else if ((ip = ParseIp(urlarg, -1)) != -1) {
host = urlarg;
port = "80";
} else {
ip = servers.n ? ntohl(servers.p[0].addr.sin_addr.s_addr) : INADDR_LOOPBACK;
host =
gc(xasprintf("%hhu.%hhu.%hhu.%hhu", ip >> 24, ip >> 16, ip >> 8, ip));
port =
gc(xasprintf("%d", servers.n ? ntohs(servers.p[0].addr.sin_port) : 80));
return LuaNilError(L, "invalid host");
}
if (!IsAcceptableHost(host, -1)) {
luaL_argerror(L, 1, "invalid host");
unreachable;
return LuaNilError(L, "invalid host");
}
if (!hosthdr) hosthdr = gc(xasprintf("%s:%s", host, port));
@ -3859,9 +3860,8 @@ static int LuaFetch(lua_State *L) {
*/
DEBUGF("(ftch) client resolving %s", host);
if ((rc = getaddrinfo(host, port, &hints, &addr)) != EAI_SUCCESS) {
luaL_error(L, "getaddrinfo(%s:%s) error: EAI_%s %s", host, port,
return LuaNilError(L, "getaddrinfo(%s:%s) error: EAI_%s %s", host, port,
gai_strerror(rc), strerror(errno));
unreachable;
}
/*
@ -3876,8 +3876,7 @@ static int LuaFetch(lua_State *L) {
freeaddrinfo(addr), addr = 0;
if (rc == -1) {
close(sock);
luaL_error(L, "connect(%s:%s) error: %s", host, port, strerror(errno));
unreachable;
return LuaNilError(L, "connect(%s:%s) error: %s", host, port, strerror(errno));
}
#ifndef UNSECURE
@ -3906,8 +3905,7 @@ static int LuaFetch(lua_State *L) {
goto VerifyFailed;
default:
close(sock);
LuaThrowTlsError(L, "handshake", ret);
unreachable;
return LuaNilTlsError(L, "handshake", ret);
}
}
LockInc(&shared->c.sslhandshakes);
@ -3927,15 +3925,13 @@ static int LuaFetch(lua_State *L) {
if (ret != requestlen) {
if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) goto VerifyFailed;
close(sock);
LuaThrowTlsError(L, "write", ret);
unreachable;
return LuaNilTlsError(L, "write", ret);
}
} else
#endif
if (WRITE(sock, request, requestlen) != requestlen) {
close(sock);
luaL_error(L, "write error: %s", strerror(errno));
unreachable;
return LuaNilError(L, "write error: %s", strerror(errno));
}
if (logmessages) {
LogMessage("sent", request, requestlen);
@ -3963,8 +3959,7 @@ static int LuaFetch(lua_State *L) {
close(sock);
free(inbuf.p);
DestroyHttpMessage(&msg);
LuaThrowTlsError(L, "read", rc);
unreachable;
return LuaNilTlsError(L, "read", rc);
}
}
} else
@ -3973,8 +3968,7 @@ static int LuaFetch(lua_State *L) {
close(sock);
free(inbuf.p);
DestroyHttpMessage(&msg);
luaL_error(L, "read error: %s", strerror(errno));
unreachable;
return LuaNilError(L, "read error: %s", strerror(errno));
}
g = rc;
inbuf.n += g;
@ -4117,16 +4111,14 @@ TransportError:
DestroyHttpMessage(&msg);
free(inbuf.p);
close(sock);
luaL_error(L, "transport error");
unreachable;
return LuaNilError(L, "transport error");
#ifndef UNSECURE
VerifyFailed:
LockInc(&shared->c.sslverifyfailed);
close(sock);
LuaThrowTlsError(
return LuaNilTlsError(
L, gc(DescribeSslVerifyFailure(sslcli.session_negotiate->verify_result)),
ret);
unreachable;
#endif
#undef ssl
}