From a7bd4ed9eaba65ab33d619d3bfde24990631c285 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sun, 4 Jul 2021 12:21:29 -0700 Subject: [PATCH] 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. --- libc/log/vflogf.c | 2 +- libc/runtime/fork.c | 2 +- libc/time/iso8601.c | 54 +++++++++++++++++++ libc/time/time.h | 1 + net/http/formathttpdatetime.c | 2 +- tool/net/{.help.txt => help.txt} | 0 tool/net/net.mk | 17 +++--- tool/net/redbean.c | 91 +++++++++++++++++++------------- 8 files changed, 123 insertions(+), 46 deletions(-) create mode 100644 libc/time/iso8601.c rename tool/net/{.help.txt => help.txt} (100%) diff --git a/libc/log/vflogf.c b/libc/log/vflogf.c index 7339ac0af..6df133511 100644 --- a/libc/log/vflogf.c +++ b/libc/log/vflogf.c @@ -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 = "--------------------"; diff --git a/libc/runtime/fork.c b/libc/runtime/fork.c index 8f274f015..2c86fd8d6 100644 --- a/libc/runtime/fork.c +++ b/libc/runtime/fork.c @@ -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 diff --git a/libc/time/iso8601.c b/libc/time/iso8601.c new file mode 100644 index 000000000..8a9b40d60 --- /dev/null +++ b/libc/time/iso8601.c @@ -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; +} diff --git a/libc/time/time.h b/libc/time/time.h index 6d126dd4f..17c05ebdc 100644 --- a/libc/time/time.h +++ b/libc/time/time.h @@ -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) */ diff --git a/net/http/formathttpdatetime.c b/net/http/formathttpdatetime.c index 847ac8c8b..9a5258fd5 100644 --- a/net/http/formathttpdatetime.c +++ b/net/http/formathttpdatetime.c @@ -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++ = ' '; diff --git a/tool/net/.help.txt b/tool/net/help.txt similarity index 100% rename from tool/net/.help.txt rename to tool/net/help.txt diff --git a/tool/net/net.mk b/tool/net/net.mk index 385f8810f..0a69714cb 100644 --- a/tool/net/net.mk +++ b/tool/net/net.mk @@ -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) \ diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 4c15e9c6a..e92ba6c53 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -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,35 +2045,40 @@ 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); - zs.next_in = sp; - zs.avail_in = sn; - zs.total_in = sn; - zs.next_out = dp; - zs.avail_out = dn; - zs.total_out = dn; - zs.zfree = Z_NULL; - zs.zalloc = Z_NULL; - CHECK_EQ(Z_OK, inflateInit2(&zs, -MAX_WBITS)); - switch ((rc = inflate(&zs, Z_NO_FLUSH))) { - case Z_STREAM_END: - CHECK_EQ(Z_OK, inflateEnd(&zs)); - return true; - case Z_DATA_ERROR: - inflateEnd(&zs); - WARNF("Z_DATA_ERROR"); - return false; - case Z_NEED_DICT: - inflateEnd(&zs); - WARNF("Z_NEED_DICT"); - return false; - case Z_MEM_ERROR: - FATALF("Z_MEM_ERROR"); - default: - FATALF("inflate()→%d dn=%ld sn=%ld " - "next_in=%ld avail_in=%ld next_out=%ld avail_out=%ld", - rc, dn, sn, (char *)zs.next_in - (char *)sp, zs.avail_in, - (char *)zs.next_out - (char *)dp, zs.avail_out); + if (IsTiny()) { + return undeflate(dp, dn, sp, sn, &ds) != -1; + } else { + zs.next_in = sp; + zs.avail_in = sn; + zs.total_in = sn; + zs.next_out = dp; + zs.avail_out = dn; + zs.total_out = dn; + zs.zfree = Z_NULL; + zs.zalloc = Z_NULL; + CHECK_EQ(Z_OK, inflateInit2(&zs, -MAX_WBITS)); + switch ((rc = inflate(&zs, Z_NO_FLUSH))) { + case Z_STREAM_END: + CHECK_EQ(Z_OK, inflateEnd(&zs)); + return true; + case Z_DATA_ERROR: + inflateEnd(&zs); + WARNF("Z_DATA_ERROR"); + return false; + case Z_NEED_DICT: + inflateEnd(&zs); + WARNF("Z_NEED_DICT"); + return false; + case Z_MEM_ERROR: + FATALF("Z_MEM_ERROR"); + default: + FATALF("inflate()→%d dn=%ld sn=%ld " + "next_in=%ld avail_in=%ld next_out=%ld avail_out=%ld", + rc, dn, sn, (char *)zs.next_in - (char *)sp, zs.avail_in, + (char *)zs.next_out - (char *)dp, zs.avail_out); + } } } @@ -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("%-*.*s %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 */