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