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