summaryrefslogtreecommitdiff
path: root/src/http_kv.c
blob: 90270be0511780e30eb6e231f0440a414ca1457d (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
/*
 * http_kv - HTTP version, method, status key-value string mapping
 *
 * Fully-rewritten from original
 * Copyright(c) 2018 Glenn Strauss gstrauss()gluelogic.com  All rights reserved
 * License: BSD 3-clause (same as lighttpd)
 */
#include "first.h"

#include "http_kv.h"
#include "buffer.h"

#include <string.h>

typedef struct {
	int key;
	unsigned int vlen;
	const char *value;
} keyvalue;

/* HTTP version string as SERVER_PROTOCOL string */
static const buffer http_versions[] = { /*(must by ordered by enum)*/
    { CONST_STR_LEN("HTTP/1.0")+1, 0 } /* HTTP_VERSION_1_0 */
   ,{ CONST_STR_LEN("HTTP/1.1")+1, 0 } /* HTTP_VERSION_1_1 */
   ,{ CONST_STR_LEN("HTTP/2.0")+1, 0 } /* HTTP_VERSION_2 */
   ,{ CONST_STR_LEN("HTTP/3.0")+1, 0 } /* HTTP_VERSION_3 */
   ,{ "", 0, 0 }
};

static const buffer http_methods[] = {
	{ CONST_STR_LEN("GET")+1, 0 },
	{ CONST_STR_LEN("HEAD")+1, 0 },
	{ CONST_STR_LEN("QUERY")+1, 0 },
	{ CONST_STR_LEN("POST")+1, 0 },
	{ CONST_STR_LEN("PUT")+1, 0 },
	{ CONST_STR_LEN("DELETE")+1, 0 },
	{ CONST_STR_LEN("CONNECT")+1, 0 },
	{ CONST_STR_LEN("OPTIONS")+1, 0 },
	{ CONST_STR_LEN("TRACE")+1, 0 },
	{ CONST_STR_LEN("ACL")+1, 0 },
	{ CONST_STR_LEN("BASELINE-CONTROL")+1, 0 },
	{ CONST_STR_LEN("BIND")+1, 0 },
	{ CONST_STR_LEN("CHECKIN")+1, 0 },
	{ CONST_STR_LEN("CHECKOUT")+1, 0 },
	{ CONST_STR_LEN("COPY")+1, 0 },
	{ CONST_STR_LEN("LABEL")+1, 0 },
	{ CONST_STR_LEN("LINK")+1, 0 },
	{ CONST_STR_LEN("LOCK")+1, 0 },
	{ CONST_STR_LEN("MERGE")+1, 0 },
	{ CONST_STR_LEN("MKACTIVITY")+1, 0 },
	{ CONST_STR_LEN("MKCALENDAR")+1, 0 },
	{ CONST_STR_LEN("MKCOL")+1, 0 },
	{ CONST_STR_LEN("MKREDIRECTREF")+1, 0 },
	{ CONST_STR_LEN("MKWORKSPACE")+1, 0 },
	{ CONST_STR_LEN("MOVE")+1, 0 },
	{ CONST_STR_LEN("ORDERPATCH")+1, 0 },
	{ CONST_STR_LEN("PATCH")+1, 0 },
	{ CONST_STR_LEN("PROPFIND")+1, 0 },
	{ CONST_STR_LEN("PROPPATCH")+1, 0 },
	{ CONST_STR_LEN("REBIND")+1, 0 },
	{ CONST_STR_LEN("REPORT")+1, 0 },
	{ CONST_STR_LEN("SEARCH")+1, 0 },
	{ CONST_STR_LEN("UNBIND")+1, 0 },
	{ CONST_STR_LEN("UNCHECKOUT")+1, 0 },
	{ CONST_STR_LEN("UNLINK")+1, 0 },
	{ CONST_STR_LEN("UNLOCK")+1, 0 },
	{ CONST_STR_LEN("UPDATE")+1, 0 },
	{ CONST_STR_LEN("UPDATEREDIRECTREF")+1, 0 },
	{ CONST_STR_LEN("VERSION-CONTROL")+1, 0 },

	{ CONST_STR_LEN("PRI")+1, 0 },
	{ "", 0, 0 }
};

/* https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml */
static const keyvalue http_status[] = {
	{ 100, CONST_LEN_STR("100 Continue") },
	{ 101, CONST_LEN_STR("101 Switching Protocols") },
	{ 102, CONST_LEN_STR("102 Processing") }, /* WebDAV */
	{ 103, CONST_LEN_STR("103 Early Hints") },
	{ 200, CONST_LEN_STR("200 OK") },
	{ 201, CONST_LEN_STR("201 Created") },
	{ 202, CONST_LEN_STR("202 Accepted") },
	{ 203, CONST_LEN_STR("203 Non-Authoritative Information") },
	{ 204, CONST_LEN_STR("204 No Content") },
	{ 205, CONST_LEN_STR("205 Reset Content") },
	{ 206, CONST_LEN_STR("206 Partial Content") },
	{ 207, CONST_LEN_STR("207 Multi-status") }, /* WebDAV */
	{ 208, CONST_LEN_STR("208 Already Reported") },
	{ 226, CONST_LEN_STR("226 IM Used") },
	{ 300, CONST_LEN_STR("300 Multiple Choices") },
	{ 301, CONST_LEN_STR("301 Moved Permanently") },
	{ 302, CONST_LEN_STR("302 Found") },
	{ 303, CONST_LEN_STR("303 See Other") },
	{ 304, CONST_LEN_STR("304 Not Modified") },
	{ 305, CONST_LEN_STR("305 Use Proxy") },
	{ 306, CONST_LEN_STR("306 (Unused)") },
	{ 307, CONST_LEN_STR("307 Temporary Redirect") },
	{ 308, CONST_LEN_STR("308 Permanent Redirect") },
	{ 400, CONST_LEN_STR("400 Bad Request") },
	{ 401, CONST_LEN_STR("401 Unauthorized") },
	{ 402, CONST_LEN_STR("402 Payment Required") },
	{ 403, CONST_LEN_STR("403 Forbidden") },
	{ 404, CONST_LEN_STR("404 Not Found") },
	{ 405, CONST_LEN_STR("405 Method Not Allowed") },
	{ 406, CONST_LEN_STR("406 Not Acceptable") },
	{ 407, CONST_LEN_STR("407 Proxy Authentication Required") },
	{ 408, CONST_LEN_STR("408 Request Timeout") },
	{ 409, CONST_LEN_STR("409 Conflict") },
	{ 410, CONST_LEN_STR("410 Gone") },
	{ 411, CONST_LEN_STR("411 Length Required") },
	{ 412, CONST_LEN_STR("412 Precondition Failed") },
	{ 413, CONST_LEN_STR("413 Payload Too Large") },
	{ 414, CONST_LEN_STR("414 URI Too Long") },
	{ 415, CONST_LEN_STR("415 Unsupported Media Type") },
	{ 416, CONST_LEN_STR("416 Range Not Satisfiable") },
	{ 417, CONST_LEN_STR("417 Expectation Failed") },
	{ 421, CONST_LEN_STR("421 Misdirected Request") }, /* RFC 7540 */
	{ 422, CONST_LEN_STR("422 Unprocessable Entity") }, /* WebDAV */
	{ 423, CONST_LEN_STR("423 Locked") }, /* WebDAV */
	{ 424, CONST_LEN_STR("424 Failed Dependency") }, /* WebDAV */
	{ 425, CONST_LEN_STR("425 Too Early") },
	{ 426, CONST_LEN_STR("426 Upgrade Required") }, /* TLS */
	{ 428, CONST_LEN_STR("428 Precondition Required") },
	{ 429, CONST_LEN_STR("429 Too Many Requests") },
	{ 431, CONST_LEN_STR("431 Request Header Fields Too Large") },
	{ 451, CONST_LEN_STR("451 Unavailable For Legal Reasons") },
	{ 500, CONST_LEN_STR("500 Internal Server Error") },
	{ 501, CONST_LEN_STR("501 Not Implemented") },
	{ 502, CONST_LEN_STR("502 Bad Gateway") },
	{ 503, CONST_LEN_STR("503 Service Unavailable") },
	{ 504, CONST_LEN_STR("504 Gateway Timeout") },
	{ 505, CONST_LEN_STR("505 HTTP Version Not Supported") },
	{ 506, CONST_LEN_STR("506 Variant Also Negotiates") },
	{ 507, CONST_LEN_STR("507 Insufficient Storage") }, /* WebDAV */
	{ 508, CONST_LEN_STR("508 Loop Detected") },
	{ 510, CONST_LEN_STR("510 Not Extended") },
	{ 511, CONST_LEN_STR("511 Network Authentication Required") },

	{ -1, 0, NULL }
};


const buffer *
http_version_buf (http_version_t i)
{
    return ((unsigned int)i < sizeof(http_versions)/sizeof(*http_versions))
      ? http_versions+i
      : http_versions+sizeof(http_versions)/sizeof(*http_versions)-1;
}


const buffer *
http_method_buf (http_method_t i)
{
    return ((unsigned int)i < sizeof(http_methods)/sizeof(*http_methods)-2)
      ? http_methods+i
      : http_methods+i+sizeof(http_methods)/sizeof(*http_methods);
        /* HTTP_METHOD_PRI is -2, HTTP_METHOD_UNSET is -1 */
}


__attribute_pure__
static const keyvalue *
keyvalue_from_key (const keyvalue *kv, const int k)
{
    /*(expects sentinel to have key == -1 and value == NULL)*/
    while (kv->key != k && kv->key != -1) ++kv;
    return kv;
}


http_method_t
http_method_key_get (const char *s, const size_t slen)
{
    if (slen == 3 && s[0] == 'G' && s[1] == 'E' && s[2] == 'T')
        return HTTP_METHOD_GET;
    const buffer *kv = http_methods+1; /*(step over http_methods[0] ("GET"))*/
    while (kv->used && (kv->used-1 != slen || 0 != memcmp(kv->ptr, s, slen)))
        ++kv;
    const uint_fast32_t i = kv - http_methods;
    /*(not done: could overload kv->size and store enum in kv->size)*/
    return (i < sizeof(http_methods)/sizeof(*http_methods)-2)
      ? (http_method_t)i
      : i == sizeof(http_methods)/sizeof(*http_methods)-2
        ? HTTP_METHOD_PRI
        : HTTP_METHOD_UNSET;
}


void
http_status_append (buffer * const b, const int status)
{
    if (200 == status) { /*(short-circuit common case)*/
        buffer_append_string_len(b, CONST_STR_LEN("200 OK"));
        return;
    }

    const keyvalue * const kv = keyvalue_from_key(http_status, status);
    if (__builtin_expect( (0 != kv->vlen), 1))
        buffer_append_string_len(b, kv->value, kv->vlen);
    else {
        buffer_append_int(b, status);
        buffer_append_char(b, ' ');
    }
}