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