Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/common/http.h |
| 3 | * |
| 4 | * Version-agnostic and implementation-agnostic HTTP protocol definitions. |
| 5 | * |
| 6 | * Copyright (C) 2000-2018 Willy Tarreau - w@1wt.eu |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation, version 2.1 |
| 11 | * exclusively. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #ifndef _COMMON_HTTP_H |
| 24 | #define _COMMON_HTTP_H |
| 25 | |
| 26 | #include <common/buf.h> |
| 27 | #include <common/ist.h> |
| 28 | |
Christopher Faulet | 7e266c7 | 2018-10-03 14:15:28 +0200 | [diff] [blame] | 29 | /* |
| 30 | * some macros mainly used when parsing header fileds. |
| 31 | * from RFC7230: |
| 32 | * CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)> |
| 33 | * SEP = one of the 17 defined separators or SP or HT |
| 34 | * LWS = CR, LF, SP or HT |
| 35 | * SPHT = SP or HT. Use this macro and not a boolean expression for best speed. |
| 36 | * CRLF = CR or LF. Use this macro and not a boolean expression for best speed. |
| 37 | * token = any CHAR except CTL or SEP. Use this macro and not a boolean expression for best speed. |
| 38 | * |
| 39 | * added for ease of use: |
| 40 | * ver_token = 'H', 'P', 'T', '/', '.', and digits. |
| 41 | */ |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 42 | #define HTTP_FLG_CTL 0x01 |
| 43 | #define HTTP_FLG_SEP 0x02 |
| 44 | #define HTTP_FLG_LWS 0x04 |
| 45 | #define HTTP_FLG_SPHT 0x08 |
| 46 | #define HTTP_FLG_CRLF 0x10 |
| 47 | #define HTTP_FLG_TOK 0x20 |
| 48 | #define HTTP_FLG_VER 0x40 |
| 49 | #define HTTP_FLG_DIG 0x80 |
| 50 | |
| 51 | #define HTTP_IS_CTL(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_CTL) |
| 52 | #define HTTP_IS_SEP(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_SEP) |
| 53 | #define HTTP_IS_LWS(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_LWS) |
| 54 | #define HTTP_IS_SPHT(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_SPHT) |
| 55 | #define HTTP_IS_CRLF(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_CRLF) |
| 56 | #define HTTP_IS_TOKEN(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_TOK) |
| 57 | #define HTTP_IS_VER_TOKEN(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_VER) |
| 58 | #define HTTP_IS_DIGIT(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_DIG) |
| 59 | |
| 60 | /* Known HTTP methods */ |
| 61 | enum http_meth_t { |
| 62 | HTTP_METH_OPTIONS, |
| 63 | HTTP_METH_GET, |
| 64 | HTTP_METH_HEAD, |
| 65 | HTTP_METH_POST, |
| 66 | HTTP_METH_PUT, |
| 67 | HTTP_METH_DELETE, |
| 68 | HTTP_METH_TRACE, |
| 69 | HTTP_METH_CONNECT, |
| 70 | HTTP_METH_OTHER, /* Must be the last entry */ |
| 71 | } __attribute__((packed)); |
| 72 | |
| 73 | /* Known HTTP authentication schemes */ |
| 74 | enum ht_auth_m { |
| 75 | HTTP_AUTH_WRONG = -1, /* missing or unknown */ |
| 76 | HTTP_AUTH_UNKNOWN = 0, |
| 77 | HTTP_AUTH_BASIC, |
| 78 | HTTP_AUTH_DIGEST, |
| 79 | } __attribute__((packed)); |
| 80 | |
| 81 | /* All implemented HTTP status codes */ |
| 82 | enum { |
| 83 | HTTP_ERR_200 = 0, |
| 84 | HTTP_ERR_400, |
| 85 | HTTP_ERR_403, |
Florian Tham | 9f3bda0 | 2020-01-08 13:35:30 +0100 | [diff] [blame] | 86 | HTTP_ERR_404, |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 87 | HTTP_ERR_405, |
| 88 | HTTP_ERR_408, |
Florian Tham | c09f797 | 2020-01-08 10:19:05 +0100 | [diff] [blame] | 89 | HTTP_ERR_410, |
Anthonin Bonnefoy | b1e9407 | 2020-06-22 09:17:01 +0200 | [diff] [blame] | 90 | HTTP_ERR_413, |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 91 | HTTP_ERR_421, |
| 92 | HTTP_ERR_425, |
| 93 | HTTP_ERR_429, |
| 94 | HTTP_ERR_500, |
| 95 | HTTP_ERR_502, |
| 96 | HTTP_ERR_503, |
| 97 | HTTP_ERR_504, |
| 98 | HTTP_ERR_SIZE |
| 99 | }; |
| 100 | |
| 101 | /* Note: the strings below make use of chunks. Chunks may carry an allocated |
| 102 | * size in addition to the length. The size counts from the beginning (str) |
| 103 | * to the end. If the size is unknown, it MUST be zero, in which case the |
| 104 | * sample will automatically be duplicated when a change larger than <len> has |
| 105 | * to be performed. Thus it is safe to always set size to zero. |
| 106 | */ |
| 107 | struct http_meth { |
| 108 | enum http_meth_t meth; |
| 109 | struct buffer str; |
| 110 | }; |
| 111 | |
| 112 | struct http_auth_data { |
| 113 | enum ht_auth_m method; /* one of HTTP_AUTH_* */ |
| 114 | /* 7 bytes unused here */ |
| 115 | struct buffer method_data; /* points to the creditial part from 'Authorization:' header */ |
| 116 | char *user, *pass; /* extracted username & password */ |
| 117 | }; |
| 118 | |
| 119 | struct http_method_desc { |
| 120 | enum http_meth_t meth; |
| 121 | const struct ist text; |
| 122 | }; |
| 123 | |
| 124 | extern const int http_err_codes[HTTP_ERR_SIZE]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 125 | extern const char *http_err_msgs[HTTP_ERR_SIZE]; |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 126 | extern struct buffer http_err_chunks[HTTP_ERR_SIZE]; |
Willy Tarreau | e01d11a | 2019-03-29 17:52:50 +0100 | [diff] [blame] | 127 | extern const struct ist http_known_methods[HTTP_METH_OTHER]; |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 128 | extern const uint8_t http_char_classes[256]; |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 129 | |
Willy Tarreau | e01d11a | 2019-03-29 17:52:50 +0100 | [diff] [blame] | 130 | extern const struct ist HTTP_100; |
| 131 | extern const struct ist HTTP_103; |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 132 | extern const char *HTTP_301; |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 133 | extern const char *HTTP_302; |
| 134 | extern const char *HTTP_303; |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 135 | extern const char *HTTP_307; |
| 136 | extern const char *HTTP_308; |
| 137 | extern const char *HTTP_401_fmt; |
| 138 | extern const char *HTTP_407_fmt; |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 139 | |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 140 | int init_http(char **err); |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 141 | enum http_meth_t find_http_meth(const char *str, const int len); |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 142 | int http_get_status_idx(unsigned int status); |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 143 | const char *http_get_reason(unsigned int status); |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 144 | struct ist http_get_path(const struct ist uri); |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 145 | int http_header_match2(const char *hdr, const char *end, |
| 146 | const char *name, int len); |
| 147 | char *http_find_hdr_value_end(char *s, const char *e); |
| 148 | char *http_find_cookie_value_end(char *s, const char *e); |
| 149 | char *http_extract_cookie_value(char *hdr, const char *hdr_end, |
| 150 | char *cookie_name, size_t cookie_name_l, |
| 151 | int list, char **value, size_t *value_l); |
| 152 | int http_parse_qvalue(const char *qvalue, const char **end); |
| 153 | const char *http_find_url_param_pos(const char **chunks, |
| 154 | const char* url_param_name, |
| 155 | size_t url_param_name_l, char delim); |
| 156 | int http_find_next_url_param(const char **chunks, |
| 157 | const char* url_param_name, size_t url_param_name_l, |
| 158 | const char **vstart, const char **vend, char delim); |
| 159 | |
Christopher Faulet | 8277ca7 | 2018-10-22 15:12:04 +0200 | [diff] [blame] | 160 | int http_parse_header(const struct ist hdr, struct ist *name, struct ist *value); |
| 161 | int http_parse_stline(const struct ist line, struct ist *p1, struct ist *p2, struct ist *p3); |
| 162 | |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 163 | /* |
| 164 | * Given a path string and its length, find the position of beginning of the |
| 165 | * query string. Returns NULL if no query string is found in the path. |
| 166 | * |
| 167 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 168 | * |
| 169 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
| 170 | */ |
| 171 | static inline char *http_find_param_list(char *path, size_t path_l, char delim) |
| 172 | { |
| 173 | char *p; |
| 174 | |
| 175 | p = memchr(path, delim, path_l); |
| 176 | return p ? p + 1 : NULL; |
| 177 | } |
| 178 | |
| 179 | static inline int http_is_param_delimiter(char c, char delim) |
| 180 | { |
| 181 | return c == '&' || c == ';' || c == delim; |
| 182 | } |
| 183 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 184 | /* Match language range with language tag. RFC2616 14.4: |
| 185 | * |
| 186 | * A language-range matches a language-tag if it exactly equals |
| 187 | * the tag, or if it exactly equals a prefix of the tag such |
| 188 | * that the first tag character following the prefix is "-". |
| 189 | * |
| 190 | * Return 1 if the strings match, else return 0. |
| 191 | */ |
| 192 | static inline int http_language_range_match(const char *range, int range_len, |
| 193 | const char *tag, int tag_len) |
| 194 | { |
| 195 | const char *end = range + range_len; |
| 196 | const char *tend = tag + tag_len; |
| 197 | |
| 198 | while (range < end) { |
| 199 | if (*range == '-' && tag == tend) |
| 200 | return 1; |
| 201 | if (*range != *tag || tag == tend) |
| 202 | return 0; |
| 203 | range++; |
| 204 | tag++; |
| 205 | } |
| 206 | /* Return true only if the last char of the tag is matched. */ |
| 207 | return tag == tend; |
| 208 | } |
| 209 | |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 210 | |
| 211 | #endif /* _COMMON_HTTP_H */ |
| 212 | |
| 213 | /* |
| 214 | * Local variables: |
| 215 | * c-indent-level: 8 |
| 216 | * c-basic-offset: 8 |
| 217 | * End: |
| 218 | */ |