2022-05-16 21:33:19 +00:00
|
|
|
#if 0
|
|
|
|
/*─────────────────────────────────────────────────────────────────╗
|
|
|
|
│ To the extent possible under law, Justine Tunney has waived │
|
|
|
|
│ all copyright and related or neighboring rights to this file, │
|
|
|
|
│ as it is written in the following disclaimers: │
|
|
|
|
│ • http://unlicense.org/ │
|
|
|
|
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
|
|
|
╚─────────────────────────────────────────────────────────────────*/
|
|
|
|
#endif
|
2022-05-16 20:20:08 +00:00
|
|
|
#include "libc/assert.h"
|
2022-09-11 18:02:07 +00:00
|
|
|
#include "libc/atomic.h"
|
2022-05-14 14:44:02 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2022-06-09 03:01:28 +00:00
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2022-05-14 14:44:02 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
|
|
|
#include "libc/calls/struct/timespec.h"
|
|
|
|
#include "libc/calls/struct/timeval.h"
|
2022-05-15 04:13:45 +00:00
|
|
|
#include "libc/dce.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/errno.h"
|
2022-05-16 21:33:19 +00:00
|
|
|
#include "libc/fmt/conv.h"
|
2022-05-14 14:44:02 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
2022-09-09 18:30:33 +00:00
|
|
|
#include "libc/intrin/atomic.h"
|
2022-05-14 14:44:02 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2022-05-23 17:15:53 +00:00
|
|
|
#include "libc/limits.h"
|
2022-05-14 14:44:02 +00:00
|
|
|
#include "libc/log/check.h"
|
|
|
|
#include "libc/log/log.h"
|
2022-05-16 20:20:08 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2022-05-14 14:44:02 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2022-07-25 02:40:32 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2022-05-15 16:14:48 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2022-05-18 23:41:29 +00:00
|
|
|
#include "libc/runtime/stack.h"
|
2022-05-16 21:33:19 +00:00
|
|
|
#include "libc/runtime/sysconf.h"
|
2022-05-14 14:44:02 +00:00
|
|
|
#include "libc/sock/sock.h"
|
2022-08-06 10:51:50 +00:00
|
|
|
#include "libc/sock/struct/pollfd.h"
|
|
|
|
#include "libc/sock/struct/sockaddr.h"
|
2022-05-14 14:44:02 +00:00
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/af.h"
|
|
|
|
#include "libc/sysv/consts/clock.h"
|
|
|
|
#include "libc/sysv/consts/clone.h"
|
|
|
|
#include "libc/sysv/consts/ipproto.h"
|
|
|
|
#include "libc/sysv/consts/map.h"
|
2022-05-15 04:13:45 +00:00
|
|
|
#include "libc/sysv/consts/poll.h"
|
2022-05-14 14:44:02 +00:00
|
|
|
#include "libc/sysv/consts/prot.h"
|
|
|
|
#include "libc/sysv/consts/rlimit.h"
|
|
|
|
#include "libc/sysv/consts/sig.h"
|
|
|
|
#include "libc/sysv/consts/so.h"
|
|
|
|
#include "libc/sysv/consts/sock.h"
|
|
|
|
#include "libc/sysv/consts/sol.h"
|
|
|
|
#include "libc/sysv/consts/tcp.h"
|
2022-07-10 11:01:17 +00:00
|
|
|
#include "libc/thread/spawn.h"
|
2022-09-11 18:02:07 +00:00
|
|
|
#include "libc/thread/thread.h"
|
|
|
|
#include "libc/thread/tls.h"
|
|
|
|
#include "libc/thread/wait0.internal.h"
|
2022-05-14 14:44:02 +00:00
|
|
|
#include "libc/time/struct/tm.h"
|
|
|
|
#include "libc/time/time.h"
|
|
|
|
#include "net/http/http.h"
|
|
|
|
#include "net/http/url.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview greenbean lightweighht threaded web server
|
|
|
|
*
|
|
|
|
* $ make -j8 o//tool/net/greenbean.com
|
|
|
|
* $ o//tool/net/greenbean.com &
|
|
|
|
* $ printf 'GET /\n\n' | nc 127.0.0.1 8080
|
|
|
|
* HTTP/1.1 200 OK
|
|
|
|
* Server: greenbean/1.o
|
|
|
|
* Referrer-Policy: origin
|
|
|
|
* Cache-Control: private; max-age=0
|
|
|
|
* Content-Type: text/html; charset=utf-8
|
|
|
|
* Date: Sat, 14 May 2022 14:13:07 GMT
|
|
|
|
* Content-Length: 118
|
|
|
|
*
|
|
|
|
* <!doctype html>
|
|
|
|
* <title>hello world</title>
|
|
|
|
* <h1>hello world</h1>
|
|
|
|
* <p>this is a fun webpage
|
|
|
|
* <p>hosted by greenbean
|
|
|
|
*
|
|
|
|
* Like redbean, greenbean has superior performance too, with an
|
|
|
|
* advantage on benchmarks biased towards high connection counts
|
|
|
|
*
|
2022-06-22 10:04:25 +00:00
|
|
|
* $ wrk -c 300 -t 32 --latency http://127.0.0.1:8080/
|
|
|
|
* Running 10s test @ http://127.0.0.1:8080/
|
2022-05-14 14:44:02 +00:00
|
|
|
* 32 threads and 300 connections
|
|
|
|
* Thread Stats Avg Stdev Max +/- Stdev
|
2022-05-24 17:19:39 +00:00
|
|
|
* Latency 661.06us 5.11ms 96.22ms 98.85%
|
|
|
|
* Req/Sec 42.38k 8.90k 90.47k 84.65%
|
2022-05-14 14:44:02 +00:00
|
|
|
* Latency Distribution
|
2022-05-24 17:19:39 +00:00
|
|
|
* 50% 184.00us
|
|
|
|
* 75% 201.00us
|
|
|
|
* 90% 224.00us
|
|
|
|
* 99% 11.99ms
|
|
|
|
* 10221978 requests in 7.60s, 3.02GB read
|
|
|
|
* Requests/sec: 1345015.69
|
|
|
|
* Transfer/sec: 406.62MB
|
2022-05-14 14:44:02 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-05-16 20:20:08 +00:00
|
|
|
#define PORT 8080
|
2022-05-15 16:14:48 +00:00
|
|
|
#define HEARTBEAT 100
|
2022-05-14 14:44:02 +00:00
|
|
|
#define KEEPALIVE 5000
|
|
|
|
#define LOGGING 0
|
|
|
|
|
|
|
|
#define STANDARD_RESPONSE_HEADERS \
|
|
|
|
"Server: greenbean/1.o\r\n" \
|
|
|
|
"Referrer-Policy: origin\r\n" \
|
|
|
|
"Cache-Control: private; max-age=0\r\n"
|
|
|
|
|
2022-05-24 17:19:39 +00:00
|
|
|
int threads;
|
2022-09-11 18:02:07 +00:00
|
|
|
atomic_int workers;
|
|
|
|
atomic_int messages;
|
|
|
|
atomic_int listening;
|
|
|
|
atomic_int connections;
|
|
|
|
atomic_int closingtime;
|
2022-05-23 17:15:53 +00:00
|
|
|
const char *volatile status;
|
2022-05-15 16:14:48 +00:00
|
|
|
|
2022-09-09 18:30:33 +00:00
|
|
|
void *Worker(void *id) {
|
2022-05-15 16:14:48 +00:00
|
|
|
int server, yes = 1;
|
|
|
|
|
2022-05-14 14:44:02 +00:00
|
|
|
// load balance incoming connections for port 8080 across all threads
|
|
|
|
// hangup on any browser clients that lag for more than a few seconds
|
|
|
|
struct timeval timeo = {KEEPALIVE / 1000, KEEPALIVE % 1000};
|
2022-05-16 20:20:08 +00:00
|
|
|
struct sockaddr_in addr = {.sin_family = AF_INET, .sin_port = htons(PORT)};
|
|
|
|
|
2022-07-10 15:27:50 +00:00
|
|
|
server = socket(AF_INET, SOCK_STREAM, 0);
|
2022-05-16 20:20:08 +00:00
|
|
|
if (server == -1) {
|
2022-05-24 17:19:39 +00:00
|
|
|
kprintf("socket() failed %m\n"
|
|
|
|
" try running: sudo prlimit --pid=$$ --nofile=%d\n",
|
|
|
|
threads * 2);
|
2022-05-16 20:20:08 +00:00
|
|
|
goto WorkerFinished;
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:44:02 +00:00
|
|
|
setsockopt(server, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
|
|
|
|
setsockopt(server, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));
|
|
|
|
setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
|
|
|
|
setsockopt(server, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes));
|
|
|
|
setsockopt(server, SOL_TCP, TCP_FASTOPEN, &yes, sizeof(yes));
|
|
|
|
setsockopt(server, SOL_TCP, TCP_QUICKACK, &yes, sizeof(yes));
|
2022-05-17 11:14:28 +00:00
|
|
|
errno = 0;
|
2022-05-16 20:20:08 +00:00
|
|
|
|
|
|
|
if (bind(server, &addr, sizeof(addr)) == -1) {
|
2022-05-24 17:19:39 +00:00
|
|
|
kprintf("%s() failed %m\n", "socket");
|
|
|
|
goto CloseWorker;
|
2022-05-16 20:20:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 21:33:19 +00:00
|
|
|
listen(server, 1);
|
2022-05-14 14:44:02 +00:00
|
|
|
|
|
|
|
// connection loop
|
2022-05-23 17:15:53 +00:00
|
|
|
++listening;
|
2022-05-15 16:14:48 +00:00
|
|
|
while (!closingtime) {
|
2022-05-14 14:44:02 +00:00
|
|
|
struct tm tm;
|
|
|
|
int64_t unixts;
|
|
|
|
struct Url url;
|
|
|
|
ssize_t got, sent;
|
|
|
|
struct timespec ts;
|
|
|
|
struct HttpMessage msg;
|
|
|
|
uint32_t clientaddrsize;
|
|
|
|
struct sockaddr_in clientaddr;
|
|
|
|
char inbuf[1500], outbuf[512], *p, *q;
|
|
|
|
int clientip, client, inmsglen, outmsglen;
|
|
|
|
|
2022-05-16 21:33:19 +00:00
|
|
|
// this slows the server down a lot but is needed on non-Linux to
|
|
|
|
// react to keyboard ctrl-c
|
2022-05-16 20:20:08 +00:00
|
|
|
if (!IsLinux() &&
|
|
|
|
poll(&(struct pollfd){server, POLLIN}, 1, HEARTBEAT) < 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:44:02 +00:00
|
|
|
// wait for client connection
|
|
|
|
clientaddrsize = sizeof(clientaddr);
|
|
|
|
client = accept(server, &clientaddr, &clientaddrsize);
|
|
|
|
|
|
|
|
// accept() can raise a very diverse number of errors but none of
|
|
|
|
// them are really true showstoppers that would necessitate us to
|
|
|
|
// panic and abort the entire server, so we can just ignore these
|
|
|
|
if (client == -1) {
|
|
|
|
// we used SO_RCVTIMEO and SO_SNDTIMEO because those settings are
|
|
|
|
// inherited by the accepted sockets, but using them also has the
|
|
|
|
// side-effect that the listening socket fails with EAGAIN, every
|
|
|
|
// several seconds. we can use that to our advantage to check for
|
2022-05-15 16:14:48 +00:00
|
|
|
// the ctrl-c shutdowne event; otherwise, we retry the accept call
|
2022-05-14 14:44:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-23 17:15:53 +00:00
|
|
|
++connections;
|
2022-05-16 20:20:08 +00:00
|
|
|
|
2022-05-14 14:44:02 +00:00
|
|
|
// message loop
|
|
|
|
do {
|
|
|
|
// parse the incoming http message
|
|
|
|
InitHttpMessage(&msg, kHttpRequest);
|
|
|
|
// we're not terrible concerned when errors happen here
|
|
|
|
if ((got = read(client, inbuf, sizeof(inbuf))) <= 0) break;
|
|
|
|
// check that client message wasn't fragmented into more reads
|
|
|
|
if (!(inmsglen = ParseHttpMessage(&msg, inbuf, got))) break;
|
2022-05-23 17:15:53 +00:00
|
|
|
++messages;
|
2022-05-14 14:44:02 +00:00
|
|
|
|
|
|
|
#if LOGGING
|
|
|
|
// log the incoming http message
|
|
|
|
clientip = ntohl(clientaddr.sin_addr.s_addr);
|
2022-05-23 17:15:53 +00:00
|
|
|
kprintf("%6P get some %d.%d.%d.%d:%d %#.*s\n",
|
2022-05-14 14:44:02 +00:00
|
|
|
(clientip & 0xff000000) >> 030, (clientip & 0x00ff0000) >> 020,
|
|
|
|
(clientip & 0x0000ff00) >> 010, (clientip & 0x000000ff) >> 000,
|
|
|
|
ntohs(clientaddr.sin_port), msg.uri.b - msg.uri.a,
|
|
|
|
inbuf + msg.uri.a);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// display hello world html page for http://127.0.0.1:8080/
|
|
|
|
if (msg.method == kHttpGet &&
|
|
|
|
(msg.uri.b - msg.uri.a == 1 && inbuf[msg.uri.a + 0] == '/')) {
|
|
|
|
q = "<!doctype html>\r\n"
|
|
|
|
"<title>hello world</title>\r\n"
|
|
|
|
"<h1>hello world</h1>\r\n"
|
|
|
|
"<p>this is a fun webpage\r\n"
|
|
|
|
"<p>hosted by greenbean\r\n";
|
|
|
|
p = stpcpy(outbuf, "HTTP/1.1 200 OK\r\n" STANDARD_RESPONSE_HEADERS
|
|
|
|
"Content-Type: text/html; charset=utf-8\r\n"
|
|
|
|
"Date: ");
|
2022-05-23 17:15:53 +00:00
|
|
|
clock_gettime(0, &ts), unixts = ts.tv_sec;
|
2022-05-14 14:44:02 +00:00
|
|
|
p = FormatHttpDateTime(p, gmtime_r(&unixts, &tm));
|
|
|
|
p = stpcpy(p, "\r\nContent-Length: ");
|
|
|
|
p = FormatInt32(p, strlen(q));
|
|
|
|
p = stpcpy(p, "\r\n\r\n");
|
|
|
|
p = stpcpy(p, q);
|
|
|
|
outmsglen = p - outbuf;
|
|
|
|
sent = write(client, outbuf, outmsglen);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// display 404 not found error page for every thing else
|
|
|
|
q = "<!doctype html>\r\n"
|
|
|
|
"<title>404 not found</title>\r\n"
|
|
|
|
"<h1>404 not found</h1>\r\n";
|
|
|
|
p = stpcpy(outbuf,
|
|
|
|
"HTTP/1.1 404 Not Found\r\n" STANDARD_RESPONSE_HEADERS
|
|
|
|
"Content-Type: text/html; charset=utf-8\r\n"
|
|
|
|
"Date: ");
|
2022-05-23 17:15:53 +00:00
|
|
|
clock_gettime(0, &ts), unixts = ts.tv_sec;
|
2022-05-14 14:44:02 +00:00
|
|
|
p = FormatHttpDateTime(p, gmtime_r(&unixts, &tm));
|
|
|
|
p = stpcpy(p, "\r\nContent-Length: ");
|
|
|
|
p = FormatInt32(p, strlen(q));
|
|
|
|
p = stpcpy(p, "\r\n\r\n");
|
|
|
|
p = stpcpy(p, q);
|
|
|
|
outmsglen = p - outbuf;
|
|
|
|
sent = write(client, outbuf, p - outbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the client isn't pipelining and write() wrote the full
|
|
|
|
// amount, then since we sent the content length and checked
|
|
|
|
// that the client didn't attach a payload, we are so synced
|
|
|
|
// thus we can safely process more messages
|
|
|
|
} while (got == inmsglen && sent == outmsglen &&
|
|
|
|
!msg.headers[kHttpContentLength].a &&
|
|
|
|
!msg.headers[kHttpTransferEncoding].a &&
|
|
|
|
(msg.method == kHttpGet || msg.method == kHttpHead));
|
|
|
|
DestroyHttpMessage(&msg);
|
|
|
|
close(client);
|
2022-05-23 17:15:53 +00:00
|
|
|
--connections;
|
2022-05-14 14:44:02 +00:00
|
|
|
}
|
2022-05-23 17:15:53 +00:00
|
|
|
--listening;
|
2022-05-14 14:44:02 +00:00
|
|
|
|
|
|
|
// inform the parent that this clone has finished
|
2022-05-24 17:19:39 +00:00
|
|
|
CloseWorker:
|
2022-05-14 14:44:02 +00:00
|
|
|
close(server);
|
2022-05-24 17:19:39 +00:00
|
|
|
WorkerFinished:
|
2022-05-23 17:15:53 +00:00
|
|
|
--workers;
|
2022-05-14 14:44:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnCtrlC(int sig) {
|
|
|
|
closingtime = true;
|
2022-05-16 20:20:08 +00:00
|
|
|
status = " shutting down...";
|
2022-05-14 14:44:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 17:15:53 +00:00
|
|
|
void PrintStatus(void) {
|
|
|
|
kprintf("\r\e[K\e[32mgreenbean\e[0m "
|
|
|
|
"workers=%d "
|
|
|
|
"listening=%d "
|
|
|
|
"connections=%d "
|
|
|
|
"messages=%d%s ",
|
|
|
|
workers, listening, connections, messages, status);
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:44:02 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2022-09-09 18:30:33 +00:00
|
|
|
int i, rc;
|
|
|
|
pthread_t *th;
|
2022-05-16 20:20:08 +00:00
|
|
|
uint32_t *hostips;
|
2022-05-16 21:33:19 +00:00
|
|
|
// ShowCrashReports();
|
2022-05-23 17:15:53 +00:00
|
|
|
|
|
|
|
// listen for ctrl-c, hangup, and kill which shut down greenbean
|
|
|
|
status = "";
|
|
|
|
struct sigaction sa = {.sa_handler = OnCtrlC};
|
|
|
|
sigaction(SIGHUP, &sa, 0);
|
|
|
|
sigaction(SIGINT, &sa, 0);
|
|
|
|
sigaction(SIGTERM, &sa, 0);
|
|
|
|
|
|
|
|
// print all the ips that 0.0.0.0 will bind
|
2022-05-16 20:20:08 +00:00
|
|
|
for (hostips = GetHostIps(), i = 0; hostips[i]; ++i) {
|
|
|
|
kprintf("listening on http://%d.%d.%d.%d:%d\n",
|
|
|
|
(hostips[i] & 0xff000000) >> 030, (hostips[i] & 0x00ff0000) >> 020,
|
|
|
|
(hostips[i] & 0x0000ff00) >> 010, (hostips[i] & 0x000000ff) >> 000,
|
|
|
|
PORT);
|
|
|
|
}
|
2022-05-23 17:15:53 +00:00
|
|
|
|
|
|
|
threads = argc > 1 ? atoi(argv[1]) : GetCpuCount();
|
2022-07-10 15:27:50 +00:00
|
|
|
if (!(1 <= threads && threads <= 100000)) {
|
2022-09-09 18:30:33 +00:00
|
|
|
kprintf("error: invalid number of threads: %d\n", threads);
|
2022-07-10 11:01:17 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-07-25 02:40:32 +00:00
|
|
|
// secure the server
|
|
|
|
__enable_threads();
|
|
|
|
unveil("/dev/null", "rw");
|
|
|
|
unveil(0, 0);
|
|
|
|
pledge("stdio inet", 0);
|
|
|
|
|
2022-07-10 11:01:17 +00:00
|
|
|
// spawn over 9,000 worker threads
|
2022-09-09 18:30:33 +00:00
|
|
|
th = calloc(threads, sizeof(pthread_t));
|
2022-07-10 11:01:17 +00:00
|
|
|
for (i = 0; i < threads; ++i) {
|
|
|
|
++workers;
|
2022-09-09 18:30:33 +00:00
|
|
|
if ((rc = pthread_create(th + i, 0, Worker, (void *)(intptr_t)i))) {
|
2022-07-10 11:01:17 +00:00
|
|
|
--workers;
|
2022-09-09 18:30:33 +00:00
|
|
|
kprintf("error: pthread_create(%d) failed %s\n", i, strerror(rc));
|
2022-07-10 11:01:17 +00:00
|
|
|
}
|
|
|
|
if (!(i % 500)) {
|
|
|
|
PrintStatus();
|
|
|
|
}
|
2022-05-14 14:44:02 +00:00
|
|
|
}
|
2022-05-23 17:15:53 +00:00
|
|
|
|
|
|
|
// wait for workers to terminate
|
2022-05-16 20:20:08 +00:00
|
|
|
while (workers) {
|
2022-05-23 17:15:53 +00:00
|
|
|
PrintStatus();
|
2022-05-16 20:20:08 +00:00
|
|
|
usleep(HEARTBEAT * 1000);
|
|
|
|
}
|
2022-05-23 17:15:53 +00:00
|
|
|
|
|
|
|
// clean up terminal line
|
2022-05-16 20:20:08 +00:00
|
|
|
kprintf("\r\e[K");
|
2022-05-23 17:15:53 +00:00
|
|
|
|
2022-06-16 16:06:09 +00:00
|
|
|
// join the workers
|
2022-07-10 11:01:17 +00:00
|
|
|
for (i = 0; i < threads; ++i) {
|
2022-09-09 18:30:33 +00:00
|
|
|
pthread_join(th[i], 0);
|
2022-05-23 17:15:53 +00:00
|
|
|
}
|
2022-06-16 16:06:09 +00:00
|
|
|
|
|
|
|
// clean up memory
|
2022-05-23 17:15:53 +00:00
|
|
|
free(hostips);
|
2022-07-10 11:01:17 +00:00
|
|
|
free(th);
|
2022-05-14 14:44:02 +00:00
|
|
|
}
|