blob: fbb995e44bedd06567ad6f2e5452ff8b6b55b572 [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,
Willy Tarreau1ba30162022-05-24 15:34:26 +020093 ['A'] = HTTP_FLG_TOK | HTTP_FLG_VER,
94 ['B'] = HTTP_FLG_TOK | HTTP_FLG_VER,
95 ['C'] = HTTP_FLG_TOK | HTTP_FLG_VER,
96 ['D'] = HTTP_FLG_TOK | HTTP_FLG_VER,
97 ['E'] = HTTP_FLG_TOK | HTTP_FLG_VER,
98 ['F'] = HTTP_FLG_TOK | HTTP_FLG_VER,
99 ['G'] = HTTP_FLG_TOK | HTTP_FLG_VER,
Willy Tarreau35b51c62018-09-10 15:38:55 +0200100 ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER,
Willy Tarreau1ba30162022-05-24 15:34:26 +0200101 ['I'] = HTTP_FLG_TOK | HTTP_FLG_VER,
102 ['J'] = HTTP_FLG_TOK | HTTP_FLG_VER,
103 ['K'] = HTTP_FLG_TOK | HTTP_FLG_VER,
104 ['L'] = HTTP_FLG_TOK | HTTP_FLG_VER,
105 ['M'] = HTTP_FLG_TOK | HTTP_FLG_VER,
106 ['N'] = HTTP_FLG_TOK | HTTP_FLG_VER,
107 ['O'] = HTTP_FLG_TOK | HTTP_FLG_VER,
Willy Tarreau35b51c62018-09-10 15:38:55 +0200108 ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER,
Willy Tarreau1ba30162022-05-24 15:34:26 +0200109 ['Q'] = HTTP_FLG_TOK | HTTP_FLG_VER,
Willy Tarreau35b51c62018-09-10 15:38:55 +0200110 ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER,
111 ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER,
112 ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER,
Willy Tarreau1ba30162022-05-24 15:34:26 +0200113 ['U'] = HTTP_FLG_TOK | HTTP_FLG_VER,
114 ['V'] = HTTP_FLG_TOK | HTTP_FLG_VER,
115 ['W'] = HTTP_FLG_TOK | HTTP_FLG_VER,
116 ['X'] = HTTP_FLG_TOK | HTTP_FLG_VER,
117 ['Y'] = HTTP_FLG_TOK | HTTP_FLG_VER,
118 ['Z'] = HTTP_FLG_TOK | HTTP_FLG_VER,
Willy Tarreau35b51c62018-09-10 15:38:55 +0200119 ['['] = 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,
Anthonin Bonnefoy85048f82020-06-22 09:17:01 +0200168 [HTTP_ERR_413] = 413,
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200169 [HTTP_ERR_421] = 421,
Christopher Faulet92cafb32021-09-28 08:48:51 +0200170 [HTTP_ERR_422] = 422,
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200171 [HTTP_ERR_425] = 425,
172 [HTTP_ERR_429] = 429,
173 [HTTP_ERR_500] = 500,
Christopher Faulete095f312020-12-07 11:22:24 +0100174 [HTTP_ERR_501] = 501,
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200175 [HTTP_ERR_502] = 502,
176 [HTTP_ERR_503] = 503,
177 [HTTP_ERR_504] = 504,
178};
179
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100180const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200181 [HTTP_ERR_200] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200182 "HTTP/1.1 200 OK\r\n"
183 "Content-length: 58\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200184 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
188
189 [HTTP_ERR_400] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200190 "HTTP/1.1 400 Bad request\r\n"
191 "Content-length: 90\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
197
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200198 [HTTP_ERR_401] =
199 "HTTP/1.1 401 Unauthorized\r\n"
200 "Content-length: 112\r\n"
201 "Cache-Control: no-cache\r\n"
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n",
205
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200206 [HTTP_ERR_403] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200207 "HTTP/1.1 403 Forbidden\r\n"
208 "Content-length: 93\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200209 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200210 "Content-Type: text/html\r\n"
211 "\r\n"
212 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
213
Florian Tham9205fea2020-01-08 13:35:30 +0100214 [HTTP_ERR_404] =
215 "HTTP/1.1 404 Not Found\r\n"
216 "Content-length: 83\r\n"
217 "Cache-Control: no-cache\r\n"
Florian Tham9205fea2020-01-08 13:35:30 +0100218 "Content-Type: text/html\r\n"
219 "\r\n"
220 "<html><body><h1>404 Not Found</h1>\nThe resource could not be found.\n</body></html>\n",
221
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200222 [HTTP_ERR_405] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200223 "HTTP/1.1 405 Method Not Allowed\r\n"
224 "Content-length: 146\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200225 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200226 "Content-Type: text/html\r\n"
227 "\r\n"
228 "<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",
229
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200230 [HTTP_ERR_407] =
231 "HTTP/1.1 407 Unauthorized\r\n"
232 "Content-length: 112\r\n"
233 "Cache-Control: no-cache\r\n"
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200234 "Content-Type: text/html\r\n"
235 "\r\n"
236 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n",
237
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200238 [HTTP_ERR_408] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200239 "HTTP/1.1 408 Request Time-out\r\n"
240 "Content-length: 110\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200241 "Cache-Control: no-cache\r\n"
242 "Connection: close\r\n"
243 "Content-Type: text/html\r\n"
244 "\r\n"
245 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
246
Florian Tham272e29b2020-01-08 10:19:05 +0100247 [HTTP_ERR_410] =
248 "HTTP/1.1 410 Gone\r\n"
249 "Content-length: 114\r\n"
250 "Cache-Control: no-cache\r\n"
Florian Tham272e29b2020-01-08 10:19:05 +0100251 "Content-Type: text/html\r\n"
252 "\r\n"
253 "<html><body><h1>410 Gone</h1>\nThe resource is no longer available and will not be available again.\n</body></html>\n",
254
Anthonin Bonnefoy85048f82020-06-22 09:17:01 +0200255 [HTTP_ERR_413] =
256 "HTTP/1.1 413 Payload Too Large\r\n"
257 "Content-length: 106\r\n"
258 "Cache-Control: no-cache\r\n"
259 "Content-Type: text/html\r\n"
260 "\r\n"
261 "<html><body><h1>413 Payload Too Large</h1>\nThe request entity exceeds the maximum allowed.\n</body></html>\n",
262
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200263 [HTTP_ERR_421] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200264 "HTTP/1.1 421 Misdirected Request\r\n"
265 "Content-length: 104\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200266 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200267 "Content-Type: text/html\r\n"
268 "\r\n"
269 "<html><body><h1>421 Misdirected Request</h1>\nRequest sent to a non-authoritative server.\n</body></html>\n",
270
Christopher Faulet92cafb32021-09-28 08:48:51 +0200271 [HTTP_ERR_422] =
272 "HTTP/1.1 422 Unprocessable Content\r\n"
273 "Content-length: 116\r\n"
274 "Cache-Control: no-cache\r\n"
275 "Content-Type: text/html\r\n"
276 "\r\n"
277 "<html><body><h1>422 Unprocessable Content</h1>\nThe server cannot process the contained instructions.\n</body></html>\n",
278
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200279 [HTTP_ERR_425] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200280 "HTTP/1.1 425 Too Early\r\n"
281 "Content-length: 80\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200282 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200283 "Content-Type: text/html\r\n"
284 "\r\n"
285 "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n",
286
287 [HTTP_ERR_429] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200288 "HTTP/1.1 429 Too Many Requests\r\n"
289 "Content-length: 117\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200290 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200291 "Content-Type: text/html\r\n"
292 "\r\n"
293 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
294
295 [HTTP_ERR_500] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200296 "HTTP/1.1 500 Internal Server Error\r\n"
Christopher Faulet55633922020-10-09 08:39:26 +0200297 "Content-length: 97\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200298 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200299 "Content-Type: text/html\r\n"
300 "\r\n"
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500301 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n",
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200302
Christopher Faulete095f312020-12-07 11:22:24 +0100303 [HTTP_ERR_501] =
304 "HTTP/1.1 501 Not Implemented\r\n"
305 "Content-length: 136\r\n"
306 "Cache-Control: no-cache\r\n"
307 "Content-Type: text/html\r\n"
308 "\r\n"
309 "<html><body><h1>501 Not Implemented</h1>\n.The server does not support the functionality required to fulfill the request.\n</body></html>\n",
310
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200311 [HTTP_ERR_502] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200312 "HTTP/1.1 502 Bad Gateway\r\n"
313 "Content-length: 107\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200314 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200315 "Content-Type: text/html\r\n"
316 "\r\n"
317 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
318
319 [HTTP_ERR_503] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200320 "HTTP/1.1 503 Service Unavailable\r\n"
321 "Content-length: 107\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200322 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200323 "Content-Type: text/html\r\n"
324 "\r\n"
325 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
326
327 [HTTP_ERR_504] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200328 "HTTP/1.1 504 Gateway Time-out\r\n"
329 "Content-length: 92\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200330 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200331 "Content-Type: text/html\r\n"
332 "\r\n"
333 "<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 +0200334};
335
Willy Tarreau35b51c62018-09-10 15:38:55 +0200336const struct ist http_known_methods[HTTP_METH_OTHER] = {
337 [HTTP_METH_OPTIONS] = IST("OPTIONS"),
338 [HTTP_METH_GET] = IST("GET"),
339 [HTTP_METH_HEAD] = IST("HEAD"),
340 [HTTP_METH_POST] = IST("POST"),
341 [HTTP_METH_PUT] = IST("PUT"),
342 [HTTP_METH_DELETE] = IST("DELETE"),
343 [HTTP_METH_TRACE] = IST("TRACE"),
344 [HTTP_METH_CONNECT] = IST("CONNECT"),
345};
346
347/*
348 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
349 * ones.
350 */
351enum http_meth_t find_http_meth(const char *str, const int len)
352{
353 const struct ist m = ist2(str, len);
354
355 if (isteq(m, ist("GET"))) return HTTP_METH_GET;
356 else if (isteq(m, ist("HEAD"))) return HTTP_METH_HEAD;
357 else if (isteq(m, ist("POST"))) return HTTP_METH_POST;
358 else if (isteq(m, ist("CONNECT"))) return HTTP_METH_CONNECT;
359 else if (isteq(m, ist("PUT"))) return HTTP_METH_PUT;
360 else if (isteq(m, ist("OPTIONS"))) return HTTP_METH_OPTIONS;
361 else if (isteq(m, ist("DELETE"))) return HTTP_METH_DELETE;
362 else if (isteq(m, ist("TRACE"))) return HTTP_METH_TRACE;
363 else return HTTP_METH_OTHER;
364}
Willy Tarreau6b952c82018-09-10 17:45:34 +0200365
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200366/* This function returns HTTP_ERR_<num> (enum) matching http status code.
367 * Returned value should match codes from http_err_codes.
368 */
Willy Tarreau8de1df92019-04-15 21:27:18 +0200369int http_get_status_idx(unsigned int status)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200370{
371 switch (status) {
372 case 200: return HTTP_ERR_200;
373 case 400: return HTTP_ERR_400;
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200374 case 401: return HTTP_ERR_401;
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200375 case 403: return HTTP_ERR_403;
Florian Tham9205fea2020-01-08 13:35:30 +0100376 case 404: return HTTP_ERR_404;
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200377 case 405: return HTTP_ERR_405;
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200378 case 407: return HTTP_ERR_407;
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200379 case 408: return HTTP_ERR_408;
Florian Tham272e29b2020-01-08 10:19:05 +0100380 case 410: return HTTP_ERR_410;
Anthonin Bonnefoy85048f82020-06-22 09:17:01 +0200381 case 413: return HTTP_ERR_413;
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200382 case 421: return HTTP_ERR_421;
Christopher Faulet92cafb32021-09-28 08:48:51 +0200383 case 422: return HTTP_ERR_422;
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200384 case 425: return HTTP_ERR_425;
385 case 429: return HTTP_ERR_429;
386 case 500: return HTTP_ERR_500;
Christopher Faulete095f312020-12-07 11:22:24 +0100387 case 501: return HTTP_ERR_501;
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200388 case 502: return HTTP_ERR_502;
389 case 503: return HTTP_ERR_503;
390 case 504: return HTTP_ERR_504;
391 default: return HTTP_ERR_500;
392 }
393}
394
395/* This function returns a reason associated with the HTTP status.
396 * This function never fails, a message is always returned.
397 */
398const char *http_get_reason(unsigned int status)
399{
400 switch (status) {
401 case 100: return "Continue";
402 case 101: return "Switching Protocols";
403 case 102: return "Processing";
404 case 200: return "OK";
405 case 201: return "Created";
406 case 202: return "Accepted";
407 case 203: return "Non-Authoritative Information";
408 case 204: return "No Content";
409 case 205: return "Reset Content";
410 case 206: return "Partial Content";
411 case 207: return "Multi-Status";
412 case 210: return "Content Different";
413 case 226: return "IM Used";
414 case 300: return "Multiple Choices";
415 case 301: return "Moved Permanently";
416 case 302: return "Moved Temporarily";
417 case 303: return "See Other";
418 case 304: return "Not Modified";
419 case 305: return "Use Proxy";
420 case 307: return "Temporary Redirect";
421 case 308: return "Permanent Redirect";
422 case 310: return "Too many Redirects";
423 case 400: return "Bad Request";
424 case 401: return "Unauthorized";
425 case 402: return "Payment Required";
426 case 403: return "Forbidden";
427 case 404: return "Not Found";
428 case 405: return "Method Not Allowed";
429 case 406: return "Not Acceptable";
430 case 407: return "Proxy Authentication Required";
431 case 408: return "Request Time-out";
432 case 409: return "Conflict";
433 case 410: return "Gone";
434 case 411: return "Length Required";
435 case 412: return "Precondition Failed";
436 case 413: return "Request Entity Too Large";
437 case 414: return "Request-URI Too Long";
438 case 415: return "Unsupported Media Type";
439 case 416: return "Requested range unsatisfiable";
440 case 417: return "Expectation failed";
441 case 418: return "I'm a teapot";
442 case 421: return "Misdirected Request";
Christopher Faulet92cafb32021-09-28 08:48:51 +0200443 case 422: return "Unprocessable Content";
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200444 case 423: return "Locked";
445 case 424: return "Method failure";
446 case 425: return "Too Early";
447 case 426: return "Upgrade Required";
448 case 428: return "Precondition Required";
449 case 429: return "Too Many Requests";
450 case 431: return "Request Header Fields Too Large";
451 case 449: return "Retry With";
452 case 450: return "Blocked by Windows Parental Controls";
453 case 451: return "Unavailable For Legal Reasons";
454 case 456: return "Unrecoverable Error";
455 case 499: return "client has closed connection";
456 case 500: return "Internal Server Error";
457 case 501: return "Not Implemented";
458 case 502: return "Bad Gateway or Proxy Error";
459 case 503: return "Service Unavailable";
460 case 504: return "Gateway Time-out";
461 case 505: return "HTTP Version not supported";
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500462 case 506: return "Variant also negotiate";
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200463 case 507: return "Insufficient storage";
464 case 508: return "Loop detected";
465 case 509: return "Bandwidth Limit Exceeded";
466 case 510: return "Not extended";
467 case 511: return "Network authentication required";
468 case 520: return "Web server is returning an unknown error";
469 default:
470 switch (status) {
471 case 100 ... 199: return "Informational";
472 case 200 ... 299: return "Success";
473 case 300 ... 399: return "Redirection";
474 case 400 ... 499: return "Client Error";
475 case 500 ... 599: return "Server Error";
476 default: return "Other";
477 }
478 }
479}
480
Christopher Faulet658f9712022-07-05 09:48:39 +0200481/* Returns the ist string corresponding to port part (without ':') in the host
482 * <host> or IST_NULL if not found.
483*/
484struct ist http_get_host_port(const struct ist host)
485{
486 char *start, *end, *ptr;
487
488 start = istptr(host);
489 end = istend(host);
490 for (ptr = end; ptr > start && isdigit((unsigned char)*--ptr););
491
492 /* no port found */
493 if (likely(*ptr != ':' || ptr+1 == end || ptr == start))
494 return IST_NULL;
495
496 return istnext(ist2(ptr, end - ptr));
497}
498
Christopher Fauletca7218a2022-07-05 09:53:37 +0200499
500/* Return non-zero if the port <port> is a default port. If the scheme <schm> is
501 * set, it is used to detect default ports (HTTP => 80 and HTTPS => 443)
502 * port. Otherwise, both are considered as default ports.
503 */
504int http_is_default_port(const struct ist schm, const struct ist port)
505{
506 if (!isttest(schm))
507 return (isteq(port, ist("443")) || isteq(port, ist("80")));
508 else
509 return (isteq(port, ist("443")) && isteqi(schm, ist("https://"))) ||
510 (isteq(port, ist("80")) && isteqi(schm, ist("http://")));
511}
512
Willy Tarreaud3d8d032021-08-10 15:35:36 +0200513/* Returns non-zero if the scheme <schm> is syntactically correct according to
514 * RFC3986#3.1, otherwise zero. It expects only the scheme and nothing else
515 * (particularly not the following "://").
516 * Scheme = alpha *(alpha|digit|'+'|'-'|'.')
517 */
518int http_validate_scheme(const struct ist schm)
519{
520 size_t i;
521
522 for (i = 0; i < schm.len; i++) {
523 if (likely((schm.ptr[i] >= 'a' && schm.ptr[i] <= 'z') ||
524 (schm.ptr[i] >= 'A' && schm.ptr[i] <= 'Z')))
525 continue;
526 if (unlikely(!i)) // first char must be alpha
527 return 0;
528 if ((schm.ptr[i] >= '0' && schm.ptr[i] <= '9') ||
529 schm.ptr[i] == '+' || schm.ptr[i] == '-' || schm.ptr[i] == '.')
530 continue;
531 return 0;
532 }
533 return !!i;
534}
535
Amaury Denoyelleef088112021-07-07 10:49:25 +0200536/* Parse the uri and looks for the scheme. If not found, an empty ist is
537 * returned. Otherwise, the ist pointing to the scheme is returned.
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +0200538 *
539 * <parser> must have been initialized via http_uri_parser_init. See the
540 * related http_uri_parser documentation for the specific API usage.
Amaury Denoyelleef088112021-07-07 10:49:25 +0200541 */
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +0200542struct ist http_parse_scheme(struct http_uri_parser *parser)
Amaury Denoyelleef088112021-07-07 10:49:25 +0200543{
544 const char *ptr, *start, *end;
545
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +0200546 if (parser->state >= URI_PARSER_STATE_SCHEME_DONE)
Amaury Denoyelleef088112021-07-07 10:49:25 +0200547 goto not_found;
548
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +0200549 if (parser->format != URI_PARSER_FORMAT_ABSURI_OR_AUTHORITY)
Amaury Denoyelleef088112021-07-07 10:49:25 +0200550 goto not_found;
551
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +0200552 ptr = start = istptr(parser->uri);
553 end = istend(parser->uri);
554
Amaury Denoyelleef088112021-07-07 10:49:25 +0200555 if (isalpha((unsigned char)*ptr)) {
556 /* this is a scheme as described by RFC3986, par. 3.1, or only
557 * an authority (in case of a CONNECT method).
558 */
559 ptr++;
560 /* retrieve the scheme up to the suffix '://'. If the suffix is
561 * not found, this means there is no scheme and it is an
562 * authority-only uri.
563 */
564 while (ptr < end &&
565 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
566 ptr++;
567 if (ptr == end || *ptr++ != ':')
568 goto not_found;
569 if (ptr == end || *ptr++ != '/')
570 goto not_found;
571 if (ptr == end || *ptr++ != '/')
572 goto not_found;
573 }
574 else {
575 goto not_found;
576 }
577
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +0200578 parser->uri = ist2(ptr, end - ptr);
579 parser->state = URI_PARSER_STATE_SCHEME_DONE;
Amaury Denoyelleef088112021-07-07 10:49:25 +0200580 return ist2(start, ptr - start);
581
582 not_found:
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +0200583 parser->state = URI_PARSER_STATE_SCHEME_DONE;
Amaury Denoyelleef088112021-07-07 10:49:25 +0200584 return IST_NULL;
585}
586
Christopher Faulet16fdc552019-10-08 14:56:58 +0200587/* Parse the uri and looks for the authority, between the scheme and the
588 * path. if no_userinfo is not zero, the part before the '@' (including it) is
589 * skipped. If not found, an empty ist is returned. Otherwise, the ist pointing
590 * on the authority is returned.
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200591 *
592 * <parser> must have been initialized via http_uri_parser_init. See the
593 * related http_uri_parser documentation for the specific API usage.
Christopher Faulet16fdc552019-10-08 14:56:58 +0200594 */
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200595struct ist http_parse_authority(struct http_uri_parser *parser, int no_userinfo)
Christopher Faulet16fdc552019-10-08 14:56:58 +0200596{
597 const char *ptr, *start, *end;
598
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200599 if (parser->state >= URI_PARSER_STATE_AUTHORITY_DONE)
Christopher Faulet16fdc552019-10-08 14:56:58 +0200600 goto not_found;
601
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200602 if (parser->format != URI_PARSER_FORMAT_ABSURI_OR_AUTHORITY)
Christopher Faulet16fdc552019-10-08 14:56:58 +0200603 goto not_found;
604
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200605 if (parser->state < URI_PARSER_STATE_SCHEME_DONE)
606 http_parse_scheme(parser);
Christopher Faulet16fdc552019-10-08 14:56:58 +0200607
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200608 ptr = start = istptr(parser->uri);
609 end = istend(parser->uri);
610
Christopher Faulet16fdc552019-10-08 14:56:58 +0200611 while (ptr < end && *ptr != '/') {
612 if (*ptr++ == '@' && no_userinfo)
613 start = ptr;
614 }
615
616 /* OK, ptr point on the '/' or the end */
Christopher Faulet16fdc552019-10-08 14:56:58 +0200617
618 authority:
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200619 parser->uri = ist2(ptr, end - ptr);
620 parser->state = URI_PARSER_STATE_AUTHORITY_DONE;
621 return ist2(start, ptr - start);
Christopher Faulet16fdc552019-10-08 14:56:58 +0200622
623 not_found:
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200624 parser->state = URI_PARSER_STATE_AUTHORITY_DONE;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100625 return IST_NULL;
Christopher Faulet16fdc552019-10-08 14:56:58 +0200626}
627
Willy Tarreau6b952c82018-09-10 17:45:34 +0200628/* Parse the URI from the given transaction (which is assumed to be in request
629 * phase) and look for the "/" beginning the PATH. If not found, ist2(0,0) is
630 * returned. Otherwise the pointer and length are returned.
Amaury Denoyellec453f952021-07-06 11:40:12 +0200631 *
632 * <parser> must have been initialized via http_uri_parser_init. See the
633 * related http_uri_parser documentation for the specific API usage.
Willy Tarreau6b952c82018-09-10 17:45:34 +0200634 */
Amaury Denoyellec453f952021-07-06 11:40:12 +0200635struct ist http_parse_path(struct http_uri_parser *parser)
Willy Tarreau6b952c82018-09-10 17:45:34 +0200636{
637 const char *ptr, *end;
638
Amaury Denoyellec453f952021-07-06 11:40:12 +0200639 if (parser->state >= URI_PARSER_STATE_PATH_DONE)
Willy Tarreau6b952c82018-09-10 17:45:34 +0200640 goto not_found;
641
Amaury Denoyellec453f952021-07-06 11:40:12 +0200642 if (parser->format == URI_PARSER_FORMAT_EMPTY ||
643 parser->format == URI_PARSER_FORMAT_ASTERISK) {
644 goto not_found;
645 }
646
647 ptr = istptr(parser->uri);
648 end = istend(parser->uri);
Willy Tarreau6b952c82018-09-10 17:45:34 +0200649
Amaury Denoyellec453f952021-07-06 11:40:12 +0200650 /* If the uri is in absolute-path format, first skip the scheme and
651 * authority parts. No scheme will be found if the uri is in authority
652 * format, which indicates that the path won't be present.
Willy Tarreau6b952c82018-09-10 17:45:34 +0200653 */
Amaury Denoyellec453f952021-07-06 11:40:12 +0200654 if (parser->format == URI_PARSER_FORMAT_ABSURI_OR_AUTHORITY) {
655 if (parser->state < URI_PARSER_STATE_SCHEME_DONE) {
656 /* If no scheme found, uri is in authority format. No
657 * path is present.
658 */
659 if (!isttest(http_parse_scheme(parser)))
660 goto not_found;
661 }
Willy Tarreau6b952c82018-09-10 17:45:34 +0200662
Amaury Denoyellec453f952021-07-06 11:40:12 +0200663 if (parser->state < URI_PARSER_STATE_AUTHORITY_DONE)
664 http_parse_authority(parser, 1);
Willy Tarreau6b952c82018-09-10 17:45:34 +0200665
Amaury Denoyellec453f952021-07-06 11:40:12 +0200666 ptr = istptr(parser->uri);
667
668 if (ptr == end)
Willy Tarreau6b952c82018-09-10 17:45:34 +0200669 goto not_found;
670 }
Willy Tarreau6b952c82018-09-10 17:45:34 +0200671
Amaury Denoyellec453f952021-07-06 11:40:12 +0200672 parser->state = URI_PARSER_STATE_PATH_DONE;
Willy Tarreau6b952c82018-09-10 17:45:34 +0200673 return ist2(ptr, end - ptr);
674
675 not_found:
Amaury Denoyellec453f952021-07-06 11:40:12 +0200676 parser->state = URI_PARSER_STATE_PATH_DONE;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100677 return IST_NULL;
Willy Tarreau6b952c82018-09-10 17:45:34 +0200678}
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200679
Willy Tarreauab813a42018-09-10 18:41:28 +0200680/*
681 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
682 * If so, returns the position of the first non-space character relative to
683 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
684 * to return a pointer to the place after the first space. Returns 0 if the
685 * header name does not match. Checks are case-insensitive.
686 */
687int http_header_match2(const char *hdr, const char *end,
688 const char *name, int len)
689{
690 const char *val;
691
692 if (hdr + len >= end)
693 return 0;
694 if (hdr[len] != ':')
695 return 0;
696 if (strncasecmp(hdr, name, len) != 0)
697 return 0;
698 val = hdr + len + 1;
699 while (val < end && HTTP_IS_SPHT(*val))
700 val++;
701 if ((val >= end) && (len + 2 <= end - hdr))
702 return len + 2; /* we may replace starting from second space */
703 return val - hdr;
704}
705
706/* Find the end of the header value contained between <s> and <e>. See RFC7230,
707 * par 3.2 for more information. Note that it requires a valid header to return
708 * a valid result. This works for headers defined as comma-separated lists.
709 */
710char *http_find_hdr_value_end(char *s, const char *e)
711{
712 int quoted, qdpair;
713
714 quoted = qdpair = 0;
715
Willy Tarreau02ac9502020-02-21 16:31:22 +0100716#ifdef HA_UNALIGNED_LE
Willy Tarreauab813a42018-09-10 18:41:28 +0200717 /* speedup: skip everything not a comma nor a double quote */
718 for (; s <= e - sizeof(int); s += sizeof(int)) {
719 unsigned int c = *(int *)s; // comma
720 unsigned int q = c; // quote
721
722 c ^= 0x2c2c2c2c; // contains one zero on a comma
723 q ^= 0x22222222; // contains one zero on a quote
724
725 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
726 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
727
728 if ((c | q) & 0x80808080)
729 break; // found a comma or a quote
730 }
731#endif
732 for (; s < e; s++) {
733 if (qdpair) qdpair = 0;
734 else if (quoted) {
735 if (*s == '\\') qdpair = 1;
736 else if (*s == '"') quoted = 0;
737 }
738 else if (*s == '"') quoted = 1;
739 else if (*s == ',') return s;
740 }
741 return s;
742}
743
744/* Find the end of a cookie value contained between <s> and <e>. It works the
745 * same way as with headers above except that the semi-colon also ends a token.
746 * See RFC2965 for more information. Note that it requires a valid header to
747 * return a valid result.
748 */
749char *http_find_cookie_value_end(char *s, const char *e)
750{
751 int quoted, qdpair;
752
753 quoted = qdpair = 0;
754 for (; s < e; s++) {
755 if (qdpair) qdpair = 0;
756 else if (quoted) {
757 if (*s == '\\') qdpair = 1;
758 else if (*s == '"') quoted = 0;
759 }
760 else if (*s == '"') quoted = 1;
761 else if (*s == ',' || *s == ';') return s;
762 }
763 return s;
764}
765
766/* Try to find the next occurrence of a cookie name in a cookie header value.
Maciej Zdebdea7c202020-11-13 09:38:06 +0000767 * To match on any cookie name, <cookie_name_l> must be set to 0.
Willy Tarreauab813a42018-09-10 18:41:28 +0200768 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
769 * the cookie value is returned into *value and *value_l, and the function
770 * returns a pointer to the next pointer to search from if the value was found.
771 * Otherwise if the cookie was not found, NULL is returned and neither value
772 * nor value_l are touched. The input <hdr> string should first point to the
773 * header's value, and the <hdr_end> pointer must point to the first character
774 * not part of the value. <list> must be non-zero if value may represent a list
775 * of values (cookie headers). This makes it faster to abort parsing when no
776 * list is expected.
777 */
778char *http_extract_cookie_value(char *hdr, const char *hdr_end,
779 char *cookie_name, size_t cookie_name_l,
780 int list, char **value, size_t *value_l)
781{
782 char *equal, *att_end, *att_beg, *val_beg, *val_end;
783 char *next;
784
785 /* we search at least a cookie name followed by an equal, and more
786 * generally something like this :
787 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
788 */
789 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
790 /* Iterate through all cookies on this line */
791
792 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
793 att_beg++;
794
795 /* find att_end : this is the first character after the last non
796 * space before the equal. It may be equal to hdr_end.
797 */
798 equal = att_end = att_beg;
799
800 while (equal < hdr_end) {
801 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
802 break;
803 if (HTTP_IS_SPHT(*equal++))
804 continue;
805 att_end = equal;
806 }
807
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500808 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Willy Tarreauab813a42018-09-10 18:41:28 +0200809 * is between <att_beg> and <equal>, both may be identical.
810 */
811
812 /* look for end of cookie if there is an equal sign */
813 if (equal < hdr_end && *equal == '=') {
814 /* look for the beginning of the value */
815 val_beg = equal + 1;
816 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
817 val_beg++;
818
819 /* find the end of the value, respecting quotes */
820 next = http_find_cookie_value_end(val_beg, hdr_end);
821
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500822 /* make val_end point to the first white space or delimiter after the value */
Willy Tarreauab813a42018-09-10 18:41:28 +0200823 val_end = next;
824 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
825 val_end--;
826 } else {
827 val_beg = val_end = next = equal;
828 }
829
830 /* We have nothing to do with attributes beginning with '$'. However,
831 * they will automatically be removed if a header before them is removed,
832 * since they're supposed to be linked together.
833 */
834 if (*att_beg == '$')
835 continue;
836
837 /* Ignore cookies with no equal sign */
838 if (equal == next)
839 continue;
840
841 /* Now we have the cookie name between att_beg and att_end, and
842 * its value between val_beg and val_end.
843 */
844
Maciej Zdebdea7c202020-11-13 09:38:06 +0000845 if (cookie_name_l == 0 || (att_end - att_beg == cookie_name_l &&
846 memcmp(att_beg, cookie_name, cookie_name_l) == 0)) {
Willy Tarreauab813a42018-09-10 18:41:28 +0200847 /* let's return this value and indicate where to go on from */
848 *value = val_beg;
849 *value_l = val_end - val_beg;
850 return next + 1;
851 }
852
853 /* Set-Cookie headers only have the name in the first attr=value part */
854 if (!list)
855 break;
856 }
857
858 return NULL;
859}
860
Joseph Herlant942eea32018-11-15 13:57:22 -0800861/* Parses a qvalue and returns it multiplied by 1000, from 0 to 1000. If the
Willy Tarreauab813a42018-09-10 18:41:28 +0200862 * value is larger than 1000, it is bound to 1000. The parser consumes up to
863 * 1 digit, one dot and 3 digits and stops on the first invalid character.
864 * Unparsable qvalues return 1000 as "q=1.000".
865 */
866int http_parse_qvalue(const char *qvalue, const char **end)
867{
868 int q = 1000;
869
870 if (!isdigit((unsigned char)*qvalue))
871 goto out;
872 q = (*qvalue++ - '0') * 1000;
873
874 if (*qvalue++ != '.')
875 goto out;
876
877 if (!isdigit((unsigned char)*qvalue))
878 goto out;
879 q += (*qvalue++ - '0') * 100;
880
881 if (!isdigit((unsigned char)*qvalue))
882 goto out;
883 q += (*qvalue++ - '0') * 10;
884
885 if (!isdigit((unsigned char)*qvalue))
886 goto out;
887 q += (*qvalue++ - '0') * 1;
888 out:
889 if (q > 1000)
890 q = 1000;
891 if (end)
892 *end = qvalue;
893 return q;
894}
895
896/*
Joseph Herlant942eea32018-11-15 13:57:22 -0800897 * Given a url parameter, find the starting position of the first occurrence,
Willy Tarreauab813a42018-09-10 18:41:28 +0200898 * or NULL if the parameter is not found.
899 *
900 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
901 * the function will return query_string+8.
902 *
903 * Warning: this function returns a pointer that can point to the first chunk
904 * or the second chunk. The caller must be check the position before using the
905 * result.
906 */
907const char *http_find_url_param_pos(const char **chunks,
908 const char* url_param_name, size_t url_param_name_l,
909 char delim)
910{
911 const char *pos, *last, *equal;
912 const char **bufs = chunks;
913 int l1, l2;
914
915
916 pos = bufs[0];
917 last = bufs[1];
918 while (pos < last) {
919 /* Check the equal. */
920 equal = pos + url_param_name_l;
921 if (fix_pointer_if_wrap(chunks, &equal)) {
922 if (equal >= chunks[3])
923 return NULL;
924 } else {
925 if (equal >= chunks[1])
926 return NULL;
927 }
928 if (*equal == '=') {
929 if (pos + url_param_name_l > last) {
930 /* process wrap case, we detect a wrap. In this case, the
931 * comparison is performed in two parts.
932 */
933
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700934 /* This is the end, we don't have any other chunk. */
Willy Tarreauab813a42018-09-10 18:41:28 +0200935 if (bufs != chunks || !bufs[2])
936 return NULL;
937
938 /* Compute the length of each part of the comparison. */
939 l1 = last - pos;
940 l2 = url_param_name_l - l1;
941
942 /* The second buffer is too short to contain the compared string. */
943 if (bufs[2] + l2 > bufs[3])
944 return NULL;
945
946 if (memcmp(pos, url_param_name, l1) == 0 &&
947 memcmp(bufs[2], url_param_name+l1, l2) == 0)
948 return pos;
949
950 /* Perform wrapping and jump the string who fail the comparison. */
951 bufs += 2;
952 pos = bufs[0] + l2;
953 last = bufs[1];
954
955 } else {
956 /* process a simple comparison. */
957 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
958 return pos;
959 pos += url_param_name_l + 1;
960 if (fix_pointer_if_wrap(chunks, &pos))
961 last = bufs[2];
962 }
963 }
964
965 while (1) {
966 /* Look for the next delimiter. */
967 while (pos < last && !http_is_param_delimiter(*pos, delim))
968 pos++;
969 if (pos < last)
970 break;
971 /* process buffer wrapping. */
972 if (bufs != chunks || !bufs[2])
973 return NULL;
974 bufs += 2;
975 pos = bufs[0];
976 last = bufs[1];
977 }
978 pos++;
979 }
980 return NULL;
981}
982
983/*
984 * Given a url parameter name and a query string, find the next value.
985 * An empty url_param_name matches the first available parameter.
986 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
987 * respectively provide a pointer to the value and its end.
988 * Otherwise, 0 is returned and vstart/vend are not modified.
989 */
990int http_find_next_url_param(const char **chunks,
991 const char* url_param_name, size_t url_param_name_l,
992 const char **vstart, const char **vend, char delim)
993{
994 const char *arg_start, *qs_end;
995 const char *value_start, *value_end;
996
997 arg_start = chunks[0];
998 qs_end = chunks[1];
999 if (url_param_name_l) {
1000 /* Looks for an argument name. */
1001 arg_start = http_find_url_param_pos(chunks,
1002 url_param_name, url_param_name_l,
1003 delim);
1004 /* Check for wrapping. */
1005 if (arg_start >= qs_end)
1006 qs_end = chunks[3];
1007 }
1008 if (!arg_start)
1009 return 0;
1010
1011 if (!url_param_name_l) {
1012 while (1) {
1013 /* looks for the first argument. */
1014 value_start = memchr(arg_start, '=', qs_end - arg_start);
1015 if (!value_start) {
1016 /* Check for wrapping. */
1017 if (arg_start >= chunks[0] &&
1018 arg_start < chunks[1] &&
1019 chunks[2]) {
1020 arg_start = chunks[2];
1021 qs_end = chunks[3];
1022 continue;
1023 }
1024 return 0;
1025 }
1026 break;
1027 }
1028 value_start++;
1029 }
1030 else {
1031 /* Jump the argument length. */
1032 value_start = arg_start + url_param_name_l + 1;
1033
1034 /* Check for pointer wrapping. */
1035 if (fix_pointer_if_wrap(chunks, &value_start)) {
1036 /* Update the end pointer. */
1037 qs_end = chunks[3];
1038
1039 /* Check for overflow. */
1040 if (value_start >= qs_end)
1041 return 0;
1042 }
1043 }
1044
1045 value_end = value_start;
1046
1047 while (1) {
1048 while ((value_end < qs_end) && !http_is_param_delimiter(*value_end, delim))
1049 value_end++;
1050 if (value_end < qs_end)
1051 break;
1052 /* process buffer wrapping. */
1053 if (value_end >= chunks[0] &&
1054 value_end < chunks[1] &&
1055 chunks[2]) {
1056 value_end = chunks[2];
1057 qs_end = chunks[3];
1058 continue;
1059 }
1060 break;
1061 }
1062
1063 *vstart = value_start;
1064 *vend = value_end;
1065 return 1;
1066}
1067
Christopher Faulet8277ca72018-10-22 15:12:04 +02001068/* Parses a single header line (without the CRLF) and splits it into its name
1069 * and its value. The parsing is pretty naive and just skip spaces.
1070 */
1071int http_parse_header(const struct ist hdr, struct ist *name, struct ist *value)
1072{
1073 char *p = hdr.ptr;
1074 char *end = p + hdr.len;
1075
1076 name->len = value->len = 0;
1077
1078 /* Skip leading spaces */
1079 for (; p < end && HTTP_IS_SPHT(*p); p++);
1080
1081 /* Set the header name */
1082 name->ptr = p;
1083 for (; p < end && HTTP_IS_TOKEN(*p); p++);
1084 name->len = p - name->ptr;
1085
1086 /* Skip the ':' and spaces before and after it */
1087 for (; p < end && HTTP_IS_SPHT(*p); p++);
1088 if (p < end && *p == ':') p++;
1089 for (; p < end && HTTP_IS_SPHT(*p); p++);
1090
1091 /* Set the header value */
1092 value->ptr = p;
1093 value->len = end - p;
1094
1095 return 1;
1096}
1097
1098/* Parses a single start line (without the CRLF) and splits it into 3 parts. The
1099 * parsing is pretty naive and just skip spaces.
1100 */
1101int http_parse_stline(const struct ist line, struct ist *p1, struct ist *p2, struct ist *p3)
1102{
1103 char *p = line.ptr;
1104 char *end = p + line.len;
1105
1106 p1->len = p2->len = p3->len = 0;
1107
1108 /* Skip leading spaces */
1109 for (; p < end && HTTP_IS_SPHT(*p); p++);
1110
1111 /* Set the first part */
1112 p1->ptr = p;
1113 for (; p < end && HTTP_IS_TOKEN(*p); p++);
1114 p1->len = p - p1->ptr;
1115
1116 /* Skip spaces between p1 and p2 */
1117 for (; p < end && HTTP_IS_SPHT(*p); p++);
1118
1119 /* Set the second part */
1120 p2->ptr = p;
1121 for (; p < end && !HTTP_IS_SPHT(*p); p++);
1122 p2->len = p - p2->ptr;
1123
1124 /* Skip spaces between p2 and p3 */
1125 for (; p < end && HTTP_IS_SPHT(*p); p++);
1126
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001127 /* The remaining is the third value */
Christopher Faulet8277ca72018-10-22 15:12:04 +02001128 p3->ptr = p;
1129 p3->len = end - p;
1130
1131 return 1;
1132}
Christopher Faulet341fac12019-09-16 11:37:05 +02001133
1134/* Parses value of a Status header with the following format: "Status: Code[
1135 * Reason]". The parsing is pretty naive and just skip spaces. It return the
1136 * numeric value of the status code.
1137 */
1138int http_parse_status_val(const struct ist value, struct ist *status, struct ist *reason)
1139{
1140 char *p = value.ptr;
1141 char *end = p + value.len;
1142 uint16_t code;
1143
1144 status->len = reason->len = 0;
1145
1146 /* Skip leading spaces */
1147 for (; p < end && HTTP_IS_SPHT(*p); p++);
1148
1149 /* Set the status part */
1150 status->ptr = p;
1151 for (; p < end && HTTP_IS_TOKEN(*p); p++);
1152 status->len = p - status->ptr;
1153
1154 /* Skip spaces between status and reason */
1155 for (; p < end && HTTP_IS_SPHT(*p); p++);
1156
1157 /* the remaining is the reason */
1158 reason->ptr = p;
1159 reason->len = end - p;
1160
1161 code = strl2ui(status->ptr, status->len);
1162 return code;
1163}
Remi Tricot-Le Bretonbcced092020-10-22 10:40:03 +02001164
1165
1166/* Returns non-zero if the two ETags are comparable (see RFC 7232#2.3.2).
1167 * If any of them is a weak ETag, we discard the weakness prefix and perform
1168 * a strict string comparison.
1169 * Returns 0 otherwise.
1170 */
1171int http_compare_etags(struct ist etag1, struct ist etag2)
1172{
1173 enum http_etag_type etag_type1;
1174 enum http_etag_type etag_type2;
1175
1176 etag_type1 = http_get_etag_type(etag1);
1177 etag_type2 = http_get_etag_type(etag2);
1178
1179 if (etag_type1 == ETAG_INVALID || etag_type2 == ETAG_INVALID)
1180 return 0;
1181
1182 /* Discard the 'W/' prefix an ETag is a weak one. */
1183 if (etag_type1 == ETAG_WEAK)
1184 etag1 = istadv(etag1, 2);
1185 if (etag_type2 == ETAG_WEAK)
1186 etag2 = istadv(etag2, 2);
1187
1188 return isteq(etag1, etag2);
1189}
Remi Tricot-Le Breton56e46cb2020-12-23 18:13:48 +01001190
1191
1192/*
1193 * Trim leading space or horizontal tab characters from <value> string.
1194 * Returns the trimmed string.
1195 */
1196struct ist http_trim_leading_spht(struct ist value)
1197{
1198 struct ist ret = value;
1199
1200 while (ret.len && HTTP_IS_SPHT(ret.ptr[0])) {
1201 ++ret.ptr;
1202 --ret.len;
1203 }
1204
1205 return ret;
1206}
1207
1208/*
1209 * Trim trailing space or horizontal tab characters from <value> string.
1210 * Returns the trimmed string.
1211 */
1212struct ist http_trim_trailing_spht(struct ist value)
1213{
1214 struct ist ret = value;
1215
1216 while (ret.len && HTTP_IS_SPHT(ret.ptr[-1]))
1217 --ret.len;
1218
1219 return ret;
1220}