#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 #include "libc/assert.h" #include "libc/atomic.h" #include "libc/calls/calls.h" #include "libc/calls/pledge.h" #include "libc/calls/struct/sigaction.h" #include "libc/calls/struct/timespec.h" #include "libc/calls/struct/timeval.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/fmt/conv.h" #include "libc/fmt/itoa.h" #include "libc/intrin/kprintf.h" #include "libc/log/log.h" #include "libc/mem/gc.internal.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/sock/sock.h" #include "libc/sock/struct/sockaddr.h" #include "libc/str/str.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/auxv.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" #include "libc/thread/thread.h" #include "libc/thread/thread2.h" #include "net/http/http.h" /** * @fileoverview greenbean lightweight 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 * * *
this is a fun webpage *
hosted by greenbean * * Like redbean, greenbean has superior performance too, with an * advantage on benchmarks biased towards high connection counts * * $ wrk -c 300 -t 32 --latency http://127.0.0.1:8080/ * Running 10s test @ http://127.0.0.1:8080/ * 32 threads and 300 connections * Thread Stats Avg Stdev Max +/- Stdev * Latency 661.06us 5.11ms 96.22ms 98.85% * Req/Sec 42.38k 8.90k 90.47k 84.65% * Latency Distribution * 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 * */ #define PORT 8080 #define KEEPALIVE 30000 #define LOGGING 1 #define STANDARD_RESPONSE_HEADERS \ "Server: greenbean/1.o\r\n" \ "Referrer-Policy: origin\r\n" \ "Cache-Control: private; max-age=0\r\n" int threads; int alwaysclose; atomic_int a_termsig; atomic_int a_workers; atomic_int a_messages; atomic_int a_listening; atomic_int a_connections; pthread_cond_t statuscond; pthread_mutex_t statuslock; const char *volatile status = ""; void SomethingHappened(void) { unassert(!pthread_cond_signal(&statuscond)); } void SomethingImportantHappened(void) { unassert(!pthread_mutex_lock(&statuslock)); unassert(!pthread_cond_signal(&statuscond)); unassert(!pthread_mutex_unlock(&statuslock)); } void *Worker(void *id) { int server, yes = 1; // 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}; struct sockaddr_in addr = {.sin_family = AF_INET, .sin_port = htons(PORT)}; server = socket(AF_INET, SOCK_STREAM, 0); if (server == -1) { kprintf("\r\e[Ksocket() failed %m\n"); if (errno == ENFILE || errno == EMFILE) { TooManyFileDescriptors: kprintf("sudo prlimit --pid=$$ --nofile=%d\n", threads * 3); } goto WorkerFinished; } // we don't bother checking for errors here since OS support for the // advanced features tends to be a bit spotty and harmless to ignore 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)); errno = 0; // open our ears to incoming connections; so_reuseport makes it // possible for our many threads to bind to the same interface! // otherwise we'd need to create a complex multi-threaded queue if (bind(server, (struct sockaddr *)&addr, sizeof(addr)) == -1) { kprintf("\r\e[Ksocket() returned %m\n"); goto CloseWorker; } unassert(!listen(server, 1)); // connection loop ++a_listening; SomethingImportantHappened(); while (!a_termsig) { uint32_t clientaddrsize; struct sockaddr_in clientaddr; int client, inmsglen, outmsglen; char inbuf[512], outbuf[512], *p, *q; // musl libc and cosmopolitan libc support a posix thread extension // that makes thread cancelation work much better. your io routines // will just raise ECANCELED so you can check for cancellation with // normal logic rather than needing to push and pop cleanup handler // functions onto the stack, or worse dealing with async interrupts unassert(!pthread_setcancelstate(PTHREAD_CANCEL_MASKED, 0)); // wait for client connection // we don't bother with poll() because this is actually very speedy clientaddrsize = sizeof(clientaddr); client = accept(server, (struct sockaddr *)&clientaddr, &clientaddrsize); // turns cancellation off so we don't interrupt active http clients unassert(!pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0)); if (client == -1) { if (errno != EAGAIN && errno != ECANCELED) { kprintf("\r\e[Kaccept() returned %m\n"); if (errno == ENFILE || errno == EMFILE) { goto TooManyFileDescriptors; } usleep(10000); } continue; } ++a_connections; SomethingHappened(); // message loop ssize_t got, sent; struct HttpMessage msg; do { // parse the incoming http message InitHttpMessage(&msg, kHttpRequest); // wait for http message (non-fragmented required) // we're not terrible concerned when errors happen here unassert(!pthread_setcancelstate(PTHREAD_CANCEL_MASKED, 0)); if ((got = read(client, inbuf, sizeof(inbuf))) <= 0) break; unassert(!pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0)); // check that client message wasn't fragmented into more reads if ((inmsglen = ParseHttpMessage(&msg, inbuf, got)) <= 0) break; ++a_messages; SomethingHappened(); #if LOGGING // log the incoming http message unsigned clientip = ntohl(clientaddr.sin_addr.s_addr); kprintf("\r\e[K%6P get some %hhu.%hhu.%hhu.%hhu:%hu %#.*s\n", clientip >> 24, clientip >> 16, clientip >> 8, clientip, ntohs(clientaddr.sin_port), msg.uri.b - msg.uri.a, inbuf + msg.uri.a); SomethingHappened(); #endif // display hello world html page for http://127.0.0.1:8080/ struct tm tm; int64_t unixts; struct timespec ts; if (msg.method == kHttpGet && (msg.uri.b - msg.uri.a == 1 && inbuf[msg.uri.a + 0] == '/')) { q = "\r\n" "
this is a fun webpage\r\n" "
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: "); clock_gettime(0, &ts), unixts = ts.tv_sec; p = FormatHttpDateTime(p, gmtime_r(&unixts, &tm)); p = stpcpy(p, "\r\nContent-Length: "); p = FormatInt32(p, strlen(q)); if (alwaysclose) { p = stpcpy(p, "\r\nConnection: close"); } 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 = "\r\n" "