Merge HTTP request / response parsing code

This change also fixes a bug so that DNS lookups work correctly when the
first answer is a CNAME record.
This commit is contained in:
Justine Tunney 2021-06-27 17:03:06 -07:00
parent 5a6c0f27c3
commit a68cc690ff
20 changed files with 561 additions and 616 deletions

View file

@ -76,6 +76,7 @@ void lookup(const char *name) {
int main(int argc, char *argv[]) {
int i;
showcrashreports();
for (i = 1; i < argc; ++i) lookup(argv[i]);
lookup("time-a.timefreq.bldrdoc.gov");
/* for (i = 1; i < argc; ++i) lookup(argv[i]); */
return 0;
}

View file

@ -15,6 +15,8 @@ TOOL_NET_BINS = \
$(TOOL_NET_COMS:%=%.dbg)
TOOL_NET_COMS = \
o/$(MODE)/tool/net/dig.com \
o/$(MODE)/tool/net/echoserver.com \
o/$(MODE)/tool/net/redbean.com \
o/$(MODE)/tool/net/redbean-demo.com \
o/$(MODE)/tool/net/redbean-static.com \
@ -127,10 +129,10 @@ o/$(MODE)/tool/net/redbean-demo.com: \
tool/net/demo/seekable.txt \
tool/net/demo/virtualbean.html \
tool/net/redbean.c \
net/http/parsehttprequest.c \
net/http/parsehttpmessage.c \
net/http/parseurl.c \
net/http/encodeurl.c \
test/net/http/parsehttprequest_test.c \
test/net/http/parsehttpmessage_test.c \
test/net/http/parseurl_test.c
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-demo
@ -143,7 +145,7 @@ o/$(MODE)/tool/net/redbean-demo.com: \
@echo "&lt;-- check out this lua server page" | $(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -cqj $@ tool/net/demo/redbean.lua
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -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$@ o/$(MODE)/host/third_party/infozip/zip.com -cqj0 $@ tool/net/demo/seekable.txt
@$(COMPILE) -AZIP -T$@ o/$(MODE)/host/third_party/infozip/zip.com -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$@ o/$(MODE)/host/third_party/infozip/zip.com -q $@ tool/net/ tool/net/demo/ tool/net/demo/index.html tool/net/demo/redbean.css tool/net/redbean.c net/http/parsehttpmessage.c net/http/parseurl.c net/http/encodeurl.c test/net/http/parsehttpmessage_test.c test/net/http/parseurl_test.c
@printf "<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.\r\nSince 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)\nin addition to SQLite and SSL. The text you're reading now is a PKZIP End Of Central Directory comment.\r\n<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.\r\nIt automatically generates this listing page based on your zip contents. If you use redbean as an application server / web development environment,\r\nthen 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$@ o/$(MODE)/host/third_party/infozip/zip.com -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

View file

@ -552,7 +552,7 @@ static void UnmapLater(int f, void *p, size_t n) {
}
static void CollectGarbage(void) {
DestroyHttpRequest(&msg);
DestroyHttpMessage(&msg);
while (freelist.n) {
free(freelist.p[--freelist.n]);
}
@ -3058,8 +3058,8 @@ static void GetDosLocalTime(int64_t utcunixts, uint16_t *out_time,
static bool IsUtf8(const void *data, size_t size) {
const unsigned char *p, *pe;
for (p = data, pe = p + size; p + 2 <= pe; ++p) {
if (*p >= 0300) {
if (*p >= 0200 && *p < 0300) {
if (p[0] >= 0300) {
if (p[1] >= 0200 && p[1] < 0300) {
return true;
} else {
return false;
@ -5236,7 +5236,7 @@ static bool HandleMessage(void) {
struct iovec iov[4];
long actualcontentlength;
g_syscount = 0;
if ((rc = ParseHttpRequest(&msg, inbuf.p, amtread)) != -1) {
if ((rc = ParseHttpMessage(&msg, inbuf.p, amtread)) != -1) {
if (!rc) return false;
hdrsize = rc;
if (logmessages) LogMessage("received", inbuf.p, hdrsize);
@ -5317,7 +5317,7 @@ static void InitRequest(void) {
outbuf.n = 0;
luaheaderp = 0;
contentlength = 0;
InitHttpRequest(&msg);
InitHttpMessage(&msg, kHttpRequest);
}
static void HandleMessages(void) {