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