mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-03 17:58:30 +00:00
Make major improvements to redbean and libraries
The most exciting improvement is dynamic pages will soon be able to use the executable itself as an object store. it required a heroic technique for overcoming ETXTBSY restrictions which lets us open the executable in read/write mode, which means (1) wa can restore the APE header, and (2) we can potentially containerize redbean extension code so that modules you download for your redbean online will only impact your redbean. Here's a list of breaking changes to redbean: - Remove /tool/net/ prefix from magic ZIP paths - GetHeader() now returns NIL if header is absent Here's a list of fixes and enhancements to redbean: - Support 64-bit ZIP archives - Record User-Agent header in logs - Add twelve error handlers to accept() - Display octal st_mode on listing page - Show ZIP file comments on listing page - Restore APE MZ header on redbean startup - Track request count on redbean index page - Report server uptime on redbean index page - Don't bind server socket using SO_REUSEPORT - Fix #151 where Lua LoadAsset() could free twice - Report rusage accounting when workers exit w/ -vv - Use ZIP iattr field as text/plain vs. binary hint - Add ParseUrl() API for parsing things like a.href - Add ParseParams() API for parsing HTTP POST bodies - Add IsAcceptablePath() API for checking dots, etc. - Add IsValidHttpToken() API for validating sane ASCII - Add IsAcceptableHostPort() for validating HOST[:PORT] - Send 400 response to HTTP/1.1 requests without a Host - Send 403 response if ZIP or file isn't other readable - Add virtual hosting that tries prepending Host to path - Route requests based on Host in Request-URI if present - Host routing will attempt to remove or add the www. prefix - Sign-extend UNIX timestamps and don't adjust FileTime zone Here's some of the improvements made to Cosmopolitan Libc: - Fix ape.S indentation - Improve consts.sh magnums - Write pretty good URL parser - Improve rusage accounting apis - Bring mremap() closer to working - Added ZIP APIs which will change - Check for overflow in reallocarray() - Remove overly fancy linkage in strerror() - Fix GDB attach on crash w/ OpenBSD msyscall() - Make sigqueue() portable to most UNIX distros - Make integer serialization macros more elegant - Bring back 34x tprecode8to16() performance boost - Make malloc() more resilient to absurdly large sizes
This commit is contained in:
parent
69c508729e
commit
bf03b2e64c
307 changed files with 4557 additions and 2581 deletions
|
@ -55,16 +55,6 @@
|
|||
#define ZIP_LOCALFILE_SECTION ".zip.2."
|
||||
#define ZIP_DIRECTORY_SECTION ".zip.4."
|
||||
|
||||
#define PUT8(P, V) *P++ = V
|
||||
#define PUT16(P, V) P[0] = V & 0xff, P[1] = V >> 010 & 0xff, P += 2
|
||||
#define PUT32(P, V) \
|
||||
P[0] = V & 0xff, P[1] = V >> 010 & 0xff, P[2] = V >> 020 & 0xff, \
|
||||
P[3] = V >> 030 & 0xff, P += 4
|
||||
#define PUT64(P, V) \
|
||||
P[0] = V & 0xff, P[1] = V >> 010 & 0xff, P[2] = V >> 020 & 0xff, \
|
||||
P[3] = V >> 030 & 0xff, P[4] = V >> 040 & 0xff, P[5] = V >> 050 & 0xff, \
|
||||
P[6] = V >> 060 & 0xff, P[7] = V >> 070 & 0xff, P += 8
|
||||
|
||||
#define DOS_DATE(YEAR, MONTH_IDX1, DAY_IDX1) \
|
||||
(((YEAR)-1980) << 9 | (MONTH_IDX1) << 5 | (DAY_IDX1))
|
||||
#define DOS_TIME(HOUR, MINUTE, SECOND) \
|
||||
|
@ -116,10 +106,24 @@ void GetOpts(int *argc, char ***argv) {
|
|||
CHECK_NOTNULL(outpath_);
|
||||
}
|
||||
|
||||
bool IsPureAscii(const void *data, size_t size) {
|
||||
bool IsUtf8(const void *data, size_t size) {
|
||||
const unsigned char *p, *pe;
|
||||
for (p = data, pe = p + size; p + 2 <= pe; ++p) {
|
||||
if (*p >= 0300) {
|
||||
if (*p >= 0200 && *p < 0300) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IsText(const void *data, size_t size) {
|
||||
const unsigned char *p, *pe;
|
||||
for (p = data, pe = p + size; p < pe; ++p) {
|
||||
if (!*p || *p >= 0x80) {
|
||||
if (*p <= 3) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -146,86 +150,81 @@ void GetDosLocalTime(int64_t utcunixts, uint16_t *out_time,
|
|||
*out_date = DOS_DATE(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday + 1);
|
||||
}
|
||||
|
||||
static unsigned char *EmitZipLfileHdr(unsigned char *op, const void *name,
|
||||
static int DetermineVersionNeededToExtract(int method) {
|
||||
if (method == kZipCompressionDeflate) {
|
||||
return kZipEra1993;
|
||||
} else {
|
||||
return kZipEra1989;
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned char *EmitZipLfileHdr(unsigned char *p, const void *name,
|
||||
size_t namesize, uint32_t crc,
|
||||
uint8_t era, uint16_t gflags,
|
||||
uint16_t method, uint16_t mtime,
|
||||
uint16_t mdate, size_t compsize,
|
||||
size_t uncompsize) {
|
||||
PUT32(op, kZipLfileHdrMagic);
|
||||
PUT8(op, kZipEra1993);
|
||||
PUT8(op, kZipOsDos);
|
||||
PUT16(op, gflags);
|
||||
PUT16(op, method);
|
||||
PUT16(op, mtime);
|
||||
PUT16(op, mdate);
|
||||
PUT32(op, crc);
|
||||
PUT32(op, compsize);
|
||||
PUT32(op, uncompsize);
|
||||
PUT16(op, namesize);
|
||||
PUT16(op, 0); /* extra */
|
||||
return mempcpy(op, name, namesize);
|
||||
p = WRITE32LE(p, kZipLfileHdrMagic);
|
||||
*p++ = era;
|
||||
*p++ = kZipOsDos;
|
||||
p = WRITE16LE(p, gflags);
|
||||
p = WRITE16LE(p, method);
|
||||
p = WRITE16LE(p, mtime);
|
||||
p = WRITE16LE(p, mdate);
|
||||
p = WRITE32LE(p, crc);
|
||||
p = WRITE32LE(p, compsize);
|
||||
p = WRITE32LE(p, uncompsize);
|
||||
p = WRITE16LE(p, namesize);
|
||||
p = WRITE16LE(p, 0); /* extra */
|
||||
return mempcpy(p, name, namesize);
|
||||
}
|
||||
|
||||
static void EmitZipCdirHdr(unsigned char *op, const void *name, size_t namesize,
|
||||
static void EmitZipCdirHdr(unsigned char *p, const void *name, size_t namesize,
|
||||
uint32_t crc, uint8_t era, uint16_t gflags,
|
||||
uint16_t method, uint16_t mtime, uint16_t mdate,
|
||||
uint16_t iattrs, uint16_t dosmode, uint16_t unixmode,
|
||||
size_t compsize, size_t uncompsize,
|
||||
size_t commentsize, struct stat *st) {
|
||||
uint64_t mt, at, ct;
|
||||
PUT32(op, kZipCfileHdrMagic);
|
||||
PUT8(op, 20);
|
||||
PUT8(op, kZipOsDos);
|
||||
PUT8(op, kZipEra1993);
|
||||
PUT8(op, kZipOsDos);
|
||||
PUT16(op, gflags);
|
||||
PUT16(op, method);
|
||||
PUT16(op, mtime);
|
||||
PUT16(op, mdate);
|
||||
p = WRITE32LE(p, kZipCfileHdrMagic);
|
||||
*p++ = kZipCosmopolitanVersion;
|
||||
*p++ = kZipOsUnix;
|
||||
*p++ = era;
|
||||
*p++ = kZipOsDos;
|
||||
p = WRITE16LE(p, gflags);
|
||||
p = WRITE16LE(p, method);
|
||||
p = WRITE16LE(p, mtime);
|
||||
p = WRITE16LE(p, mdate);
|
||||
/* 16 */
|
||||
PUT32(op, crc);
|
||||
PUT32(op, compsize);
|
||||
PUT32(op, uncompsize);
|
||||
PUT16(op, namesize);
|
||||
#if 0
|
||||
#define CFILE_HDR_SIZE kZipCfileHdrMinSize
|
||||
PUT16(op, 0); /* extra size */
|
||||
/* 32 */
|
||||
PUT16(op, commentsize);
|
||||
PUT16(op, 0); /* disk */
|
||||
PUT16(op, iattrs);
|
||||
PUT16(op, dosmode);
|
||||
PUT16(op, unixmode);
|
||||
PUT32(op, 0); /* RELOCATE ME (kZipCfileOffsetOffset) */
|
||||
/* 46 */
|
||||
memcpy(op, name, namesize);
|
||||
#else
|
||||
p = WRITE32LE(p, crc);
|
||||
p = WRITE32LE(p, compsize);
|
||||
p = WRITE32LE(p, uncompsize);
|
||||
p = WRITE16LE(p, namesize);
|
||||
#define CFILE_HDR_SIZE (kZipCfileHdrMinSize + 36)
|
||||
PUT16(op, 36); /* extra size */
|
||||
p = WRITE16LE(p, 36); /* extra size */
|
||||
/* 32 */
|
||||
PUT16(op, commentsize);
|
||||
PUT16(op, 0); /* disk */
|
||||
PUT16(op, iattrs);
|
||||
PUT32(op, dosmode);
|
||||
PUT32(op, 0); /* RELOCATE ME (kZipCfileOffsetOffset) */
|
||||
p = WRITE16LE(p, commentsize);
|
||||
p = WRITE16LE(p, 0); /* disk */
|
||||
p = WRITE16LE(p, iattrs);
|
||||
p = WRITE16LE(p, dosmode);
|
||||
p = WRITE16LE(p, unixmode);
|
||||
p = WRITE32LE(p, 0); /* RELOCATE ME (kZipCfileOffsetOffset) */
|
||||
/* 46 */
|
||||
memcpy(op, name, namesize);
|
||||
op += namesize;
|
||||
PUT16(op, kZipExtraNtfs);
|
||||
PUT16(op, 32);
|
||||
PUT32(op, 0);
|
||||
PUT16(op, 1);
|
||||
PUT16(op, 24);
|
||||
memcpy(p, name, namesize);
|
||||
p += namesize;
|
||||
p = WRITE16LE(p, kZipExtraNtfs);
|
||||
p = WRITE16LE(p, 32);
|
||||
p = WRITE32LE(p, 0);
|
||||
p = WRITE16LE(p, 1);
|
||||
p = WRITE16LE(p, 24);
|
||||
#define NTTIME(t) \
|
||||
(t.tv_sec + MODERNITYSECONDS) * HECTONANOSECONDS + t.tv_nsec / 100
|
||||
mt = NTTIME(st->st_mtim);
|
||||
at = NTTIME(st->st_atim);
|
||||
ct = NTTIME(st->st_ctim);
|
||||
PUT64(op, mt);
|
||||
PUT64(op, at);
|
||||
PUT64(op, ct);
|
||||
#endif
|
||||
p = WRITE64LE(p, mt);
|
||||
p = WRITE64LE(p, at);
|
||||
p = WRITE64LE(p, ct);
|
||||
}
|
||||
|
||||
void EmitZip(struct ElfWriter *elf, const char *name, size_t namesize,
|
||||
|
@ -238,15 +237,17 @@ void EmitZip(struct ElfWriter *elf, const char *name, size_t namesize,
|
|||
size_t lfilehdrsize, uncompsize, compsize, commentsize;
|
||||
uint16_t method, gflags, mtime, mdate, iattrs, dosmode;
|
||||
|
||||
gflags = 0;
|
||||
iattrs = 0;
|
||||
compsize = st->st_size;
|
||||
uncompsize = st->st_size;
|
||||
CHECK_LE(uncompsize, UINT32_MAX);
|
||||
lfilehdrsize = kZipLfileHdrMinSize + namesize;
|
||||
crc = crc32_z(0, data, uncompsize);
|
||||
GetDosLocalTime(st->st_mtim.tv_sec, &mtime, &mdate);
|
||||
gflags = IsPureAscii(name, namesize) ? 0 : kZipGflagUtf8;
|
||||
if (IsUtf8(name, namesize)) gflags |= kZipGflagUtf8;
|
||||
if (IsText(data, st->st_size)) iattrs |= kZipIattrText;
|
||||
commentsize = kZipCdirHdrLinkableSize - (CFILE_HDR_SIZE + namesize);
|
||||
iattrs = IsPureAscii(data, st->st_size) ? kZipIattrAscii : kZipIattrBinary;
|
||||
dosmode = !(st->st_mode & 0200) ? kNtFileAttributeReadonly : 0;
|
||||
method = (st->st_size >= kMinCompressSize && ShouldCompress(name, namesize))
|
||||
? kZipCompressionDeflate
|
||||
|
@ -280,7 +281,7 @@ void EmitZip(struct ElfWriter *elf, const char *name, size_t namesize,
|
|||
if (method == kZipCompressionNone) {
|
||||
memcpy(lfile + lfilehdrsize, data, uncompsize);
|
||||
}
|
||||
era = (gflags || method) ? kZipEra1993 : kZipEra1989;
|
||||
era = method ? kZipEra1993 : kZipEra1989;
|
||||
EmitZipLfileHdr(lfile, name, namesize, crc, era, gflags, method, mtime, mdate,
|
||||
compsize, uncompsize);
|
||||
elfwriter_commit(elf, lfilehdrsize + compsize);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue