mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-13 06:29:11 +00:00
Implement new API
This commit is contained in:
parent
6550c5fae1
commit
b5993e4e1d
1 changed files with 48 additions and 18 deletions
|
@ -5164,13 +5164,15 @@ static bool LuaRunAsset(const char *path, bool mandatory) {
|
||||||
return !!a;
|
return !!a;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int LuaUpgradeWS(lua_State *L) {
|
static int LuaWSUpgrade(lua_State *L) {
|
||||||
size_t i;
|
size_t i;
|
||||||
char *p, *q;
|
char *p, *q;
|
||||||
bool haskey;
|
bool haskey;
|
||||||
mbedtls_sha1_context ctx;
|
mbedtls_sha1_context ctx;
|
||||||
unsigned char hash[20];
|
unsigned char hash[20];
|
||||||
OnlyCallDuringRequest(L, "UpgradeWS");
|
|
||||||
|
if (cpm.generator)
|
||||||
|
luaL_error(L, "Cannot upgrade to websocket after yielding normally");
|
||||||
|
|
||||||
if (!HasHeader(kHttpWebsocketKey))
|
if (!HasHeader(kHttpWebsocketKey))
|
||||||
luaL_error(L, "No Sec-WebSocket-Key header");
|
luaL_error(L, "No Sec-WebSocket-Key header");
|
||||||
|
@ -5200,17 +5202,18 @@ static int LuaUpgradeWS(lua_State *L) {
|
||||||
|
|
||||||
cpm.luaheaderp = p;
|
cpm.luaheaderp = p;
|
||||||
cpm.wstype = 1;
|
cpm.wstype = 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int LuaReadWS(lua_State *L) {
|
static int LuaWSRead(lua_State *L) {
|
||||||
ssize_t rc;
|
ssize_t rc;
|
||||||
size_t i, got, amt, bufsize;
|
size_t i, got, amt, bufsize;
|
||||||
unsigned char wshdr[10], wshdrlen, *extlen, *mask, op;
|
unsigned char wshdr[10], wshdrlen, *extlen, *mask, op;
|
||||||
char *bufstart;
|
char *bufstart;
|
||||||
uint64_t len;
|
uint64_t len;
|
||||||
struct iovec iov[2];
|
struct iovec iov[2];
|
||||||
OnlyCallDuringRequest(L, "ReadWS");
|
OnlyCallDuringRequest(L, "ws.Read");
|
||||||
|
|
||||||
got = 0;
|
got = 0;
|
||||||
do {
|
do {
|
||||||
|
@ -5282,21 +5285,21 @@ static int LuaReadWS(lua_State *L) {
|
||||||
|
|
||||||
if (op == 0x1 && !isutf8(bufstart, bufsize)) goto close;
|
if (op == 0x1 && !isutf8(bufstart, bufsize)) goto close;
|
||||||
lua_pushlstring(L, bufstart, bufsize);
|
lua_pushlstring(L, bufstart, bufsize);
|
||||||
lua_pushnumber(L, wshdr[0]);
|
lua_pushinteger(L, op);
|
||||||
} else {
|
} else {
|
||||||
bufstart = inbuf.p + amtread;
|
bufstart = inbuf.p + amtread;
|
||||||
bufsize = (wsfragread - amtread) + got;
|
bufsize = (wsfragread - amtread) + got;
|
||||||
|
|
||||||
if (wsfragtype == 0x1 && !isutf8(bufstart, bufsize)) goto close;
|
if (wsfragtype == 0x1 && !isutf8(bufstart, bufsize)) goto close;
|
||||||
lua_pushlstring(L, bufstart, bufsize);
|
lua_pushlstring(L, bufstart, bufsize);
|
||||||
lua_pushnumber(L, wsfragtype);
|
lua_pushinteger(L, wsfragtype);
|
||||||
|
|
||||||
wsfragread = amtread;
|
wsfragread = amtread;
|
||||||
wsfragtype = 0;
|
wsfragtype = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
lua_pushnumber(L, 0);
|
lua_pushinteger(L, 0);
|
||||||
|
|
||||||
if (!wsfragtype) wsfragtype = op;
|
if (!wsfragtype) wsfragtype = op;
|
||||||
wsfragread += got;
|
wsfragread += got;
|
||||||
|
@ -5306,21 +5309,50 @@ static int LuaReadWS(lua_State *L) {
|
||||||
|
|
||||||
close:
|
close:
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
lua_pushnumber(L, 0x08);
|
lua_pushinteger(L, 0x08);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int LuaSetWSFlags(lua_State *L) {
|
static int LuaWSWrite(lua_State *L) {
|
||||||
OnlyCallDuringRequest(L, "SetWSFlags");
|
int type;
|
||||||
char flags = lround(lua_tonumber(L, 1));
|
size_t size;
|
||||||
if (flags & 0x01) {
|
const char *data;
|
||||||
cpm.wstype = 1;
|
|
||||||
} else if (flags & 0x02) {
|
OnlyCallDuringRequest(L, "ws.Write");
|
||||||
cpm.wstype = 2;
|
if (!cpm.wstype)
|
||||||
|
LuaWSUpgrade(L);
|
||||||
|
|
||||||
|
type = luaL_optinteger(L, 2, -1);
|
||||||
|
if (type == 1 || type == 2) {
|
||||||
|
cpm.wstype = type;
|
||||||
|
} else if (type != -1) {
|
||||||
|
luaL_error(L, "Invalid WS type");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!lua_isnil(L, 1)) {
|
||||||
|
data = luaL_checklstring(L, 1, &size);
|
||||||
|
appendd(&cpm.outbuf, data, size);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const luaL_Reg kLuaWS[] = {
|
||||||
|
{"Read", LuaWSRead}, //
|
||||||
|
{"Write", LuaWSWrite}, //
|
||||||
|
{0} //
|
||||||
|
};
|
||||||
|
|
||||||
|
int LuaWS(lua_State *L) {
|
||||||
|
luaL_newlib(L, kLuaWS);
|
||||||
|
lua_pushinteger(L, 0); lua_setfield(L, -2, "CONT");
|
||||||
|
lua_pushinteger(L, 1); lua_setfield(L, -2, "TEXT");
|
||||||
|
lua_pushinteger(L, 2); lua_setfield(L, -2, "BIN");
|
||||||
|
lua_pushinteger(L, 8); lua_setfield(L, -2, "CLOSE");
|
||||||
|
lua_pushinteger(L, 9); lua_setfield(L, -2, "PING");
|
||||||
|
lua_pushinteger(L, 10); lua_setfield(L, -2, "PONG");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// <SORTED>
|
// <SORTED>
|
||||||
// list of functions that can't be run from the repl
|
// list of functions that can't be run from the repl
|
||||||
static const char *const kDontAutoComplete[] = {
|
static const char *const kDontAutoComplete[] = {
|
||||||
|
@ -5521,7 +5553,6 @@ static const luaL_Reg kLuaFuncs[] = {
|
||||||
{"ProgramUid", LuaProgramUid}, //
|
{"ProgramUid", LuaProgramUid}, //
|
||||||
{"ProgramUniprocess", LuaProgramUniprocess}, //
|
{"ProgramUniprocess", LuaProgramUniprocess}, //
|
||||||
{"Rand64", LuaRand64}, //
|
{"Rand64", LuaRand64}, //
|
||||||
{"ReadWS", LuaReadWS}, // undocumented
|
|
||||||
{"Rdrand", LuaRdrand}, //
|
{"Rdrand", LuaRdrand}, //
|
||||||
{"Rdseed", LuaRdseed}, //
|
{"Rdseed", LuaRdseed}, //
|
||||||
{"Rdtsc", LuaRdtsc}, //
|
{"Rdtsc", LuaRdtsc}, //
|
||||||
|
@ -5539,7 +5570,6 @@ static const luaL_Reg kLuaFuncs[] = {
|
||||||
{"SetHeader", LuaSetHeader}, //
|
{"SetHeader", LuaSetHeader}, //
|
||||||
{"SetLogLevel", LuaSetLogLevel}, //
|
{"SetLogLevel", LuaSetLogLevel}, //
|
||||||
{"SetStatus", LuaSetStatus}, //
|
{"SetStatus", LuaSetStatus}, //
|
||||||
{"SetWSFlags", LuaSetWSFlags}, // undocumented
|
|
||||||
{"Sha1", LuaSha1}, //
|
{"Sha1", LuaSha1}, //
|
||||||
{"Sha224", LuaSha224}, //
|
{"Sha224", LuaSha224}, //
|
||||||
{"Sha256", LuaSha256}, //
|
{"Sha256", LuaSha256}, //
|
||||||
|
@ -5552,7 +5582,6 @@ static const luaL_Reg kLuaFuncs[] = {
|
||||||
{"Underlong", LuaUnderlong}, //
|
{"Underlong", LuaUnderlong}, //
|
||||||
{"UuidV4", LuaUuidV4}, //
|
{"UuidV4", LuaUuidV4}, //
|
||||||
{"UuidV7", LuaUuidV7}, //
|
{"UuidV7", LuaUuidV7}, //
|
||||||
{"UpgradeWS", LuaUpgradeWS}, // undocumented
|
|
||||||
{"VisualizeControlCodes", LuaVisualizeControlCodes}, //
|
{"VisualizeControlCodes", LuaVisualizeControlCodes}, //
|
||||||
{"Write", LuaWrite}, //
|
{"Write", LuaWrite}, //
|
||||||
{"bin", LuaBin}, //
|
{"bin", LuaBin}, //
|
||||||
|
@ -5591,6 +5620,7 @@ static const luaL_Reg kLuaLibs[] = {
|
||||||
{"path", LuaPath}, //
|
{"path", LuaPath}, //
|
||||||
{"re", LuaRe}, //
|
{"re", LuaRe}, //
|
||||||
{"unix", LuaUnix}, //
|
{"unix", LuaUnix}, //
|
||||||
|
{"ws", LuaWS} //
|
||||||
};
|
};
|
||||||
|
|
||||||
static void LuaSetArgv(lua_State *L) {
|
static void LuaSetArgv(lua_State *L) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue