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

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/elf/def.h"
@ -138,16 +139,7 @@
#define APPEND(L) L.p = realloc(L.p, ++L.n * sizeof(*L.p))
#define IS(P, N, S) (N == sizeof(S) - 1 && !strncasecmp(P, S, sizeof(S) - 1))
#define MAX(X, Y) ((Y) < (X) ? (X) : (Y))
#define LOAD128BE(S) ((unsigned __int128)LOAD64BE(S) << 64 | LOAD64BE((S) + 8))
#define LOAD64BE(S) \
((unsigned long)((unsigned char *)(S))[0] << 070 | \
(unsigned long)((unsigned char *)(S))[1] << 060 | \
(unsigned long)((unsigned char *)(S))[2] << 050 | \
(unsigned long)((unsigned char *)(S))[3] << 040 | \
(unsigned long)((unsigned char *)(S))[4] << 030 | \
(unsigned long)((unsigned char *)(S))[5] << 020 | \
(unsigned long)((unsigned char *)(S))[6] << 010 | \
(unsigned long)((unsigned char *)(S))[7] << 000)
#define READ128BE(S) ((unsigned __int128)READ64BE(S) << 64 | READ64BE((S) + 8))
struct As {
int i; // things
@ -1911,13 +1903,13 @@ static void CopyLower(char *k, const char *p, int n) {
static unsigned long MakeKey64(const char *p, int n) {
char k[8] = {0};
CopyLower(k, p, n);
return LOAD64BE(k);
return READ64BE(k);
}
static unsigned __int128 MakeKey128(const char *p, int n) {
char k[16] = {0};
CopyLower(k, p, n);
return LOAD128BE(k);
return READ128BE(k);
}
static bool Prefix(struct As *a, const char *p, int n) {
@ -1929,7 +1921,7 @@ static bool Prefix(struct As *a, const char *p, int n) {
r = ARRAYLEN(kPrefix) - 1;
while (l <= r) {
m = (l + r) >> 1;
y = LOAD64BE(kPrefix[m]);
y = READ64BE(kPrefix[m]);
if (x < y) {
r = m - 1;
} else if (x > y) {
@ -1954,7 +1946,7 @@ static bool FindReg(const char *p, int n, struct Reg *out_reg) {
r = ARRAYLEN(kRegs) - 1;
while (l <= r) {
m = (l + r) >> 1;
y = LOAD64BE(kRegs[m].s);
y = READ64BE(kRegs[m].s);
if (x < y) {
r = m - 1;
} else if (x > y) {
@ -3710,7 +3702,7 @@ static bool OnDirective8(struct As *a, struct Slice s) {
r = ARRAYLEN(kDirective8) - 1;
while (l <= r) {
m = (l + r) >> 1;
y = LOAD64BE(kDirective8[m].s);
y = READ64BE(kDirective8[m].s);
if (x < y) {
r = m - 1;
} else if (x > y) {
@ -3733,7 +3725,7 @@ static bool OnDirective16(struct As *a, struct Slice s) {
r = ARRAYLEN(kDirective16) - 1;
while (l <= r) {
m = (l + r) >> 1;
y = LOAD128BE(kDirective16[m].s);
y = READ128BE(kDirective16[m].s);
if (x < y) {
r = m - 1;
} else if (x > y) {