Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP semantics |
| 3 | * |
| 4 | * Copyright 2000-2018 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 14 | #include <haproxy/api.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 15 | #include <haproxy/http.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 16 | #include <haproxy/tools.h> |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 17 | |
| 18 | /* It is about twice as fast on recent architectures to lookup a byte in a |
| 19 | * table than to perform a boolean AND or OR between two tests. Refer to |
| 20 | * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is |
| 21 | * neither a separator nor a CTL char. An http ver_token is any ASCII which can |
| 22 | * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any |
| 23 | * digit. Note: please do not overwrite values in assignment since gcc-2.95 |
| 24 | * will not handle them correctly. It's worth noting that chars 128..255 are |
| 25 | * nothing, not even control chars. |
| 26 | */ |
| 27 | const unsigned char http_char_classes[256] = { |
| 28 | [ 0] = HTTP_FLG_CTL, |
| 29 | [ 1] = HTTP_FLG_CTL, |
| 30 | [ 2] = HTTP_FLG_CTL, |
| 31 | [ 3] = HTTP_FLG_CTL, |
| 32 | [ 4] = HTTP_FLG_CTL, |
| 33 | [ 5] = HTTP_FLG_CTL, |
| 34 | [ 6] = HTTP_FLG_CTL, |
| 35 | [ 7] = HTTP_FLG_CTL, |
| 36 | [ 8] = HTTP_FLG_CTL, |
| 37 | [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL, |
| 38 | [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL, |
| 39 | [ 11] = HTTP_FLG_CTL, |
| 40 | [ 12] = HTTP_FLG_CTL, |
| 41 | [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL, |
| 42 | [ 14] = HTTP_FLG_CTL, |
| 43 | [ 15] = HTTP_FLG_CTL, |
| 44 | [ 16] = HTTP_FLG_CTL, |
| 45 | [ 17] = HTTP_FLG_CTL, |
| 46 | [ 18] = HTTP_FLG_CTL, |
| 47 | [ 19] = HTTP_FLG_CTL, |
| 48 | [ 20] = HTTP_FLG_CTL, |
| 49 | [ 21] = HTTP_FLG_CTL, |
| 50 | [ 22] = HTTP_FLG_CTL, |
| 51 | [ 23] = HTTP_FLG_CTL, |
| 52 | [ 24] = HTTP_FLG_CTL, |
| 53 | [ 25] = HTTP_FLG_CTL, |
| 54 | [ 26] = HTTP_FLG_CTL, |
| 55 | [ 27] = HTTP_FLG_CTL, |
| 56 | [ 28] = HTTP_FLG_CTL, |
| 57 | [ 29] = HTTP_FLG_CTL, |
| 58 | [ 30] = HTTP_FLG_CTL, |
| 59 | [ 31] = HTTP_FLG_CTL, |
| 60 | [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP, |
| 61 | ['!'] = HTTP_FLG_TOK, |
| 62 | ['"'] = HTTP_FLG_SEP, |
| 63 | ['#'] = HTTP_FLG_TOK, |
| 64 | ['$'] = HTTP_FLG_TOK, |
| 65 | ['%'] = HTTP_FLG_TOK, |
| 66 | ['&'] = HTTP_FLG_TOK, |
| 67 | [ 39] = HTTP_FLG_TOK, |
| 68 | ['('] = HTTP_FLG_SEP, |
| 69 | [')'] = HTTP_FLG_SEP, |
| 70 | ['*'] = HTTP_FLG_TOK, |
| 71 | ['+'] = HTTP_FLG_TOK, |
| 72 | [','] = HTTP_FLG_SEP, |
| 73 | ['-'] = HTTP_FLG_TOK, |
| 74 | ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 75 | ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER, |
| 76 | ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER | HTTP_FLG_DIG, |
| 77 | ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER | HTTP_FLG_DIG, |
| 78 | ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER | HTTP_FLG_DIG, |
| 79 | ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER | HTTP_FLG_DIG, |
| 80 | ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER | HTTP_FLG_DIG, |
| 81 | ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER | HTTP_FLG_DIG, |
| 82 | ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER | HTTP_FLG_DIG, |
| 83 | ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER | HTTP_FLG_DIG, |
| 84 | ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER | HTTP_FLG_DIG, |
| 85 | ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER | HTTP_FLG_DIG, |
| 86 | [':'] = HTTP_FLG_SEP, |
| 87 | [';'] = HTTP_FLG_SEP, |
| 88 | ['<'] = HTTP_FLG_SEP, |
| 89 | ['='] = HTTP_FLG_SEP, |
| 90 | ['>'] = HTTP_FLG_SEP, |
| 91 | ['?'] = HTTP_FLG_SEP, |
| 92 | ['@'] = HTTP_FLG_SEP, |
| 93 | ['A'] = HTTP_FLG_TOK, |
| 94 | ['B'] = HTTP_FLG_TOK, |
| 95 | ['C'] = HTTP_FLG_TOK, |
| 96 | ['D'] = HTTP_FLG_TOK, |
| 97 | ['E'] = HTTP_FLG_TOK, |
| 98 | ['F'] = HTTP_FLG_TOK, |
| 99 | ['G'] = HTTP_FLG_TOK, |
| 100 | ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 101 | ['I'] = HTTP_FLG_TOK, |
| 102 | ['J'] = HTTP_FLG_TOK, |
| 103 | ['K'] = HTTP_FLG_TOK, |
| 104 | ['L'] = HTTP_FLG_TOK, |
| 105 | ['M'] = HTTP_FLG_TOK, |
| 106 | ['N'] = HTTP_FLG_TOK, |
| 107 | ['O'] = HTTP_FLG_TOK, |
| 108 | ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 109 | ['Q'] = HTTP_FLG_TOK, |
| 110 | ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 111 | ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 112 | ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 113 | ['U'] = HTTP_FLG_TOK, |
| 114 | ['V'] = HTTP_FLG_TOK, |
| 115 | ['W'] = HTTP_FLG_TOK, |
| 116 | ['X'] = HTTP_FLG_TOK, |
| 117 | ['Y'] = HTTP_FLG_TOK, |
| 118 | ['Z'] = HTTP_FLG_TOK, |
| 119 | ['['] = HTTP_FLG_SEP, |
| 120 | [ 92] = HTTP_FLG_SEP, |
| 121 | [']'] = HTTP_FLG_SEP, |
| 122 | ['^'] = HTTP_FLG_TOK, |
| 123 | ['_'] = HTTP_FLG_TOK, |
| 124 | ['`'] = HTTP_FLG_TOK, |
| 125 | ['a'] = HTTP_FLG_TOK, |
| 126 | ['b'] = HTTP_FLG_TOK, |
| 127 | ['c'] = HTTP_FLG_TOK, |
| 128 | ['d'] = HTTP_FLG_TOK, |
| 129 | ['e'] = HTTP_FLG_TOK, |
| 130 | ['f'] = HTTP_FLG_TOK, |
| 131 | ['g'] = HTTP_FLG_TOK, |
| 132 | ['h'] = HTTP_FLG_TOK, |
| 133 | ['i'] = HTTP_FLG_TOK, |
| 134 | ['j'] = HTTP_FLG_TOK, |
| 135 | ['k'] = HTTP_FLG_TOK, |
| 136 | ['l'] = HTTP_FLG_TOK, |
| 137 | ['m'] = HTTP_FLG_TOK, |
| 138 | ['n'] = HTTP_FLG_TOK, |
| 139 | ['o'] = HTTP_FLG_TOK, |
| 140 | ['p'] = HTTP_FLG_TOK, |
| 141 | ['q'] = HTTP_FLG_TOK, |
| 142 | ['r'] = HTTP_FLG_TOK, |
| 143 | ['s'] = HTTP_FLG_TOK, |
| 144 | ['t'] = HTTP_FLG_TOK, |
| 145 | ['u'] = HTTP_FLG_TOK, |
| 146 | ['v'] = HTTP_FLG_TOK, |
| 147 | ['w'] = HTTP_FLG_TOK, |
| 148 | ['x'] = HTTP_FLG_TOK, |
| 149 | ['y'] = HTTP_FLG_TOK, |
| 150 | ['z'] = HTTP_FLG_TOK, |
| 151 | ['{'] = HTTP_FLG_SEP, |
| 152 | ['|'] = HTTP_FLG_TOK, |
| 153 | ['}'] = HTTP_FLG_SEP, |
| 154 | ['~'] = HTTP_FLG_TOK, |
| 155 | [127] = HTTP_FLG_CTL, |
| 156 | }; |
| 157 | |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 158 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 159 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
| 160 | [HTTP_ERR_400] = 400, |
Christopher Faulet | 612f2ea | 2020-05-27 09:57:28 +0200 | [diff] [blame] | 161 | [HTTP_ERR_401] = 401, |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 162 | [HTTP_ERR_403] = 403, |
Florian Tham | 9205fea | 2020-01-08 13:35:30 +0100 | [diff] [blame] | 163 | [HTTP_ERR_404] = 404, |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 164 | [HTTP_ERR_405] = 405, |
Christopher Faulet | 612f2ea | 2020-05-27 09:57:28 +0200 | [diff] [blame] | 165 | [HTTP_ERR_407] = 407, |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 166 | [HTTP_ERR_408] = 408, |
Florian Tham | 272e29b | 2020-01-08 10:19:05 +0100 | [diff] [blame] | 167 | [HTTP_ERR_410] = 410, |
Anthonin Bonnefoy | 85048f8 | 2020-06-22 09:17:01 +0200 | [diff] [blame] | 168 | [HTTP_ERR_413] = 413, |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 169 | [HTTP_ERR_421] = 421, |
| 170 | [HTTP_ERR_425] = 425, |
| 171 | [HTTP_ERR_429] = 429, |
| 172 | [HTTP_ERR_500] = 500, |
Christopher Faulet | e095f31 | 2020-12-07 11:22:24 +0100 | [diff] [blame] | 173 | [HTTP_ERR_501] = 501, |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 174 | [HTTP_ERR_502] = 502, |
| 175 | [HTTP_ERR_503] = 503, |
| 176 | [HTTP_ERR_504] = 504, |
| 177 | }; |
| 178 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 179 | const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 180 | [HTTP_ERR_200] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 181 | "HTTP/1.1 200 OK\r\n" |
| 182 | "Content-length: 58\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 183 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 184 | "Content-Type: text/html\r\n" |
| 185 | "\r\n" |
| 186 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 187 | |
| 188 | [HTTP_ERR_400] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 189 | "HTTP/1.1 400 Bad request\r\n" |
| 190 | "Content-length: 90\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 191 | "Cache-Control: no-cache\r\n" |
| 192 | "Connection: close\r\n" |
| 193 | "Content-Type: text/html\r\n" |
| 194 | "\r\n" |
| 195 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 196 | |
Christopher Faulet | 612f2ea | 2020-05-27 09:57:28 +0200 | [diff] [blame] | 197 | [HTTP_ERR_401] = |
| 198 | "HTTP/1.1 401 Unauthorized\r\n" |
| 199 | "Content-length: 112\r\n" |
| 200 | "Cache-Control: no-cache\r\n" |
Christopher Faulet | 612f2ea | 2020-05-27 09:57:28 +0200 | [diff] [blame] | 201 | "Content-Type: text/html\r\n" |
| 202 | "\r\n" |
| 203 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n", |
| 204 | |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 205 | [HTTP_ERR_403] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 206 | "HTTP/1.1 403 Forbidden\r\n" |
| 207 | "Content-length: 93\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 208 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 209 | "Content-Type: text/html\r\n" |
| 210 | "\r\n" |
| 211 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 212 | |
Florian Tham | 9205fea | 2020-01-08 13:35:30 +0100 | [diff] [blame] | 213 | [HTTP_ERR_404] = |
| 214 | "HTTP/1.1 404 Not Found\r\n" |
| 215 | "Content-length: 83\r\n" |
| 216 | "Cache-Control: no-cache\r\n" |
Florian Tham | 9205fea | 2020-01-08 13:35:30 +0100 | [diff] [blame] | 217 | "Content-Type: text/html\r\n" |
| 218 | "\r\n" |
| 219 | "<html><body><h1>404 Not Found</h1>\nThe resource could not be found.\n</body></html>\n", |
| 220 | |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 221 | [HTTP_ERR_405] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 222 | "HTTP/1.1 405 Method Not Allowed\r\n" |
| 223 | "Content-length: 146\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 224 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 225 | "Content-Type: text/html\r\n" |
| 226 | "\r\n" |
| 227 | "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n", |
| 228 | |
Christopher Faulet | 612f2ea | 2020-05-27 09:57:28 +0200 | [diff] [blame] | 229 | [HTTP_ERR_407] = |
| 230 | "HTTP/1.1 407 Unauthorized\r\n" |
| 231 | "Content-length: 112\r\n" |
| 232 | "Cache-Control: no-cache\r\n" |
Christopher Faulet | 612f2ea | 2020-05-27 09:57:28 +0200 | [diff] [blame] | 233 | "Content-Type: text/html\r\n" |
| 234 | "\r\n" |
| 235 | "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n", |
| 236 | |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 237 | [HTTP_ERR_408] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 238 | "HTTP/1.1 408 Request Time-out\r\n" |
| 239 | "Content-length: 110\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 240 | "Cache-Control: no-cache\r\n" |
| 241 | "Connection: close\r\n" |
| 242 | "Content-Type: text/html\r\n" |
| 243 | "\r\n" |
| 244 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 245 | |
Florian Tham | 272e29b | 2020-01-08 10:19:05 +0100 | [diff] [blame] | 246 | [HTTP_ERR_410] = |
| 247 | "HTTP/1.1 410 Gone\r\n" |
| 248 | "Content-length: 114\r\n" |
| 249 | "Cache-Control: no-cache\r\n" |
Florian Tham | 272e29b | 2020-01-08 10:19:05 +0100 | [diff] [blame] | 250 | "Content-Type: text/html\r\n" |
| 251 | "\r\n" |
| 252 | "<html><body><h1>410 Gone</h1>\nThe resource is no longer available and will not be available again.\n</body></html>\n", |
| 253 | |
Anthonin Bonnefoy | 85048f8 | 2020-06-22 09:17:01 +0200 | [diff] [blame] | 254 | [HTTP_ERR_413] = |
| 255 | "HTTP/1.1 413 Payload Too Large\r\n" |
| 256 | "Content-length: 106\r\n" |
| 257 | "Cache-Control: no-cache\r\n" |
| 258 | "Content-Type: text/html\r\n" |
| 259 | "\r\n" |
| 260 | "<html><body><h1>413 Payload Too Large</h1>\nThe request entity exceeds the maximum allowed.\n</body></html>\n", |
| 261 | |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 262 | [HTTP_ERR_421] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 263 | "HTTP/1.1 421 Misdirected Request\r\n" |
| 264 | "Content-length: 104\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 265 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 266 | "Content-Type: text/html\r\n" |
| 267 | "\r\n" |
| 268 | "<html><body><h1>421 Misdirected Request</h1>\nRequest sent to a non-authoritative server.\n</body></html>\n", |
| 269 | |
| 270 | [HTTP_ERR_425] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 271 | "HTTP/1.1 425 Too Early\r\n" |
| 272 | "Content-length: 80\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 273 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 274 | "Content-Type: text/html\r\n" |
| 275 | "\r\n" |
| 276 | "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n", |
| 277 | |
| 278 | [HTTP_ERR_429] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 279 | "HTTP/1.1 429 Too Many Requests\r\n" |
| 280 | "Content-length: 117\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 281 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 282 | "Content-Type: text/html\r\n" |
| 283 | "\r\n" |
| 284 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 285 | |
| 286 | [HTTP_ERR_500] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 287 | "HTTP/1.1 500 Internal Server Error\r\n" |
Christopher Faulet | 5563392 | 2020-10-09 08:39:26 +0200 | [diff] [blame] | 288 | "Content-length: 97\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 289 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 290 | "Content-Type: text/html\r\n" |
| 291 | "\r\n" |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 292 | "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n", |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 293 | |
Christopher Faulet | e095f31 | 2020-12-07 11:22:24 +0100 | [diff] [blame] | 294 | [HTTP_ERR_501] = |
| 295 | "HTTP/1.1 501 Not Implemented\r\n" |
| 296 | "Content-length: 136\r\n" |
| 297 | "Cache-Control: no-cache\r\n" |
| 298 | "Content-Type: text/html\r\n" |
| 299 | "\r\n" |
| 300 | "<html><body><h1>501 Not Implemented</h1>\n.The server does not support the functionality required to fulfill the request.\n</body></html>\n", |
| 301 | |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 302 | [HTTP_ERR_502] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 303 | "HTTP/1.1 502 Bad Gateway\r\n" |
| 304 | "Content-length: 107\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 305 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 306 | "Content-Type: text/html\r\n" |
| 307 | "\r\n" |
| 308 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 309 | |
| 310 | [HTTP_ERR_503] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 311 | "HTTP/1.1 503 Service Unavailable\r\n" |
| 312 | "Content-length: 107\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 313 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 314 | "Content-Type: text/html\r\n" |
| 315 | "\r\n" |
| 316 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 317 | |
| 318 | [HTTP_ERR_504] = |
Willy Tarreau | b5ba2b0 | 2019-06-11 16:08:25 +0200 | [diff] [blame] | 319 | "HTTP/1.1 504 Gateway Time-out\r\n" |
| 320 | "Content-length: 92\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 321 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 322 | "Content-Type: text/html\r\n" |
| 323 | "\r\n" |
| 324 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 325 | }; |
| 326 | |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 327 | const struct ist http_known_methods[HTTP_METH_OTHER] = { |
| 328 | [HTTP_METH_OPTIONS] = IST("OPTIONS"), |
| 329 | [HTTP_METH_GET] = IST("GET"), |
| 330 | [HTTP_METH_HEAD] = IST("HEAD"), |
| 331 | [HTTP_METH_POST] = IST("POST"), |
| 332 | [HTTP_METH_PUT] = IST("PUT"), |
| 333 | [HTTP_METH_DELETE] = IST("DELETE"), |
| 334 | [HTTP_METH_TRACE] = IST("TRACE"), |
| 335 | [HTTP_METH_CONNECT] = IST("CONNECT"), |
| 336 | }; |
| 337 | |
| 338 | /* |
| 339 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 340 | * ones. |
| 341 | */ |
| 342 | enum http_meth_t find_http_meth(const char *str, const int len) |
| 343 | { |
| 344 | const struct ist m = ist2(str, len); |
| 345 | |
| 346 | if (isteq(m, ist("GET"))) return HTTP_METH_GET; |
| 347 | else if (isteq(m, ist("HEAD"))) return HTTP_METH_HEAD; |
| 348 | else if (isteq(m, ist("POST"))) return HTTP_METH_POST; |
| 349 | else if (isteq(m, ist("CONNECT"))) return HTTP_METH_CONNECT; |
| 350 | else if (isteq(m, ist("PUT"))) return HTTP_METH_PUT; |
| 351 | else if (isteq(m, ist("OPTIONS"))) return HTTP_METH_OPTIONS; |
| 352 | else if (isteq(m, ist("DELETE"))) return HTTP_METH_DELETE; |
| 353 | else if (isteq(m, ist("TRACE"))) return HTTP_METH_TRACE; |
| 354 | else return HTTP_METH_OTHER; |
| 355 | } |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 356 | |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 357 | /* This function returns HTTP_ERR_<num> (enum) matching http status code. |
| 358 | * Returned value should match codes from http_err_codes. |
| 359 | */ |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 360 | int http_get_status_idx(unsigned int status) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 361 | { |
| 362 | switch (status) { |
| 363 | case 200: return HTTP_ERR_200; |
| 364 | case 400: return HTTP_ERR_400; |
Christopher Faulet | 612f2ea | 2020-05-27 09:57:28 +0200 | [diff] [blame] | 365 | case 401: return HTTP_ERR_401; |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 366 | case 403: return HTTP_ERR_403; |
Florian Tham | 9205fea | 2020-01-08 13:35:30 +0100 | [diff] [blame] | 367 | case 404: return HTTP_ERR_404; |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 368 | case 405: return HTTP_ERR_405; |
Christopher Faulet | 612f2ea | 2020-05-27 09:57:28 +0200 | [diff] [blame] | 369 | case 407: return HTTP_ERR_407; |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 370 | case 408: return HTTP_ERR_408; |
Florian Tham | 272e29b | 2020-01-08 10:19:05 +0100 | [diff] [blame] | 371 | case 410: return HTTP_ERR_410; |
Anthonin Bonnefoy | 85048f8 | 2020-06-22 09:17:01 +0200 | [diff] [blame] | 372 | case 413: return HTTP_ERR_413; |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 373 | case 421: return HTTP_ERR_421; |
| 374 | case 425: return HTTP_ERR_425; |
| 375 | case 429: return HTTP_ERR_429; |
| 376 | case 500: return HTTP_ERR_500; |
Christopher Faulet | e095f31 | 2020-12-07 11:22:24 +0100 | [diff] [blame] | 377 | case 501: return HTTP_ERR_501; |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 378 | case 502: return HTTP_ERR_502; |
| 379 | case 503: return HTTP_ERR_503; |
| 380 | case 504: return HTTP_ERR_504; |
| 381 | default: return HTTP_ERR_500; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /* This function returns a reason associated with the HTTP status. |
| 386 | * This function never fails, a message is always returned. |
| 387 | */ |
| 388 | const char *http_get_reason(unsigned int status) |
| 389 | { |
| 390 | switch (status) { |
| 391 | case 100: return "Continue"; |
| 392 | case 101: return "Switching Protocols"; |
| 393 | case 102: return "Processing"; |
| 394 | case 200: return "OK"; |
| 395 | case 201: return "Created"; |
| 396 | case 202: return "Accepted"; |
| 397 | case 203: return "Non-Authoritative Information"; |
| 398 | case 204: return "No Content"; |
| 399 | case 205: return "Reset Content"; |
| 400 | case 206: return "Partial Content"; |
| 401 | case 207: return "Multi-Status"; |
| 402 | case 210: return "Content Different"; |
| 403 | case 226: return "IM Used"; |
| 404 | case 300: return "Multiple Choices"; |
| 405 | case 301: return "Moved Permanently"; |
| 406 | case 302: return "Moved Temporarily"; |
| 407 | case 303: return "See Other"; |
| 408 | case 304: return "Not Modified"; |
| 409 | case 305: return "Use Proxy"; |
| 410 | case 307: return "Temporary Redirect"; |
| 411 | case 308: return "Permanent Redirect"; |
| 412 | case 310: return "Too many Redirects"; |
| 413 | case 400: return "Bad Request"; |
| 414 | case 401: return "Unauthorized"; |
| 415 | case 402: return "Payment Required"; |
| 416 | case 403: return "Forbidden"; |
| 417 | case 404: return "Not Found"; |
| 418 | case 405: return "Method Not Allowed"; |
| 419 | case 406: return "Not Acceptable"; |
| 420 | case 407: return "Proxy Authentication Required"; |
| 421 | case 408: return "Request Time-out"; |
| 422 | case 409: return "Conflict"; |
| 423 | case 410: return "Gone"; |
| 424 | case 411: return "Length Required"; |
| 425 | case 412: return "Precondition Failed"; |
| 426 | case 413: return "Request Entity Too Large"; |
| 427 | case 414: return "Request-URI Too Long"; |
| 428 | case 415: return "Unsupported Media Type"; |
| 429 | case 416: return "Requested range unsatisfiable"; |
| 430 | case 417: return "Expectation failed"; |
| 431 | case 418: return "I'm a teapot"; |
| 432 | case 421: return "Misdirected Request"; |
| 433 | case 422: return "Unprocessable entity"; |
| 434 | case 423: return "Locked"; |
| 435 | case 424: return "Method failure"; |
| 436 | case 425: return "Too Early"; |
| 437 | case 426: return "Upgrade Required"; |
| 438 | case 428: return "Precondition Required"; |
| 439 | case 429: return "Too Many Requests"; |
| 440 | case 431: return "Request Header Fields Too Large"; |
| 441 | case 449: return "Retry With"; |
| 442 | case 450: return "Blocked by Windows Parental Controls"; |
| 443 | case 451: return "Unavailable For Legal Reasons"; |
| 444 | case 456: return "Unrecoverable Error"; |
| 445 | case 499: return "client has closed connection"; |
| 446 | case 500: return "Internal Server Error"; |
| 447 | case 501: return "Not Implemented"; |
| 448 | case 502: return "Bad Gateway or Proxy Error"; |
| 449 | case 503: return "Service Unavailable"; |
| 450 | case 504: return "Gateway Time-out"; |
| 451 | case 505: return "HTTP Version not supported"; |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 452 | case 506: return "Variant also negotiate"; |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 453 | case 507: return "Insufficient storage"; |
| 454 | case 508: return "Loop detected"; |
| 455 | case 509: return "Bandwidth Limit Exceeded"; |
| 456 | case 510: return "Not extended"; |
| 457 | case 511: return "Network authentication required"; |
| 458 | case 520: return "Web server is returning an unknown error"; |
| 459 | default: |
| 460 | switch (status) { |
| 461 | case 100 ... 199: return "Informational"; |
| 462 | case 200 ... 299: return "Success"; |
| 463 | case 300 ... 399: return "Redirection"; |
| 464 | case 400 ... 499: return "Client Error"; |
| 465 | case 500 ... 599: return "Server Error"; |
| 466 | default: return "Other"; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
Willy Tarreau | a120549 | 2021-08-10 15:35:36 +0200 | [diff] [blame] | 471 | /* Returns non-zero if the scheme <schm> is syntactically correct according to |
| 472 | * RFC3986#3.1, otherwise zero. It expects only the scheme and nothing else |
| 473 | * (particularly not the following "://"). |
| 474 | * Scheme = alpha *(alpha|digit|'+'|'-'|'.') |
| 475 | */ |
| 476 | int http_validate_scheme(const struct ist schm) |
| 477 | { |
| 478 | size_t i; |
| 479 | |
| 480 | for (i = 0; i < schm.len; i++) { |
| 481 | if (likely((schm.ptr[i] >= 'a' && schm.ptr[i] <= 'z') || |
| 482 | (schm.ptr[i] >= 'A' && schm.ptr[i] <= 'Z'))) |
| 483 | continue; |
| 484 | if (unlikely(!i)) // first char must be alpha |
| 485 | return 0; |
| 486 | if ((schm.ptr[i] >= '0' && schm.ptr[i] <= '9') || |
| 487 | schm.ptr[i] == '+' || schm.ptr[i] == '-' || schm.ptr[i] == '.') |
| 488 | continue; |
| 489 | return 0; |
| 490 | } |
| 491 | return !!i; |
| 492 | } |
| 493 | |
Amaury Denoyelle | e09651e | 2021-07-07 10:49:25 +0200 | [diff] [blame] | 494 | /* Parse the uri and looks for the scheme. If not found, an empty ist is |
| 495 | * returned. Otherwise, the ist pointing to the scheme is returned. |
| 496 | */ |
| 497 | struct ist http_get_scheme(const struct ist uri) |
| 498 | { |
| 499 | const char *ptr, *start, *end; |
| 500 | |
| 501 | if (!uri.len) |
| 502 | goto not_found; |
| 503 | |
| 504 | ptr = uri.ptr; |
| 505 | start = ptr; |
| 506 | end = ptr + uri.len; |
| 507 | |
| 508 | /* RFC7230, par. 2.7 : |
| 509 | * Request-URI = "*" | absuri | abspath | authority |
| 510 | */ |
| 511 | |
| 512 | if (*ptr == '*' || *ptr == '/') |
| 513 | goto not_found; |
| 514 | |
| 515 | if (isalpha((unsigned char)*ptr)) { |
| 516 | /* this is a scheme as described by RFC3986, par. 3.1, or only |
| 517 | * an authority (in case of a CONNECT method). |
| 518 | */ |
| 519 | ptr++; |
| 520 | /* retrieve the scheme up to the suffix '://'. If the suffix is |
| 521 | * not found, this means there is no scheme and it is an |
| 522 | * authority-only uri. |
| 523 | */ |
| 524 | while (ptr < end && |
| 525 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 526 | ptr++; |
| 527 | if (ptr == end || *ptr++ != ':') |
| 528 | goto not_found; |
| 529 | if (ptr == end || *ptr++ != '/') |
| 530 | goto not_found; |
| 531 | if (ptr == end || *ptr++ != '/') |
| 532 | goto not_found; |
| 533 | } |
| 534 | else { |
| 535 | goto not_found; |
| 536 | } |
| 537 | |
| 538 | return ist2(start, ptr - start); |
| 539 | |
| 540 | not_found: |
| 541 | return IST_NULL; |
| 542 | } |
| 543 | |
Christopher Faulet | 16fdc55 | 2019-10-08 14:56:58 +0200 | [diff] [blame] | 544 | /* Parse the uri and looks for the authority, between the scheme and the |
| 545 | * path. if no_userinfo is not zero, the part before the '@' (including it) is |
| 546 | * skipped. If not found, an empty ist is returned. Otherwise, the ist pointing |
| 547 | * on the authority is returned. |
| 548 | */ |
| 549 | struct ist http_get_authority(const struct ist uri, int no_userinfo) |
| 550 | { |
| 551 | const char *ptr, *start, *end; |
| 552 | |
| 553 | if (!uri.len) |
| 554 | goto not_found; |
| 555 | |
| 556 | ptr = uri.ptr; |
| 557 | start = ptr; |
| 558 | end = ptr + uri.len; |
| 559 | |
| 560 | /* RFC7230, par. 2.7 : |
| 561 | * Request-URI = "*" | absuri | abspath | authority |
| 562 | */ |
| 563 | |
| 564 | if (*ptr == '*' || *ptr == '/') |
| 565 | goto not_found; |
| 566 | |
| 567 | if (isalpha((unsigned char)*ptr)) { |
| 568 | /* this is a scheme as described by RFC3986, par. 3.1, or only |
| 569 | * an authority (in case of a CONNECT method). |
| 570 | */ |
| 571 | ptr++; |
| 572 | while (ptr < end && |
| 573 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 574 | ptr++; |
| 575 | /* skip '://' or take the whole as authority if not found */ |
| 576 | if (ptr == end || *ptr++ != ':') |
| 577 | goto authority; |
| 578 | if (ptr == end || *ptr++ != '/') |
| 579 | goto authority; |
| 580 | if (ptr == end || *ptr++ != '/') |
| 581 | goto authority; |
| 582 | } |
| 583 | |
| 584 | start = ptr; |
| 585 | while (ptr < end && *ptr != '/') { |
| 586 | if (*ptr++ == '@' && no_userinfo) |
| 587 | start = ptr; |
| 588 | } |
| 589 | |
| 590 | /* OK, ptr point on the '/' or the end */ |
| 591 | end = ptr; |
| 592 | |
| 593 | authority: |
| 594 | return ist2(start, end - start); |
| 595 | |
| 596 | not_found: |
Tim Duesterhus | 241e29e | 2020-03-05 17:56:30 +0100 | [diff] [blame] | 597 | return IST_NULL; |
Christopher Faulet | 16fdc55 | 2019-10-08 14:56:58 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 600 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 601 | * phase) and look for the "/" beginning the PATH. If not found, ist2(0,0) is |
| 602 | * returned. Otherwise the pointer and length are returned. |
| 603 | */ |
| 604 | struct ist http_get_path(const struct ist uri) |
| 605 | { |
| 606 | const char *ptr, *end; |
| 607 | |
| 608 | if (!uri.len) |
| 609 | goto not_found; |
| 610 | |
| 611 | ptr = uri.ptr; |
| 612 | end = ptr + uri.len; |
| 613 | |
| 614 | /* RFC7230, par. 2.7 : |
| 615 | * Request-URI = "*" | absuri | abspath | authority |
| 616 | */ |
| 617 | |
| 618 | if (*ptr == '*') |
| 619 | goto not_found; |
| 620 | |
| 621 | if (isalpha((unsigned char)*ptr)) { |
| 622 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 623 | ptr++; |
| 624 | while (ptr < end && |
| 625 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 626 | ptr++; |
| 627 | /* skip '://' */ |
| 628 | if (ptr == end || *ptr++ != ':') |
| 629 | goto not_found; |
| 630 | if (ptr == end || *ptr++ != '/') |
| 631 | goto not_found; |
| 632 | if (ptr == end || *ptr++ != '/') |
| 633 | goto not_found; |
| 634 | } |
| 635 | /* skip [user[:passwd]@]host[:[port]] */ |
| 636 | |
| 637 | while (ptr < end && *ptr != '/') |
| 638 | ptr++; |
| 639 | |
| 640 | if (ptr == end) |
| 641 | goto not_found; |
| 642 | |
| 643 | /* OK, we got the '/' ! */ |
| 644 | return ist2(ptr, end - ptr); |
| 645 | |
| 646 | not_found: |
Tim Duesterhus | 241e29e | 2020-03-05 17:56:30 +0100 | [diff] [blame] | 647 | return IST_NULL; |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 648 | } |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 649 | |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 650 | /* |
| 651 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 652 | * If so, returns the position of the first non-space character relative to |
| 653 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 654 | * to return a pointer to the place after the first space. Returns 0 if the |
| 655 | * header name does not match. Checks are case-insensitive. |
| 656 | */ |
| 657 | int http_header_match2(const char *hdr, const char *end, |
| 658 | const char *name, int len) |
| 659 | { |
| 660 | const char *val; |
| 661 | |
| 662 | if (hdr + len >= end) |
| 663 | return 0; |
| 664 | if (hdr[len] != ':') |
| 665 | return 0; |
| 666 | if (strncasecmp(hdr, name, len) != 0) |
| 667 | return 0; |
| 668 | val = hdr + len + 1; |
| 669 | while (val < end && HTTP_IS_SPHT(*val)) |
| 670 | val++; |
| 671 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 672 | return len + 2; /* we may replace starting from second space */ |
| 673 | return val - hdr; |
| 674 | } |
| 675 | |
| 676 | /* Find the end of the header value contained between <s> and <e>. See RFC7230, |
| 677 | * par 3.2 for more information. Note that it requires a valid header to return |
| 678 | * a valid result. This works for headers defined as comma-separated lists. |
| 679 | */ |
| 680 | char *http_find_hdr_value_end(char *s, const char *e) |
| 681 | { |
| 682 | int quoted, qdpair; |
| 683 | |
| 684 | quoted = qdpair = 0; |
| 685 | |
Willy Tarreau | 02ac950 | 2020-02-21 16:31:22 +0100 | [diff] [blame] | 686 | #ifdef HA_UNALIGNED_LE |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 687 | /* speedup: skip everything not a comma nor a double quote */ |
| 688 | for (; s <= e - sizeof(int); s += sizeof(int)) { |
| 689 | unsigned int c = *(int *)s; // comma |
| 690 | unsigned int q = c; // quote |
| 691 | |
| 692 | c ^= 0x2c2c2c2c; // contains one zero on a comma |
| 693 | q ^= 0x22222222; // contains one zero on a quote |
| 694 | |
| 695 | c = (c - 0x01010101) & ~c; // contains 0x80 below a comma |
| 696 | q = (q - 0x01010101) & ~q; // contains 0x80 below a quote |
| 697 | |
| 698 | if ((c | q) & 0x80808080) |
| 699 | break; // found a comma or a quote |
| 700 | } |
| 701 | #endif |
| 702 | for (; s < e; s++) { |
| 703 | if (qdpair) qdpair = 0; |
| 704 | else if (quoted) { |
| 705 | if (*s == '\\') qdpair = 1; |
| 706 | else if (*s == '"') quoted = 0; |
| 707 | } |
| 708 | else if (*s == '"') quoted = 1; |
| 709 | else if (*s == ',') return s; |
| 710 | } |
| 711 | return s; |
| 712 | } |
| 713 | |
| 714 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 715 | * same way as with headers above except that the semi-colon also ends a token. |
| 716 | * See RFC2965 for more information. Note that it requires a valid header to |
| 717 | * return a valid result. |
| 718 | */ |
| 719 | char *http_find_cookie_value_end(char *s, const char *e) |
| 720 | { |
| 721 | int quoted, qdpair; |
| 722 | |
| 723 | quoted = qdpair = 0; |
| 724 | for (; s < e; s++) { |
| 725 | if (qdpair) qdpair = 0; |
| 726 | else if (quoted) { |
| 727 | if (*s == '\\') qdpair = 1; |
| 728 | else if (*s == '"') quoted = 0; |
| 729 | } |
| 730 | else if (*s == '"') quoted = 1; |
| 731 | else if (*s == ',' || *s == ';') return s; |
| 732 | } |
| 733 | return s; |
| 734 | } |
| 735 | |
| 736 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
Maciej Zdeb | dea7c20 | 2020-11-13 09:38:06 +0000 | [diff] [blame] | 737 | * To match on any cookie name, <cookie_name_l> must be set to 0. |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 738 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 739 | * the cookie value is returned into *value and *value_l, and the function |
| 740 | * returns a pointer to the next pointer to search from if the value was found. |
| 741 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 742 | * nor value_l are touched. The input <hdr> string should first point to the |
| 743 | * header's value, and the <hdr_end> pointer must point to the first character |
| 744 | * not part of the value. <list> must be non-zero if value may represent a list |
| 745 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 746 | * list is expected. |
| 747 | */ |
| 748 | char *http_extract_cookie_value(char *hdr, const char *hdr_end, |
| 749 | char *cookie_name, size_t cookie_name_l, |
| 750 | int list, char **value, size_t *value_l) |
| 751 | { |
| 752 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 753 | char *next; |
| 754 | |
| 755 | /* we search at least a cookie name followed by an equal, and more |
| 756 | * generally something like this : |
| 757 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 758 | */ |
| 759 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 760 | /* Iterate through all cookies on this line */ |
| 761 | |
| 762 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 763 | att_beg++; |
| 764 | |
| 765 | /* find att_end : this is the first character after the last non |
| 766 | * space before the equal. It may be equal to hdr_end. |
| 767 | */ |
| 768 | equal = att_end = att_beg; |
| 769 | |
| 770 | while (equal < hdr_end) { |
| 771 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 772 | break; |
| 773 | if (HTTP_IS_SPHT(*equal++)) |
| 774 | continue; |
| 775 | att_end = equal; |
| 776 | } |
| 777 | |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 778 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 779 | * is between <att_beg> and <equal>, both may be identical. |
| 780 | */ |
| 781 | |
| 782 | /* look for end of cookie if there is an equal sign */ |
| 783 | if (equal < hdr_end && *equal == '=') { |
| 784 | /* look for the beginning of the value */ |
| 785 | val_beg = equal + 1; |
| 786 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 787 | val_beg++; |
| 788 | |
| 789 | /* find the end of the value, respecting quotes */ |
| 790 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 791 | |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 792 | /* make val_end point to the first white space or delimiter after the value */ |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 793 | val_end = next; |
| 794 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 795 | val_end--; |
| 796 | } else { |
| 797 | val_beg = val_end = next = equal; |
| 798 | } |
| 799 | |
| 800 | /* We have nothing to do with attributes beginning with '$'. However, |
| 801 | * they will automatically be removed if a header before them is removed, |
| 802 | * since they're supposed to be linked together. |
| 803 | */ |
| 804 | if (*att_beg == '$') |
| 805 | continue; |
| 806 | |
| 807 | /* Ignore cookies with no equal sign */ |
| 808 | if (equal == next) |
| 809 | continue; |
| 810 | |
| 811 | /* Now we have the cookie name between att_beg and att_end, and |
| 812 | * its value between val_beg and val_end. |
| 813 | */ |
| 814 | |
Maciej Zdeb | dea7c20 | 2020-11-13 09:38:06 +0000 | [diff] [blame] | 815 | if (cookie_name_l == 0 || (att_end - att_beg == cookie_name_l && |
| 816 | memcmp(att_beg, cookie_name, cookie_name_l) == 0)) { |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 817 | /* let's return this value and indicate where to go on from */ |
| 818 | *value = val_beg; |
| 819 | *value_l = val_end - val_beg; |
| 820 | return next + 1; |
| 821 | } |
| 822 | |
| 823 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 824 | if (!list) |
| 825 | break; |
| 826 | } |
| 827 | |
| 828 | return NULL; |
| 829 | } |
| 830 | |
Joseph Herlant | 942eea3 | 2018-11-15 13:57:22 -0800 | [diff] [blame] | 831 | /* Parses a qvalue and returns it multiplied by 1000, from 0 to 1000. If the |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 832 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 833 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 834 | * Unparsable qvalues return 1000 as "q=1.000". |
| 835 | */ |
| 836 | int http_parse_qvalue(const char *qvalue, const char **end) |
| 837 | { |
| 838 | int q = 1000; |
| 839 | |
| 840 | if (!isdigit((unsigned char)*qvalue)) |
| 841 | goto out; |
| 842 | q = (*qvalue++ - '0') * 1000; |
| 843 | |
| 844 | if (*qvalue++ != '.') |
| 845 | goto out; |
| 846 | |
| 847 | if (!isdigit((unsigned char)*qvalue)) |
| 848 | goto out; |
| 849 | q += (*qvalue++ - '0') * 100; |
| 850 | |
| 851 | if (!isdigit((unsigned char)*qvalue)) |
| 852 | goto out; |
| 853 | q += (*qvalue++ - '0') * 10; |
| 854 | |
| 855 | if (!isdigit((unsigned char)*qvalue)) |
| 856 | goto out; |
| 857 | q += (*qvalue++ - '0') * 1; |
| 858 | out: |
| 859 | if (q > 1000) |
| 860 | q = 1000; |
| 861 | if (end) |
| 862 | *end = qvalue; |
| 863 | return q; |
| 864 | } |
| 865 | |
| 866 | /* |
Joseph Herlant | 942eea3 | 2018-11-15 13:57:22 -0800 | [diff] [blame] | 867 | * Given a url parameter, find the starting position of the first occurrence, |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 868 | * or NULL if the parameter is not found. |
| 869 | * |
| 870 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 871 | * the function will return query_string+8. |
| 872 | * |
| 873 | * Warning: this function returns a pointer that can point to the first chunk |
| 874 | * or the second chunk. The caller must be check the position before using the |
| 875 | * result. |
| 876 | */ |
| 877 | const char *http_find_url_param_pos(const char **chunks, |
| 878 | const char* url_param_name, size_t url_param_name_l, |
| 879 | char delim) |
| 880 | { |
| 881 | const char *pos, *last, *equal; |
| 882 | const char **bufs = chunks; |
| 883 | int l1, l2; |
| 884 | |
| 885 | |
| 886 | pos = bufs[0]; |
| 887 | last = bufs[1]; |
| 888 | while (pos < last) { |
| 889 | /* Check the equal. */ |
| 890 | equal = pos + url_param_name_l; |
| 891 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 892 | if (equal >= chunks[3]) |
| 893 | return NULL; |
| 894 | } else { |
| 895 | if (equal >= chunks[1]) |
| 896 | return NULL; |
| 897 | } |
| 898 | if (*equal == '=') { |
| 899 | if (pos + url_param_name_l > last) { |
| 900 | /* process wrap case, we detect a wrap. In this case, the |
| 901 | * comparison is performed in two parts. |
| 902 | */ |
| 903 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 904 | /* This is the end, we don't have any other chunk. */ |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 905 | if (bufs != chunks || !bufs[2]) |
| 906 | return NULL; |
| 907 | |
| 908 | /* Compute the length of each part of the comparison. */ |
| 909 | l1 = last - pos; |
| 910 | l2 = url_param_name_l - l1; |
| 911 | |
| 912 | /* The second buffer is too short to contain the compared string. */ |
| 913 | if (bufs[2] + l2 > bufs[3]) |
| 914 | return NULL; |
| 915 | |
| 916 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 917 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 918 | return pos; |
| 919 | |
| 920 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 921 | bufs += 2; |
| 922 | pos = bufs[0] + l2; |
| 923 | last = bufs[1]; |
| 924 | |
| 925 | } else { |
| 926 | /* process a simple comparison. */ |
| 927 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 928 | return pos; |
| 929 | pos += url_param_name_l + 1; |
| 930 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 931 | last = bufs[2]; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | while (1) { |
| 936 | /* Look for the next delimiter. */ |
| 937 | while (pos < last && !http_is_param_delimiter(*pos, delim)) |
| 938 | pos++; |
| 939 | if (pos < last) |
| 940 | break; |
| 941 | /* process buffer wrapping. */ |
| 942 | if (bufs != chunks || !bufs[2]) |
| 943 | return NULL; |
| 944 | bufs += 2; |
| 945 | pos = bufs[0]; |
| 946 | last = bufs[1]; |
| 947 | } |
| 948 | pos++; |
| 949 | } |
| 950 | return NULL; |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Given a url parameter name and a query string, find the next value. |
| 955 | * An empty url_param_name matches the first available parameter. |
| 956 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 957 | * respectively provide a pointer to the value and its end. |
| 958 | * Otherwise, 0 is returned and vstart/vend are not modified. |
| 959 | */ |
| 960 | int http_find_next_url_param(const char **chunks, |
| 961 | const char* url_param_name, size_t url_param_name_l, |
| 962 | const char **vstart, const char **vend, char delim) |
| 963 | { |
| 964 | const char *arg_start, *qs_end; |
| 965 | const char *value_start, *value_end; |
| 966 | |
| 967 | arg_start = chunks[0]; |
| 968 | qs_end = chunks[1]; |
| 969 | if (url_param_name_l) { |
| 970 | /* Looks for an argument name. */ |
| 971 | arg_start = http_find_url_param_pos(chunks, |
| 972 | url_param_name, url_param_name_l, |
| 973 | delim); |
| 974 | /* Check for wrapping. */ |
| 975 | if (arg_start >= qs_end) |
| 976 | qs_end = chunks[3]; |
| 977 | } |
| 978 | if (!arg_start) |
| 979 | return 0; |
| 980 | |
| 981 | if (!url_param_name_l) { |
| 982 | while (1) { |
| 983 | /* looks for the first argument. */ |
| 984 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 985 | if (!value_start) { |
| 986 | /* Check for wrapping. */ |
| 987 | if (arg_start >= chunks[0] && |
| 988 | arg_start < chunks[1] && |
| 989 | chunks[2]) { |
| 990 | arg_start = chunks[2]; |
| 991 | qs_end = chunks[3]; |
| 992 | continue; |
| 993 | } |
| 994 | return 0; |
| 995 | } |
| 996 | break; |
| 997 | } |
| 998 | value_start++; |
| 999 | } |
| 1000 | else { |
| 1001 | /* Jump the argument length. */ |
| 1002 | value_start = arg_start + url_param_name_l + 1; |
| 1003 | |
| 1004 | /* Check for pointer wrapping. */ |
| 1005 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 1006 | /* Update the end pointer. */ |
| 1007 | qs_end = chunks[3]; |
| 1008 | |
| 1009 | /* Check for overflow. */ |
| 1010 | if (value_start >= qs_end) |
| 1011 | return 0; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | value_end = value_start; |
| 1016 | |
| 1017 | while (1) { |
| 1018 | while ((value_end < qs_end) && !http_is_param_delimiter(*value_end, delim)) |
| 1019 | value_end++; |
| 1020 | if (value_end < qs_end) |
| 1021 | break; |
| 1022 | /* process buffer wrapping. */ |
| 1023 | if (value_end >= chunks[0] && |
| 1024 | value_end < chunks[1] && |
| 1025 | chunks[2]) { |
| 1026 | value_end = chunks[2]; |
| 1027 | qs_end = chunks[3]; |
| 1028 | continue; |
| 1029 | } |
| 1030 | break; |
| 1031 | } |
| 1032 | |
| 1033 | *vstart = value_start; |
| 1034 | *vend = value_end; |
| 1035 | return 1; |
| 1036 | } |
| 1037 | |
Christopher Faulet | 8277ca7 | 2018-10-22 15:12:04 +0200 | [diff] [blame] | 1038 | /* Parses a single header line (without the CRLF) and splits it into its name |
| 1039 | * and its value. The parsing is pretty naive and just skip spaces. |
| 1040 | */ |
| 1041 | int http_parse_header(const struct ist hdr, struct ist *name, struct ist *value) |
| 1042 | { |
| 1043 | char *p = hdr.ptr; |
| 1044 | char *end = p + hdr.len; |
| 1045 | |
| 1046 | name->len = value->len = 0; |
| 1047 | |
| 1048 | /* Skip leading spaces */ |
| 1049 | for (; p < end && HTTP_IS_SPHT(*p); p++); |
| 1050 | |
| 1051 | /* Set the header name */ |
| 1052 | name->ptr = p; |
| 1053 | for (; p < end && HTTP_IS_TOKEN(*p); p++); |
| 1054 | name->len = p - name->ptr; |
| 1055 | |
| 1056 | /* Skip the ':' and spaces before and after it */ |
| 1057 | for (; p < end && HTTP_IS_SPHT(*p); p++); |
| 1058 | if (p < end && *p == ':') p++; |
| 1059 | for (; p < end && HTTP_IS_SPHT(*p); p++); |
| 1060 | |
| 1061 | /* Set the header value */ |
| 1062 | value->ptr = p; |
| 1063 | value->len = end - p; |
| 1064 | |
| 1065 | return 1; |
| 1066 | } |
| 1067 | |
| 1068 | /* Parses a single start line (without the CRLF) and splits it into 3 parts. The |
| 1069 | * parsing is pretty naive and just skip spaces. |
| 1070 | */ |
| 1071 | int http_parse_stline(const struct ist line, struct ist *p1, struct ist *p2, struct ist *p3) |
| 1072 | { |
| 1073 | char *p = line.ptr; |
| 1074 | char *end = p + line.len; |
| 1075 | |
| 1076 | p1->len = p2->len = p3->len = 0; |
| 1077 | |
| 1078 | /* Skip leading spaces */ |
| 1079 | for (; p < end && HTTP_IS_SPHT(*p); p++); |
| 1080 | |
| 1081 | /* Set the first part */ |
| 1082 | p1->ptr = p; |
| 1083 | for (; p < end && HTTP_IS_TOKEN(*p); p++); |
| 1084 | p1->len = p - p1->ptr; |
| 1085 | |
| 1086 | /* Skip spaces between p1 and p2 */ |
| 1087 | for (; p < end && HTTP_IS_SPHT(*p); p++); |
| 1088 | |
| 1089 | /* Set the second part */ |
| 1090 | p2->ptr = p; |
| 1091 | for (; p < end && !HTTP_IS_SPHT(*p); p++); |
| 1092 | p2->len = p - p2->ptr; |
| 1093 | |
| 1094 | /* Skip spaces between p2 and p3 */ |
| 1095 | for (; p < end && HTTP_IS_SPHT(*p); p++); |
| 1096 | |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1097 | /* The remaining is the third value */ |
Christopher Faulet | 8277ca7 | 2018-10-22 15:12:04 +0200 | [diff] [blame] | 1098 | p3->ptr = p; |
| 1099 | p3->len = end - p; |
| 1100 | |
| 1101 | return 1; |
| 1102 | } |
Christopher Faulet | 341fac1 | 2019-09-16 11:37:05 +0200 | [diff] [blame] | 1103 | |
| 1104 | /* Parses value of a Status header with the following format: "Status: Code[ |
| 1105 | * Reason]". The parsing is pretty naive and just skip spaces. It return the |
| 1106 | * numeric value of the status code. |
| 1107 | */ |
| 1108 | int http_parse_status_val(const struct ist value, struct ist *status, struct ist *reason) |
| 1109 | { |
| 1110 | char *p = value.ptr; |
| 1111 | char *end = p + value.len; |
| 1112 | uint16_t code; |
| 1113 | |
| 1114 | status->len = reason->len = 0; |
| 1115 | |
| 1116 | /* Skip leading spaces */ |
| 1117 | for (; p < end && HTTP_IS_SPHT(*p); p++); |
| 1118 | |
| 1119 | /* Set the status part */ |
| 1120 | status->ptr = p; |
| 1121 | for (; p < end && HTTP_IS_TOKEN(*p); p++); |
| 1122 | status->len = p - status->ptr; |
| 1123 | |
| 1124 | /* Skip spaces between status and reason */ |
| 1125 | for (; p < end && HTTP_IS_SPHT(*p); p++); |
| 1126 | |
| 1127 | /* the remaining is the reason */ |
| 1128 | reason->ptr = p; |
| 1129 | reason->len = end - p; |
| 1130 | |
| 1131 | code = strl2ui(status->ptr, status->len); |
| 1132 | return code; |
| 1133 | } |
Remi Tricot-Le Breton | bcced09 | 2020-10-22 10:40:03 +0200 | [diff] [blame] | 1134 | |
| 1135 | |
| 1136 | /* Returns non-zero if the two ETags are comparable (see RFC 7232#2.3.2). |
| 1137 | * If any of them is a weak ETag, we discard the weakness prefix and perform |
| 1138 | * a strict string comparison. |
| 1139 | * Returns 0 otherwise. |
| 1140 | */ |
| 1141 | int http_compare_etags(struct ist etag1, struct ist etag2) |
| 1142 | { |
| 1143 | enum http_etag_type etag_type1; |
| 1144 | enum http_etag_type etag_type2; |
| 1145 | |
| 1146 | etag_type1 = http_get_etag_type(etag1); |
| 1147 | etag_type2 = http_get_etag_type(etag2); |
| 1148 | |
| 1149 | if (etag_type1 == ETAG_INVALID || etag_type2 == ETAG_INVALID) |
| 1150 | return 0; |
| 1151 | |
| 1152 | /* Discard the 'W/' prefix an ETag is a weak one. */ |
| 1153 | if (etag_type1 == ETAG_WEAK) |
| 1154 | etag1 = istadv(etag1, 2); |
| 1155 | if (etag_type2 == ETAG_WEAK) |
| 1156 | etag2 = istadv(etag2, 2); |
| 1157 | |
| 1158 | return isteq(etag1, etag2); |
| 1159 | } |
Remi Tricot-Le Breton | 56e46cb | 2020-12-23 18:13:48 +0100 | [diff] [blame] | 1160 | |
| 1161 | |
| 1162 | /* |
| 1163 | * Trim leading space or horizontal tab characters from <value> string. |
| 1164 | * Returns the trimmed string. |
| 1165 | */ |
| 1166 | struct ist http_trim_leading_spht(struct ist value) |
| 1167 | { |
| 1168 | struct ist ret = value; |
| 1169 | |
| 1170 | while (ret.len && HTTP_IS_SPHT(ret.ptr[0])) { |
| 1171 | ++ret.ptr; |
| 1172 | --ret.len; |
| 1173 | } |
| 1174 | |
| 1175 | return ret; |
| 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | * Trim trailing space or horizontal tab characters from <value> string. |
| 1180 | * Returns the trimmed string. |
| 1181 | */ |
| 1182 | struct ist http_trim_trailing_spht(struct ist value) |
| 1183 | { |
| 1184 | struct ist ret = value; |
| 1185 | |
| 1186 | while (ret.len && HTTP_IS_SPHT(ret.ptr[-1])) |
| 1187 | --ret.len; |
| 1188 | |
| 1189 | return ret; |
| 1190 | } |