Make redbean tinier

Under MODE=tiny or MODE=tinylinux we now go back to using my homebrew
version of DEFLATE decompression which is 10x smaller but 10x slower
than Chromium Zlib. In tiny mode we also disable compressed responses
howewver redbean will still serve precompressed responses. This change
also removes a few other dependencies like strftime() and getaddrinfo()
which means you can't say `-l localhost` in tiny mode, you have to say
something like `-l 127.0.0.1`.

    m=tinylinux
    make -j8 MODE=$m o/$m/tool/net/redbean-original.com
    ls -hal o/$m/tool/net/redbean-original.com

This change reduces the above size from 191.4 to 150.9 kb.
This commit is contained in:
Justine Tunney 2021-07-04 12:21:29 -07:00
parent 300876ee50
commit a7bd4ed9ea
8 changed files with 123 additions and 46 deletions

View file

@ -96,7 +96,7 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
vflogf_ts.tv_nsec = nsec;
if (!issamesecond) {
localtime_r(&secs, &tm);
strftime(buf32, sizeof(buf32), "%Y-%m-%dT%H:%M:%S.", &tm);
strcpy(iso8601(buf32, &tm), ".");
buf32p = buf32;
} else {
buf32p = "--------------------";

View file

@ -22,7 +22,7 @@
#include "libc/dce.h"
/**
* Creates new process zygote style.
* Creates new process.
*
* @return 0 to child, child pid to parent, or -1 on error
* @asyncsignalsafe

54
libc/time/iso8601.c Normal file
View file

@ -0,0 +1,54 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 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/time/struct/tm.h"
#include "libc/time/time.h"
/**
* Converts timestamp to ISO-8601 formatted string.
*/
char *iso8601(char p[hasatleast 20], struct tm *tm) {
int x;
x = tm->tm_year + 1900;
*p++ = '0' + x / 1000;
*p++ = '0' + x / 100 % 10;
*p++ = '0' + x / 10 % 10;
*p++ = '0' + x % 10;
*p++ = '-';
x = tm->tm_mon + 1;
*p++ = '0' + x / 10;
*p++ = '0' + x % 10;
*p++ = '-';
x = tm->tm_mday;
*p++ = '0' + x / 10;
*p++ = '0' + x % 10;
*p++ = 'T';
x = tm->tm_hour;
*p++ = '0' + x / 10;
*p++ = '0' + x % 10;
*p++ = ':';
x = tm->tm_min;
*p++ = '0' + x / 10;
*p++ = '0' + x % 10;
*p++ = ':';
x = tm->tm_sec;
*p++ = '0' + x / 10;
*p++ = '0' + x % 10;
*p = 0;
return p;
}

View file

@ -64,6 +64,7 @@ long double ConvertTicksToNanos(uint64_t);
void RefreshTime(void);
double difftime(int64_t, int64_t) nothrow pureconst;
char *iso8601(char[hasatleast 20], struct tm *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -35,7 +35,7 @@ char *FormatHttpDateTime(char p[hasatleast 30], struct tm *tm) {
p = mempcpy(p, kWeekdayNameShort[tm->tm_wday], 3);
*p++ = ',';
*p++ = ' ';
i = MIN(MAX(tm->tm_mday, 0), 31);
i = MIN(MAX(tm->tm_mday, 1), 31);
*p++ = '0' + i / 10;
*p++ = '0' + i % 10;
*p++ = ' ';

View file

@ -91,13 +91,13 @@ o/$(MODE)/tool/net/redbean.com: \
o/$(MODE)/tool/net/redbean.com.dbg \
o/$(MODE)/host/third_party/infozip/zip.com \
tool/net/net.mk \
tool/net/.help.txt \
tool/net/help.txt \
tool/net/.init.lua \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.ape tool/net/.help.txt tool/net/.init.lua tool/net/favicon.ico tool/net/redbean.png
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.ape tool/net/help.txt tool/net/.init.lua tool/net/favicon.ico tool/net/redbean.png
# REDBEAN-DEMO.COM
#
@ -115,7 +115,7 @@ o/$(MODE)/tool/net/redbean-demo.com: \
tool/net/net.mk \
tool/net/favicon.ico \
tool/net/redbean.png \
tool/net/.help.txt \
tool/net/help.txt \
tool/net/demo/.init.lua \
tool/net/demo/.reload.lua \
tool/net/demo/.lua/mymodule.lua \
@ -138,7 +138,7 @@ o/$(MODE)/tool/net/redbean-demo.com: \
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-demo
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean-demo/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-demo/.ape tool/net/.help.txt tool/net/demo/.init.lua tool/net/demo/.reload.lua
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-demo/.ape tool/net/help.txt tool/net/demo/.init.lua tool/net/demo/.reload.lua
@$(COMPILE) -ARM -T$@ rm -rf o/$(MODE)/tool/net/.lua
@$(COMPILE) -ACP -T$@ cp -R tool/net/demo/.lua o/$(MODE)/tool/net/
@(cd o/$(MODE)/tool/net && ../../host/third_party/infozip/zip.com -qr redbean-demo.com .lua)
@ -163,12 +163,13 @@ o/$(MODE)/tool/net/redbean-demo.com: \
o/$(MODE)/tool/net/redbean-static.com: \
o/$(MODE)/tool/net/redbean-static.com.dbg \
o/$(MODE)/host/third_party/infozip/zip.com \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-static
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean-static/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-static/.ape tool/net/favicon.ico tool/net/redbean.png
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-static/.ape tool/net/help.txt tool/net/favicon.ico tool/net/redbean.png
o/$(MODE)/tool/net/redbean-static.com.dbg: \
$(TOOL_NET_DEPS) \
@ -190,12 +191,13 @@ o/$(MODE)/tool/net/redbean-static.o: tool/net/redbean.c
o/$(MODE)/tool/net/redbean-unsecure.com: \
o/$(MODE)/tool/net/redbean-unsecure.com.dbg \
o/$(MODE)/host/third_party/infozip/zip.com \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-unsecure
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean-unsecure/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-unsecure/.ape tool/net/favicon.ico tool/net/redbean.png
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-unsecure/.ape tool/net/help.txt tool/net/favicon.ico tool/net/redbean.png
o/$(MODE)/tool/net/redbean-unsecure.com.dbg: \
$(TOOL_NET_DEPS) \
@ -218,12 +220,13 @@ o/$(MODE)/tool/net/redbean-unsecure.o: tool/net/redbean.c
o/$(MODE)/tool/net/redbean-original.com: \
o/$(MODE)/tool/net/redbean-original.com.dbg \
o/$(MODE)/host/third_party/infozip/zip.com \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-original
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean-original/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-original/.ape tool/net/favicon.ico tool/net/redbean.png
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-original/.ape tool/net/help.txt tool/net/favicon.ico tool/net/redbean.png
o/$(MODE)/tool/net/redbean-original.com.dbg: \
$(TOOL_NET_DEPS) \

View file

@ -52,6 +52,7 @@
#include "libc/sock/sock.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/str/undeflate.h"
#include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/auxv.h"
#include "libc/sysv/consts/dt.h"
@ -861,17 +862,29 @@ static void ProgramPort(long port) {
ports.p[ports.n - 1] = port;
}
static void ProgramAddr(const char *addr) {
static uint32_t ResolveIp(const char *addr) {
ssize_t rc;
uint32_t ip;
struct addrinfo *ai = NULL;
struct addrinfo hint = {AI_NUMERICSERV, AF_INET, SOCK_STREAM, IPPROTO_TCP};
if ((rc = getaddrinfo(addr, "0", &hint, &ai)) != EAI_SUCCESS) {
fprintf(stderr, "error: bad addr: %s (EAI_%s)\n", addr, gai_strerror(rc));
exit(1);
}
ips.p = realloc(ips.p, ++ips.n * sizeof(*ips.p));
ips.p[ips.n - 1] = ntohl(ai->ai_addr4->sin_addr.s_addr);
ip = ntohl(ai->ai_addr4->sin_addr.s_addr);
freeaddrinfo(ai);
return ip;
}
static void ProgramAddr(const char *addr) {
uint32_t ip;
if (IsTiny()) {
ip = ParseIp(addr, -1);
} else {
ip = ResolveIp(addr);
}
ips.p = realloc(ips.p, ++ips.n * sizeof(*ips.p));
ips.p[ips.n - 1] = ip;
}
static void ProgramRedirect(int code, const char *sp, size_t sn, const char *dp,
@ -2032,7 +2045,11 @@ static char *AppendContentRange(char *p, long a, long b, long c) {
static bool Inflate(void *dp, size_t dn, const void *sp, size_t sn) {
int rc;
z_stream zs;
struct DeflateState ds;
LockInc(&shared->c.inflates);
if (IsTiny()) {
return undeflate(dp, dn, sp, sn, &ds) != -1;
} else {
zs.next_in = sp;
zs.avail_in = sn;
zs.total_in = sn;
@ -2062,6 +2079,7 @@ static bool Inflate(void *dp, size_t dn, const void *sp, size_t sn) {
rc, dn, sn, (char *)zs.next_in - (char *)sp, zs.avail_in,
(char *)zs.next_out - (char *)dp, zs.avail_out);
}
}
}
static bool Verify(void *data, size_t size, uint32_t crc) {
@ -2130,7 +2148,7 @@ static wontreturn void PrintUsage(FILE *f, int rc) {
size_t n;
const char *p;
struct Asset *a;
if ((a = GetAssetZip("/.help.txt", 10)) && (p = LoadAsset(a, &n))) {
if ((a = GetAssetZip("/help.txt", 10)) && (p = LoadAsset(a, &n))) {
fwrite(p, 1, n, f);
free(p);
}
@ -2285,7 +2303,7 @@ static char *CommitOutput(char *p) {
if (!contentlength) {
if (istext && outbuf.n >= 100) {
p = stpcpy(p, "Vary: Accept-Encoding\r\n");
if (ClientAcceptsGzip()) {
if (!IsTiny() && ClientAcceptsGzip()) {
gzipped = true;
crc = crc32_z(0, outbuf.p, outbuf.n);
WRITE32LE(gzip_footer + 0, crc);
@ -2593,7 +2611,7 @@ static char *ServeListing(void) {
int64_t lastmod;
struct rusage ru;
char *p, *q, *path;
char rb[8], tb[64], *rp[6];
char rb[8], tb[20], *rp[6];
size_t i, n, pathlen, rn[6];
LockInc(&shared->c.listingrequests);
if (msg.method != kHttpGet && msg.method != kHttpHead) return BadMethod();
@ -2651,7 +2669,7 @@ td { padding-right: 3em; }\r\n\
rp[4] = EscapeHtml(rp[0], rn[0], &rn[4]);
lastmod = GetZipCfileLastModified(zcf);
localtime_r(&lastmod, &tm);
strftime(tb, sizeof(tb), "%Y-%m-%d %H:%M:%S %Z", &tm);
iso8601(tb, &tm);
if (IsCompressionMethodSupported(ZIP_CFILE_COMPRESSIONMETHOD(zcf)) &&
IsAcceptablePath(path, pathlen)) {
Append("<a href=\"%.*s\">%-*.*s</a> %s %0*o %4s %,*ld %'s\r\n",
@ -5140,7 +5158,7 @@ static char *ServeAsset(struct Asset *a, const char *path, size_t pathlen) {
} else {
return ServeError(500, "Internal Server Error");
}
} else if (ClientAcceptsGzip() &&
} else if (!IsTiny() && ClientAcceptsGzip() &&
(strlen(ct) >= 5 && !memcasecmp(ct, "text/", 5)) &&
100 < contentlength && contentlength < 1024 * 1024 * 1024) {
p = ServeAssetCompressed(a);
@ -5571,6 +5589,7 @@ static void RestoreApe(void) {
size_t n;
struct Asset *a;
extern char ape_rom_vaddr[] __attribute__((__weak__));
if (!(SUPPORT_VECTOR & (METAL | WINDOWS | XNU))) return;
if (IsWindows()) return; /* TODO */
if (IsOpenbsd()) return; /* TODO */
if (IsNetbsd()) return; /* TODO */