Make major improvements to stdio

Buffering now has optimal performance, bugs have been fixed, and some
missing apis have been introduced. This implementation is also now more
production worthy since it's less brittle now in terms of system errors.
That's going to help redbean since lua i/o is all based on stdio.

See #97
This commit is contained in:
Justine Tunney 2021-03-26 22:31:41 -07:00
parent 09bcfa23d5
commit da36e7e256
69 changed files with 1595 additions and 735 deletions

View file

@ -27,44 +27,130 @@
* HTML entities and forward slash are escaped too for added safety.
*
* We assume the UTF-8 is well-formed and can be represented as UTF-16.
* Things that can't be decoded or encoded will be replaced with invalid
* code-point markers. This function is agnostic to numbers that have
* been used with malicious intent in the past under buggy software.
* Noncanonical encodings such as overlong NUL are canonicalized as NUL.
* Things that can't be decoded will fall back to binary. Things that
* can't be encoded will use invalid codepoint markers. This function is
* agnostic to numbers that have been used with malicious intent in the
* past under buggy software. Noncanonical encodings such as overlong
* NUL are canonicalized as NUL.
*/
struct EscapeResult EscapeJsStringLiteral(const char *data, size_t size) {
char *p;
size_t i;
unsigned n;
uint64_t w;
wint_t x, y;
unsigned i, n;
wint_t x, a, b;
const char *d, *e;
struct EscapeResult r;
d = data;
e = data + size;
p = r.data = xmalloc(size * 6 + 6 + 1);
for (i = 0; i < size;) {
x = data[i++] & 0xff;
if (x >= 0200) {
if (x >= 0300) {
n = ThomPikeLen(x);
x = ThomPikeByte(x);
while (--n) {
if (i < size) {
y = data[i++] & 0xff;
if (ThomPikeCont(y)) {
x = ThomPikeMerge(x, y);
} else {
x = 0xFFFD;
break;
}
} else {
x = 0xFFFD;
while (d < e) {
x = *d++ & 0xff;
if (x >= 0300) {
a = ThomPikeByte(x);
n = ThomPikeLen(x) - 1;
if (d + n <= e) {
for (i = 0;;) {
b = d[i] & 0xff;
if (!ThomPikeCont(b)) break;
a = ThomPikeMerge(a, b);
if (++i == n) {
x = a;
d += i;
break;
}
}
} else {
x = 0xFFFD;
}
}
switch (x) {
case ' ':
case '!':
case '#':
case '$':
case '%':
case '(':
case ')':
case '*':
case '+':
case ',':
case '-':
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case ':':
case ';':
case '?':
case '@':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '[':
case ']':
case '^':
case '_':
case '`':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case '{':
case '|':
case '}':
case '~':
*p++ = x;
break;
case '\t':
p[0] = '\\';
p[1] = 't';
@ -105,24 +191,19 @@ struct EscapeResult EscapeJsStringLiteral(const char *data, size_t size) {
p[1] = '\'';
p += 2;
break;
default:
if (0x20 <= x && x < 0x7F) {
*p++ = x;
break;
}
/* fallthrough */
case '<':
case '>':
case '&':
case '=':
default:
w = EncodeUtf16(x);
do {
p[0] = '\\';
p[1] = 'u';
p[2] = "0123456789ABCDEF"[(w & 0xF000) >> 014];
p[3] = "0123456789ABCDEF"[(w & 0x0F00) >> 010];
p[4] = "0123456789ABCDEF"[(w & 0x00F0) >> 004];
p[5] = "0123456789ABCDEF"[(w & 0x000F) >> 000];
p[2] = "0123456789abcdef"[(w & 0xF000) >> 014];
p[3] = "0123456789abcdef"[(w & 0x0F00) >> 010];
p[4] = "0123456789abcdef"[(w & 0x00F0) >> 004];
p[5] = "0123456789abcdef"[(w & 0x000F) >> 000];
p += 6;
} while ((w >>= 16));
break;

View file

@ -31,7 +31,7 @@ Content-Md5, kHttpContentMd5
Content-Range, kHttpContentRange
Content-Type, kHttpContentType
Date, kHttpDate
Etag, kHttpEtag
ETag, kHttpEtag
Expires, kHttpExpires
From, kHttpFrom
Host, kHttpHost
@ -58,5 +58,10 @@ Retry-After, kHttpRetryAfter
Server, kHttpServer
Vary, kHttpVary
Warning, kHttpWarning
Www-Authenticate, kHttpWwwAuthenticate
WWW-Authenticate, kHttpWwwAuthenticate
Last-Modified, kHttpLastModified
Cookie, kHttpCookie
Trailer, kHttpTrailer
TE, kHttpTe
DNT, kHttpDnt
Expect, kHttpExpect

View file

@ -1,6 +1,6 @@
/* ANSI-C code produced by gperf version 3.1 */
/* Command-line: gperf gethttpheader.gperf */
/* Computed positions: -k'1,9,$' */
/* Computed positions: -k'3-4,10' */
#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
&& ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
@ -37,12 +37,12 @@
#line 12 "gethttpheader.gperf"
struct HttpHeaderSlot { char *name; char code; };
#define TOTAL_KEYWORDS 49
#define MIN_WORD_LENGTH 3
#define TOTAL_KEYWORDS 54
#define MIN_WORD_LENGTH 2
#define MAX_WORD_LENGTH 19
#define MIN_HASH_VALUE 5
#define MAX_HASH_VALUE 72
/* maximum key range = 68, duplicates = 0 */
#define MIN_HASH_VALUE 2
#define MAX_HASH_VALUE 89
/* maximum key range = 88, duplicates = 0 */
#ifndef GPERF_DOWNCASE
#define GPERF_DOWNCASE 1
@ -101,52 +101,55 @@ hash (register const char *str, register size_t len)
{
static const unsigned char asso_values[] =
{
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 0, 73, 73, 73, 73,
73, 73, 73, 0, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 5, 20, 0, 15, 0,
0, 45, 20, 5, 73, 10, 35, 5, 5, 5,
20, 73, 5, 30, 0, 0, 15, 20, 73, 15,
73, 73, 73, 73, 73, 73, 73, 5, 20, 0,
15, 0, 0, 45, 20, 5, 73, 10, 35, 5,
5, 5, 20, 73, 5, 30, 0, 0, 15, 20,
73, 15, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
73, 73, 73, 73, 73, 73
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 30, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 5, 5, 30, 50, 0,
35, 30, 0, 5, 90, 40, 0, 30, 0, 20,
45, 90, 0, 5, 10, 5, 0, 15, 20, 25,
90, 90, 90, 90, 90, 90, 90, 5, 5, 30,
50, 0, 35, 30, 0, 5, 90, 40, 0, 30,
0, 20, 45, 90, 0, 5, 10, 5, 0, 15,
20, 25, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90
};
register unsigned int hval = len;
switch (hval)
{
default:
hval += asso_values[(unsigned char)str[8]];
hval += asso_values[(unsigned char)str[9]];
/*FALLTHROUGH*/
case 9:
case 8:
case 7:
case 6:
case 5:
case 4:
hval += asso_values[(unsigned char)str[3]];
/*FALLTHROUGH*/
case 3:
hval += asso_values[(unsigned char)str[2]];
/*FALLTHROUGH*/
case 2:
case 1:
hval += asso_values[(unsigned char)str[0]];
break;
}
return hval + asso_values[(unsigned char)str[len - 1]];
return hval;
}
const struct HttpHeaderSlot *
@ -154,116 +157,134 @@ LookupHttpHeader (register const char *str, register size_t len)
{
static const struct HttpHeaderSlot wordlist[] =
{
{""}, {""}, {""}, {""}, {""},
#line 23 "gethttpheader.gperf"
{"Close", kHttpClose},
{""},
#line 52 "gethttpheader.gperf"
{"Upgrade", kHttpUpgrade},
{""}, {""},
#line 65 "gethttpheader.gperf"
{"TE", kHttpTe},
#line 18 "gethttpheader.gperf"
{"Age", kHttpAge},
#line 36 "gethttpheader.gperf"
{"From", kHttpFrom},
{""}, {""},
#line 58 "gethttpheader.gperf"
{"Server", kHttpServer},
#line 60 "gethttpheader.gperf"
{"Warning", kHttpWarning},
#line 54 "gethttpheader.gperf"
{"Via", kHttpVia},
{""},
#line 24 "gethttpheader.gperf"
{"Connection", kHttpConnection},
#line 56 "gethttpheader.gperf"
{"Public", kHttpPublic},
#line 22 "gethttpheader.gperf"
{"Chunked", kHttpChunked},
#line 66 "gethttpheader.gperf"
{"DNT", kHttpDnt},
#line 33 "gethttpheader.gperf"
{"Date", kHttpDate},
{""}, {""},
#line 64 "gethttpheader.gperf"
{"Trailer", kHttpTrailer},
{""},
#line 37 "gethttpheader.gperf"
{"Host", kHttpHost},
#line 53 "gethttpheader.gperf"
{"User-Agent", kHttpUserAgent},
#line 57 "gethttpheader.gperf"
{"Retry-After", kHttpRetryAfter},
#line 51 "gethttpheader.gperf"
{"Transfer-Encoding", kHttpTransferEncoding},
{""},
#line 28 "gethttpheader.gperf"
{"Content-Length", kHttpContentLength},
#line 19 "gethttpheader.gperf"
{"Allow", kHttpAllow},
#line 26 "gethttpheader.gperf"
{"Content-Encoding", kHttpContentEncoding},
#line 25 "gethttpheader.gperf"
{"Content-Base", kHttpContentBase},
#line 31 "gethttpheader.gperf"
{"Content-Range", kHttpContentRange},
#line 59 "gethttpheader.gperf"
{"Vary", kHttpVary},
#line 23 "gethttpheader.gperf"
{"Close", kHttpClose},
#line 27 "gethttpheader.gperf"
{"Content-Language", kHttpContentLanguage},
{""},
#line 20 "gethttpheader.gperf"
{"Authorization", kHttpAuthorization},
{""},
#line 49 "gethttpheader.gperf"
{"Range", kHttpRange},
#line 14 "gethttpheader.gperf"
{"Accept", kHttpAccept},
#line 32 "gethttpheader.gperf"
{"Content-Type", kHttpContentType},
#line 52 "gethttpheader.gperf"
{"Upgrade", kHttpUpgrade},
#line 41 "gethttpheader.gperf"
{"If-Range", kHttpIfRange},
#line 34 "gethttpheader.gperf"
{"ETag", kHttpEtag},
{""},
#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 50 "gethttpheader.gperf"
{"Referer", kHttpReferer},
#line 55 "gethttpheader.gperf"
{"Location", kHttpLocation},
{""},
#line 17 "gethttpheader.gperf"
{"Accept-Language", kHttpAcceptLanguage},
#line 29 "gethttpheader.gperf"
{"Content-Location", kHttpContentLocation},
#line 32 "gethttpheader.gperf"
{"Content-Type", kHttpContentType},
#line 40 "gethttpheader.gperf"
{"If-None-Match", kHttpIfNoneMatch},
#line 15 "gethttpheader.gperf"
{"Accept-Charset", kHttpAcceptCharset},
{""},
#line 67 "gethttpheader.gperf"
{"Expect", kHttpExpect},
{""},
#line 21 "gethttpheader.gperf"
{"Cache-Control", kHttpCacheControl},
#line 36 "gethttpheader.gperf"
{"From", kHttpFrom},
#line 43 "gethttpheader.gperf"
{"Keep-Alive", kHttpKeepAlive},
#line 61 "gethttpheader.gperf"
{"Www-Authenticate", kHttpWwwAuthenticate},
#line 48 "gethttpheader.gperf"
{"Proxy-Connection", kHttpProxyConnection},
#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 61 "gethttpheader.gperf"
{"WWW-Authenticate", kHttpWwwAuthenticate},
#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 63 "gethttpheader.gperf"
{"Cookie", kHttpCookie},
{""},
#line 38 "gethttpheader.gperf"
{"If-Match", kHttpIfMatch},
{""}, {""},
#line 30 "gethttpheader.gperf"
{"Content-Md5", kHttpContentMd5},
{""}, {""}, {""},
#line 16 "gethttpheader.gperf"
{"Accept-Encoding", kHttpAcceptEncoding},
{""},
#line 60 "gethttpheader.gperf"
{"Warning", kHttpWarning}
#line 39 "gethttpheader.gperf"
{"If-Modified-Since", kHttpIfModifiedSince},
{""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
{""}, {""},
#line 42 "gethttpheader.gperf"
{"If-Unmodified-Since", kHttpIfUnmodifiedSince}
};
if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)

View file

@ -91,6 +91,9 @@ static const struct HttpReason {
/**
* Returns string describing HTTP reason phrase.
*
* @see https://tools.ietf.org/html/rfc2616
* @see https://tools.ietf.org/html/rfc6585
*/
const char *GetHttpReason(int code) {
int m, l, r;

View file

@ -70,7 +70,12 @@
#define kHttpWarning 46
#define kHttpWwwAuthenticate 47
#define kHttpLastModified 48
#define kHttpHeadersMax 49
#define kHttpCookie 49
#define kHttpTrailer 50
#define kHttpTe 51
#define kHttpDnt 52
#define kHttpExpect 53
#define kHttpHeadersMax 54
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_