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