mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
Set up http://redbean.justine.lol/ demo server
This commit is contained in:
parent
472b95fea3
commit
c029e83dd8
7 changed files with 118 additions and 25 deletions
|
@ -232,8 +232,6 @@ relegated void __oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
|
|||
err = errno;
|
||||
if (once) _exit(119);
|
||||
once = true;
|
||||
/* TODO(jart): Needs translation for ucontext_t and possibly siginfo_t. */
|
||||
if (IsFreebsd() || IsOpenbsd()) ctx = NULL;
|
||||
rip = ctx ? ctx->uc_mcontext.rip : 0;
|
||||
if ((gdbpid = IsDebuggerPresent(true))) {
|
||||
DebugBreak();
|
||||
|
|
30
libc/str/getzipcdircomment.c
Normal file
30
libc/str/getzipcdircomment.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*-*- 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/zip.h"
|
||||
|
||||
/**
|
||||
* Returns comment of zip central directory.
|
||||
*/
|
||||
void *GetZipCdirComment(const uint8_t *eocd) {
|
||||
if (READ32LE(eocd) == kZipCdir64HdrMagic) {
|
||||
return ZIP_CDIR64_COMMENT(eocd);
|
||||
} else {
|
||||
return ZIP_CDIR_COMMENT(eocd);
|
||||
}
|
||||
}
|
30
libc/str/getzipcdircommentsize.c
Normal file
30
libc/str/getzipcdircommentsize.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*-*- 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/zip.h"
|
||||
|
||||
/**
|
||||
* Returns comment of zip central directory.
|
||||
*/
|
||||
uint64_t GetZipCdirCommentSize(const uint8_t *eocd) {
|
||||
if (READ32LE(eocd) == kZipCdir64HdrMagic) {
|
||||
return ZIP_CDIR64_COMMENTSIZE(eocd);
|
||||
} else {
|
||||
return ZIP_CDIR_COMMENTSIZE(eocd);
|
||||
}
|
||||
}
|
|
@ -184,6 +184,8 @@ bool IsZipCdir64(const uint8_t *, size_t, size_t);
|
|||
int GetZipCfileMode(const uint8_t *);
|
||||
uint64_t GetZipCdirOffset(const uint8_t *);
|
||||
uint64_t GetZipCdirRecords(const uint8_t *);
|
||||
void *GetZipCdirComment(const uint8_t *);
|
||||
uint64_t GetZipCdirCommentSize(const uint8_t *);
|
||||
uint64_t GetZipCfileUncompressedSize(const uint8_t *);
|
||||
uint64_t GetZipCfileCompressedSize(const uint8_t *);
|
||||
uint64_t GetZipCfileOffset(const uint8_t *);
|
||||
|
|
7
tool/net/demo/virtualbean.html
Normal file
7
tool/net/demo/virtualbean.html
Normal file
|
@ -0,0 +1,7 @@
|
|||
<!doctype html>
|
||||
<meta charset="utf-8">
|
||||
<title>redbean virtual host</title>
|
||||
<h1>virtualbean.justine.lol</h1>
|
||||
<p>This page uses redbean virtual hosting. Please refer to
|
||||
<a href="//redbean.justine.lol">//redbean.justine.lol</a>
|
||||
to see the primary instance.
|
|
@ -98,6 +98,7 @@ o/$(MODE)/tool/net/redbean-demo.com: \
|
|||
tool/net/demo/redbean-form.lua \
|
||||
tool/net/demo/redbean-xhr.lua \
|
||||
tool/net/demo/seekable.txt \
|
||||
tool/net/demo/virtualbean.html \
|
||||
tool/net/redbean.c \
|
||||
net/http/parsehttprequest.c \
|
||||
net/http/parseurl.c \
|
||||
|
@ -107,9 +108,18 @@ 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$@ zip -qj $@ o/$(MODE)/tool/net/.redbean-demo/.ape tool/net/demo/.init.lua tool/net/demo/.reload.lua tool/net/demo/hello.lua tool/net/demo/redbean.lua tool/net/demo/404.html tool/net/favicon.ico tool/net/redbean.png tool/net/demo/redbean-form.lua tool/net/demo/redbean-xhr.lua
|
||||
@$(COMPILE) -AZIP -T$@ zip -qj0 $@ tool/net/demo/seekable.txt
|
||||
@$(COMPILE) -AZIP -T$@ zip -q $@ tool/net/ tool/net/demo/ tool/net/demo/index.html tool/net/demo/redbean.css tool/net/redbean.c net/http/parsehttprequest.c net/http/parseurl.c net/http/encodeurl.c test/net/http/parsehttprequest_test.c test/net/http/parseurl_test.c
|
||||
@$(COMPILE) -AZIP -T$@ zip -qj $@ o/$(MODE)/tool/net/.redbean-demo/.ape tool/net/demo/.init.lua tool/net/demo/.reload.lua tool/net/demo/hello.lua
|
||||
@echo "<-- check out this lua server page" | $(COMPILE) -AZIP -T$@ zip -cqj $@ tool/net/demo/redbean.lua
|
||||
@$(COMPILE) -AZIP -T$@ zip -qj $@ tool/net/demo/404.html tool/net/favicon.ico tool/net/redbean.png tool/net/demo/redbean-form.lua tool/net/demo/redbean-xhr.lua
|
||||
@echo Uncompressed for HTTP Range requests | $(COMPILE) -AZIP -T$@ zip -cqj0 $@ tool/net/demo/seekable.txt
|
||||
@$(COMPILE) -AZIP -T$@ zip -q $@ tool/net/ tool/net/demo/ tool/net/demo/index.html tool/net/demo/redbean.css tool/net/redbean.c net/http/parsehttprequest.c net/http/parseurl.c net/http/encodeurl.c test/net/http/parsehttprequest_test.c test/net/http/parseurl_test.c
|
||||
@echo "<p>This is a live instance of <a href=https://justine.lol/redbean/>redbean</a>: a tiny multiplatform webserver that <a href=https://news.ycombinator.com/item?id=26271117>went viral</a> on hacker news a few months ago. since then, we've added Lua dynamic serving, which also goes as fast as 1,000,000 requests per second on a core i9 (rather than a cheap virtual machine like this). the text you're reading now is a PKZIP End Of Central Directory comment.<p>redbean aims to be production worthy across six operating systems, using a single executable file (this demo is hosted on FreeBSD 13). redbean has been enhanced to restore the APE header after startup. It automatically generates this listing page based on your ZIP contents. If you use redbean as an application server / web development environment, then you'll find other new and useful features like function call logging so you can get that sweet sweet microsecond scale latency." | $(COMPILE) -AZIP -T$@ zip -z $@
|
||||
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/virtualbean.justine.lol/
|
||||
@$(COMPILE) -ACP -T$@ cp tool/net/redbean.png o/$(MODE)/tool/net/virtualbean.justine.lol/redbean.png
|
||||
@$(COMPILE) -ACP -T$@ cp tool/net/demo/virtualbean.html o/$(MODE)/tool/net/virtualbean.justine.lol/index.html
|
||||
@(cd o/$(MODE)/tool/net && zip -q redbean-demo.com virtualbean.justine.lol/)
|
||||
@(cd o/$(MODE)/tool/net && echo 'Go to <a href=http://virtualbean.justine.lol>http://virtualbean.justine.lol</a>' | zip -cq redbean-demo.com virtualbean.justine.lol/index.html)
|
||||
@(cd o/$(MODE)/tool/net && zip -q redbean-demo.com virtualbean.justine.lol/redbean.png)
|
||||
|
||||
o/$(MODE)/tool/net/redbean-static.com: \
|
||||
o/$(MODE)/tool/net/redbean-static.com.dbg \
|
||||
|
|
|
@ -1091,7 +1091,11 @@ static void GetOpts(int argc, char *argv[]) {
|
|||
static void Daemonize(void) {
|
||||
char ibuf[21];
|
||||
int i, fd, pid;
|
||||
for (i = 0; i < 128; ++i) close(i);
|
||||
for (i = 3; i < 128; ++i) {
|
||||
if (i != server) {
|
||||
close(i);
|
||||
}
|
||||
}
|
||||
if ((pid = fork()) > 0) exit(0);
|
||||
setsid();
|
||||
if ((pid = fork()) > 0) _exit(0);
|
||||
|
@ -1105,8 +1109,8 @@ static void Daemonize(void) {
|
|||
freopen("/dev/null", "r", stdin);
|
||||
freopen(logpath, "a", stdout);
|
||||
freopen(logpath, "a", stderr);
|
||||
LOGIFNEG1(setuid(daemonuid));
|
||||
LOGIFNEG1(setgid(daemongid));
|
||||
LOGIFNEG1(setuid(daemonuid));
|
||||
}
|
||||
|
||||
static void ReportWorkerExit(int pid, int ws) {
|
||||
|
@ -1164,13 +1168,13 @@ static void AppendResourceReport(struct rusage *ru, const char *nl) {
|
|||
(int)((long double)ru->ru_nvcsw / (ru->ru_nvcsw + ru->ru_nivcsw) * 100),
|
||||
nl);
|
||||
}
|
||||
if (ru->ru_inblock || ru->ru_oublock) {
|
||||
Append("performed %,ld read and %,ld write i/o operations%s",
|
||||
ru->ru_inblock, ru->ru_oublock, nl);
|
||||
}
|
||||
if (ru->ru_msgrcv || ru->ru_msgsnd) {
|
||||
Append("received %,ld message and sent %,ld%s", ru->ru_msgrcv,
|
||||
ru->ru_msgsnd, nl);
|
||||
Append("received %,ld message%s and sent %,ld%s", ru->ru_msgrcv,
|
||||
ru->ru_msgrcv == 1 ? "" : "s", ru->ru_msgsnd, nl);
|
||||
}
|
||||
if (ru->ru_inblock || ru->ru_oublock) {
|
||||
Append("performed %,ld read%s and %,ld write i/o operations%s",
|
||||
ru->ru_inblock, ru->ru_inblock == 1 ? "" : "s", ru->ru_oublock, nl);
|
||||
}
|
||||
if (ru->ru_nsignals) {
|
||||
Append("received %,ld signals%s", ru->ru_nsignals, nl);
|
||||
|
@ -2035,17 +2039,25 @@ static char *ServeListing(void) {
|
|||
<style>\r\n\
|
||||
html { color: #111; font-family: sans-serif; }\r\n\
|
||||
a { text-decoration: none; }\r\n\
|
||||
a:hover { color: #00e; border-bottom: 1px solid #ccc; }\r\n\
|
||||
pre a:hover { color: #00e; border-bottom: 1px solid #ccc; }\r\n\
|
||||
h1 a { color: #111; }\r\n\
|
||||
img { vertical-align: middle; }\r\n\
|
||||
footer { color: #555; font-size: 10pt; }\r\n\
|
||||
td { padding-right: 3em; }\r\n\
|
||||
.eocdcomment { max-width: 800px; color: #333; font-size: 11pt; }\r\n\
|
||||
</style>\r\n\
|
||||
<header><h1>\r\n");
|
||||
AppendLogo();
|
||||
rp[0] = EscapeHtml(brand, -1, &rn[0]);
|
||||
AppendData(rp[0], rn[0]);
|
||||
free(rp[0]);
|
||||
Append("</h1><hr></header><pre>\r\n");
|
||||
Append("</h1>\r\n"
|
||||
"<div class=\"eocdcomment\">%.*s</div>\r\n"
|
||||
"<hr>\r\n"
|
||||
"</header>\r\n"
|
||||
"<pre>\r\n",
|
||||
strnlen(GetZipCdirComment(cdir), GetZipCdirCommentSize(cdir)),
|
||||
GetZipCdirComment(cdir));
|
||||
memset(w, 0, sizeof(w));
|
||||
n = GetZipCdirRecords(cdir);
|
||||
for (cf = GetZipCdirOffset(cdir); n--; cf += ZIP_CFILE_HDRSIZE(zmap + cf)) {
|
||||
|
@ -2073,7 +2085,6 @@ td { padding-right: 3em; }\r\n\
|
|||
ZIP_CFILE_COMMENTSIZE(zmap + cf)),
|
||||
&rn[3]);
|
||||
rp[4] = EscapeHtml(rp[0], rn[0], &rn[4]);
|
||||
rp[5] = EscapeHtml(rp[3], rn[3], &rn[0]);
|
||||
lastmod = GetZipCfileLastModified(zmap + cf);
|
||||
localtime_r(&lastmod, &tm);
|
||||
strftime(tb, sizeof(tb), "%Y-%m-%d %H:%M:%S %Z", &tm);
|
||||
|
@ -2083,14 +2094,13 @@ td { padding-right: 3em; }\r\n\
|
|||
Append("<a href=\"%.*s\">%-*.*s</a> %s %0*o %4s %,*ld %'s\r\n",
|
||||
rn[2], rp[2], w[0], rn[4], rp[4], tb, w[1],
|
||||
GetZipCfileMode(zmap + cf), DescribeCompressionRatio(rb, lf),
|
||||
w[2], GetZipLfileUncompressedSize(zmap + lf), rp[5]);
|
||||
w[2], GetZipLfileUncompressedSize(zmap + lf), rp[3]);
|
||||
} else {
|
||||
Append("%-*.*s %s %0*o %4s %,*ld %'s\r\n", w[0], rn[4], rp[4], tb,
|
||||
w[1], GetZipCfileMode(zmap + cf),
|
||||
DescribeCompressionRatio(rb, lf), w[2],
|
||||
GetZipLfileUncompressedSize(zmap + lf), rp[5]);
|
||||
GetZipLfileUncompressedSize(zmap + lf), rp[3]);
|
||||
}
|
||||
free(rp[5]);
|
||||
free(rp[4]);
|
||||
free(rp[3]);
|
||||
free(rp[2]);
|
||||
|
@ -2100,9 +2110,13 @@ td { padding-right: 3em; }\r\n\
|
|||
free(path);
|
||||
}
|
||||
Append("</pre><footer><hr>\r\n");
|
||||
Append("<table border=\"0\"><tr><td valign=\"top\">\r\n");
|
||||
Append("<a href=\"/statusz\">/statusz</a> says your redbean<br>\r\n");
|
||||
AppendResourceReport(&shared->children, "<br>\r\n");
|
||||
Append("<table border=\"0\"><tr>\r\n");
|
||||
Append("<td valign=\"top\">\r\n");
|
||||
Append("<a href=\"/statusz\">/statusz</a>\r\n");
|
||||
if (shared->connectionshandled) {
|
||||
Append("says your redbean<br>\r\n");
|
||||
AppendResourceReport(&shared->children, "<br>\r\n");
|
||||
}
|
||||
Append("<td valign=\"top\">\r\n");
|
||||
and = "";
|
||||
x = nowl() - startserver;
|
||||
|
@ -4507,7 +4521,6 @@ void RedBean(int argc, char *argv[], const char *prog) {
|
|||
}
|
||||
exit(1);
|
||||
}
|
||||
if (daemonize) Daemonize();
|
||||
CHECK_NE(-1, listen(server, 10));
|
||||
addrsize = sizeof(serveraddr);
|
||||
CHECK_NE(-1, getsockname(server, &serveraddr, &addrsize));
|
||||
|
@ -4516,6 +4529,7 @@ void RedBean(int argc, char *argv[], const char *prog) {
|
|||
printf("%d\n", ntohs(serveraddr.sin_port));
|
||||
fflush(stdout);
|
||||
}
|
||||
if (daemonize) Daemonize();
|
||||
UpdateCurrentDate(nowl());
|
||||
freelist.c = 8;
|
||||
freelist.p = xcalloc(freelist.c, sizeof(*freelist.p));
|
||||
|
@ -4558,8 +4572,10 @@ void RedBean(int argc, char *argv[], const char *prog) {
|
|||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
setenv("GDB", "", true);
|
||||
if (!IsTiny()) showcrashreports();
|
||||
if (!IsTiny()) {
|
||||
setenv("GDB", "", true);
|
||||
showcrashreports();
|
||||
}
|
||||
RedBean(argc, argv, (const char *)getauxval(AT_EXECFN));
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue