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