Make improvements

- Emulator can now test the αcτµαlly pδrταblε εxεcµταblε bootloader

- Whipped up a webserver named redbean. It services 150k requests per
  second on a single core. Bundling assets inside zip enables extremely
  fast serving for two reasons. The first is that zip central directory
  lookups go faster than stat() system calls. The second is that both
  zip and gzip content-encoding use DEFLATE, therefore, compressed
  responses can be served via the sendfile() system call which does an
  in-kernel copy directly from the zip executable structure. Also note
  that red bean zip executables can be deployed easily to all platforms,
  since these native executables work on Linux, Mac, BSD, and Windows.

- Address sanitizer now works very well
This commit is contained in:
Justine Tunney 2020-09-06 21:39:00 -07:00
parent 7327c345f9
commit 416fd86676
230 changed files with 9835 additions and 5682 deletions

View file

@ -1,33 +0,0 @@
/*-*- 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 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/runtime/runtime.h"
#include "net/http/http.h"
void freehttprequest(struct HttpRequest **req) {
if (*req) {
critbit0_clear(&(*req)->headers);
free_s(&(*req)->method.p);
free_s(&(*req)->scratch.p);
free_s(&(*req)->uri.p);
free_s(&(*req)->version.p);
free_s(req);
}
}

View file

@ -21,13 +21,13 @@
#include "net/http/http.h"
/**
* Returns small number for HTTP header, or 0 if not found.
* Returns small number for HTTP header, or -1 if not found.
*/
enum HttpHeader gethttpheader(const char *str, unsigned int len) {
int GetHttpHeader(const char *str, size_t len) {
const struct HttpHeaderSlot *slot;
if ((slot = in_word_set(str, len))) {
if ((slot = LookupHttpHeader(str, len))) {
return slot->code;
} else {
return 0;
return -1;
}
}

View file

@ -6,57 +6,57 @@
%compare-strncmp
%ignore-case
%language=ANSI-C
%pic
%readonly-tables
%struct-type
struct HttpHeaderSlot { unsigned char name; unsigned char code; };
%define lookup-function-name LookupHttpHeader
struct HttpHeaderSlot { char *name; char code; };
%%
Accept, kHttpAccept
Accept-Charset, kHttpAcceptCharset
Accept-Encoding, kHttpAcceptEncoding
Accept-Language, kHttpAcceptLanguage
Age, kHttpAge
Allow, kHttpAllow
Authorization, kHttpAuthorization
Cache-Control, kHttpCacheControl
Chunked, kHttpChunked
Close, kHttpClose
Connection, kHttpConnection
Content-Base, kHttpContentBase
Content-Encoding, kHttpContentEncoding
Content-Language, kHttpContentLanguage
Content-Length, kHttpContentLength
Content-Location, kHttpContentLocation
Content-Md5, kHttpContentMd5
Content-Range, kHttpContentRange
Content-Type, kHttpContentType
Date, kHttpDate
Etag, kHttpEtag
Expires, kHttpExpires
From, kHttpFrom
Host, kHttpHost
If-Match, kHttpIfMatch
If-Modified-Since, kHttpIfModifiedSince
If-None-Match, kHttpIfNoneMatch
If-Range, kHttpIfRange
If-Unmodified-Since, kHttpIfUnmodifiedSince
Keep-Alive, kHttpKeepAlive
Max-Forwards, kHttpMaxForwards
Pragma, kHttpPragma
Proxy-Authenticate, kHttpProxyAuthenticate
Proxy-Authorization, kHttpProxyAuthorization
Proxy-Connection, kHttpProxyConnection
Range, kHttpRange
Referer, kHttpReferer
Transfer-Encoding, kHttpTransferEncoding
Upgrade, kHttpUpgrade
User-Agent, kHttpUserAgent
Via, kHttpVia
Location, kHttpLocation
Public, kHttpPublic
Retry-After, kHttpRetryAfter
Server, kHttpServer
Vary, kHttpVary
Warning, kHttpWarning
WWW-Authenticate, kHttpWwwAuthenticate
Last-Modified, kHttpLastModified
Accept, kHttpAccept
Accept-Charset, kHttpAcceptCharset
Accept-Encoding, kHttpAcceptEncoding
Accept-Language, kHttpAcceptLanguage
Age, kHttpAge
Allow, kHttpAllow
Authorization, kHttpAuthorization
Cache-Control, kHttpCacheControl
Chunked, kHttpChunked
Close, kHttpClose
Connection, kHttpConnection
Content-Base, kHttpContentBase
Content-Encoding, kHttpContentEncoding
Content-Language, kHttpContentLanguage
Content-Length, kHttpContentLength
Content-Location, kHttpContentLocation
Content-Md5, kHttpContentMd5
Content-Range, kHttpContentRange
Content-Type, kHttpContentType
Date, kHttpDate
Etag, kHttpEtag
Expires, kHttpExpires
From, kHttpFrom
Host, kHttpHost
If-Match, kHttpIfMatch
If-Modified-Since, kHttpIfModifiedSince
If-None-Match, kHttpIfNoneMatch
If-Range, kHttpIfRange
If-Unmodified-Since, kHttpIfUnmodifiedSince
Keep-Alive, kHttpKeepAlive
Max-Forwards, kHttpMaxForwards
Pragma, kHttpPragma
Proxy-Authenticate, kHttpProxyAuthenticate
Proxy-Authorization, kHttpProxyAuthorization
Proxy-Connection, kHttpProxyConnection
Range, kHttpRange
Referer, kHttpReferer
Transfer-Encoding, kHttpTransferEncoding
Upgrade, kHttpUpgrade
User-Agent, kHttpUserAgent
Via, kHttpVia
Location, kHttpLocation
Public, kHttpPublic
Retry-After, kHttpRetryAfter
Server, kHttpServer
Vary, kHttpVary
Warning, kHttpWarning
WWW-Authenticate, kHttpWwwAuthenticate
Last-Modified, kHttpLastModified

View file

@ -1,5 +1,5 @@
/* ANSI-C code produced by gperf version 3.0.4 */
/* Command-line: gperf net/http/gethttpheader.gperf */
/* ANSI-C code produced by gperf version 3.1 */
/* Command-line: gperf gethttpheader.gperf */
/* Computed positions: -k'1,9,$' */
#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
@ -26,16 +26,16 @@
&& ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
&& ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))
/* The character set is not based on ISO-646. */
#error "gperf generated tables don't work with this execution character set. Please report a bug to <bug-gnu-gperf@gnu.org>."
#error "gperf generated tables don't work with this execution character set. Please report a bug to <bug-gperf@gnu.org>."
#endif
#line 1 "net/http/gethttpheader.gperf"
#line 1 "gethttpheader.gperf"
#include "libc/str/str.h"
#include "net/http/http.h"
#define GPERF_DOWNCASE
#line 12 "net/http/gethttpheader.gperf"
struct HttpHeaderSlot { unsigned char name; unsigned char code; };
#line 12 "gethttpheader.gperf"
struct HttpHeaderSlot { char *name; char code; };
#define TOTAL_KEYWORDS 49
#define MIN_WORD_LENGTH 3
@ -72,7 +72,7 @@ static unsigned char gperf_downcase[256] =
#ifndef GPERF_CASE_STRNCMP
#define GPERF_CASE_STRNCMP 1
static int
gperf_case_strncmp (register const char *s1, register const char *s2, register unsigned int n)
gperf_case_strncmp (register const char *s1, register const char *s2, register size_t n)
{
for (; n > 0;)
{
@ -97,7 +97,7 @@ inline
#endif
#endif
static unsigned int
hash (register const char *str, register unsigned int len)
hash (register const char *str, register size_t len)
{
static const unsigned char asso_values[] =
{
@ -128,7 +128,7 @@ hash (register const char *str, register unsigned int len)
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73
};
register int hval = len;
register unsigned int hval = len;
switch (hval)
{
@ -149,248 +149,133 @@ hash (register const char *str, register unsigned int len)
return hval + asso_values[(unsigned char)str[len - 1]];
}
struct stringpool_t
{
char stringpool_str5[sizeof("Close")];
char stringpool_str7[sizeof("Upgrade")];
char stringpool_str8[sizeof("Age")];
char stringpool_str9[sizeof("From")];
char stringpool_str10[sizeof("Range")];
char stringpool_str11[sizeof("Accept")];
char stringpool_str12[sizeof("Content-Type")];
char stringpool_str13[sizeof("If-Range")];
char stringpool_str15[sizeof("User-Agent")];
char stringpool_str16[sizeof("Content-Md5")];
char stringpool_str17[sizeof("Referer")];
char stringpool_str18[sizeof("Content-Range")];
char stringpool_str19[sizeof("Date")];
char stringpool_str20[sizeof("Connection")];
char stringpool_str21[sizeof("Retry-After")];
char stringpool_str22[sizeof("Chunked")];
char stringpool_str23[sizeof("Via")];
char stringpool_str24[sizeof("Host")];
char stringpool_str25[sizeof("Accept-Language")];
char stringpool_str26[sizeof("Public")];
char stringpool_str27[sizeof("If-Modified-Since")];
char stringpool_str28[sizeof("Authorization")];
char stringpool_str29[sizeof("If-Unmodified-Since")];
char stringpool_str30[sizeof("Allow")];
char stringpool_str31[sizeof("Pragma")];
char stringpool_str32[sizeof("Content-Base")];
char stringpool_str33[sizeof("If-Match")];
char stringpool_str34[sizeof("Vary")];
char stringpool_str35[sizeof("Keep-Alive")];
char stringpool_str36[sizeof("WWW-Authenticate")];
char stringpool_str37[sizeof("Expires")];
char stringpool_str38[sizeof("Proxy-Authenticate")];
char stringpool_str39[sizeof("Accept-Charset")];
char stringpool_str41[sizeof("Server")];
char stringpool_str43[sizeof("If-None-Match")];
char stringpool_str44[sizeof("Proxy-Authorization")];
char stringpool_str46[sizeof("Proxy-Connection")];
char stringpool_str48[sizeof("Location")];
char stringpool_str49[sizeof("Etag")];
char stringpool_str51[sizeof("Content-Language")];
char stringpool_str52[sizeof("Max-Forwards")];
char stringpool_str53[sizeof("Cache-Control")];
char stringpool_str56[sizeof("Content-Location")];
char stringpool_str61[sizeof("Content-Encoding")];
char stringpool_str62[sizeof("Transfer-Encoding")];
char stringpool_str68[sizeof("Last-Modified")];
char stringpool_str69[sizeof("Content-Length")];
char stringpool_str70[sizeof("Accept-Encoding")];
char stringpool_str72[sizeof("Warning")];
};
static const struct stringpool_t stringpool_contents =
{
"Close",
"Upgrade",
"Age",
"From",
"Range",
"Accept",
"Content-Type",
"If-Range",
"User-Agent",
"Content-Md5",
"Referer",
"Content-Range",
"Date",
"Connection",
"Retry-After",
"Chunked",
"Via",
"Host",
"Accept-Language",
"Public",
"If-Modified-Since",
"Authorization",
"If-Unmodified-Since",
"Allow",
"Pragma",
"Content-Base",
"If-Match",
"Vary",
"Keep-Alive",
"WWW-Authenticate",
"Expires",
"Proxy-Authenticate",
"Accept-Charset",
"Server",
"If-None-Match",
"Proxy-Authorization",
"Proxy-Connection",
"Location",
"Etag",
"Content-Language",
"Max-Forwards",
"Cache-Control",
"Content-Location",
"Content-Encoding",
"Transfer-Encoding",
"Last-Modified",
"Content-Length",
"Accept-Encoding",
"Warning"
};
#define stringpool ((const char *) &stringpool_contents)
#ifdef __GNUC__
__inline
#if defined __GNUC_STDC_INLINE__ || defined __GNUC_GNU_INLINE__
__attribute__ ((__gnu_inline__))
#endif
#endif
const struct HttpHeaderSlot *
in_word_set (register const char *str, register unsigned int len)
LookupHttpHeader (register const char *str, register size_t len)
{
static const struct HttpHeaderSlot wordlist[] =
{
{-1}, {-1}, {-1}, {-1}, {-1},
#line 23 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str5, kHttpClose },
{-1},
#line 52 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str7, kHttpUpgrade },
#line 18 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str8, kHttpAge },
#line 36 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str9, kHttpFrom },
#line 49 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str10, kHttpRange },
#line 14 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str11, kHttpAccept },
#line 32 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str12, kHttpContentType },
#line 41 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str13, kHttpIfRange },
{-1},
#line 53 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str15, kHttpUserAgent },
#line 30 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str16, kHttpContentMd5 },
#line 50 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str17, kHttpReferer },
#line 31 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str18, kHttpContentRange },
#line 33 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str19, kHttpDate },
#line 24 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str20, kHttpConnection },
#line 57 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str21, kHttpRetryAfter },
#line 22 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str22, kHttpChunked },
#line 54 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str23, kHttpVia },
#line 37 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str24, kHttpHost },
#line 17 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str25, kHttpAcceptLanguage },
#line 56 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str26, kHttpPublic },
#line 39 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str27, kHttpIfModifiedSince },
#line 20 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str28, kHttpAuthorization },
#line 42 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str29, kHttpIfUnmodifiedSince },
#line 19 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str30, kHttpAllow },
#line 45 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str31, kHttpPragma },
#line 25 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str32, kHttpContentBase },
#line 38 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str33, kHttpIfMatch },
#line 59 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str34, kHttpVary },
#line 43 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str35, kHttpKeepAlive },
#line 61 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str36, kHttpWwwAuthenticate },
#line 35 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str37, kHttpExpires },
#line 46 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str38, kHttpProxyAuthenticate },
#line 15 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str39, kHttpAcceptCharset },
{-1},
#line 58 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str41, kHttpServer },
{-1},
#line 40 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str43, kHttpIfNoneMatch },
#line 47 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str44, kHttpProxyAuthorization },
{-1},
#line 48 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str46, kHttpProxyConnection },
{-1},
#line 55 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str48, kHttpLocation },
#line 34 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str49, kHttpEtag },
{-1},
#line 27 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str51, kHttpContentLanguage },
#line 44 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str52, kHttpMaxForwards },
#line 21 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str53, kHttpCacheControl },
{-1}, {-1},
#line 29 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str56, kHttpContentLocation },
{-1}, {-1}, {-1}, {-1},
#line 26 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str61, kHttpContentEncoding },
#line 51 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str62, kHttpTransferEncoding },
{-1}, {-1}, {-1}, {-1}, {-1},
#line 62 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str68, kHttpLastModified },
#line 28 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str69, kHttpContentLength },
#line 16 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str70, kHttpAcceptEncoding },
{-1},
#line 60 "net/http/gethttpheader.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str72, kHttpWarning }
{""}, {""}, {""}, {""}, {""},
#line 23 "gethttpheader.gperf"
{"Close", kHttpClose},
{""},
#line 52 "gethttpheader.gperf"
{"Upgrade", kHttpUpgrade},
#line 18 "gethttpheader.gperf"
{"Age", kHttpAge},
#line 36 "gethttpheader.gperf"
{"From", kHttpFrom},
#line 49 "gethttpheader.gperf"
{"Range", kHttpRange},
#line 14 "gethttpheader.gperf"
{"Accept", kHttpAccept},
#line 32 "gethttpheader.gperf"
{"Content-Type", kHttpContentType},
#line 41 "gethttpheader.gperf"
{"If-Range", kHttpIfRange},
{""},
#line 53 "gethttpheader.gperf"
{"User-Agent", kHttpUserAgent},
#line 30 "gethttpheader.gperf"
{"Content-Md5", kHttpContentMd5},
#line 50 "gethttpheader.gperf"
{"Referer", kHttpReferer},
#line 31 "gethttpheader.gperf"
{"Content-Range", kHttpContentRange},
#line 33 "gethttpheader.gperf"
{"Date", kHttpDate},
#line 24 "gethttpheader.gperf"
{"Connection", kHttpConnection},
#line 57 "gethttpheader.gperf"
{"Retry-After", kHttpRetryAfter},
#line 22 "gethttpheader.gperf"
{"Chunked", kHttpChunked},
#line 54 "gethttpheader.gperf"
{"Via", kHttpVia},
#line 37 "gethttpheader.gperf"
{"Host", kHttpHost},
#line 17 "gethttpheader.gperf"
{"Accept-Language", kHttpAcceptLanguage},
#line 56 "gethttpheader.gperf"
{"Public", kHttpPublic},
#line 39 "gethttpheader.gperf"
{"If-Modified-Since", kHttpIfModifiedSince},
#line 20 "gethttpheader.gperf"
{"Authorization", kHttpAuthorization},
#line 42 "gethttpheader.gperf"
{"If-Unmodified-Since", kHttpIfUnmodifiedSince},
#line 19 "gethttpheader.gperf"
{"Allow", kHttpAllow},
#line 45 "gethttpheader.gperf"
{"Pragma", kHttpPragma},
#line 25 "gethttpheader.gperf"
{"Content-Base", kHttpContentBase},
#line 38 "gethttpheader.gperf"
{"If-Match", kHttpIfMatch},
#line 59 "gethttpheader.gperf"
{"Vary", kHttpVary},
#line 43 "gethttpheader.gperf"
{"Keep-Alive", kHttpKeepAlive},
#line 61 "gethttpheader.gperf"
{"WWW-Authenticate", kHttpWwwAuthenticate},
#line 35 "gethttpheader.gperf"
{"Expires", kHttpExpires},
#line 46 "gethttpheader.gperf"
{"Proxy-Authenticate", kHttpProxyAuthenticate},
#line 15 "gethttpheader.gperf"
{"Accept-Charset", kHttpAcceptCharset},
{""},
#line 58 "gethttpheader.gperf"
{"Server", kHttpServer},
{""},
#line 40 "gethttpheader.gperf"
{"If-None-Match", kHttpIfNoneMatch},
#line 47 "gethttpheader.gperf"
{"Proxy-Authorization", kHttpProxyAuthorization},
{""},
#line 48 "gethttpheader.gperf"
{"Proxy-Connection", kHttpProxyConnection},
{""},
#line 55 "gethttpheader.gperf"
{"Location", kHttpLocation},
#line 34 "gethttpheader.gperf"
{"Etag", kHttpEtag},
{""},
#line 27 "gethttpheader.gperf"
{"Content-Language", kHttpContentLanguage},
#line 44 "gethttpheader.gperf"
{"Max-Forwards", kHttpMaxForwards},
#line 21 "gethttpheader.gperf"
{"Cache-Control", kHttpCacheControl},
{""}, {""},
#line 29 "gethttpheader.gperf"
{"Content-Location", kHttpContentLocation},
{""}, {""}, {""}, {""},
#line 26 "gethttpheader.gperf"
{"Content-Encoding", kHttpContentEncoding},
#line 51 "gethttpheader.gperf"
{"Transfer-Encoding", kHttpTransferEncoding},
{""}, {""}, {""}, {""}, {""},
#line 62 "gethttpheader.gperf"
{"Last-Modified", kHttpLastModified},
#line 28 "gethttpheader.gperf"
{"Content-Length", kHttpContentLength},
#line 16 "gethttpheader.gperf"
{"Accept-Encoding", kHttpAcceptEncoding},
{""},
#line 60 "gethttpheader.gperf"
{"Warning", kHttpWarning}
};
if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
{
register int key = hash (str, len);
register unsigned int key = hash (str, len);
if (key <= MAX_HASH_VALUE && key >= 0)
if (key <= MAX_HASH_VALUE)
{
register int o = wordlist[key].name;
if (o >= 0)
{
register const char *s = o + stringpool;
register const char *s = wordlist[key].name;
if ((((unsigned char)*str ^ (unsigned char)*s) & ~32) == 0 && !gperf_case_strncmp (str, s, len) && s[len] == '\0')
return &wordlist[key];
}
if ((((unsigned char)*str ^ (unsigned char)*s) & ~32) == 0 && !gperf_case_strncmp (str, s, len) && s[len] == '\0')
return &wordlist[key];
}
}
return 0;

View file

@ -17,15 +17,17 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/bits/bits.h"
#include "libc/bits/pushpop.h"
#include "net/http/gethttpmethod.inc"
#include "net/http/http.h"
void clearhttprequest(struct HttpRequest *req) {
req->uri.i = pushpop(0);
req->method.i = pushpop(0);
req->version.i = pushpop(0);
req->scratch.i = pushpop(0);
critbit0_clear(&req->headers);
/**
* Returns small number for HTTP method, or -1 if not found.
*/
int GetHttpMethod(const char *str, size_t len) {
const struct HttpMethodSlot *slot;
if ((slot = LookupHttpMethod(str, len))) {
return slot->code;
} else {
return -1;
}
}

View file

@ -0,0 +1,36 @@
%{
#include "libc/str/str.h"
#include "net/http/http.h"
#define GPERF_DOWNCASE
%}
%compare-strncmp
%ignore-case
%language=ANSI-C
%readonly-tables
%struct-type
%define lookup-function-name LookupHttpMethod
struct HttpMethodSlot { char *name; char code; };
%%
DELETE, kHttpDelete
GET, kHttpGet
HEAD, kHttpHead
POST, kHttpPost
PUT, kHttpPut
CONNECT, kHttpConnect
OPTIONS, kHttpOptions
TRACE, kHttpTrace
COPY, kHttpCopy
CHECKOUT, kHttpCheckout
LOCK, kHttpLock
MERGE, kHttpMerge
MKACTIVITY, kHttpMkactivity
MKCOL, kHttpMkcol
MOVE, kHttpMove
NOTIFY, kHttpNotify
PATCH, kHttpPatch
PROPFIND, kHttpPropfind
PROPPATCH, kHttpProppatch
REPORT, kHttpReport
SUBSCRIBE, kHttpSubscribe
UNLOCK, kHttpUnlock
UNSUBSCRIBE, kHttpUnsubscribe

206
net/http/gethttpmethod.inc Normal file
View file

@ -0,0 +1,206 @@
/* ANSI-C code produced by gperf version 3.1 */
/* Command-line: gperf gethttpmethod.gperf */
/* Computed positions: -k'1-2' */
#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
&& ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
&& (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \
&& ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \
&& ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \
&& ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \
&& ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \
&& ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \
&& ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \
&& ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \
&& ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \
&& ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \
&& ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \
&& ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \
&& ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \
&& ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \
&& ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \
&& ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \
&& ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \
&& ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \
&& ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \
&& ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
&& ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))
/* The character set is not based on ISO-646. */
#error "gperf generated tables don't work with this execution character set. Please report a bug to <bug-gperf@gnu.org>."
#endif
#line 1 "gethttpmethod.gperf"
#include "libc/str/str.h"
#include "net/http/http.h"
#define GPERF_DOWNCASE
#line 12 "gethttpmethod.gperf"
struct HttpMethodSlot { char *name; char code; };
#define TOTAL_KEYWORDS 23
#define MIN_WORD_LENGTH 3
#define MAX_WORD_LENGTH 11
#define MIN_HASH_VALUE 4
#define MAX_HASH_VALUE 34
/* maximum key range = 31, duplicates = 0 */
#ifndef GPERF_DOWNCASE
#define GPERF_DOWNCASE 1
static unsigned char gperf_downcase[256] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224,
225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254,
255
};
#endif
#ifndef GPERF_CASE_STRNCMP
#define GPERF_CASE_STRNCMP 1
static int
gperf_case_strncmp (register const char *s1, register const char *s2, register size_t n)
{
for (; n > 0;)
{
unsigned char c1 = gperf_downcase[(unsigned char)*s1++];
unsigned char c2 = gperf_downcase[(unsigned char)*s2++];
if (c1 != 0 && c1 == c2)
{
n--;
continue;
}
return (int)c1 - (int)c2;
}
return 0;
}
#endif
#ifdef __GNUC__
__inline
#else
#ifdef __cplusplus
inline
#endif
#endif
static unsigned int
hash (register const char *str, register size_t len)
{
static const unsigned char asso_values[] =
{
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 0, 35, 5, 10, 10,
35, 5, 10, 35, 35, 0, 30, 15, 0, 0,
0, 35, 5, 15, 0, 5, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 0, 35, 5,
10, 10, 35, 5, 10, 35, 35, 0, 30, 15,
0, 0, 0, 35, 5, 15, 0, 5, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35
};
return len + asso_values[(unsigned char)str[1]] + asso_values[(unsigned char)str[0]];
}
const struct HttpMethodSlot *
LookupHttpMethod (register const char *str, register size_t len)
{
static const struct HttpMethodSlot wordlist[] =
{
{""}, {""}, {""}, {""},
#line 17 "gethttpmethod.gperf"
{"POST", kHttpPost},
#line 30 "gethttpmethod.gperf"
{"PATCH", kHttpPatch},
#line 29 "gethttpmethod.gperf"
{"NOTIFY", kHttpNotify},
#line 20 "gethttpmethod.gperf"
{"OPTIONS", kHttpOptions},
#line 18 "gethttpmethod.gperf"
{"PUT", kHttpPut},
#line 22 "gethttpmethod.gperf"
{"COPY", kHttpCopy},
#line 21 "gethttpmethod.gperf"
{"TRACE", kHttpTrace},
#line 35 "gethttpmethod.gperf"
{"UNLOCK", kHttpUnlock},
#line 19 "gethttpmethod.gperf"
{"CONNECT", kHttpConnect},
#line 31 "gethttpmethod.gperf"
{"PROPFIND", kHttpPropfind},
#line 32 "gethttpmethod.gperf"
{"PROPPATCH", kHttpProppatch},
{""},
#line 36 "gethttpmethod.gperf"
{"UNSUBSCRIBE", kHttpUnsubscribe},
{""},
#line 15 "gethttpmethod.gperf"
{"GET", kHttpGet},
#line 28 "gethttpmethod.gperf"
{"MOVE", kHttpMove},
#line 27 "gethttpmethod.gperf"
{"MKCOL", kHttpMkcol},
#line 33 "gethttpmethod.gperf"
{"REPORT", kHttpReport},
{""},
#line 23 "gethttpmethod.gperf"
{"CHECKOUT", kHttpCheckout},
#line 16 "gethttpmethod.gperf"
{"HEAD", kHttpHead},
#line 26 "gethttpmethod.gperf"
{"MKACTIVITY", kHttpMkactivity},
#line 14 "gethttpmethod.gperf"
{"DELETE", kHttpDelete},
{""}, {""},
#line 34 "gethttpmethod.gperf"
{"SUBSCRIBE", kHttpSubscribe},
#line 25 "gethttpmethod.gperf"
{"MERGE", kHttpMerge},
{""}, {""}, {""},
#line 24 "gethttpmethod.gperf"
{"LOCK", kHttpLock}
};
if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
{
register unsigned int key = hash (str, len);
if (key <= MAX_HASH_VALUE)
{
register const char *s = wordlist[key].name;
if ((((unsigned char)*str ^ (unsigned char)*s) & ~32) == 0 && !gperf_case_strncmp (str, s, len) && s[len] == '\0')
return &wordlist[key];
}
}
return 0;
}

View file

@ -1,80 +1,101 @@
#ifndef COSMOPOLITAN_LIBC_HTTP_HTTP_H_
#define COSMOPOLITAN_LIBC_HTTP_HTTP_H_
#include "libc/alg/alg.h"
#define kHttpGet 0
#define kHttpHead 1
#define kHttpPost 2
#define kHttpPut 3
#define kHttpDelete 4
#define kHttpConnect 5
#define kHttpOptions 6
#define kHttpTrace 7
#define kHttpCopy 8
#define kHttpCheckout 9
#define kHttpLock 10
#define kHttpMerge 11
#define kHttpMkactivity 12
#define kHttpMkcol 13
#define kHttpMove 14
#define kHttpNotify 15
#define kHttpPatch 16
#define kHttpPropfind 17
#define kHttpProppatch 18
#define kHttpReport 19
#define kHttpSubscribe 20
#define kHttpUnlock 21
#define kHttpUnsubscribe 22
#define kHttpAccept 0
#define kHttpAcceptCharset 1
#define kHttpAcceptEncoding 2
#define kHttpAcceptLanguage 3
#define kHttpAge 4
#define kHttpAllow 5
#define kHttpAuthorization 6
#define kHttpCacheControl 7
#define kHttpChunked 8
#define kHttpClose 9
#define kHttpConnection 10
#define kHttpContentBase 11
#define kHttpContentEncoding 12
#define kHttpContentLanguage 13
#define kHttpContentLength 14
#define kHttpContentLocation 15
#define kHttpContentMd5 16
#define kHttpContentRange 17
#define kHttpContentType 18
#define kHttpDate 19
#define kHttpEtag 20
#define kHttpExpires 21
#define kHttpFrom 22
#define kHttpHost 23
#define kHttpIfMatch 24
#define kHttpIfModifiedSince 25
#define kHttpIfNoneMatch 26
#define kHttpIfRange 27
#define kHttpIfUnmodifiedSince 28
#define kHttpKeepAlive 29
#define kHttpMaxForwards 30
#define kHttpPragma 31
#define kHttpProxyAuthenticate 32
#define kHttpProxyAuthorization 33
#define kHttpProxyConnection 34
#define kHttpRange 35
#define kHttpReferer 36
#define kHttpTransferEncoding 37
#define kHttpUpgrade 38
#define kHttpUserAgent 39
#define kHttpVia 40
#define kHttpLocation 41
#define kHttpPublic 42
#define kHttpRetryAfter 43
#define kHttpServer 44
#define kHttpVary 45
#define kHttpWarning 46
#define kHttpWwwAuthenticate 47
#define kHttpLastModified 48
#define kHttpHeadersMax 49
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct FILE;
enum HttpHeader {
kHttpAccept = 1,
kHttpAcceptCharset,
kHttpAcceptEncoding,
kHttpAcceptLanguage,
kHttpAge,
kHttpAllow,
kHttpAuthorization,
kHttpCacheControl,
kHttpChunked,
kHttpClose,
kHttpConnection,
kHttpContentBase,
kHttpContentEncoding,
kHttpContentLanguage,
kHttpContentLength,
kHttpContentLocation,
kHttpContentMd5,
kHttpContentRange,
kHttpContentType,
kHttpDate,
kHttpEtag,
kHttpExpires,
kHttpFrom,
kHttpHost,
kHttpIfMatch,
kHttpIfModifiedSince,
kHttpIfNoneMatch,
kHttpIfRange,
kHttpIfUnmodifiedSince,
kHttpKeepAlive,
kHttpMaxForwards,
kHttpPragma,
kHttpProxyAuthenticate,
kHttpProxyAuthorization,
kHttpProxyConnection,
kHttpRange,
kHttpReferer,
kHttpTransferEncoding,
kHttpUpgrade,
kHttpUserAgent,
kHttpVia,
kHttpLocation,
kHttpPublic,
kHttpRetryAfter,
kHttpServer,
kHttpVary,
kHttpWarning,
kHttpWwwAuthenticate,
kHttpLastModified,
};
struct HttpStr {
char *p;
size_t i, n;
struct HttpRequestSlice {
short a, b;
};
struct HttpRequest {
struct HttpStr uri; /* /foo/bar.html, etc. */
struct HttpStr method; /* "GET", "POST", etc. */
struct critbit0 headers; /* TreeMultiMap<"key:value"> (no space delims) */
struct HttpStr version; /* "HTTP/1.1", etc. */
struct HttpStr scratch; /* "HTTP/1.1", etc. */
int method;
struct HttpRequestSlice uri;
struct HttpRequestSlice version;
struct HttpRequestSlice scratch;
struct HttpRequestSlice headers[kHttpHeadersMax];
};
int parsehttprequest(struct HttpRequest *, struct FILE *) paramsnonnull();
void clearhttprequest(struct HttpRequest *) paramsnonnull();
void freehttprequest(struct HttpRequest **) paramsnonnull();
int negotiatehttprequest(int, const char *, uint32_t *, char *, uint32_t *,
int GetHttpHeader(const char *, size_t);
int GetHttpMethod(const char *, size_t);
int ParseHttpRequest(struct HttpRequest *, const char *, size_t);
int NegotiateHttpRequest(int, const char *, uint32_t *, char *, uint32_t *,
uint32_t *, bool, long double);
COSMOPOLITAN_C_END_

View file

@ -1,24 +0,0 @@
DELETE
GET
HEAD
POST
PUT
CONNECT
OPTIONS
TRACE
COPY
CHECKOUT
LOCK
M-SEARCH
MERGE
MKACTIVITY
MKCOL
MOVE
NOTIFY
PATCH
PROPFIND
PROPPATCH
REPORT
SUBSCRIBE
UNLOCK
UNSUBSCRIBE

View file

@ -42,7 +42,7 @@ static pureconst long AsMilliseconds(long double ts) {
* @param singleshot should be true w/ connection: close
* @return 0 on success, or -1 w/ errno
*/
int negotiatehttprequest(int sock, const char *req, uint32_t *inout_reqsize,
int NegotiateHttpRequest(int sock, const char *req, uint32_t *inout_reqsize,
char *resp, uint32_t *inout_respsize,
uint32_t *out_resphdrsize, bool singleshot,
long double timeout) {

View file

@ -39,95 +39,84 @@ enum ParseHttpRequestState {
};
/**
* Parses req line and headers of HTTP req.
*
* This parser is very lax. All decoding is ISO-8859-1. A 1mB upper
* bound on memory is enforced.
* Parses HTTP request header.
*/
int parsehttprequest(struct HttpRequest *req, FILE *f) {
enum ParseHttpRequestState state = METHOD;
int toto = 0;
clearhttprequest(req);
while (true) {
char ch;
{
int c = fgetc(f);
if (c == -1) {
if (toto && !ferror(f)) {
return ebadmsg(); /* RFC7230 § 3.4 */
}
return -1;
}
ch = (unsigned char)c;
}
if (++toto == UINT16_MAX) {
return ebadmsg(); /* RFC7230 § 3.2.5 */
}
switch (state) {
int ParseHttpRequest(struct HttpRequest *req, const char *p, size_t n) {
int a, h, c, i, x;
enum ParseHttpRequestState t;
memset(req, 0, sizeof(*req));
a = h = 0;
t = METHOD;
if (n > SHRT_MAX - 1) n = SHRT_MAX - 1;
for (i = 0; i < n; ++i) {
c = p[i] & 0xFF;
switch (t) {
case METHOD:
if (ch == '\r' || ch == '\n') break; /* RFC7230 § 3.5 */
if (ch == ' ') {
if (req->method.i == 0) return ebadmsg();
state = URI;
} else {
append(&req->method, &ch);
if (c == '\r' || c == '\n') break; /* RFC7230 § 3.5 */
if (c == ' ') {
if (!i) return ebadmsg();
if ((x = GetHttpMethod(p, i)) == -1) return ebadmsg();
req->method = x;
req->uri.a = i + 1;
t = URI;
}
break;
case URI:
if (ch == ' ') {
if (req->uri.i == 0) return ebadmsg();
state = VERSION;
} else {
append(&req->uri, &ch);
if (c == ' ') {
req->uri.b = i;
req->version.a = i + 1;
if (req->uri.a == req->uri.b) return ebadmsg();
t = VERSION;
}
break;
case VERSION:
if (ch == '\r' || ch == '\n') {
state = ch == '\r' ? CR1 : LF1;
} else {
append(&req->version, &ch);
if (c == '\r' || c == '\n') {
req->version.b = i;
t = c == '\r' ? CR1 : LF1;
}
break;
case CR1:
if (ch != '\n') return ebadmsg();
state = LF1;
if (c != '\n') return ebadmsg();
t = LF1;
break;
case LF1:
if (ch == '\r') {
state = LF2;
if (c == '\r') {
t = LF2;
break;
} else if (ch == '\n') {
} else if (c == '\n') {
return 0;
} else if (ch == ' ' || ch == '\t') { /* line folding!!! */
return eprotonosupport(); /* RFC7230 § 3.2.4 */
} else if (c == ' ' || c == '\t') { /* line folding!!! */
return eprotonosupport(); /* RFC7230 § 3.2.4 */
}
state = HKEY;
a = i;
t = HKEY;
/* εpsilon transition */
case HKEY:
if (ch == ':') state = HSEP;
ch = tolower(ch);
append(&req->scratch, &ch);
if (c == ':') {
h = GetHttpHeader(p + a, i - a);
t = HSEP;
}
break;
case HSEP:
if (ch == ' ' || ch == '\t') break;
state = HVAL;
if (c == ' ' || c == '\t') break;
a = i;
t = HVAL;
/* εpsilon transition */
case HVAL:
if (ch == '\r' || ch == '\n') {
state = ch == '\r' ? CR1 : LF1;
ch = '\0';
}
append(&req->scratch, &ch);
if (!ch) {
critbit0_insert(&req->headers, req->scratch.p);
req->scratch.i = 0;
if (c == '\r' || c == '\n') {
if (h != -1) {
req->headers[h].a = a;
req->headers[h].b = i;
}
t = c == '\r' ? CR1 : LF1;
}
break;
case LF2:
if (ch == '\n') return 0;
if (c == '\n') return 0;
return ebadmsg();
default:
unreachable;
}
}
return ebadmsg();
}