blob: 8e38b0c629ced63725bf2e66bba5dcb60c8fdccf [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,
Anthonin Bonnefoy85048f82020-06-22 09:17:01 +0200168 [HTTP_ERR_413] = 413,
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200169 [HTTP_ERR_421] = 421,
170 [HTTP_ERR_425] = 425,
171 [HTTP_ERR_429] = 429,
172 [HTTP_ERR_500] = 500,
173 [HTTP_ERR_502] = 502,
174 [HTTP_ERR_503] = 503,
175 [HTTP_ERR_504] = 504,
176};
177
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100178const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200179 [HTTP_ERR_200] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200180 "HTTP/1.1 200 OK\r\n"
181 "Content-length: 58\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200182 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200183 "Content-Type: text/html\r\n"
184 "\r\n"
185 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
186
187 [HTTP_ERR_400] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200188 "HTTP/1.1 400 Bad request\r\n"
189 "Content-length: 90\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200190 "Cache-Control: no-cache\r\n"
191 "Connection: close\r\n"
192 "Content-Type: text/html\r\n"
193 "\r\n"
194 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
195
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200196 [HTTP_ERR_401] =
197 "HTTP/1.1 401 Unauthorized\r\n"
198 "Content-length: 112\r\n"
199 "Cache-Control: no-cache\r\n"
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200200 "Content-Type: text/html\r\n"
201 "\r\n"
202 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n",
203
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200204 [HTTP_ERR_403] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200205 "HTTP/1.1 403 Forbidden\r\n"
206 "Content-length: 93\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200207 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200208 "Content-Type: text/html\r\n"
209 "\r\n"
210 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
211
Florian Tham9205fea2020-01-08 13:35:30 +0100212 [HTTP_ERR_404] =
213 "HTTP/1.1 404 Not Found\r\n"
214 "Content-length: 83\r\n"
215 "Cache-Control: no-cache\r\n"
Florian Tham9205fea2020-01-08 13:35:30 +0100216 "Content-Type: text/html\r\n"
217 "\r\n"
218 "<html><body><h1>404 Not Found</h1>\nThe resource could not be found.\n</body></html>\n",
219
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200220 [HTTP_ERR_405] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200221 "HTTP/1.1 405 Method Not Allowed\r\n"
222 "Content-length: 146\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200223 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200224 "Content-Type: text/html\r\n"
225 "\r\n"
226 "<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",
227
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200228 [HTTP_ERR_407] =
229 "HTTP/1.1 407 Unauthorized\r\n"
230 "Content-length: 112\r\n"
231 "Cache-Control: no-cache\r\n"
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200232 "Content-Type: text/html\r\n"
233 "\r\n"
234 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n",
235
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200236 [HTTP_ERR_408] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200237 "HTTP/1.1 408 Request Time-out\r\n"
238 "Content-length: 110\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>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
244
Florian Tham272e29b2020-01-08 10:19:05 +0100245 [HTTP_ERR_410] =
246 "HTTP/1.1 410 Gone\r\n"
247 "Content-length: 114\r\n"
248 "Cache-Control: no-cache\r\n"
Florian Tham272e29b2020-01-08 10:19:05 +0100249 "Content-Type: text/html\r\n"
250 "\r\n"
251 "<html><body><h1>410 Gone</h1>\nThe resource is no longer available and will not be available again.\n</body></html>\n",
252
Anthonin Bonnefoy85048f82020-06-22 09:17:01 +0200253 [HTTP_ERR_413] =
254 "HTTP/1.1 413 Payload Too Large\r\n"
255 "Content-length: 106\r\n"
256 "Cache-Control: no-cache\r\n"
257 "Content-Type: text/html\r\n"
258 "\r\n"
259 "<html><body><h1>413 Payload Too Large</h1>\nThe request entity exceeds the maximum allowed.\n</body></html>\n",
260
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200261 [HTTP_ERR_421] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200262 "HTTP/1.1 421 Misdirected Request\r\n"
263 "Content-length: 104\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200264 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200265 "Content-Type: text/html\r\n"
266 "\r\n"
267 "<html><body><h1>421 Misdirected Request</h1>\nRequest sent to a non-authoritative server.\n</body></html>\n",
268
269 [HTTP_ERR_425] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200270 "HTTP/1.1 425 Too Early\r\n"
271 "Content-length: 80\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200272 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200273 "Content-Type: text/html\r\n"
274 "\r\n"
275 "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n",
276
277 [HTTP_ERR_429] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200278 "HTTP/1.1 429 Too Many Requests\r\n"
279 "Content-length: 117\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200280 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200281 "Content-Type: text/html\r\n"
282 "\r\n"
283 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
284
285 [HTTP_ERR_500] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200286 "HTTP/1.1 500 Internal Server Error\r\n"
Christopher Faulet55633922020-10-09 08:39:26 +0200287 "Content-length: 97\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200288 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200289 "Content-Type: text/html\r\n"
290 "\r\n"
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500291 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n",
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200292
293 [HTTP_ERR_502] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200294 "HTTP/1.1 502 Bad Gateway\r\n"
295 "Content-length: 107\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200296 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200297 "Content-Type: text/html\r\n"
298 "\r\n"
299 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
300
301 [HTTP_ERR_503] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200302 "HTTP/1.1 503 Service Unavailable\r\n"
303 "Content-length: 107\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200304 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200305 "Content-Type: text/html\r\n"
306 "\r\n"
307 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
308
309 [HTTP_ERR_504] =
Willy Tarreaub5ba2b02019-06-11 16:08:25 +0200310 "HTTP/1.1 504 Gateway Time-out\r\n"
311 "Content-length: 92\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200312 "Cache-Control: no-cache\r\n"
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200313 "Content-Type: text/html\r\n"
314 "\r\n"
315 "<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 +0200316};
317
Willy Tarreau35b51c62018-09-10 15:38:55 +0200318const struct ist http_known_methods[HTTP_METH_OTHER] = {
319 [HTTP_METH_OPTIONS] = IST("OPTIONS"),
320 [HTTP_METH_GET] = IST("GET"),
321 [HTTP_METH_HEAD] = IST("HEAD"),
322 [HTTP_METH_POST] = IST("POST"),
323 [HTTP_METH_PUT] = IST("PUT"),
324 [HTTP_METH_DELETE] = IST("DELETE"),
325 [HTTP_METH_TRACE] = IST("TRACE"),
326 [HTTP_METH_CONNECT] = IST("CONNECT"),
327};
328
329/*
330 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
331 * ones.
332 */
333enum http_meth_t find_http_meth(const char *str, const int len)
334{
335 const struct ist m = ist2(str, len);
336
337 if (isteq(m, ist("GET"))) return HTTP_METH_GET;
338 else if (isteq(m, ist("HEAD"))) return HTTP_METH_HEAD;
339 else if (isteq(m, ist("POST"))) return HTTP_METH_POST;
340 else if (isteq(m, ist("CONNECT"))) return HTTP_METH_CONNECT;
341 else if (isteq(m, ist("PUT"))) return HTTP_METH_PUT;
342 else if (isteq(m, ist("OPTIONS"))) return HTTP_METH_OPTIONS;
343 else if (isteq(m, ist("DELETE"))) return HTTP_METH_DELETE;
344 else if (isteq(m, ist("TRACE"))) return HTTP_METH_TRACE;
345 else return HTTP_METH_OTHER;
346}
Willy Tarreau6b952c82018-09-10 17:45:34 +0200347
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200348/* This function returns HTTP_ERR_<num> (enum) matching http status code.
349 * Returned value should match codes from http_err_codes.
350 */
Willy Tarreau8de1df92019-04-15 21:27:18 +0200351int http_get_status_idx(unsigned int status)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200352{
353 switch (status) {
354 case 200: return HTTP_ERR_200;
355 case 400: return HTTP_ERR_400;
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200356 case 401: return HTTP_ERR_401;
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200357 case 403: return HTTP_ERR_403;
Florian Tham9205fea2020-01-08 13:35:30 +0100358 case 404: return HTTP_ERR_404;
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200359 case 405: return HTTP_ERR_405;
Christopher Faulet612f2ea2020-05-27 09:57:28 +0200360 case 407: return HTTP_ERR_407;
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200361 case 408: return HTTP_ERR_408;
Florian Tham272e29b2020-01-08 10:19:05 +0100362 case 410: return HTTP_ERR_410;
Anthonin Bonnefoy85048f82020-06-22 09:17:01 +0200363 case 413: return HTTP_ERR_413;
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200364 case 421: return HTTP_ERR_421;
365 case 425: return HTTP_ERR_425;
366 case 429: return HTTP_ERR_429;
367 case 500: return HTTP_ERR_500;
368 case 502: return HTTP_ERR_502;
369 case 503: return HTTP_ERR_503;
370 case 504: return HTTP_ERR_504;
371 default: return HTTP_ERR_500;
372 }
373}
374
375/* This function returns a reason associated with the HTTP status.
376 * This function never fails, a message is always returned.
377 */
378const char *http_get_reason(unsigned int status)
379{
380 switch (status) {
381 case 100: return "Continue";
382 case 101: return "Switching Protocols";
383 case 102: return "Processing";
384 case 200: return "OK";
385 case 201: return "Created";
386 case 202: return "Accepted";
387 case 203: return "Non-Authoritative Information";
388 case 204: return "No Content";
389 case 205: return "Reset Content";
390 case 206: return "Partial Content";
391 case 207: return "Multi-Status";
392 case 210: return "Content Different";
393 case 226: return "IM Used";
394 case 300: return "Multiple Choices";
395 case 301: return "Moved Permanently";
396 case 302: return "Moved Temporarily";
397 case 303: return "See Other";
398 case 304: return "Not Modified";
399 case 305: return "Use Proxy";
400 case 307: return "Temporary Redirect";
401 case 308: return "Permanent Redirect";
402 case 310: return "Too many Redirects";
403 case 400: return "Bad Request";
404 case 401: return "Unauthorized";
405 case 402: return "Payment Required";
406 case 403: return "Forbidden";
407 case 404: return "Not Found";
408 case 405: return "Method Not Allowed";
409 case 406: return "Not Acceptable";
410 case 407: return "Proxy Authentication Required";
411 case 408: return "Request Time-out";
412 case 409: return "Conflict";
413 case 410: return "Gone";
414 case 411: return "Length Required";
415 case 412: return "Precondition Failed";
416 case 413: return "Request Entity Too Large";
417 case 414: return "Request-URI Too Long";
418 case 415: return "Unsupported Media Type";
419 case 416: return "Requested range unsatisfiable";
420 case 417: return "Expectation failed";
421 case 418: return "I'm a teapot";
422 case 421: return "Misdirected Request";
423 case 422: return "Unprocessable entity";
424 case 423: return "Locked";
425 case 424: return "Method failure";
426 case 425: return "Too Early";
427 case 426: return "Upgrade Required";
428 case 428: return "Precondition Required";
429 case 429: return "Too Many Requests";
430 case 431: return "Request Header Fields Too Large";
431 case 449: return "Retry With";
432 case 450: return "Blocked by Windows Parental Controls";
433 case 451: return "Unavailable For Legal Reasons";
434 case 456: return "Unrecoverable Error";
435 case 499: return "client has closed connection";
436 case 500: return "Internal Server Error";
437 case 501: return "Not Implemented";
438 case 502: return "Bad Gateway or Proxy Error";
439 case 503: return "Service Unavailable";
440 case 504: return "Gateway Time-out";
441 case 505: return "HTTP Version not supported";
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500442 case 506: return "Variant also negotiate";
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200443 case 507: return "Insufficient storage";
444 case 508: return "Loop detected";
445 case 509: return "Bandwidth Limit Exceeded";
446 case 510: return "Not extended";
447 case 511: return "Network authentication required";
448 case 520: return "Web server is returning an unknown error";
449 default:
450 switch (status) {
451 case 100 ... 199: return "Informational";
452 case 200 ... 299: return "Success";
453 case 300 ... 399: return "Redirection";
454 case 400 ... 499: return "Client Error";
455 case 500 ... 599: return "Server Error";
456 default: return "Other";
457 }
458 }
459}
460
Christopher Faulet16fdc552019-10-08 14:56:58 +0200461/* Parse the uri and looks for the authority, between the scheme and the
462 * path. if no_userinfo is not zero, the part before the '@' (including it) is
463 * skipped. If not found, an empty ist is returned. Otherwise, the ist pointing
464 * on the authority is returned.
465 */
466struct ist http_get_authority(const struct ist uri, int no_userinfo)
467{
468 const char *ptr, *start, *end;
469
470 if (!uri.len)
471 goto not_found;
472
473 ptr = uri.ptr;
474 start = ptr;
475 end = ptr + uri.len;
476
477 /* RFC7230, par. 2.7 :
478 * Request-URI = "*" | absuri | abspath | authority
479 */
480
481 if (*ptr == '*' || *ptr == '/')
482 goto not_found;
483
484 if (isalpha((unsigned char)*ptr)) {
485 /* this is a scheme as described by RFC3986, par. 3.1, or only
486 * an authority (in case of a CONNECT method).
487 */
488 ptr++;
489 while (ptr < end &&
490 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
491 ptr++;
492 /* skip '://' or take the whole as authority if not found */
493 if (ptr == end || *ptr++ != ':')
494 goto authority;
495 if (ptr == end || *ptr++ != '/')
496 goto authority;
497 if (ptr == end || *ptr++ != '/')
498 goto authority;
499 }
500
501 start = ptr;
502 while (ptr < end && *ptr != '/') {
503 if (*ptr++ == '@' && no_userinfo)
504 start = ptr;
505 }
506
507 /* OK, ptr point on the '/' or the end */
508 end = ptr;
509
510 authority:
511 return ist2(start, end - start);
512
513 not_found:
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100514 return IST_NULL;
Christopher Faulet16fdc552019-10-08 14:56:58 +0200515}
516
Willy Tarreau6b952c82018-09-10 17:45:34 +0200517/* Parse the URI from the given transaction (which is assumed to be in request
518 * phase) and look for the "/" beginning the PATH. If not found, ist2(0,0) is
519 * returned. Otherwise the pointer and length are returned.
520 */
521struct ist http_get_path(const struct ist uri)
522{
523 const char *ptr, *end;
524
525 if (!uri.len)
526 goto not_found;
527
528 ptr = uri.ptr;
529 end = ptr + uri.len;
530
531 /* RFC7230, par. 2.7 :
532 * Request-URI = "*" | absuri | abspath | authority
533 */
534
535 if (*ptr == '*')
536 goto not_found;
537
538 if (isalpha((unsigned char)*ptr)) {
539 /* this is a scheme as described by RFC3986, par. 3.1 */
540 ptr++;
541 while (ptr < end &&
542 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
543 ptr++;
544 /* skip '://' */
545 if (ptr == end || *ptr++ != ':')
546 goto not_found;
547 if (ptr == end || *ptr++ != '/')
548 goto not_found;
549 if (ptr == end || *ptr++ != '/')
550 goto not_found;
551 }
552 /* skip [user[:passwd]@]host[:[port]] */
553
554 while (ptr < end && *ptr != '/')
555 ptr++;
556
557 if (ptr == end)
558 goto not_found;
559
560 /* OK, we got the '/' ! */
561 return ist2(ptr, end - ptr);
562
563 not_found:
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100564 return IST_NULL;
Willy Tarreau6b952c82018-09-10 17:45:34 +0200565}
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200566
Willy Tarreauab813a42018-09-10 18:41:28 +0200567/*
568 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
569 * If so, returns the position of the first non-space character relative to
570 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
571 * to return a pointer to the place after the first space. Returns 0 if the
572 * header name does not match. Checks are case-insensitive.
573 */
574int http_header_match2(const char *hdr, const char *end,
575 const char *name, int len)
576{
577 const char *val;
578
579 if (hdr + len >= end)
580 return 0;
581 if (hdr[len] != ':')
582 return 0;
583 if (strncasecmp(hdr, name, len) != 0)
584 return 0;
585 val = hdr + len + 1;
586 while (val < end && HTTP_IS_SPHT(*val))
587 val++;
588 if ((val >= end) && (len + 2 <= end - hdr))
589 return len + 2; /* we may replace starting from second space */
590 return val - hdr;
591}
592
593/* Find the end of the header value contained between <s> and <e>. See RFC7230,
594 * par 3.2 for more information. Note that it requires a valid header to return
595 * a valid result. This works for headers defined as comma-separated lists.
596 */
597char *http_find_hdr_value_end(char *s, const char *e)
598{
599 int quoted, qdpair;
600
601 quoted = qdpair = 0;
602
Willy Tarreau02ac9502020-02-21 16:31:22 +0100603#ifdef HA_UNALIGNED_LE
Willy Tarreauab813a42018-09-10 18:41:28 +0200604 /* speedup: skip everything not a comma nor a double quote */
605 for (; s <= e - sizeof(int); s += sizeof(int)) {
606 unsigned int c = *(int *)s; // comma
607 unsigned int q = c; // quote
608
609 c ^= 0x2c2c2c2c; // contains one zero on a comma
610 q ^= 0x22222222; // contains one zero on a quote
611
612 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
613 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
614
615 if ((c | q) & 0x80808080)
616 break; // found a comma or a quote
617 }
618#endif
619 for (; s < e; s++) {
620 if (qdpair) qdpair = 0;
621 else if (quoted) {
622 if (*s == '\\') qdpair = 1;
623 else if (*s == '"') quoted = 0;
624 }
625 else if (*s == '"') quoted = 1;
626 else if (*s == ',') return s;
627 }
628 return s;
629}
630
631/* Find the end of a cookie value contained between <s> and <e>. It works the
632 * same way as with headers above except that the semi-colon also ends a token.
633 * See RFC2965 for more information. Note that it requires a valid header to
634 * return a valid result.
635 */
636char *http_find_cookie_value_end(char *s, const char *e)
637{
638 int quoted, qdpair;
639
640 quoted = qdpair = 0;
641 for (; s < e; s++) {
642 if (qdpair) qdpair = 0;
643 else if (quoted) {
644 if (*s == '\\') qdpair = 1;
645 else if (*s == '"') quoted = 0;
646 }
647 else if (*s == '"') quoted = 1;
648 else if (*s == ',' || *s == ';') return s;
649 }
650 return s;
651}
652
653/* Try to find the next occurrence of a cookie name in a cookie header value.
Maciej Zdebdea7c202020-11-13 09:38:06 +0000654 * To match on any cookie name, <cookie_name_l> must be set to 0.
Willy Tarreauab813a42018-09-10 18:41:28 +0200655 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
656 * the cookie value is returned into *value and *value_l, and the function
657 * returns a pointer to the next pointer to search from if the value was found.
658 * Otherwise if the cookie was not found, NULL is returned and neither value
659 * nor value_l are touched. The input <hdr> string should first point to the
660 * header's value, and the <hdr_end> pointer must point to the first character
661 * not part of the value. <list> must be non-zero if value may represent a list
662 * of values (cookie headers). This makes it faster to abort parsing when no
663 * list is expected.
664 */
665char *http_extract_cookie_value(char *hdr, const char *hdr_end,
666 char *cookie_name, size_t cookie_name_l,
667 int list, char **value, size_t *value_l)
668{
669 char *equal, *att_end, *att_beg, *val_beg, *val_end;
670 char *next;
671
672 /* we search at least a cookie name followed by an equal, and more
673 * generally something like this :
674 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
675 */
676 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
677 /* Iterate through all cookies on this line */
678
679 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
680 att_beg++;
681
682 /* find att_end : this is the first character after the last non
683 * space before the equal. It may be equal to hdr_end.
684 */
685 equal = att_end = att_beg;
686
687 while (equal < hdr_end) {
688 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
689 break;
690 if (HTTP_IS_SPHT(*equal++))
691 continue;
692 att_end = equal;
693 }
694
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500695 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Willy Tarreauab813a42018-09-10 18:41:28 +0200696 * is between <att_beg> and <equal>, both may be identical.
697 */
698
699 /* look for end of cookie if there is an equal sign */
700 if (equal < hdr_end && *equal == '=') {
701 /* look for the beginning of the value */
702 val_beg = equal + 1;
703 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
704 val_beg++;
705
706 /* find the end of the value, respecting quotes */
707 next = http_find_cookie_value_end(val_beg, hdr_end);
708
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500709 /* make val_end point to the first white space or delimiter after the value */
Willy Tarreauab813a42018-09-10 18:41:28 +0200710 val_end = next;
711 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
712 val_end--;
713 } else {
714 val_beg = val_end = next = equal;
715 }
716
717 /* We have nothing to do with attributes beginning with '$'. However,
718 * they will automatically be removed if a header before them is removed,
719 * since they're supposed to be linked together.
720 */
721 if (*att_beg == '$')
722 continue;
723
724 /* Ignore cookies with no equal sign */
725 if (equal == next)
726 continue;
727
728 /* Now we have the cookie name between att_beg and att_end, and
729 * its value between val_beg and val_end.
730 */
731
Maciej Zdebdea7c202020-11-13 09:38:06 +0000732 if (cookie_name_l == 0 || (att_end - att_beg == cookie_name_l &&
733 memcmp(att_beg, cookie_name, cookie_name_l) == 0)) {
Willy Tarreauab813a42018-09-10 18:41:28 +0200734 /* let's return this value and indicate where to go on from */
735 *value = val_beg;
736 *value_l = val_end - val_beg;
737 return next + 1;
738 }
739
740 /* Set-Cookie headers only have the name in the first attr=value part */
741 if (!list)
742 break;
743 }
744
745 return NULL;
746}
747
Joseph Herlant942eea32018-11-15 13:57:22 -0800748/* Parses a qvalue and returns it multiplied by 1000, from 0 to 1000. If the
Willy Tarreauab813a42018-09-10 18:41:28 +0200749 * value is larger than 1000, it is bound to 1000. The parser consumes up to
750 * 1 digit, one dot and 3 digits and stops on the first invalid character.
751 * Unparsable qvalues return 1000 as "q=1.000".
752 */
753int http_parse_qvalue(const char *qvalue, const char **end)
754{
755 int q = 1000;
756
757 if (!isdigit((unsigned char)*qvalue))
758 goto out;
759 q = (*qvalue++ - '0') * 1000;
760
761 if (*qvalue++ != '.')
762 goto out;
763
764 if (!isdigit((unsigned char)*qvalue))
765 goto out;
766 q += (*qvalue++ - '0') * 100;
767
768 if (!isdigit((unsigned char)*qvalue))
769 goto out;
770 q += (*qvalue++ - '0') * 10;
771
772 if (!isdigit((unsigned char)*qvalue))
773 goto out;
774 q += (*qvalue++ - '0') * 1;
775 out:
776 if (q > 1000)
777 q = 1000;
778 if (end)
779 *end = qvalue;
780 return q;
781}
782
783/*
Joseph Herlant942eea32018-11-15 13:57:22 -0800784 * Given a url parameter, find the starting position of the first occurrence,
Willy Tarreauab813a42018-09-10 18:41:28 +0200785 * or NULL if the parameter is not found.
786 *
787 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
788 * the function will return query_string+8.
789 *
790 * Warning: this function returns a pointer that can point to the first chunk
791 * or the second chunk. The caller must be check the position before using the
792 * result.
793 */
794const char *http_find_url_param_pos(const char **chunks,
795 const char* url_param_name, size_t url_param_name_l,
796 char delim)
797{
798 const char *pos, *last, *equal;
799 const char **bufs = chunks;
800 int l1, l2;
801
802
803 pos = bufs[0];
804 last = bufs[1];
805 while (pos < last) {
806 /* Check the equal. */
807 equal = pos + url_param_name_l;
808 if (fix_pointer_if_wrap(chunks, &equal)) {
809 if (equal >= chunks[3])
810 return NULL;
811 } else {
812 if (equal >= chunks[1])
813 return NULL;
814 }
815 if (*equal == '=') {
816 if (pos + url_param_name_l > last) {
817 /* process wrap case, we detect a wrap. In this case, the
818 * comparison is performed in two parts.
819 */
820
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700821 /* This is the end, we don't have any other chunk. */
Willy Tarreauab813a42018-09-10 18:41:28 +0200822 if (bufs != chunks || !bufs[2])
823 return NULL;
824
825 /* Compute the length of each part of the comparison. */
826 l1 = last - pos;
827 l2 = url_param_name_l - l1;
828
829 /* The second buffer is too short to contain the compared string. */
830 if (bufs[2] + l2 > bufs[3])
831 return NULL;
832
833 if (memcmp(pos, url_param_name, l1) == 0 &&
834 memcmp(bufs[2], url_param_name+l1, l2) == 0)
835 return pos;
836
837 /* Perform wrapping and jump the string who fail the comparison. */
838 bufs += 2;
839 pos = bufs[0] + l2;
840 last = bufs[1];
841
842 } else {
843 /* process a simple comparison. */
844 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
845 return pos;
846 pos += url_param_name_l + 1;
847 if (fix_pointer_if_wrap(chunks, &pos))
848 last = bufs[2];
849 }
850 }
851
852 while (1) {
853 /* Look for the next delimiter. */
854 while (pos < last && !http_is_param_delimiter(*pos, delim))
855 pos++;
856 if (pos < last)
857 break;
858 /* process buffer wrapping. */
859 if (bufs != chunks || !bufs[2])
860 return NULL;
861 bufs += 2;
862 pos = bufs[0];
863 last = bufs[1];
864 }
865 pos++;
866 }
867 return NULL;
868}
869
870/*
871 * Given a url parameter name and a query string, find the next value.
872 * An empty url_param_name matches the first available parameter.
873 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
874 * respectively provide a pointer to the value and its end.
875 * Otherwise, 0 is returned and vstart/vend are not modified.
876 */
877int http_find_next_url_param(const char **chunks,
878 const char* url_param_name, size_t url_param_name_l,
879 const char **vstart, const char **vend, char delim)
880{
881 const char *arg_start, *qs_end;
882 const char *value_start, *value_end;
883
884 arg_start = chunks[0];
885 qs_end = chunks[1];
886 if (url_param_name_l) {
887 /* Looks for an argument name. */
888 arg_start = http_find_url_param_pos(chunks,
889 url_param_name, url_param_name_l,
890 delim);
891 /* Check for wrapping. */
892 if (arg_start >= qs_end)
893 qs_end = chunks[3];
894 }
895 if (!arg_start)
896 return 0;
897
898 if (!url_param_name_l) {
899 while (1) {
900 /* looks for the first argument. */
901 value_start = memchr(arg_start, '=', qs_end - arg_start);
902 if (!value_start) {
903 /* Check for wrapping. */
904 if (arg_start >= chunks[0] &&
905 arg_start < chunks[1] &&
906 chunks[2]) {
907 arg_start = chunks[2];
908 qs_end = chunks[3];
909 continue;
910 }
911 return 0;
912 }
913 break;
914 }
915 value_start++;
916 }
917 else {
918 /* Jump the argument length. */
919 value_start = arg_start + url_param_name_l + 1;
920
921 /* Check for pointer wrapping. */
922 if (fix_pointer_if_wrap(chunks, &value_start)) {
923 /* Update the end pointer. */
924 qs_end = chunks[3];
925
926 /* Check for overflow. */
927 if (value_start >= qs_end)
928 return 0;
929 }
930 }
931
932 value_end = value_start;
933
934 while (1) {
935 while ((value_end < qs_end) && !http_is_param_delimiter(*value_end, delim))
936 value_end++;
937 if (value_end < qs_end)
938 break;
939 /* process buffer wrapping. */
940 if (value_end >= chunks[0] &&
941 value_end < chunks[1] &&
942 chunks[2]) {
943 value_end = chunks[2];
944 qs_end = chunks[3];
945 continue;
946 }
947 break;
948 }
949
950 *vstart = value_start;
951 *vend = value_end;
952 return 1;
953}
954
Christopher Faulet8277ca72018-10-22 15:12:04 +0200955/* Parses a single header line (without the CRLF) and splits it into its name
956 * and its value. The parsing is pretty naive and just skip spaces.
957 */
958int http_parse_header(const struct ist hdr, struct ist *name, struct ist *value)
959{
960 char *p = hdr.ptr;
961 char *end = p + hdr.len;
962
963 name->len = value->len = 0;
964
965 /* Skip leading spaces */
966 for (; p < end && HTTP_IS_SPHT(*p); p++);
967
968 /* Set the header name */
969 name->ptr = p;
970 for (; p < end && HTTP_IS_TOKEN(*p); p++);
971 name->len = p - name->ptr;
972
973 /* Skip the ':' and spaces before and after it */
974 for (; p < end && HTTP_IS_SPHT(*p); p++);
975 if (p < end && *p == ':') p++;
976 for (; p < end && HTTP_IS_SPHT(*p); p++);
977
978 /* Set the header value */
979 value->ptr = p;
980 value->len = end - p;
981
982 return 1;
983}
984
985/* Parses a single start line (without the CRLF) and splits it into 3 parts. The
986 * parsing is pretty naive and just skip spaces.
987 */
988int http_parse_stline(const struct ist line, struct ist *p1, struct ist *p2, struct ist *p3)
989{
990 char *p = line.ptr;
991 char *end = p + line.len;
992
993 p1->len = p2->len = p3->len = 0;
994
995 /* Skip leading spaces */
996 for (; p < end && HTTP_IS_SPHT(*p); p++);
997
998 /* Set the first part */
999 p1->ptr = p;
1000 for (; p < end && HTTP_IS_TOKEN(*p); p++);
1001 p1->len = p - p1->ptr;
1002
1003 /* Skip spaces between p1 and p2 */
1004 for (; p < end && HTTP_IS_SPHT(*p); p++);
1005
1006 /* Set the second part */
1007 p2->ptr = p;
1008 for (; p < end && !HTTP_IS_SPHT(*p); p++);
1009 p2->len = p - p2->ptr;
1010
1011 /* Skip spaces between p2 and p3 */
1012 for (; p < end && HTTP_IS_SPHT(*p); p++);
1013
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001014 /* The remaining is the third value */
Christopher Faulet8277ca72018-10-22 15:12:04 +02001015 p3->ptr = p;
1016 p3->len = end - p;
1017
1018 return 1;
1019}
Christopher Faulet341fac12019-09-16 11:37:05 +02001020
1021/* Parses value of a Status header with the following format: "Status: Code[
1022 * Reason]". The parsing is pretty naive and just skip spaces. It return the
1023 * numeric value of the status code.
1024 */
1025int http_parse_status_val(const struct ist value, struct ist *status, struct ist *reason)
1026{
1027 char *p = value.ptr;
1028 char *end = p + value.len;
1029 uint16_t code;
1030
1031 status->len = reason->len = 0;
1032
1033 /* Skip leading spaces */
1034 for (; p < end && HTTP_IS_SPHT(*p); p++);
1035
1036 /* Set the status part */
1037 status->ptr = p;
1038 for (; p < end && HTTP_IS_TOKEN(*p); p++);
1039 status->len = p - status->ptr;
1040
1041 /* Skip spaces between status and reason */
1042 for (; p < end && HTTP_IS_SPHT(*p); p++);
1043
1044 /* the remaining is the reason */
1045 reason->ptr = p;
1046 reason->len = end - p;
1047
1048 code = strl2ui(status->ptr, status->len);
1049 return code;
1050}
Remi Tricot-Le Bretonbcced092020-10-22 10:40:03 +02001051
1052
1053/* Returns non-zero if the two ETags are comparable (see RFC 7232#2.3.2).
1054 * If any of them is a weak ETag, we discard the weakness prefix and perform
1055 * a strict string comparison.
1056 * Returns 0 otherwise.
1057 */
1058int http_compare_etags(struct ist etag1, struct ist etag2)
1059{
1060 enum http_etag_type etag_type1;
1061 enum http_etag_type etag_type2;
1062
1063 etag_type1 = http_get_etag_type(etag1);
1064 etag_type2 = http_get_etag_type(etag2);
1065
1066 if (etag_type1 == ETAG_INVALID || etag_type2 == ETAG_INVALID)
1067 return 0;
1068
1069 /* Discard the 'W/' prefix an ETag is a weak one. */
1070 if (etag_type1 == ETAG_WEAK)
1071 etag1 = istadv(etag1, 2);
1072 if (etag_type2 == ETAG_WEAK)
1073 etag2 = istadv(etag2, 2);
1074
1075 return isteq(etag1, etag2);
1076}
Remi Tricot-Le Breton56e46cb2020-12-23 18:13:48 +01001077
1078
1079/*
1080 * Trim leading space or horizontal tab characters from <value> string.
1081 * Returns the trimmed string.
1082 */
1083struct ist http_trim_leading_spht(struct ist value)
1084{
1085 struct ist ret = value;
1086
1087 while (ret.len && HTTP_IS_SPHT(ret.ptr[0])) {
1088 ++ret.ptr;
1089 --ret.len;
1090 }
1091
1092 return ret;
1093}
1094
1095/*
1096 * Trim trailing space or horizontal tab characters from <value> string.
1097 * Returns the trimmed string.
1098 */
1099struct ist http_trim_trailing_spht(struct ist value)
1100{
1101 struct ist ret = value;
1102
1103 while (ret.len && HTTP_IS_SPHT(ret.ptr[-1]))
1104 --ret.len;
1105
1106 return ret;
1107}