1 /* $OpenBSD: http.h,v 1.5 2014/08/14 10:30:52 reyk Exp $ */ 2 3 /* 4 * Copyright (c) 2012 - 2014 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 /* PATCH, RFC 5789 */ 48 HTTP_METHOD_PATCH, 49 50 /* Server response (internal value) */ 51 HTTP_METHOD_RESPONSE 52 }; 53 54 struct http_method { 55 enum httpmethod method_id; 56 const char *method_name; 57 }; 58 #define HTTP_METHODS { \ 59 { HTTP_METHOD_GET, "GET" }, \ 60 { HTTP_METHOD_HEAD, "HEAD" }, \ 61 { HTTP_METHOD_POST, "POST" }, \ 62 { HTTP_METHOD_PUT, "PUT" }, \ 63 { HTTP_METHOD_DELETE, "DELETE" }, \ 64 { HTTP_METHOD_OPTIONS, "OPTIONS" }, \ 65 { HTTP_METHOD_TRACE, "TRACE" }, \ 66 { HTTP_METHOD_CONNECT, "CONNECT" }, \ 67 { HTTP_METHOD_PROPFIND, "PROPFIND" }, \ 68 { HTTP_METHOD_PROPPATCH, "PROPPATCH" }, \ 69 { HTTP_METHOD_MKCOL, "MKCOL" }, \ 70 { HTTP_METHOD_COPY, "COPY" }, \ 71 { HTTP_METHOD_MOVE, "MOVE" }, \ 72 { HTTP_METHOD_LOCK, "LOCK" }, \ 73 { HTTP_METHOD_UNLOCK, "UNLOCK" }, \ 74 { HTTP_METHOD_PATCH, "PATCH" }, \ 75 { HTTP_METHOD_NONE, NULL } \ 76 } 77 78 struct http_error { 79 int error_code; 80 const char *error_name; 81 }; 82 83 /* 84 * HTTP status codes based on IANA assignments (2014-06-11 version): 85 * https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml 86 * plus legacy (306) and non-standard (420). 87 */ 88 #define HTTP_ERRORS { \ 89 { 100, "Continue" }, \ 90 { 101, "Switching Protocols" }, \ 91 { 102, "Processing" }, \ 92 /* 103-199 unassigned */ \ 93 { 200, "OK" }, \ 94 { 201, "Created" }, \ 95 { 202, "Accepted" }, \ 96 { 203, "Non-Authoritative Information" }, \ 97 { 204, "No Content" }, \ 98 { 205, "Reset Content" }, \ 99 { 206, "Partial Content" }, \ 100 { 207, "Multi-Status" }, \ 101 { 208, "Already Reported" }, \ 102 /* 209-225 unassigned */ \ 103 { 226, "IM Used" }, \ 104 /* 227-299 unassigned */ \ 105 { 300, "Multiple Choices" }, \ 106 { 301, "Moved Permanently" }, \ 107 { 302, "Found" }, \ 108 { 303, "See Other" }, \ 109 { 304, "Not Modified" }, \ 110 { 305, "Use Proxy" }, \ 111 { 306, "Switch Proxy" }, \ 112 { 307, "Temporary Redirect" }, \ 113 { 308, "Permanent Redirect" }, \ 114 /* 309-399 unassigned */ \ 115 { 400, "Bad Request" }, \ 116 { 401, "Unauthorized" }, \ 117 { 402, "Payment Required" }, \ 118 { 403, "Forbidden" }, \ 119 { 404, "Not Found" }, \ 120 { 405, "Method Not Allowed" }, \ 121 { 406, "Not Acceptable" }, \ 122 { 407, "Proxy Authentication Required" }, \ 123 { 408, "Request Timeout" }, \ 124 { 409, "Conflict" }, \ 125 { 410, "Gone" }, \ 126 { 411, "Length Required" }, \ 127 { 412, "Precondition Failed" }, \ 128 { 413, "Payload Too Large" }, \ 129 { 414, "URI Too Long" }, \ 130 { 415, "Unsupported Media Type" }, \ 131 { 416, "Range Not Satisfiable" }, \ 132 { 417, "Expectation Failed" }, \ 133 /* 418-421 unassigned */ \ 134 { 420, "Enhance Your Calm" }, \ 135 { 422, "Unprocessable Entity" }, \ 136 { 423, "Locked" }, \ 137 { 424, "Failed Dependency" }, \ 138 /* 425 unassigned */ \ 139 { 426, "Upgrade Required" }, \ 140 /* 427 unassigned */ \ 141 { 428, "Precondition Required" }, \ 142 { 429, "Too Many Requests" }, \ 143 /* 430 unassigned */ \ 144 { 431, "Request Header Fields Too Large" }, \ 145 /* 432-499 unassigned */ \ 146 { 500, "Internal Server Error" }, \ 147 { 501, "Not Implemented" }, \ 148 { 502, "Bad Gateway" }, \ 149 { 503, "Service Unavailable" }, \ 150 { 504, "Gateway Timeout" }, \ 151 { 505, "HTTP Version Not Supported" }, \ 152 { 506, "Variant Also Negotiates" }, \ 153 { 507, "Insufficient Storage" }, \ 154 { 508, "Loop Detected" }, \ 155 /* 509 unassigned */ \ 156 { 510, "Not Extended" }, \ 157 { 511, "Network Authentication Required" }, \ 158 /* 512-599 unassigned */ \ 159 { 0, NULL } \ 160 } 161 162 struct http_mediatype { 163 char *media_name; 164 char *media_type; 165 char *media_subtype; 166 }; 167 /* 168 * Some default media types based on (2014-08-04 version): 169 * https://www.iana.org/assignments/media-types/media-types.xhtml 170 */ 171 #define MEDIA_TYPES { \ 172 { "css", "text", "css" }, \ 173 { "html", "text", "html" }, \ 174 { "txt", "text", "plain" }, \ 175 { "gif", "image", "gif" }, \ 176 { "jpeg", "image", "jpeg" }, \ 177 { "jpg", "image", "jpeg" }, \ 178 { "png", "image", "png" }, \ 179 { "js", "application", "javascript" }, \ 180 { NULL } \ 181 } 182 183 /* Used during runtime */ 184 struct http_descriptor { 185 struct kv http_pathquery; 186 struct kv http_matchquery; 187 #define http_path http_pathquery.kv_key 188 #define http_query http_pathquery.kv_value 189 #define http_rescode http_pathquery.kv_key 190 #define http_resmesg http_pathquery.kv_value 191 #define query_key http_matchquery.kv_key 192 #define query_val http_matchquery.kv_value 193 194 char *http_host; 195 enum httpmethod http_method; 196 int http_chunked; 197 char *http_version; 198 199 /* Rewritten path remains NULL if not used */ 200 char *http_path_alias; 201 202 /* A tree of headers and attached lists for repeated headers. */ 203 struct kv *http_lastheader; 204 struct kvtree http_headers; 205 }; 206 207 #endif /* _HTTP_H */ 208