mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
cc1920749e
Your redbean can now interoperate with clients that require TLS crypto. This is accomplished using a protocol polyglot that lets us distinguish between HTTP and HTTPS regardless of the port number. Certificates will be generated automatically, if none are supplied by the user. Footprint increases by only a few hundred kb so redbean in MODY=tiny is now 1.0mb - Add lseek() polyfills for ZIP executable - Automatically polyfill /tmp/FOO paths on NT - Fix readdir() / ftw() / nftw() bugs on Windows - Introduce -B flag for slower SSL that's stronger - Remove mbedtls features Cosmopolitan doesn't need - Have base64 decoder support the uri-safe alternative - Remove Truncated HMAC because it's forbidden by the IETF - Add all the mbedtls test suites and make them go 3x faster - Support opendir() / readdir() / closedir() on ZIP executable - Use Everest for ECDHE-ECDSA because it's so good it's so good - Add tinier implementation of sha1 since it's not worth the rom - Add chi-square monte-carlo mean correlation tests for getrandom() - Source entropy on Windows from the proper interface everyone uses We're continuing to outperform NGINX and other servers on raw message throughput. Using SSL means that instead of 1,000,000 qps you can get around 300,000 qps. However redbean isn't as fast as NGINX yet at SSL handshakes, since redbean can do 2,627 per second and NGINX does 4.3k Right now, the SSL UX story works best if you give your redbean a key signing key since that can be easily generated by openssl using a one liner then redbean will do all the things that are impossibly hard to do like signing ecdsa and rsa certificates that'll work in chrome. We should integrate the let's encrypt acme protocol in the future. Live Demo: https://redbean.justine.lol/ Root Cert: https://redbean.justine.lol/redbean1.crt
182 lines
5.9 KiB
C
182 lines
5.9 KiB
C
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│
|
|
│vi: set et ft=c ts=4 sw=4 fenc=utf-8 :vi│
|
|
╚──────────────────────────────────────────────────────────────────────────────╝
|
|
│ │
|
|
│ Musl Libc │
|
|
│ Copyright © 2005-2014 Rich Felker, et al. │
|
|
│ │
|
|
│ Permission is hereby granted, free of charge, to any person obtaining │
|
|
│ a copy of this software and associated documentation files (the │
|
|
│ "Software"), to deal in the Software without restriction, including │
|
|
│ without limitation the rights to use, copy, modify, merge, publish, │
|
|
│ distribute, sublicense, and/or sell copies of the Software, and to │
|
|
│ permit persons to whom the Software is furnished to do so, subject to │
|
|
│ the following conditions: │
|
|
│ │
|
|
│ The above copyright notice and this permission notice shall be │
|
|
│ included in all copies or substantial portions of the Software. │
|
|
│ │
|
|
│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │
|
|
│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │
|
|
│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │
|
|
│ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │
|
|
│ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │
|
|
│ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │
|
|
│ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
|
|
│ │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/calls/calls.h"
|
|
#include "libc/calls/struct/dirent.h"
|
|
#include "libc/calls/weirdtypes.h"
|
|
#include "libc/errno.h"
|
|
#include "libc/str/str.h"
|
|
#include "libc/sysv/consts/o.h"
|
|
#include "third_party/musl/ftw.h"
|
|
|
|
asm(".ident\t\"\\n\\n\
|
|
Musl libc (MIT License)\\n\
|
|
Copyright 2005-2014 Rich Felker, et. al.\"");
|
|
asm(".include \"libc/disclaimer.inc\"");
|
|
|
|
/* no reason to impose windows limit
|
|
small enough to fit in stack frame
|
|
should be changed to use realloc */
|
|
#define PATH_MAX2 2048
|
|
|
|
/* no cosmo pthreads support atm */
|
|
#define pthread_setcancelstate(...)
|
|
|
|
/* clang-format off */
|
|
|
|
struct history
|
|
{
|
|
struct history *chain;
|
|
dev_t dev;
|
|
ino_t ino;
|
|
int level;
|
|
int base;
|
|
};
|
|
|
|
static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags, struct history *h)
|
|
{
|
|
size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l;
|
|
struct stat st;
|
|
struct history new;
|
|
int type;
|
|
int r;
|
|
int dfd=-1;
|
|
int err=0;
|
|
struct FTW lev;
|
|
|
|
if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) {
|
|
if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st))
|
|
type = FTW_SLN;
|
|
else if (errno != EACCES) return -1;
|
|
else type = FTW_NS;
|
|
} else if (S_ISDIR(st.st_mode)) {
|
|
if (flags & FTW_DEPTH) type = FTW_DP;
|
|
else type = FTW_D;
|
|
} else if (S_ISLNK(st.st_mode)) {
|
|
if (flags & FTW_PHYS) type = FTW_SL;
|
|
else type = FTW_SLN;
|
|
} else {
|
|
type = FTW_F;
|
|
}
|
|
|
|
if ((flags & FTW_MOUNT) && h && st.st_dev != h->dev)
|
|
return 0;
|
|
|
|
new.chain = h;
|
|
new.dev = st.st_dev;
|
|
new.ino = st.st_ino;
|
|
new.level = h ? h->level+1 : 0;
|
|
new.base = j+1;
|
|
|
|
lev.level = new.level;
|
|
if (h) {
|
|
lev.base = h->base;
|
|
} else {
|
|
size_t k;
|
|
for (k=j; k && path[k]=='/'; k--);
|
|
for (; k && path[k-1]!='/'; k--);
|
|
lev.base = k;
|
|
}
|
|
|
|
if (type == FTW_D || type == FTW_DP) {
|
|
dfd = open(path, O_RDONLY | O_DIRECTORY);
|
|
err = errno;
|
|
if (dfd < 0 && err == EACCES) type = FTW_DNR;
|
|
if (!fd_limit) close(dfd);
|
|
}
|
|
|
|
if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
|
|
return r;
|
|
|
|
for (; h; h = h->chain)
|
|
if (h->dev == st.st_dev && h->ino == st.st_ino)
|
|
return 0;
|
|
|
|
if ((type == FTW_D || type == FTW_DP) && fd_limit) {
|
|
if (dfd < 0) {
|
|
errno = err;
|
|
return -1;
|
|
}
|
|
DIR *d = fdopendir(dfd);
|
|
if (d) {
|
|
struct dirent *de;
|
|
while ((de = readdir(d))) {
|
|
if (de->d_name[0] == '.'
|
|
&& (!de->d_name[1]
|
|
|| (de->d_name[1]=='.'
|
|
&& !de->d_name[2]))) continue;
|
|
if (strlen(de->d_name) >= PATH_MAX2-l) {
|
|
errno = ENAMETOOLONG;
|
|
closedir(d);
|
|
return -1;
|
|
}
|
|
path[j]='/';
|
|
strcpy(path+j+1, de->d_name);
|
|
if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
|
|
closedir(d);
|
|
return r;
|
|
}
|
|
}
|
|
closedir(d);
|
|
} else {
|
|
close(dfd);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
path[l] = 0;
|
|
if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
|
|
return r;
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Walks file tree.
|
|
*
|
|
* @see examples/walk.c for example
|
|
*/
|
|
int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
|
|
{
|
|
int r, cs;
|
|
size_t l;
|
|
char pathbuf[PATH_MAX2+1];
|
|
|
|
if (fd_limit <= 0) return 0;
|
|
|
|
l = strlen(path);
|
|
if (l > PATH_MAX2) {
|
|
errno = ENAMETOOLONG;
|
|
return -1;
|
|
}
|
|
memcpy(pathbuf, path, l+1);
|
|
|
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
|
|
r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
|
|
pthread_setcancelstate(cs, 0);
|
|
return r;
|
|
}
|