Support any HTTP method

It's now possible to use redbean Fetch() with arbitrary HTTP methods,
e.g. LIST which is used by Hashicorp. There's an eight char limit and
uppercase canonicalization still happens. This change also includes a
better function for launching a browser tab, that won't deadlock on a
headless workstation running Debian.

Closes #1107
This commit is contained in:
Justine Tunney 2024-02-22 14:12:18 -08:00
parent 29eac8e2a2
commit ad3944a3b6
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
14 changed files with 249 additions and 412 deletions

View file

@ -1,30 +0,0 @@
%{
#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[8]; char code; };
%%
DELETE, kHttpDelete
GET, kHttpGet
HEAD, kHttpHead
POST, kHttpPost
PUT, kHttpPut
OPTIONS, kHttpOptions
CONNECT, kHttpConnect
TRACE, kHttpTrace
COPY, kHttpCopy
LOCK, kHttpLock
MERGE, kHttpMerge
MKCOL, kHttpMkcol
MOVE, kHttpMove
NOTIFY, kHttpNotify
PATCH, kHttpPatch
REPORT, kHttpReport
UNLOCK, kHttpUnlock

View file

@ -1,196 +0,0 @@
/* ANSI-C code produced by gperf version 3.1 */
/* Command-line: gperf gethttpmethod.gperf */
/* Computed positions: -k'1-2' */
/* clang-format off */
#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 "libc/str/tab.internal.h"
#include "net/http/http.h"
#define GPERF_DOWNCASE
#line 12 "gethttpmethod.gperf"
struct HttpMethodSlot { char name[8]; char code; };
#define TOTAL_KEYWORDS 17
#define MIN_WORD_LENGTH 3
#define MAX_WORD_LENGTH 7
#define MIN_HASH_VALUE 3
#define MAX_HASH_VALUE 25
/* maximum key range = 23, 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 inline 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[] =
{
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 0, 26, 5, 15, 0,
26, 5, 0, 26, 26, 10, 15, 10, 0, 5,
0, 26, 10, 26, 5, 0, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 0, 26, 5,
15, 0, 26, 5, 0, 26, 26, 10, 15, 10,
0, 5, 0, 26, 10, 26, 5, 0, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26
};
return len + asso_values[(unsigned char)str[1]] + asso_values[(unsigned char)str[0]];
}
static inline const struct HttpMethodSlot *
LookupHttpMethod (register const char *str, register size_t len)
{
static const struct HttpMethodSlot wordlist[] =
{
{""}, {""}, {""},
#line 18 "gethttpmethod.gperf"
{"PUT", kHttpPut},
#line 16 "gethttpmethod.gperf"
{"HEAD", kHttpHead},
#line 28 "gethttpmethod.gperf"
{"PATCH", kHttpPatch},
#line 30 "gethttpmethod.gperf"
{"UNLOCK", kHttpUnlock},
{""},
#line 15 "gethttpmethod.gperf"
{"GET", kHttpGet},
#line 17 "gethttpmethod.gperf"
{"POST", kHttpPost},
{""},
#line 27 "gethttpmethod.gperf"
{"NOTIFY", kHttpNotify},
#line 19 "gethttpmethod.gperf"
{"OPTIONS", kHttpOptions},
{""},
#line 22 "gethttpmethod.gperf"
{"COPY", kHttpCopy},
#line 24 "gethttpmethod.gperf"
{"MERGE", kHttpMerge},
#line 29 "gethttpmethod.gperf"
{"REPORT", kHttpReport},
#line 20 "gethttpmethod.gperf"
{"CONNECT", kHttpConnect},
{""},
#line 26 "gethttpmethod.gperf"
{"MOVE", kHttpMove},
#line 21 "gethttpmethod.gperf"
{"TRACE", kHttpTrace},
#line 14 "gethttpmethod.gperf"
{"DELETE", kHttpDelete},
{""}, {""},
#line 23 "gethttpmethod.gperf"
{"LOCK", kHttpLock},
#line 25 "gethttpmethod.gperf"
{"MKCOL", kHttpMkcol}
};
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,27 +1,19 @@
#ifndef COSMOPOLITAN_LIBC_HTTP_HTTP_H_
#define COSMOPOLITAN_LIBC_HTTP_HTTP_H_
#include "libc/serialize.h"
#include "libc/time/struct/tm.h"
#define kHttpRequest 0
#define kHttpResponse 1
#define kHttpGet 1
#define kHttpHead 2
#define kHttpPost 3
#define kHttpPut 4
#define kHttpDelete 5
#define kHttpOptions 6
#define kHttpConnect 7
#define kHttpTrace 8
#define kHttpCopy 9
#define kHttpLock 10
#define kHttpMerge 11
#define kHttpMkcol 12
#define kHttpMove 13
#define kHttpNotify 14
#define kHttpPatch 15
#define kHttpReport 16
#define kHttpUnlock 17
#define kHttpGet READ32LE("GET")
#define kHttpHead READ32LE("HEAD")
#define kHttpPost READ32LE("POST")
#define kHttpPut READ32LE("PUT")
#define kHttpDelete READ64LE("DELETE\0")
#define kHttpOptions READ64LE("OPTIONS")
#define kHttpConnect READ64LE("CONNECT")
#define kHttpTrace READ64LE("TRACE\0\0")
#define kHttpStateStart 0
#define kHttpStateMethod 1
@ -168,14 +160,13 @@ struct HttpMessage {
int i, a, status;
unsigned char t;
unsigned char type;
unsigned char method;
unsigned char version;
uint64_t method;
struct HttpSlice k;
struct HttpSlice uri;
struct HttpSlice scratch;
struct HttpSlice message;
struct HttpSlice headers[kHttpHeadersMax];
struct HttpSlice xmethod;
struct HttpHeaders xheaders;
};
@ -187,13 +178,11 @@ struct HttpUnchunker {
};
extern const char kHttpToken[256];
extern const char kHttpMethod[18][8];
extern const bool kHttpRepeatable[kHttpHeadersMax];
const char *GetHttpReason(int);
const char *GetHttpHeaderName(int);
int GetHttpHeader(const char *, size_t);
int GetHttpMethod(const char *, size_t);
void InitHttpMessage(struct HttpMessage *, int);
void DestroyHttpMessage(struct HttpMessage *);
int ParseHttpMessage(struct HttpMessage *, const char *, size_t);
@ -202,6 +191,7 @@ int64_t ParseContentLength(const char *, size_t);
char *FormatHttpDateTime(char[hasatleast 30], struct tm *);
bool ParseHttpRange(const char *, size_t, long, long *, long *);
int64_t ParseHttpDateTime(const char *, size_t);
uint64_t ParseHttpMethod(const char *, size_t);
bool IsValidHttpToken(const char *, size_t);
bool IsValidCookieValue(const char *, size_t);
bool IsAcceptablePath(const char *, size_t);

View file

@ -1,40 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "net/http/http.h"
const char kHttpMethod[18][8] = {
"WUT", //
"GET", //
"HEAD", //
"POST", //
"PUT", //
"DELETE", //
"OPTIONS", //
"CONNECT", //
"TRACE", //
"COPY", //
"LOCK", //
"MERGE", //
"MKCOL", //
"MOVE", //
"NOTIFY", //
"PATCH", //
"REPORT", //
"UNLOCK", //
};

View file

@ -17,14 +17,15 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/serialize.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/mem/alg.h"
#include "libc/mem/arraylist.internal.h"
#include "libc/mem/mem.h"
#include "libc/serialize.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/str/tab.internal.h"
#include "libc/sysv/errfuns.h"
#include "libc/x/x.h"
#include "net/http/http.h"
@ -90,23 +91,29 @@ int ParseHttpMessage(struct HttpMessage *r, const char *p, size_t n) {
c = p[r->i] & 0xff;
switch (r->t) {
case kHttpStateStart:
if (c == '\r' || c == '\n') break; /* RFC7230 § 3.5 */
if (c == '\r' || c == '\n') break; // RFC7230 § 3.5
if (!kHttpToken[c]) return ebadmsg();
r->t = r->type == kHttpRequest ? kHttpStateMethod : kHttpStateVersion;
r->a = r->i;
if (r->type == kHttpRequest) {
r->t = kHttpStateMethod;
r->method = kToUpper[c];
r->a = 8;
} else {
r->t = kHttpStateVersion;
r->a = r->i;
}
break;
case kHttpStateMethod:
for (;;) {
if (c == ' ') {
r->method = GetHttpMethod(p + r->a, r->i - r->a);
r->xmethod.a = r->a;
r->xmethod.b = r->i;
r->a = r->i + 1;
r->t = kHttpStateUri;
break;
} else if (!kHttpToken[c]) {
} else if (r->a == 64 || !kHttpToken[c]) {
return ebadmsg();
}
c = kToUpper[c];
r->method |= (uint64_t)c << r->a;
r->a += 8;
if (++r->i == n) break;
c = p[r->i] & 0xff;
}
@ -195,10 +202,8 @@ int ParseHttpMessage(struct HttpMessage *r, const char *p, size_t n) {
} else if (c == '\n') {
return ++r->i;
} else if (!kHttpToken[c]) {
/*
* 1. Forbid empty header name (RFC2616 §2.2)
* 2. Forbid line folding (RFC7230 §3.2.4)
*/
// 1. Forbid empty header name (RFC2616 §2.2)
// 2. Forbid line folding (RFC7230 §3.2.4)
return ebadmsg();
}
r->k.a = r->i;
@ -221,7 +226,7 @@ int ParseHttpMessage(struct HttpMessage *r, const char *p, size_t n) {
if (c == ' ' || c == '\t') break;
r->a = r->i;
r->t = kHttpStateValue;
/* fallthrough */
// fallthrough
case kHttpStateValue:
for (;;) {
if (c == '\r' || c == '\n') {

View file

@ -16,21 +16,28 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "net/http/gethttpmethod.inc"
#include "libc/str/str.h"
#include "libc/str/tab.internal.h"
#include "net/http/http.h"
/**
* Converts HTTP method string into internal index
* Converts HTTP method to word encoding.
*
* For example, `ParseHttpMethod("GET", -1)` will return `kHttpGet`.
*
* @param len if -1 implies strlen
* @return small number for HTTP method, or 0 if not found.
* @return word encoded method, or 0 if invalid
*/
int GetHttpMethod(const char *str, size_t len) {
const struct HttpMethodSlot *slot;
uint64_t ParseHttpMethod(const char *str, size_t len) {
int s = 0;
uint64_t w = 0;
if (len == -1) len = str ? strlen(str) : 0;
if ((slot = LookupHttpMethod(str, len))) {
return slot->code;
} else {
return 0;
for (size_t i = 0; i < len; ++i) {
int c = kToUpper[str[i] & 255];
if (!kHttpToken[c]) return 0;
if (s == 64) return 0;
w |= (uint64_t)c << s;
s += 8;
}
return w;
}