mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-09 03:10:27 +00:00
Add MapContentType to redbean to update extension-to-contenttype map
This commit is contained in:
parent
253cc1754b
commit
1a856ee1a8
2 changed files with 58 additions and 4 deletions
|
@ -1271,6 +1271,9 @@ FUNCTIONS
|
||||||
flag was used. If slurping large file into memory is a concern,
|
flag was used. If slurping large file into memory is a concern,
|
||||||
then consider using ServeAsset which can serve directly off disk.
|
then consider using ServeAsset which can serve directly off disk.
|
||||||
|
|
||||||
|
MapContentType(ext:str[, contenttype:str]) → str
|
||||||
|
Sets or returns content type associated with a file extension.
|
||||||
|
|
||||||
StoreAsset(path:str, data:str[, mode:int])
|
StoreAsset(path:str, data:str[, mode:int])
|
||||||
Stores asset to executable's ZIP central directory. This
|
Stores asset to executable's ZIP central directory. This
|
||||||
currently happens in an append-only fashion and is still
|
currently happens in an append-only fashion and is still
|
||||||
|
|
|
@ -463,6 +463,7 @@ static const char *serverheader;
|
||||||
static struct Strings stagedirs;
|
static struct Strings stagedirs;
|
||||||
static struct Strings hidepaths;
|
static struct Strings hidepaths;
|
||||||
static const char *launchbrowser;
|
static const char *launchbrowser;
|
||||||
|
static const char ctIdx = 'c'; // a pseudo variable to get address of
|
||||||
|
|
||||||
static struct spawn replth;
|
static struct spawn replth;
|
||||||
static struct spawn monitorth;
|
static struct spawn monitorth;
|
||||||
|
@ -4608,6 +4609,50 @@ static int LuaIsAssetCompressed(lua_State *L) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *GetContentTypeExt(const char *path, size_t n) {
|
||||||
|
char *r, *e;
|
||||||
|
int top;
|
||||||
|
lua_State *L = GL;
|
||||||
|
if ((r = FindContentType(path, n))) return r;
|
||||||
|
|
||||||
|
// extract the last .; use the entire path if none is present
|
||||||
|
if (e = strrchr(path, '.')) path = e + 1;
|
||||||
|
top = lua_gettop(L);
|
||||||
|
lua_pushlightuserdata(L, (void *)&ctIdx); // push address as unique key
|
||||||
|
CHECK_EQ(lua_gettable(L, LUA_REGISTRYINDEX), LUA_TTABLE);
|
||||||
|
|
||||||
|
lua_pushstring(L, path);
|
||||||
|
if (lua_gettable(L, -2) == LUA_TSTRING)
|
||||||
|
r = FreeLater(strdup(lua_tostring(L, -1)));
|
||||||
|
lua_settop(L, top);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int LuaMapContentType(lua_State *L) {
|
||||||
|
const char *ext = luaL_checkstring(L, 1);
|
||||||
|
const char *ct;
|
||||||
|
int n = lua_gettop(L);
|
||||||
|
|
||||||
|
lua_pushlightuserdata(L, (void *)&ctIdx); // push address as unique key
|
||||||
|
CHECK_EQ(lua_gettable(L, LUA_REGISTRYINDEX), LUA_TTABLE);
|
||||||
|
|
||||||
|
if (n == 1) {
|
||||||
|
ext = FreeLater(xasprintf(".%s", ext));
|
||||||
|
if ((ct = GetContentTypeExt(ext, strlen(ext)))) {
|
||||||
|
lua_pushstring(L, ct);
|
||||||
|
} else {
|
||||||
|
lua_pushnil(L);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
ct = luaL_checkstring(L, 2);
|
||||||
|
lua_pushstring(L, ext);
|
||||||
|
lua_pushstring(L, ct);
|
||||||
|
lua_settable(L, -3); // table[ext] = ct
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int LuaIsDaemon(lua_State *L) {
|
static int LuaIsDaemon(lua_State *L) {
|
||||||
lua_pushboolean(L, daemonize);
|
lua_pushboolean(L, daemonize);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -4822,6 +4867,7 @@ static const luaL_Reg kLuaFuncs[] = {
|
||||||
{"Lemur64", LuaLemur64}, //
|
{"Lemur64", LuaLemur64}, //
|
||||||
{"LoadAsset", LuaLoadAsset}, //
|
{"LoadAsset", LuaLoadAsset}, //
|
||||||
{"Log", LuaLog}, //
|
{"Log", LuaLog}, //
|
||||||
|
{"MapContentType", LuaMapContentType}, //
|
||||||
{"Md5", LuaMd5}, //
|
{"Md5", LuaMd5}, //
|
||||||
{"MeasureEntropy", LuaMeasureEntropy}, //
|
{"MeasureEntropy", LuaMeasureEntropy}, //
|
||||||
{"ParseHost", LuaParseHost}, //
|
{"ParseHost", LuaParseHost}, //
|
||||||
|
@ -5134,6 +5180,11 @@ static void LuaInit(void) {
|
||||||
}
|
}
|
||||||
exit(rc);
|
exit(rc);
|
||||||
}
|
}
|
||||||
|
// create a list of custom content types
|
||||||
|
lua_pushlightuserdata(L, (void *)&ctIdx); // push address as unique key
|
||||||
|
lua_newtable(L);
|
||||||
|
lua_settable(L, LUA_REGISTRYINDEX); // registry[&ctIdx] = {}
|
||||||
|
|
||||||
if (LuaRunAsset("/.init.lua", true)) {
|
if (LuaRunAsset("/.init.lua", true)) {
|
||||||
hasonhttprequest = IsHookDefined("OnHttpRequest");
|
hasonhttprequest = IsHookDefined("OnHttpRequest");
|
||||||
hasonclientconnection = IsHookDefined("OnClientConnection");
|
hasonclientconnection = IsHookDefined("OnClientConnection");
|
||||||
|
@ -5724,12 +5775,12 @@ static char *HandleAsset(struct Asset *a, const char *path, size_t pathlen) {
|
||||||
|
|
||||||
static const char *GetContentType(struct Asset *a, const char *path, size_t n) {
|
static const char *GetContentType(struct Asset *a, const char *path, size_t n) {
|
||||||
const char *r;
|
const char *r;
|
||||||
if (a->file && (r = FindContentType(a->file->path.s, a->file->path.n))) {
|
if (a->file && (r = GetContentTypeExt(a->file->path.s, a->file->path.n))) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
return firstnonnull(
|
return firstnonnull(
|
||||||
FindContentType(path, n),
|
GetContentTypeExt(path, n),
|
||||||
firstnonnull(FindContentType(ZIP_CFILE_NAME(zbase + a->cf),
|
firstnonnull(GetContentTypeExt(ZIP_CFILE_NAME(zbase + a->cf),
|
||||||
ZIP_CFILE_NAMESIZE(zbase + a->cf)),
|
ZIP_CFILE_NAMESIZE(zbase + a->cf)),
|
||||||
a->istext ? "text/plain" : "application/octet-stream"));
|
a->istext ? "text/plain" : "application/octet-stream"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue