xref: /openbsd/usr.sbin/httpd/http.h (revision 05c2c945)
1 /*	$OpenBSD: http.h,v 1.11 2015/01/13 09:21:15 reyk Exp $	*/
2 
3 /*
4  * Copyright (c) 2012 - 2015 Reyk Floeter <reyk@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef _HTTP_H
20 #define _HTTP_H
21 
22 #define HTTP_PORT	80
23 #define HTTPS_PORT	443
24 
25 enum httpmethod {
26 	HTTP_METHOD_NONE	= 0,
27 
28 	/* HTTP/1.1, RFC 7231 */
29 	HTTP_METHOD_GET,
30 	HTTP_METHOD_HEAD,
31 	HTTP_METHOD_POST,
32 	HTTP_METHOD_PUT,
33 	HTTP_METHOD_DELETE,
34 	HTTP_METHOD_OPTIONS,
35 	HTTP_METHOD_TRACE,
36 	HTTP_METHOD_CONNECT,
37 
38 	/* WebDAV, RFC 4918 */
39 	HTTP_METHOD_PROPFIND,
40 	HTTP_METHOD_PROPPATCH,
41 	HTTP_METHOD_MKCOL,
42 	HTTP_METHOD_COPY,
43 	HTTP_METHOD_MOVE,
44 	HTTP_METHOD_LOCK,
45 	HTTP_METHOD_UNLOCK,
46 
47 	/* WebDAV Versioning Extension, RFC 3253 */
48 	HTTP_METHOD_VERSION_CONTROL,
49 	HTTP_METHOD_REPORT,
50 	HTTP_METHOD_CHECKOUT,
51 	HTTP_METHOD_CHECKIN,
52 	HTTP_METHOD_UNCHECKOUT,
53 	HTTP_METHOD_MKWORKSPACE,
54 	HTTP_METHOD_UPDATE,
55 	HTTP_METHOD_LABEL,
56 	HTTP_METHOD_MERGE,
57 	HTTP_METHOD_BASELINE_CONTROL,
58 	HTTP_METHOD_MKACTIVITY,
59 
60 	/* WebDAV Ordered Collections, RFC 3648 */
61 	HTTP_METHOD_ORDERPATCH,
62 
63 	/* WebDAV Access Control, RFC 3744 */
64 	HTTP_METHOD_ACL,
65 
66 	/* WebDAV Redirect Reference Resources, RFC 4437 */
67 	HTTP_METHOD_MKREDIRECTREF,
68 	HTTP_METHOD_UPDATEREDIRECTREF,
69 
70 	/* WebDAV Search, RFC 5323 */
71 	HTTP_METHOD_SEARCH,
72 
73 	/* PATCH, RFC 5789 */
74 	HTTP_METHOD_PATCH,
75 
76 	/* Server response (internal value) */
77 	HTTP_METHOD_RESPONSE
78 };
79 
80 struct http_method {
81 	enum httpmethod		 method_id;
82 	const char		*method_name;
83 };
84 #define HTTP_METHODS		{			\
85 	{ HTTP_METHOD_GET,		"GET" },	\
86 	{ HTTP_METHOD_HEAD,		"HEAD" },	\
87 	{ HTTP_METHOD_POST,		"POST" },	\
88 	{ HTTP_METHOD_PUT,		"PUT" },	\
89 	{ HTTP_METHOD_DELETE,		"DELETE" },	\
90 	{ HTTP_METHOD_OPTIONS,		"OPTIONS" },	\
91 	{ HTTP_METHOD_TRACE,		"TRACE" },	\
92 	{ HTTP_METHOD_CONNECT,		"CONNECT" },	\
93 	{ HTTP_METHOD_PROPFIND,		"PROPFIND" },	\
94 	{ HTTP_METHOD_PROPPATCH,	"PROPPATCH" },	\
95 	{ HTTP_METHOD_MKCOL,		"MKCOL" },	\
96 	{ HTTP_METHOD_COPY,		"COPY" },	\
97 	{ HTTP_METHOD_MOVE,		"MOVE" },	\
98 	{ HTTP_METHOD_LOCK,		"LOCK" },	\
99 	{ HTTP_METHOD_UNLOCK,		"UNLOCK" },	\
100 	{ HTTP_METHOD_VERSION_CONTROL,	"VERSION-CONTROL" }, \
101 	{ HTTP_METHOD_REPORT,		"REPORT" },	\
102 	{ HTTP_METHOD_CHECKOUT,		"CHECKOUT" },	\
103 	{ HTTP_METHOD_CHECKIN,		"CHECKIN" },	\
104 	{ HTTP_METHOD_UNCHECKOUT,	"UNCHECKOUT" },	\
105 	{ HTTP_METHOD_MKWORKSPACE,	"MKWORKSPACE" }, \
106 	{ HTTP_METHOD_UPDATE,		"UPDATE" },	\
107 	{ HTTP_METHOD_LABEL,		"LABEL" },	\
108 	{ HTTP_METHOD_MERGE,		"MERGE" },	\
109 	{ HTTP_METHOD_BASELINE_CONTROL,	"BASELINE-CONTROL" }, \
110 	{ HTTP_METHOD_MKACTIVITY,	"MKACTIVITY" },	\
111 	{ HTTP_METHOD_ORDERPATCH,	"ORDERPATCH" },	\
112 	{ HTTP_METHOD_ACL,		"ACL" },	\
113 	{ HTTP_METHOD_MKREDIRECTREF,	"MKREDIRECTREF" }, \
114 	{ HTTP_METHOD_UPDATEREDIRECTREF, "UPDATEREDIRECTREF" }, \
115 	{ HTTP_METHOD_SEARCH,		"SEARCH" },	\
116 	{ HTTP_METHOD_PATCH,		"PATCH" },	\
117 	{ HTTP_METHOD_NONE,		NULL }		\
118 }
119 
120 struct http_error {
121 	int			 error_code;
122 	const char		*error_name;
123 };
124 
125 /*
126  * HTTP status codes based on IANA assignments (2014-06-11 version):
127  * https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
128  * plus legacy (306) and non-standard (420).
129  */
130 #define HTTP_ERRORS		{			\
131 	{ 100,	"Continue" },				\
132 	{ 101,	"Switching Protocols" },		\
133 	{ 102,	"Processing" },				\
134 	/* 103-199 unassigned */			\
135 	{ 200,	"OK" },					\
136 	{ 201,	"Created" },				\
137 	{ 202,	"Accepted" },				\
138 	{ 203,	"Non-Authoritative Information" },	\
139 	{ 204,	"No Content" },				\
140 	{ 205,	"Reset Content" },			\
141 	{ 206,	"Partial Content" },			\
142 	{ 207,	"Multi-Status" },			\
143 	{ 208,	"Already Reported" },			\
144 	/* 209-225 unassigned */			\
145 	{ 226,	"IM Used" },				\
146 	/* 227-299 unassigned */			\
147 	{ 300,	"Multiple Choices" },			\
148 	{ 301,	"Moved Permanently" },			\
149 	{ 302,	"Found" },				\
150 	{ 303,	"See Other" },				\
151 	{ 304,	"Not Modified" },			\
152 	{ 305,	"Use Proxy" },				\
153 	{ 306,	"Switch Proxy" },			\
154 	{ 307,	"Temporary Redirect" },			\
155 	{ 308,	"Permanent Redirect" },			\
156 	/* 309-399 unassigned */			\
157 	{ 400,	"Bad Request" },			\
158 	{ 401,	"Unauthorized" },			\
159 	{ 402,	"Payment Required" },			\
160 	{ 403,	"Forbidden" },				\
161 	{ 404,	"Not Found" },				\
162 	{ 405,	"Method Not Allowed" },			\
163 	{ 406,	"Not Acceptable" },			\
164 	{ 407,	"Proxy Authentication Required" },	\
165 	{ 408,	"Request Timeout" },			\
166 	{ 409,	"Conflict" },				\
167 	{ 410,	"Gone" },				\
168 	{ 411,	"Length Required" },			\
169 	{ 412,	"Precondition Failed" },		\
170 	{ 413,	"Payload Too Large" },			\
171 	{ 414,	"URI Too Long" },			\
172 	{ 415,	"Unsupported Media Type" },		\
173 	{ 416,	"Range Not Satisfiable" },		\
174 	{ 417,	"Expectation Failed" },			\
175 	/* 418-421 unassigned */			\
176 	{ 420,	"Enhance Your Calm" },			\
177 	{ 422,	"Unprocessable Entity" },		\
178 	{ 423,	"Locked" },				\
179 	{ 424,	"Failed Dependency" },			\
180 	/* 425 unassigned */				\
181 	{ 426,	"Upgrade Required" },			\
182 	/* 427 unassigned */				\
183 	{ 428,	"Precondition Required" },		\
184 	{ 429,	"Too Many Requests" },			\
185 	/* 430 unassigned */				\
186 	{ 431,	"Request Header Fields Too Large" },	\
187 	/* 432-499 unassigned */			\
188 	{ 500,	"Internal Server Error" },		\
189 	{ 501,	"Not Implemented" },			\
190 	{ 502,	"Bad Gateway" },			\
191 	{ 503,	"Service Unavailable" },		\
192 	{ 504,	"Gateway Timeout" },			\
193 	{ 505,	"HTTP Version Not Supported" },		\
194 	{ 506,	"Variant Also Negotiates" },		\
195 	{ 507,	"Insufficient Storage" },		\
196 	{ 508,	"Loop Detected" },			\
197 	/* 509 unassigned */				\
198 	{ 510,	"Not Extended" },			\
199 	{ 511,	"Network Authentication Required" },	\
200 	/* 512-599 unassigned */			\
201 	{ 0,	NULL }					\
202 }
203 
204 struct http_mediatype {
205 	char		*media_name;
206 	char		*media_type;
207 	char		*media_subtype;
208 };
209 /*
210  * Some default media types based on (2014-08-04 version):
211  * https://www.iana.org/assignments/media-types/media-types.xhtml
212  */
213 #define MEDIA_TYPES		{			\
214 	{ "css",	"text",		"css" },	\
215 	{ "html",	"text",		"html" },	\
216 	{ "txt",	"text",		"plain" },	\
217 	{ "gif",	"image",	"gif" },	\
218 	{ "jpeg",	"image",	"jpeg" },	\
219 	{ "jpg",	"image",	"jpeg" },	\
220 	{ "png",	"image",	"png" },	\
221 	{ "svg",	"image",	"svg+xml" },	\
222 	{ "js",		"application",	"javascript" },	\
223 	{ NULL }					\
224 }
225 
226 /* Used during runtime */
227 struct http_descriptor {
228 	struct kv		 http_pathquery;
229 	struct kv		 http_matchquery;
230 #define http_path		 http_pathquery.kv_key
231 #define http_query		 http_pathquery.kv_value
232 #define http_rescode		 http_pathquery.kv_key
233 #define http_resmesg		 http_pathquery.kv_value
234 #define query_key		 http_matchquery.kv_key
235 #define query_val		 http_matchquery.kv_value
236 
237 	char			*http_host;
238 	enum httpmethod		 http_method;
239 	int			 http_chunked;
240 	char			*http_version;
241 
242 	/* Rewritten path remains NULL if not used */
243 	char			*http_path_alias;
244 
245 	/* A tree of headers and attached lists for repeated headers. */
246 	struct kv		*http_lastheader;
247 	struct kvtree		 http_headers;
248 };
249 
250 #endif /* _HTTP_H */
251