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:
Justine Tunney 2021-04-18 11:34:59 -07:00
parent 69c508729e
commit bf03b2e64c
307 changed files with 4557 additions and 2581 deletions

View file

@ -18,6 +18,7 @@
*/
#include "libc/macros.internal.h"
#include "libc/math.h"
#include "libc/str/str.h"
#include "tool/build/lib/cvt.h"
#include "tool/build/lib/endian.h"
#include "tool/build/lib/machine.h"

View file

@ -1,116 +1,53 @@
#ifndef COSMOPOLITAN_TOOL_BUILD_LIB_ENDIAN_H_
#define COSMOPOLITAN_TOOL_BUILD_LIB_ENDIAN_H_
#include "libc/dce.h"
#include "libc/str/str.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#if __BYTE_ORDER__ + 0 == 1234
#include "libc/bits/bits.h"
#define Read8(P) \
({ \
uint8_t *Ptr = (P); \
*Ptr; \
#define Read8(P) (*(const uint8_t *)(P))
#define Read16(P) \
({ \
const uint8_t *Ptr = (const uint8_t *)(P); \
READ16LE(P); \
})
#define Read16(P) \
({ \
uint16_t Res; \
uint8_t *Ptr = (P); \
memcpy(&Res, Ptr, 2); \
Res; \
#define Read32(P) \
({ \
const uint8_t *Ptr = (const uint8_t *)(P); \
READ32LE(P); \
})
#define Read32(P) \
({ \
uint32_t Res; \
uint8_t *Ptr = (P); \
memcpy(&Res, Ptr, 4); \
Res; \
#define Read64(P) \
({ \
const uint8_t *Ptr = (const uint8_t *)(P); \
READ64LE(P); \
})
#define Read64(P) \
({ \
uint64_t Res; \
uint8_t *Ptr = (P); \
memcpy(&Res, Ptr, 8); \
Res; \
})
#define Write8(P, B) \
#define Write8(P, V) \
do { \
uint8_t Val = (V); \
uint8_t *Ptr = (P); \
*Ptr = (B); \
*Ptr = Val; \
} while (0)
#define Write16(P, V) \
do { \
uint16_t Val = (V); \
uint8_t *Ptr = (P); \
memcpy(Ptr, &Val, 2); \
#define Write16(P, V) \
do { \
uint16_t Val = (V); \
uint8_t *Ptr = (P); \
WRITE16LE(Ptr, Val); \
} while (0)
#define Write32(P, V) \
do { \
uint32_t Val = (V); \
uint8_t *Ptr = (P); \
memcpy(Ptr, &Val, 4); \
#define Write32(P, V) \
do { \
uint32_t Val = (V); \
uint8_t *Ptr = (P); \
WRITE32LE(Ptr, Val); \
} while (0)
#define Write64(P, V) \
do { \
uint64_t Val = (V); \
uint8_t *Ptr = (P); \
memcpy(Ptr, &Val, 8); \
#define Write64(P, V) \
do { \
uint64_t Val = (V); \
uint8_t *Ptr = (P); \
WRITE64LE(Ptr, Val); \
} while (0)
#else
forceinline uint16_t Read8(const uint8_t p[hasatleast 1]) {
return p[0];
}
forceinline uint16_t Read16(const uint8_t p[hasatleast 2]) {
return p[0] | p[1] << 010;
}
forceinline uint32_t Read32(const uint8_t bytes[hasatleast 4]) {
return (uint32_t)bytes[0] << 000 | (uint32_t)bytes[1] << 010 |
(uint32_t)bytes[2] << 020 | (uint32_t)bytes[3] << 030;
}
forceinline uint64_t Read64(const uint8_t bytes[hasatleast 8]) {
return (uint64_t)bytes[0] << 000 | (uint64_t)bytes[1] << 010 |
(uint64_t)bytes[2] << 020 | (uint64_t)bytes[3] << 030 |
(uint64_t)bytes[4] << 040 | (uint64_t)bytes[5] << 050 |
(uint64_t)bytes[6] << 060 | (uint64_t)bytes[7] << 070;
}
forceinline void Write8(unsigned char p[hasatleast 1], uint8_t x) {
p[0] = x >> 000;
}
forceinline void Write16(unsigned char p[hasatleast 2], uint16_t x) {
p[0] = x >> 000;
p[1] = x >> 010;
}
forceinline void Write32(unsigned char p[hasatleast 4], uint64_t x) {
p[0] = x >> 000;
p[1] = x >> 010;
p[2] = x >> 020;
p[3] = x >> 030;
}
forceinline void Write64(unsigned char p[hasatleast 8], uint64_t x) {
p[0] = x >> 000;
p[1] = x >> 010;
p[2] = x >> 020;
p[3] = x >> 030;
p[4] = x >> 040;
p[5] = x >> 050;
p[6] = x >> 060;
p[7] = x >> 070;
}
#endif /* ENDIAN */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_TOOL_BUILD_LIB_ENDIAN_H_ */

View file

@ -20,6 +20,7 @@
#include "libc/macros.internal.h"
#include "libc/rand/rand.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "tool/build/lib/abp.h"
#include "tool/build/lib/address.h"
#include "tool/build/lib/alu.h"
@ -1568,7 +1569,11 @@ static void Op1ae(struct Machine *m, uint32_t rde) {
}
static void OpSalc(struct Machine *m, uint32_t rde) {
Write8(m->ax, GetFlag(m->flags, FLAGS_CF));
if (GetFlag(m->flags, FLAGS_CF)) {
m->ax[0] = 255;
} else {
m->ax[0] = 0;
}
}
static void OpBofram(struct Machine *m, uint32_t rde) {

View file

@ -18,6 +18,7 @@
*/
#include "libc/log/check.h"
#include "libc/macros.internal.h"
#include "libc/str/str.h"
#include "tool/build/lib/address.h"
#include "tool/build/lib/endian.h"
#include "tool/build/lib/memory.h"

View file

@ -77,6 +77,8 @@
#include "tool/build/lib/throw.h"
#include "tool/build/lib/xlaterrno.h"
#define SA_RESTORER 0x04000000
#define AT_FDCWD_LINUX -100
#define TIOCGWINSZ_LINUX 0x5413
#define TCGETS_LINUX 0x5401

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/str/str.h"
#include "tool/build/lib/endian.h"
#include "tool/build/lib/memory.h"
#include "tool/build/lib/word.h"

View file

@ -25,6 +25,7 @@
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/dce.h"
#include "libc/elf/def.h"
#include "libc/elf/elf.h"
#include "libc/elf/struct/rela.h"
@ -165,8 +166,8 @@ struct Packages {
};
int CompareSymbolName(const struct Symbol *a, const struct Symbol *b,
const char *strs[hasatleast 2]) {
return strcmp(&strs[0][a->name], &strs[1][b->name]);
const char *tab) {
return strcmp(tab + a->name, tab + b->name);
}
struct Package *LoadPackage(const char *path) {
@ -189,6 +190,7 @@ struct Package *LoadPackage(const char *path) {
pkg->strings.p);
pkg->addr = pkg;
pkg->size = st.st_size;
CHECK_NE(-1, mprotect(pkg, st.st_size, PROT_READ));
return pkg;
}
@ -378,37 +380,48 @@ void LoadObjects(struct Package *pkg) {
size_t i;
struct Object *obj;
for (i = 0; i < pkg->objects.i; ++i) {
obj = &pkg->objects.p[i];
obj = pkg->objects.p + i;
OpenObject(pkg, obj, O_RDONLY, PROT_READ, MAP_SHARED);
LoadSymbols(pkg, i);
CloseObject(obj);
}
qsort_r(&pkg->symbols.p[0], pkg->symbols.i, sizeof(pkg->symbols.p[0]),
(void *)CompareSymbolName,
(const char *[2]){pkg->strings.p, pkg->strings.p});
qsort_r(pkg->symbols.p, pkg->symbols.i, sizeof(*pkg->symbols.p),
(void *)CompareSymbolName, pkg->strings.p);
}
struct Symbol *BisectSymbol(struct Package *pkg, const char *name) {
int c;
long m, l, r;
l = 0;
r = pkg->symbols.i - 1;
while (l <= r) {
m = (l + r) >> 1;
c = strcmp(pkg->strings.p + pkg->symbols.p[m].name, name);
if (c < 0) {
l = m + 1;
} else if (c > 0) {
r = m - 1;
} else {
return pkg->symbols.p + m;
}
}
return NULL;
}
bool FindSymbol(const char *name, struct Package *pkg,
struct Packages *directdeps, struct Package **out_pkg,
struct Symbol **out_sym) {
size_t i;
struct Package *dep;
struct Symbol key, *sym;
key.name = 0;
if ((sym = bisect(&key, &pkg->symbols.p[0], pkg->symbols.i,
sizeof(pkg->symbols.p[0]), (void *)CompareSymbolName,
(const char *[2]){name, pkg->strings.p}))) {
if (out_pkg) *out_pkg = pkg;
size_t i, j;
struct Symbol *sym;
if ((sym = BisectSymbol(pkg, name))) {
if (out_sym) *out_sym = sym;
if (out_pkg) *out_pkg = pkg;
return true;
}
for (i = 0; i < directdeps->i; ++i) {
dep = directdeps->p[i];
if ((sym = bisect(&key, &dep->symbols.p[0], dep->symbols.i,
sizeof(dep->symbols.p[0]), (void *)CompareSymbolName,
(const char *[2]){name, dep->strings.p}))) {
if (out_pkg) *out_pkg = dep;
if ((sym = BisectSymbol(directdeps->p[i], name))) {
if (out_sym) *out_sym = sym;
if (out_pkg) *out_pkg = directdeps->p[i];
return true;
}
}
@ -422,15 +435,15 @@ void CheckStrictDeps(struct Package *pkg, struct Packages *deps) {
for (i = 0; i < pkg->undefs.i; ++i) {
undef = &pkg->undefs.p[i];
if (undef->bind == STB_WEAK) continue;
if (!FindSymbol(&pkg->strings.p[undef->name], pkg, deps, NULL, NULL)) {
fprintf(stderr, "%s: %s (%s) %s %s\n", "error",
&pkg->strings.p[undef->name],
&pkg->strings.p[pkg->objects.p[undef->object].path],
"not defined by direct deps of", &pkg->strings.p[pkg->path]);
if (!FindSymbol(pkg->strings.p + undef->name, pkg, deps, NULL, NULL)) {
fprintf(stderr, "%s: %`'s (%s) %s %s\n", "error",
pkg->strings.p + undef->name,
pkg->strings.p + pkg->objects.p[undef->object].path,
"not defined by direct deps of", pkg->strings.p + pkg->path);
for (j = 0; j < deps->i; ++j) {
dep = deps->p[j];
fputc('\t', stderr);
fputs(&dep->strings.p[dep->path], stderr);
fputs(dep->strings.p + dep->path, stderr);
fputc('\n', stderr);
}
exit(1);

View file

@ -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);

View file

@ -29,13 +29,15 @@ const struct IdName kZipCompressionNames[] = {
const struct IdName kZipExtraNames[] = {
{kZipExtraZip64, "kZipExtraZip64"},
{kZipExtraNtfs, "kZipExtraNtfs"},
{kZipExtraUnix, "kZipExtraUnix"},
{kZipExtraExtendedTimestamp, "kZipExtraExtendedTimestamp"},
{kZipExtraInfoZipNewUnixExtra, "kZipExtraInfoZipNewUnixExtra"},
{0, 0},
};
const struct IdName kZipIattrNames[] = {
{kZipIattrBinary, "kZipIattrBinary"},
{kZipIattrAscii, "kZipIattrAscii"},
{kZipIattrText, "kZipIattrText"},
{0, 0},
};

View file

@ -22,6 +22,7 @@
#include "libc/calls/struct/stat.h"
#include "libc/fmt/conv.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/nt/struct/filetime.h"
@ -45,17 +46,17 @@
* @fileoverview Zip File Disassembler.
*/
nodiscard char *formatdosdate(uint16_t dosdate) {
nodiscard char *FormatDosDate(uint16_t dosdate) {
return xasprintf("%04u-%02u-%02u", ((dosdate >> 9) & 0b1111111) + 1980,
(dosdate >> 5) & 0b1111, dosdate & 0b11111);
}
nodiscard char *formatdostime(uint16_t dostime) {
nodiscard char *FormatDosTime(uint16_t dostime) {
return xasprintf("%02u:%02u:%02u", (dostime >> 11) & 0b11111,
(dostime >> 5) & 0b111111, (dostime << 1) & 0b111110);
}
void advancepos(uint8_t *map, size_t *pos, size_t off) {
void AdvancePosition(uint8_t *map, size_t *pos, size_t off) {
CHECK_GE(off, *pos);
if (off > *pos) {
printf("\n/\t<%s>\n", "LIMBO");
@ -65,7 +66,7 @@ void advancepos(uint8_t *map, size_t *pos, size_t off) {
*pos = off;
}
void showgeneralflag(uint16_t generalflag) {
void ShowGeneralFlag(uint16_t generalflag) {
puts("\
/ utf8\n\
/ strong encryption\n\
@ -77,21 +78,21 @@ void showgeneralflag(uint16_t generalflag) {
show(".short", format(b1, "0b%016b", generalflag), "generalflag");
}
void showtimestamp(uint16_t time, uint16_t date) {
void ShowTimestamp(uint16_t time, uint16_t date) {
show(".short", format(b1, "%#04hx", time),
gc(xasprintf("%s (%s)", "lastmodifiedtime", gc(formatdostime(time)))));
gc(xasprintf("%s (%s)", "lastmodifiedtime", gc(FormatDosTime(time)))));
show(".short", format(b1, "%#04hx", date),
gc(xasprintf("%s (%s)", "lastmodifieddate", gc(formatdosdate(date)))));
gc(xasprintf("%s (%s)", "lastmodifieddate", gc(FormatDosDate(date)))));
}
void showcompressmethod(uint16_t compressmethod) {
void ShowCompressionMethod(uint16_t compressmethod) {
show(".short",
firstnonnull(findnamebyid(kZipCompressionNames, compressmethod),
format(b1, "%hu", compressmethod)),
"compressionmethod");
}
void showextrantfs(uint8_t *ntfs) {
void ShowNtfs(uint8_t *ntfs, size_t n) {
struct timespec mtime, atime, ctime;
mtime = FileTimeToTimeSpec(
(struct NtFileTime){READ32LE(ntfs + 8), READ32LE(ntfs + 12)});
@ -115,47 +116,85 @@ void showextrantfs(uint8_t *ntfs) {
void ShowExtendedTimestamp(uint8_t *p, size_t n, bool islocal) {
int flag;
int64_t x;
struct timespec ts;
flag = *p++;
show(".byte", gc(xasprintf("0b%03hhb", flag)), "fields present in local");
if (!islocal) {
show(".quad", gc(xasprintf("%u", READ32LE(p))),
gc(xasprintf("%s (%s)", "last modified", gc(xiso8601(&ts)))));
} else {
if (flag & 1) {
ts = (struct timespec){READ32LE(p)};
show(".quad", gc(xasprintf("%u", READ32LE(p))),
gc(xasprintf("%s (%s)", "last modified", gc(xiso8601(&ts)))));
if (n) {
--n;
flag = *p++;
show(".byte", gc(xasprintf("0b%03hhb", flag)), "fields present in local");
if ((flag & 1) && n >= 4) {
show(".long", gc(xasprintf("%u", READ32LE(p))),
gc(xasprintf("%s (%s)", "last modified",
gc(xiso8601(&(struct timespec){READ32LE(p)})))));
p += 4;
n -= 4;
}
flag >>= 1;
if (flag & 1) {
ts = (struct timespec){READ32LE(p)};
show(".quad", gc(xasprintf("%u", READ32LE(p))),
gc(xasprintf("%s (%s)", "access time", gc(xiso8601(&ts)))));
p += 4;
}
flag >>= 1;
if (flag & 1) {
ts = (struct timespec){READ32LE(p)};
show(".quad", gc(xasprintf("%u", READ32LE(p))),
gc(xasprintf("%s (%s)", "creation time", gc(xiso8601(&ts)))));
if (islocal) {
if ((flag & 1) && n >= 4) {
show(".long", gc(xasprintf("%u", READ32LE(p))),
gc(xasprintf("%s (%s)", "access time",
gc(xiso8601(&(struct timespec){READ32LE(p)})))));
p += 4;
n -= 4;
}
flag >>= 1;
if ((flag & 1) && n >= 4) {
show(".long", gc(xasprintf("%u", READ32LE(p))),
gc(xasprintf("%s (%s)", "creation time",
gc(xiso8601(&(struct timespec){READ32LE(p)})))));
p += 4;
n -= 4;
}
}
}
}
void showextra(uint8_t *extra, bool islocal) {
void ShowZip64(uint8_t *p, size_t n, bool islocal) {
if (n >= 8) {
show(".quad", gc(xasprintf("%lu", READ64LE(p))),
gc(xasprintf("uncompressed size (%,ld)", READ64LE(p))));
}
if (n >= 16) {
show(".quad", gc(xasprintf("%lu", READ64LE(p + 8))),
gc(xasprintf("compressed size (%,ld)", READ64LE(p + 8))));
}
if (n >= 24) {
show(".quad", gc(xasprintf("%lu", READ64LE(p + 16))),
gc(xasprintf("lfile hdr offset (%,ld)", READ64LE(p + 16))));
}
if (n >= 28) {
show(".long", gc(xasprintf("%u", READ32LE(p + 24))), "disk number");
}
}
void ShowInfoZipNewUnixExtra(uint8_t *p, size_t n, bool islocal) {
if (p[0] == 1 && p[1] == 4 && p[6] == 4) {
show(".byte", "1", "version");
show(".byte", "4", "uid length");
show(".long", gc(xasprintf("%u", READ32LE(p + 2))), "uid");
show(".byte", "4", "gid length");
show(".long", gc(xasprintf("%u", READ32LE(p + 7))), "gid");
} else {
disassemblehex(p, n, stdout);
}
}
void ShowExtra(uint8_t *extra, bool islocal) {
switch (ZIP_EXTRA_HEADERID(extra)) {
case kZipExtraNtfs:
showextrantfs(ZIP_EXTRA_CONTENT(extra));
ShowNtfs(ZIP_EXTRA_CONTENT(extra), ZIP_EXTRA_CONTENTSIZE(extra));
break;
case kZipExtraExtendedTimestamp:
ShowExtendedTimestamp(ZIP_EXTRA_CONTENT(extra),
ZIP_EXTRA_CONTENTSIZE(extra), islocal);
break;
case kZipExtraZip64:
/* TODO */
ShowZip64(ZIP_EXTRA_CONTENT(extra), ZIP_EXTRA_CONTENTSIZE(extra),
islocal);
break;
case kZipExtraInfoZipNewUnixExtra:
ShowInfoZipNewUnixExtra(ZIP_EXTRA_CONTENT(extra),
ZIP_EXTRA_CONTENTSIZE(extra), islocal);
break;
default:
disassemblehex(ZIP_EXTRA_CONTENT(extra), ZIP_EXTRA_CONTENTSIZE(extra),
stdout);
@ -163,7 +202,7 @@ void showextra(uint8_t *extra, bool islocal) {
}
}
void showexternalattributes(uint8_t *cf) {
void ShowExternalAttributes(uint8_t *cf) {
uint32_t ea;
ea = ZIP_CFILE_EXTERNALATTRIBUTES(cf);
if (ZIP_CFILE_FILEATTRCOMPAT(cf) == kZipOsUnix) {
@ -175,7 +214,7 @@ void showexternalattributes(uint8_t *cf) {
}
}
void showextras(uint8_t *extras, uint16_t extrassize, bool islocal) {
void ShowExtras(uint8_t *extras, uint16_t extrassize, bool islocal) {
int i;
bool first;
uint8_t *p, *pe;
@ -194,14 +233,14 @@ void showextras(uint8_t *extras, uint16_t extrassize, bool islocal) {
first = false;
printf("%d:", (i + 1) * 10);
}
showextra(p, islocal);
ShowExtra(p, islocal);
printf("%d:", (i + 2) * 10);
}
}
putchar('\n');
}
void showlocalfileheader(uint8_t *lf, uint16_t idx) {
void ShowLocalFileHeader(uint8_t *lf, uint16_t idx) {
printf("\n/\t%s #%hu (%zu %s)\n", "local file", idx + 1,
ZIP_LFILE_HDRSIZE(lf), "bytes");
show(".ascii", format(b1, "%`'.*s", 4, lf), "magic");
@ -213,17 +252,23 @@ void showlocalfileheader(uint8_t *lf, uint16_t idx) {
firstnonnull(findnamebyid(kZipOsNames, ZIP_LFILE_OSNEED(lf)),
gc(xasprintf("%d", ZIP_LFILE_OSNEED(lf)))),
"os need");
showgeneralflag(ZIP_LFILE_GENERALFLAG(lf));
showcompressmethod(ZIP_LFILE_COMPRESSIONMETHOD(lf));
showtimestamp(ZIP_LFILE_LASTMODIFIEDTIME(lf), ZIP_LFILE_LASTMODIFIEDDATE(lf));
show(".long", format(b1, "%#x", ZIP_LFILE_CRC32(lf)),
gc(xasprintf(
"%s (%#x)", "crc32z",
crc32_z(0, ZIP_LFILE_CONTENT(lf), ZIP_LFILE_COMPRESSEDSIZE(lf)))));
show(".long", "3f-2f",
format(b1, "%s (%u %s)", "compressedsize", ZIP_LFILE_COMPRESSEDSIZE(lf),
"bytes"));
show(".long", format(b1, "%u", ZIP_LFILE_UNCOMPRESSEDSIZE(lf)),
ShowGeneralFlag(ZIP_LFILE_GENERALFLAG(lf));
ShowCompressionMethod(ZIP_LFILE_COMPRESSIONMETHOD(lf));
ShowTimestamp(ZIP_LFILE_LASTMODIFIEDTIME(lf), ZIP_LFILE_LASTMODIFIEDDATE(lf));
show(
".long",
format(b1, "%#x", ZIP_LFILE_CRC32(lf)), gc(xasprintf("%s (%#x)", "crc32z", GetZipLfileCompressedSize(lf) /* crc32_z(0, ZIP_LFILE_CONTENT(lf), GetZipLfileCompressedSize(lf)) */)));
if (ZIP_LFILE_COMPRESSEDSIZE(lf) == 0xFFFFFFFF) {
show(".long", "0xFFFFFFFF", "compressedsize (zip64)");
} else {
show(".long", "3f-2f",
format(b1, "%s (%u %s)", "compressedsize",
ZIP_LFILE_COMPRESSEDSIZE(lf), "bytes"));
}
show(".long",
ZIP_LFILE_UNCOMPRESSEDSIZE(lf) == 0xFFFFFFFF
? "0xFFFFFFFF"
: format(b1, "%u", ZIP_LFILE_UNCOMPRESSEDSIZE(lf)),
"uncompressedsize");
show(".short", "1f-0f",
format(b1, "%s (%hu %s)", "namesize", ZIP_LFILE_NAMESIZE(lf), "bytes"));
@ -236,17 +281,19 @@ void showlocalfileheader(uint8_t *lf, uint16_t idx) {
gc(strndup(ZIP_LFILE_NAME(lf), ZIP_LFILE_NAMESIZE(lf)))),
"name");
printf("1:");
showextras(ZIP_LFILE_EXTRA(lf), ZIP_LFILE_EXTRASIZE(lf), true);
ShowExtras(ZIP_LFILE_EXTRA(lf), ZIP_LFILE_EXTRASIZE(lf), true);
printf("2:");
disassemblehex(ZIP_LFILE_CONTENT(lf), ZIP_LFILE_COMPRESSEDSIZE(lf), stdout);
/* disassemblehex(ZIP_LFILE_CONTENT(lf), ZIP_LFILE_COMPRESSEDSIZE(lf),
* stdout); */
printf("3:\n");
}
void showcentralfileheader(uint8_t *cf) {
void ShowCentralFileHeader(uint8_t *cf) {
printf("\n/\t%s (%zu %s)\n", "central directory file header",
ZIP_CFILE_HDRSIZE(cf), "bytes");
show(".ascii", format(b1, "%`'.*s", 4, cf), "magic");
show(".byte", gc(xasprintf("%d", ZIP_CFILE_VERSIONMADE(cf))), "version made");
show(".byte", gc(xasprintf("%d", ZIP_CFILE_VERSIONMADE(cf))),
"zip version made");
show(".byte",
firstnonnull(findnamebyid(kZipOsNames, ZIP_CFILE_FILEATTRCOMPAT(cf)),
gc(xasprintf("%d", ZIP_CFILE_FILEATTRCOMPAT(cf)))),
@ -259,14 +306,22 @@ void showcentralfileheader(uint8_t *cf) {
firstnonnull(findnamebyid(kZipOsNames, ZIP_CFILE_OSNEED(cf)),
gc(xasprintf("%d", ZIP_CFILE_OSNEED(cf)))),
"os need");
showgeneralflag(ZIP_CFILE_GENERALFLAG(cf));
showcompressmethod(ZIP_CFILE_COMPRESSIONMETHOD(cf));
showtimestamp(ZIP_CFILE_LASTMODIFIEDTIME(cf), ZIP_CFILE_LASTMODIFIEDDATE(cf));
ShowGeneralFlag(ZIP_CFILE_GENERALFLAG(cf));
ShowCompressionMethod(ZIP_CFILE_COMPRESSIONMETHOD(cf));
ShowTimestamp(ZIP_CFILE_LASTMODIFIEDTIME(cf), ZIP_CFILE_LASTMODIFIEDDATE(cf));
show(".long", format(b1, "%#x", ZIP_CFILE_CRC32(cf)), "crc32z");
show(".long", format(b1, "%u", ZIP_CFILE_COMPRESSEDSIZE(cf)),
"compressedsize");
show(".long", format(b1, "%u", ZIP_CFILE_UNCOMPRESSEDSIZE(cf)),
"uncompressedsize");
if (ZIP_CFILE_COMPRESSEDSIZE(cf) == 0xFFFFFFFF) {
show(".long", "0xFFFFFFFF", "compressedsize (zip64)");
} else {
show(".long", format(b1, "%u", ZIP_CFILE_COMPRESSEDSIZE(cf)),
"compressedsize");
}
if (ZIP_CFILE_UNCOMPRESSEDSIZE(cf) == 0xFFFFFFFF) {
show(".long", "0xFFFFFFFF", "compressedsize (zip64)");
} else {
show(".long", format(b1, "%u", ZIP_CFILE_UNCOMPRESSEDSIZE(cf)),
"uncompressedsize");
}
show(".short", "1f-0f",
format(b1, "%s (%hu %s)", "namesize", ZIP_CFILE_NAMESIZE(cf), "bytes"));
show(
@ -277,23 +332,29 @@ void showcentralfileheader(uint8_t *cf) {
"bytes"));
show(".short", format(b1, "%hu", ZIP_CFILE_DISK(cf)), "disk");
show(".short",
RecreateFlags(kZipIattrNames, ZIP_CFILE_INTERNALATTRIBUTES(cf)),
RecreateFlags(kZipIattrNames, ZIP_CFILE_INTERNALATTRIBUTES(cf) & 1),
"internalattributes");
showexternalattributes(cf);
show(".long", format(b1, "%u", ZIP_CFILE_OFFSET(cf)), "lfile hdr offset");
ShowExternalAttributes(cf);
if (ZIP_CFILE_OFFSET(cf) == 0xFFFFFFFF) {
show(".long", "0xFFFFFFFF", "lfile hdr offset (zip64)");
} else {
show(".long", format(b1, "%u", ZIP_CFILE_OFFSET(cf)), "lfile hdr offset");
}
printf("0:");
show(".ascii",
format(b1, "%`'s",
gc(strndup(ZIP_CFILE_NAME(cf), ZIP_CFILE_NAMESIZE(cf)))),
"name");
printf("1:");
showextras(ZIP_CFILE_EXTRA(cf), ZIP_CFILE_EXTRASIZE(cf), false);
ShowExtras(ZIP_CFILE_EXTRA(cf), ZIP_CFILE_EXTRASIZE(cf), false);
printf("2:");
disassemblehex(ZIP_CFILE_COMMENT(cf), ZIP_CFILE_COMMENTSIZE(cf), stdout);
show(".ascii",
format(b1, "%`'.*s", ZIP_CFILE_COMMENTSIZE(cf), ZIP_CFILE_COMMENT(cf)),
"comment");
printf("3:\n");
}
void showcentraldirheader(uint8_t *cd) {
void ShowCentralDirHeader32(uint8_t *cd) {
printf("\n/\t%s (%zu %s)\n", "end of central directory header",
ZIP_CDIR_HDRSIZE(cd), "bytes");
show(".ascii", format(b1, "%`'.*s", 4, cd), "magic");
@ -312,39 +373,116 @@ void showcentraldirheader(uint8_t *cd) {
printf("1:\n");
}
void disassemblezip(uint8_t *map, size_t mapsize) {
void ShowCentralDirHeader64(uint8_t *cd) {
printf("\n/\t%s (%zu %s)\n", "zip64 end of central directory header",
ZIP_CDIR64_HDRSIZE(cd), "bytes");
show(".ascii", format(b1, "%`'.*s", 4, cd), "magic");
show(".quad", format(b1, "%lu", ZIP_CDIR64_HDRSIZE(cd) - 12), "hdr size");
show(".short", format(b1, "%hd", ZIP_CDIR64_VERSIONMADE(cd)), "version made");
show(".short", format(b1, "%hd", ZIP_CDIR64_VERSIONNEED(cd)), "version need");
show(".long", format(b1, "%d", ZIP_CDIR64_DISK(cd)), "disk");
show(".long", format(b1, "%d", ZIP_CDIR64_STARTINGDISK(cd)), "startingdisk");
show(".quad", format(b1, "%lu", ZIP_CDIR64_RECORDSONDISK(cd)),
"recordsondisk");
show(".quad", format(b1, "%lu", ZIP_CDIR64_RECORDS(cd)), "records");
show(".quad", format(b1, "%lu", ZIP_CDIR64_SIZE(cd)), "cdir size");
show(".quad", format(b1, "%lu", ZIP_CDIR64_OFFSET(cd)), "cdir offset");
printf("0:");
disassemblehex(ZIP_CDIR64_COMMENT(cd), ZIP_CDIR64_COMMENTSIZE(cd), stdout);
printf("1:\n");
}
uint8_t *GetZipCdir32(const uint8_t *p, size_t n) {
size_t i;
if (n >= kZipCdirHdrMinSize) {
i = n - kZipCdirHdrMinSize;
do {
if (READ32LE(p + i) == kZipCdirHdrMagic && IsZipCdir32(p, n, i)) {
return (/*unconst*/ uint8_t *)(p + i);
}
} while (i--);
}
return NULL;
}
uint8_t *GetZipCdir64(const uint8_t *p, size_t n) {
size_t i;
if (n >= kZipCdir64HdrMinSize) {
i = n - kZipCdir64HdrMinSize;
do {
if (READ32LE(p + i) == kZipCdir64HdrMagic && IsZipCdir64(p, n, i)) {
return (/*unconst*/ uint8_t *)(p + i);
}
} while (i--);
}
return NULL;
}
void DisassembleZip(const char *path, uint8_t *p, size_t n) {
size_t pos;
uint16_t i;
static int records;
uint8_t *cd, *cf, *lf;
CHECK_NOTNULL((cd = zipfindcentraldir(map, mapsize)));
uint8_t *eocd32, *eocd64, *cdir, *cf, *lf, *q;
if (endswith(path, ".com.dbg") && (q = memmem(p, n, "MZqFpD", 6))) {
n -= q - p;
p += q - p;
}
eocd32 = GetZipCdir32(p, n);
eocd64 = GetZipCdir64(p, n);
CHECK(eocd32 || eocd64);
pos = 0;
records = ZIP_CDIR_RECORDS(cd);
for (i = 0, cf = map + ZIP_CDIR_OFFSET(cd); i < records;
++i, cf += ZIP_CFILE_HDRSIZE(cf)) {
lf = map + ZIP_CFILE_OFFSET(cf);
if (eocd64) {
records = ZIP_CDIR64_RECORDS(eocd64);
cdir = p + ZIP_CDIR64_OFFSET(eocd64);
} else {
records = ZIP_CDIR_RECORDS(eocd32);
cdir = p + ZIP_CDIR_OFFSET(eocd32);
}
for (i = 0, cf = cdir; i < records; ++i, cf += ZIP_CFILE_HDRSIZE(cf)) {
lf = p + GetZipCfileOffset(cf);
CHECK_EQ(kZipLfileHdrMagic, ZIP_LFILE_MAGIC(lf));
advancepos(map, &pos, lf - map);
showlocalfileheader(lf, i);
pos = (lf - map) + ZIP_LFILE_SIZE(lf);
AdvancePosition(p, &pos, lf - p);
ShowLocalFileHeader(lf, i);
pos = (lf - p) + ZIP_LFILE_SIZE(lf);
}
for (i = 0, cf = map + ZIP_CDIR_OFFSET(cd); i < records;
++i, cf += ZIP_CFILE_HDRSIZE(cf)) {
for (i = 0, cf = cdir; i < records; ++i, cf += ZIP_CFILE_HDRSIZE(cf)) {
CHECK_EQ(kZipCfileHdrMagic, ZIP_CFILE_MAGIC(cf));
advancepos(map, &pos, cf - map);
showcentralfileheader(cf);
pos = (cf - map) + ZIP_CFILE_HDRSIZE(cf);
AdvancePosition(p, &pos, cf - p);
ShowCentralFileHeader(cf);
pos = (cf - p) + ZIP_CFILE_HDRSIZE(cf);
}
advancepos(map, &pos, cd - map);
showcentraldirheader(cd);
pos = (cd - map) + ZIP_CDIR_HDRSIZE(cd);
advancepos(map, &pos, mapsize);
if (eocd32 && eocd64) {
if (eocd32 < eocd64) {
ShowCentralDirHeader32(eocd32);
AdvancePosition(p, &pos, eocd32 - p);
ShowCentralDirHeader64(eocd64);
AdvancePosition(p, &pos, eocd64 - p);
} else {
ShowCentralDirHeader64(eocd64);
AdvancePosition(p, &pos, eocd64 - p);
ShowCentralDirHeader32(eocd32);
AdvancePosition(p, &pos, eocd32 - p);
}
} else if (eocd32) {
ShowCentralDirHeader32(eocd32);
AdvancePosition(p, &pos, eocd32 - p);
} else {
ShowCentralDirHeader64(eocd64);
AdvancePosition(p, &pos, eocd64 - p);
}
if (!eocd64 || eocd32 > eocd64) {
pos = eocd32 - p + ZIP_CDIR_HDRSIZE(eocd32);
} else {
pos = eocd64 - p + ZIP_CDIR_HDRSIZE(eocd64);
}
AdvancePosition(p, &pos, n);
}
int main(int argc, char *argv[]) {
int fd;
uint8_t *map;
struct stat st;
showcrashreports();
CHECK_EQ(2, argc);
CHECK_NE(-1, (fd = open(argv[1], O_RDONLY)));
CHECK_NE(-1, fstat(fd, &st));
@ -353,7 +491,7 @@ int main(int argc, char *argv[]) {
(map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)));
showtitle("αcτµαlly pδrταblε εxεcµταblε", "tool/decode/zip",
basename(argv[1]), NULL, &kModelineAsm);
disassemblezip(map, st.st_size);
DisassembleZip(argv[1], map, st.st_size);
CHECK_NE(-1, munmap(map, st.st_size));
CHECK_NE(-1, close(fd));
return 0;

View file

@ -1,3 +1,2 @@
-- special script called by main redbean process at startup
ProgramRedirect(0, '/favicon.ico', '/tool/net/redbean.ico')
HidePath('/usr/share/zoneinfo/')

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Before After
Before After

View file

@ -1,8 +1,8 @@
<!doctype html>
<meta charset="utf-8">
<title>redbean</title>
<link rel="stylesheet" href="/tool/net/redbean.css">
<img src="/tool/net/redbean.png" class="logo" width="84" height="84">
<link rel="stylesheet" href="redbean.css">
<img src="/redbean.png" class="logo" width="84" height="84">
<h2>
<big>redbean</big><br>

View file

@ -63,20 +63,35 @@ o/$(MODE)/tool/net/%.com.dbg: \
o/$(MODE)/tool/net/redbean.com.dbg: \
$(TOOL_NET_DEPS) \
o/$(MODE)/tool/net/redbean.o \
o/$(MODE)/tool/net/redbean.ico.zip.o \
o/$(MODE)/tool/net/redbean.png.zip.o \
o/$(MODE)/tool/net/redbean.css.zip.o \
o/$(MODE)/tool/net/redbean.html.zip.o \
o/$(MODE)/tool/net/redbean.lua.zip.o \
o/$(MODE)/tool/net/redbean-form.lua.zip.o \
o/$(MODE)/tool/net/redbean-xhr.lua.zip.o \
o/$(MODE)/tool/net/.init.lua.zip.o \
o/$(MODE)/tool/net/.reload.lua.zip.o \
o/$(MODE)/tool/net/net.pkg \
$(CRT) \
$(APE)
@$(APELINK)
o/$(MODE)/tool/net/redbean.com: \
o/$(MODE)/tool/net/redbean.com.dbg \
tool/net/favicon.ico \
tool/net/redbean.png \
tool/net/.init.lua \
tool/net/.reload.lua
@$(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$@ zip -qj $@ o/$(MODE)/tool/net/.ape tool/net/.init.lua tool/net/.reload.lua tool/net/favicon.ico tool/net/redbean.png
o/$(MODE)/tool/net/redbean-demo.com: \
o/$(MODE)/tool/net/redbean.com \
tool/net/redbean.mk \
tool/net/index.html \
tool/net/redbean.css \
tool/net/redbean.lua \
tool/net/redbean-form.lua \
tool/net/redbean-xhr.lua \
$(TOOL_NET_HDRS) \
$(TOOL_NET_SRCS)
@$(COMPILE) -ACP -T$@ cp $< $@
@$(COMPILE) -AZIP -T$@ zip -qj $@ tool/net/redbean.lua tool/net/redbean-form.lua tool/net/redbean-xhr.lua
@$(COMPILE) -AZIP -T$@ zip -q $@ tool/net tool/net/index.html tool/net/redbean.css $(TOOL_NET_HDRS) $(TOOL_NET_SRCS)
.PHONY: o/$(MODE)/tool/net
o/$(MODE)/tool/net: \
$(TOOL_NET_BINS) \

View file

@ -63,7 +63,7 @@ local function main()
Write('</dl>\n')
Write('<p>')
Write('<a href="/tool/net/redbean.lua">Click here</a> ')
Write('<a href="redbean.lua">Click here</a> ')
Write('to return to the previous page.\n')
end

View file

@ -1,3 +1,8 @@
-- redbean xhr handler demo
SetHeader('Vary', 'X-Custom-Header')
SetHeader('X-Custom-Header', 'hello ' .. GetHeader('x-custom-header'))
hdr = GetHeader('x-custom-header')
if hdr then
SetHeader('Vary', 'X-Custom-Header')
SetHeader('X-Custom-Header', 'hello ' .. hdr)
else
ServeError(400)
end

File diff suppressed because it is too large Load diff

View file

@ -69,7 +69,7 @@ local function main()
Write([[
<h3>post request html form demo</h3>
<form action="/tool/net/redbean-form.lua" method="post">
<form action="redbean-form.lua" method="post">
<input type="text" id="firstname" name="firstname">
<label for="firstname">first name</label>
<br>
@ -92,7 +92,7 @@ local function main()
r.onload = function() {
document.getElementById("result").innerText = this.getResponseHeader('X-Custom-Header');
};
r.open('POST', '/tool/net/redbean-xhr.lua');
r.open('POST', 'redbean-xhr.lua');
r.setRequestHeader('X-Custom-Header', document.getElementById('x').value);
r.send();
}