blob: acecd4e346cdf493c7c0bc51e3c5ba1c54efcdf8 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010029#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010043#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020044#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010046#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreau8797c062007-05-07 00:55:35 +020049#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020050#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020051#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010052#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020054#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010055#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010056#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020057#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010058#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020060#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020061#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020062#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010064#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010065#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020066#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020067#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010068#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020070#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010071#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020072#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010073#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020075#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020076#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
Willy Tarreau522d6c02009-12-06 18:49:18 +010078const char HTTP_100[] =
79 "HTTP/1.1 100 Continue\r\n\r\n";
80
81const struct chunk http_100_chunk = {
82 .str = (char *)&HTTP_100,
83 .len = sizeof(HTTP_100)-1
84};
85
Willy Tarreaua9679ac2010-01-03 17:32:57 +010086/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreau0f772532006-12-23 20:51:41 +010092const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010095 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010096 "Location: "; /* not terminated since it will be concatenated with the URL */
97
98/* same as 302 except that the browser MUST retry with the GET method */
99const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100102 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400105
106/* same as 302 except that the browser MUST retry with the same method */
107const char *HTTP_307 =
108 "HTTP/1.1 307 Temporary Redirect\r\n"
109 "Cache-Control: no-cache\r\n"
110 "Content-length: 0\r\n"
111 "Location: "; /* not terminated since it will be concatenated with the URL */
112
113/* same as 301 except that the browser MUST retry with the same method */
114const char *HTTP_308 =
115 "HTTP/1.1 308 Permanent Redirect\r\n"
116 "Content-length: 0\r\n"
117 "Location: "; /* not terminated since it will be concatenated with the URL */
118
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
120const char *HTTP_401_fmt =
121 "HTTP/1.0 401 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200124 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
127 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
128
Willy Tarreau844a7e72010-01-31 21:46:18 +0100129const char *HTTP_407_fmt =
130 "HTTP/1.0 407 Unauthorized\r\n"
131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
135 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800136 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100137
Willy Tarreau0f772532006-12-23 20:51:41 +0100138
139const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200140 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100141 [HTTP_ERR_400] = 400,
142 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400143 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100144 [HTTP_ERR_408] = 408,
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200145 [HTTP_ERR_421] = 421,
Olivier Houchard51a76d82017-10-02 16:12:07 +0200146 [HTTP_ERR_425] = 425,
CJ Ess108b1dd2015-04-07 12:03:37 -0400147 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100148 [HTTP_ERR_500] = 500,
149 [HTTP_ERR_502] = 502,
150 [HTTP_ERR_503] = 503,
151 [HTTP_ERR_504] = 504,
152};
153
Willy Tarreau80587432006-12-24 17:47:20 +0100154static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200155 [HTTP_ERR_200] =
156 "HTTP/1.0 200 OK\r\n"
157 "Cache-Control: no-cache\r\n"
158 "Connection: close\r\n"
159 "Content-Type: text/html\r\n"
160 "\r\n"
161 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
162
Willy Tarreau0f772532006-12-23 20:51:41 +0100163 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100164 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100165 "Cache-Control: no-cache\r\n"
166 "Connection: close\r\n"
167 "Content-Type: text/html\r\n"
168 "\r\n"
169 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
170
171 [HTTP_ERR_403] =
172 "HTTP/1.0 403 Forbidden\r\n"
173 "Cache-Control: no-cache\r\n"
174 "Connection: close\r\n"
175 "Content-Type: text/html\r\n"
176 "\r\n"
177 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
178
CJ Ess108b1dd2015-04-07 12:03:37 -0400179 [HTTP_ERR_405] =
180 "HTTP/1.0 405 Method Not Allowed\r\n"
181 "Cache-Control: no-cache\r\n"
182 "Connection: close\r\n"
183 "Content-Type: text/html\r\n"
184 "\r\n"
185 "<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",
186
Willy Tarreau0f772532006-12-23 20:51:41 +0100187 [HTTP_ERR_408] =
188 "HTTP/1.0 408 Request Time-out\r\n"
189 "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>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
194
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200195 [HTTP_ERR_421] =
196 "HTTP/1.0 421 Misdirected Request\r\n"
197 "Cache-Control: no-cache\r\n"
198 "Connection: close\r\n"
199 "Content-Type: text/html\r\n"
200 "\r\n"
201 "<html><body><h1>421 Misdirected Request</h1>\nRequest sent to a non-authoritative server.\n</body></html>\n",
202
Olivier Houchard51a76d82017-10-02 16:12:07 +0200203 [HTTP_ERR_425] =
204 "HTTP/1.0 425 Too Early\r\n"
205 "Cache-Control: no-cache\r\n"
206 "Connection: close\r\n"
207 "Content-Type: text/html\r\n"
208 "\r\n"
209 "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n",
210
CJ Ess108b1dd2015-04-07 12:03:37 -0400211 [HTTP_ERR_429] =
212 "HTTP/1.0 429 Too Many Requests\r\n"
213 "Cache-Control: no-cache\r\n"
214 "Connection: close\r\n"
215 "Content-Type: text/html\r\n"
216 "\r\n"
217 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
218
Willy Tarreau0f772532006-12-23 20:51:41 +0100219 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200220 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100221 "Cache-Control: no-cache\r\n"
222 "Connection: close\r\n"
223 "Content-Type: text/html\r\n"
224 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200225 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100226
227 [HTTP_ERR_502] =
228 "HTTP/1.0 502 Bad Gateway\r\n"
229 "Cache-Control: no-cache\r\n"
230 "Connection: close\r\n"
231 "Content-Type: text/html\r\n"
232 "\r\n"
233 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
234
235 [HTTP_ERR_503] =
236 "HTTP/1.0 503 Service Unavailable\r\n"
237 "Cache-Control: no-cache\r\n"
238 "Connection: close\r\n"
239 "Content-Type: text/html\r\n"
240 "\r\n"
241 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
242
243 [HTTP_ERR_504] =
244 "HTTP/1.0 504 Gateway Time-out\r\n"
245 "Cache-Control: no-cache\r\n"
246 "Connection: close\r\n"
247 "Content-Type: text/html\r\n"
248 "\r\n"
249 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
250
251};
252
Cyril Bonté19979e12012-04-04 12:57:21 +0200253/* status codes available for the stats admin page (strictly 4 chars length) */
254const char *stat_status_codes[STAT_STATUS_SIZE] = {
255 [STAT_STATUS_DENY] = "DENY",
256 [STAT_STATUS_DONE] = "DONE",
257 [STAT_STATUS_ERRP] = "ERRP",
258 [STAT_STATUS_EXCD] = "EXCD",
259 [STAT_STATUS_NONE] = "NONE",
260 [STAT_STATUS_PART] = "PART",
261 [STAT_STATUS_UNKN] = "UNKN",
262};
263
264
William Lallemand73025dd2014-04-24 14:38:37 +0200265/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200266struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200267 .list = LIST_HEAD_INIT(http_req_keywords.list)
268};
269
270/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200271struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200272 .list = LIST_HEAD_INIT(http_res_keywords.list)
273};
274
Willy Tarreau80587432006-12-24 17:47:20 +0100275/* We must put the messages here since GCC cannot initialize consts depending
276 * on strlen().
277 */
278struct chunk http_err_chunks[HTTP_ERR_SIZE];
279
Willy Tarreaua890d072013-04-02 12:01:06 +0200280/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100281static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +0200282
Willy Tarreau42250582007-04-01 01:30:43 +0200283#define FD_SETS_ARE_BITFIELDS
284#ifdef FD_SETS_ARE_BITFIELDS
285/*
286 * This map is used with all the FD_* macros to check whether a particular bit
287 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
288 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
289 * byte should be encoded. Be careful to always pass bytes from 0 to 255
290 * exclusively to the macros.
291 */
292fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
293fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100294fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200295
296#else
297#error "Check if your OS uses bitfields for fd_sets"
298#endif
299
Willy Tarreau87b09662015-04-03 00:22:06 +0200300static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200301
David Carlier7365f7d2016-04-04 11:54:42 +0100302static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
303static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100304
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200305/* This function returns a reason associated with the HTTP status.
306 * This function never fails, a message is always returned.
307 */
308const char *get_reason(unsigned int status)
309{
310 switch (status) {
311 case 100: return "Continue";
312 case 101: return "Switching Protocols";
313 case 102: return "Processing";
314 case 200: return "OK";
315 case 201: return "Created";
316 case 202: return "Accepted";
317 case 203: return "Non-Authoritative Information";
318 case 204: return "No Content";
319 case 205: return "Reset Content";
320 case 206: return "Partial Content";
321 case 207: return "Multi-Status";
322 case 210: return "Content Different";
323 case 226: return "IM Used";
324 case 300: return "Multiple Choices";
325 case 301: return "Moved Permanently";
326 case 302: return "Moved Temporarily";
327 case 303: return "See Other";
328 case 304: return "Not Modified";
329 case 305: return "Use Proxy";
330 case 307: return "Temporary Redirect";
331 case 308: return "Permanent Redirect";
332 case 310: return "Too many Redirects";
333 case 400: return "Bad Request";
334 case 401: return "Unauthorized";
335 case 402: return "Payment Required";
336 case 403: return "Forbidden";
337 case 404: return "Not Found";
338 case 405: return "Method Not Allowed";
339 case 406: return "Not Acceptable";
340 case 407: return "Proxy Authentication Required";
341 case 408: return "Request Time-out";
342 case 409: return "Conflict";
343 case 410: return "Gone";
344 case 411: return "Length Required";
345 case 412: return "Precondition Failed";
346 case 413: return "Request Entity Too Large";
347 case 414: return "Request-URI Too Long";
348 case 415: return "Unsupported Media Type";
349 case 416: return "Requested range unsatisfiable";
350 case 417: return "Expectation failed";
351 case 418: return "I'm a teapot";
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200352 case 421: return "Misdirected Request";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200353 case 422: return "Unprocessable entity";
354 case 423: return "Locked";
355 case 424: return "Method failure";
Olivier Houchard51a76d82017-10-02 16:12:07 +0200356 case 425: return "Too Early";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200357 case 426: return "Upgrade Required";
358 case 428: return "Precondition Required";
359 case 429: return "Too Many Requests";
360 case 431: return "Request Header Fields Too Large";
361 case 449: return "Retry With";
362 case 450: return "Blocked by Windows Parental Controls";
363 case 451: return "Unavailable For Legal Reasons";
364 case 456: return "Unrecoverable Error";
365 case 499: return "client has closed connection";
366 case 500: return "Internal Server Error";
367 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200368 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200369 case 503: return "Service Unavailable";
370 case 504: return "Gateway Time-out";
371 case 505: return "HTTP Version not supported";
372 case 506: return "Variant also negociate";
373 case 507: return "Insufficient storage";
374 case 508: return "Loop detected";
375 case 509: return "Bandwidth Limit Exceeded";
376 case 510: return "Not extended";
377 case 511: return "Network authentication required";
378 case 520: return "Web server is returning an unknown error";
379 default:
380 switch (status) {
381 case 100 ... 199: return "Informational";
382 case 200 ... 299: return "Success";
383 case 300 ... 399: return "Redirection";
384 case 400 ... 499: return "Client Error";
385 case 500 ... 599: return "Server Error";
386 default: return "Other";
387 }
388 }
389}
390
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200391/* This function returns HTTP_ERR_<num> (enum) matching http status code.
392 * Returned value should match codes from http_err_codes.
393 */
394static const int http_get_status_idx(unsigned int status)
395{
396 switch (status) {
397 case 200: return HTTP_ERR_200;
398 case 400: return HTTP_ERR_400;
399 case 403: return HTTP_ERR_403;
400 case 405: return HTTP_ERR_405;
401 case 408: return HTTP_ERR_408;
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200402 case 421: return HTTP_ERR_421;
Olivier Houchard51a76d82017-10-02 16:12:07 +0200403 case 425: return HTTP_ERR_425;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200404 case 429: return HTTP_ERR_429;
405 case 500: return HTTP_ERR_500;
406 case 502: return HTTP_ERR_502;
407 case 503: return HTTP_ERR_503;
408 case 504: return HTTP_ERR_504;
409 default: return HTTP_ERR_500;
410 }
411}
412
Willy Tarreau80587432006-12-24 17:47:20 +0100413void init_proto_http()
414{
Willy Tarreau42250582007-04-01 01:30:43 +0200415 int i;
416 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100417 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200418
Willy Tarreau80587432006-12-24 17:47:20 +0100419 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
420 if (!http_err_msgs[msg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100421 ha_alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
Willy Tarreau80587432006-12-24 17:47:20 +0100422 abort();
423 }
424
425 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
426 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
427 }
Willy Tarreau42250582007-04-01 01:30:43 +0200428
429 /* initialize the log header encoding map : '{|}"#' should be encoded with
430 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
431 * URL encoding only requires '"', '#' to be encoded as well as non-
432 * printable characters above.
433 */
434 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
435 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100436 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200437 for (i = 0; i < 32; i++) {
438 FD_SET(i, hdr_encode_map);
439 FD_SET(i, url_encode_map);
440 }
441 for (i = 127; i < 256; i++) {
442 FD_SET(i, hdr_encode_map);
443 FD_SET(i, url_encode_map);
444 }
445
446 tmp = "\"#{|}";
447 while (*tmp) {
448 FD_SET(*tmp, hdr_encode_map);
449 tmp++;
450 }
451
452 tmp = "\"#";
453 while (*tmp) {
454 FD_SET(*tmp, url_encode_map);
455 tmp++;
456 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200457
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100458 /* initialize the http header encoding map. The draft httpbis define the
459 * header content as:
460 *
461 * HTTP-message = start-line
462 * *( header-field CRLF )
463 * CRLF
464 * [ message-body ]
465 * header-field = field-name ":" OWS field-value OWS
466 * field-value = *( field-content / obs-fold )
467 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
468 * obs-fold = CRLF 1*( SP / HTAB )
469 * field-vchar = VCHAR / obs-text
470 * VCHAR = %x21-7E
471 * obs-text = %x80-FF
472 *
473 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
474 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
475 * "obs-fold" is volontary forgotten because haproxy remove this.
476 */
477 memset(http_encode_map, 0, sizeof(http_encode_map));
478 for (i = 0x00; i <= 0x08; i++)
479 FD_SET(i, http_encode_map);
480 for (i = 0x0a; i <= 0x1f; i++)
481 FD_SET(i, http_encode_map);
482 FD_SET(0x7f, http_encode_map);
483
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200484 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100485 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
486 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100487}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200488
Willy Tarreau53b6c742006-12-17 13:37:46 +0100489/*
490 * We have 26 list of methods (1 per first letter), each of which can have
491 * up to 3 entries (2 valid, 1 null).
492 */
493struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100494 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100495 int len;
496 const char text[8];
497};
498
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100499const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100500 ['C' - 'A'] = {
501 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
502 },
503 ['D' - 'A'] = {
504 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
505 },
506 ['G' - 'A'] = {
507 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
508 },
509 ['H' - 'A'] = {
510 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
511 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200512 ['O' - 'A'] = {
513 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
514 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100515 ['P' - 'A'] = {
516 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
517 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
518 },
519 ['T' - 'A'] = {
520 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
521 },
522 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200523 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100524 */
525};
526
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100527const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100528 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
529 [HTTP_METH_GET] = { "GET", 3 },
530 [HTTP_METH_HEAD] = { "HEAD", 4 },
531 [HTTP_METH_POST] = { "POST", 4 },
532 [HTTP_METH_PUT] = { "PUT", 3 },
533 [HTTP_METH_DELETE] = { "DELETE", 6 },
534 [HTTP_METH_TRACE] = { "TRACE", 5 },
535 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
536};
537
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100538/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100539 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200540 * the last CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100541 * The header is also automatically added to the index <hdr_idx>, and the end
542 * of headers is automatically adjusted. The number of bytes added is returned
543 * on success, otherwise <0 is returned indicating an error.
544 */
Willy Tarreau4d893d42018-07-12 15:43:32 +0200545static inline int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100546{
Willy Tarreau4d893d42018-07-12 15:43:32 +0200547 return http_header_add_tail2(msg, hdr_idx, text, strlen(text));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100548}
549
550/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100551 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200552 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100553 * The header is also automatically added to the index <hdr_idx>, and the end
554 * of headers is automatically adjusted. The number of bytes added is returned
555 * on success, otherwise <0 is returned indicating an error.
556 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100557int http_header_add_tail2(struct http_msg *msg,
558 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100559{
560 int bytes;
561
Willy Tarreau4d893d42018-07-12 15:43:32 +0200562 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100563 if (!bytes)
564 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100565 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100566 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
567}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200568
569/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100570 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
571 * If so, returns the position of the first non-space character relative to
572 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
573 * to return a pointer to the place after the first space. Returns 0 if the
574 * header name does not match. Checks are case-insensitive.
575 */
576int http_header_match2(const char *hdr, const char *end,
577 const char *name, int len)
578{
579 const char *val;
580
581 if (hdr + len >= end)
582 return 0;
583 if (hdr[len] != ':')
584 return 0;
585 if (strncasecmp(hdr, name, len) != 0)
586 return 0;
587 val = hdr + len + 1;
588 while (val < end && HTTP_IS_SPHT(*val))
589 val++;
590 if ((val >= end) && (len + 2 <= end - hdr))
591 return len + 2; /* we may replace starting from second space */
592 return val - hdr;
593}
594
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200595/* Find the first or next occurrence of header <name> in message buffer <sol>
596 * using headers index <idx>, and return it in the <ctx> structure. This
597 * structure holds everything necessary to use the header and find next
598 * occurrence. If its <idx> member is 0, the header is searched from the
599 * beginning. Otherwise, the next occurrence is returned. The function returns
600 * 1 when it finds a value, and 0 when there is no more. It is very similar to
601 * http_find_header2() except that it is designed to work with full-line headers
602 * whose comma is not a delimiter but is part of the syntax. As a special case,
603 * if ctx->val is NULL when searching for a new values of a header, the current
604 * header is rescanned. This allows rescanning after a header deletion.
605 */
606int http_find_full_header2(const char *name, int len,
607 char *sol, struct hdr_idx *idx,
608 struct hdr_ctx *ctx)
609{
610 char *eol, *sov;
611 int cur_idx, old_idx;
612
613 cur_idx = ctx->idx;
614 if (cur_idx) {
615 /* We have previously returned a header, let's search another one */
616 sol = ctx->line;
617 eol = sol + idx->v[cur_idx].len;
618 goto next_hdr;
619 }
620
621 /* first request for this header */
622 sol += hdr_idx_first_pos(idx);
623 old_idx = 0;
624 cur_idx = hdr_idx_first_idx(idx);
625 while (cur_idx) {
626 eol = sol + idx->v[cur_idx].len;
627
628 if (len == 0) {
629 /* No argument was passed, we want any header.
630 * To achieve this, we simply build a fake request. */
631 while (sol + len < eol && sol[len] != ':')
632 len++;
633 name = sol;
634 }
635
636 if ((len < eol - sol) &&
637 (sol[len] == ':') &&
638 (strncasecmp(sol, name, len) == 0)) {
639 ctx->del = len;
640 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100641 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200642 sov++;
643
644 ctx->line = sol;
645 ctx->prev = old_idx;
646 ctx->idx = cur_idx;
647 ctx->val = sov - sol;
648 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100649 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200650 eol--;
651 ctx->tws++;
652 }
653 ctx->vlen = eol - sov;
654 return 1;
655 }
656 next_hdr:
657 sol = eol + idx->v[cur_idx].cr + 1;
658 old_idx = cur_idx;
659 cur_idx = idx->v[cur_idx].next;
660 }
661 return 0;
662}
663
Willy Tarreauc90dc232015-02-20 13:51:36 +0100664/* Find the first or next header field in message buffer <sol> using headers
665 * index <idx>, and return it in the <ctx> structure. This structure holds
666 * everything necessary to use the header and find next occurrence. If its
667 * <idx> member is 0, the first header is retrieved. Otherwise, the next
668 * occurrence is returned. The function returns 1 when it finds a value, and
669 * 0 when there is no more. It is equivalent to http_find_full_header2() with
670 * no header name.
671 */
672int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
673{
674 char *eol, *sov;
675 int cur_idx, old_idx;
676 int len;
677
678 cur_idx = ctx->idx;
679 if (cur_idx) {
680 /* We have previously returned a header, let's search another one */
681 sol = ctx->line;
682 eol = sol + idx->v[cur_idx].len;
683 goto next_hdr;
684 }
685
686 /* first request for this header */
687 sol += hdr_idx_first_pos(idx);
688 old_idx = 0;
689 cur_idx = hdr_idx_first_idx(idx);
690 while (cur_idx) {
691 eol = sol + idx->v[cur_idx].len;
692
693 len = 0;
694 while (1) {
695 if (len >= eol - sol)
696 goto next_hdr;
697 if (sol[len] == ':')
698 break;
699 len++;
700 }
701
702 ctx->del = len;
703 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100704 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100705 sov++;
706
707 ctx->line = sol;
708 ctx->prev = old_idx;
709 ctx->idx = cur_idx;
710 ctx->val = sov - sol;
711 ctx->tws = 0;
712
Willy Tarreau2235b262016-11-05 15:50:20 +0100713 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100714 eol--;
715 ctx->tws++;
716 }
717 ctx->vlen = eol - sov;
718 return 1;
719
720 next_hdr:
721 sol = eol + idx->v[cur_idx].cr + 1;
722 old_idx = cur_idx;
723 cur_idx = idx->v[cur_idx].next;
724 }
725 return 0;
726}
727
Lukas Tribus23953682017-04-28 13:24:30 +0000728/* Find the end of the header value contained between <s> and <e>. See RFC7230,
729 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100730 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200731 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100732char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200733{
734 int quoted, qdpair;
735
736 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100737
738#if defined(__x86_64__) || \
739 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
740 defined(__ARM_ARCH_7A__)
741 /* speedup: skip everything not a comma nor a double quote */
742 for (; s <= e - sizeof(int); s += sizeof(int)) {
743 unsigned int c = *(int *)s; // comma
744 unsigned int q = c; // quote
745
746 c ^= 0x2c2c2c2c; // contains one zero on a comma
747 q ^= 0x22222222; // contains one zero on a quote
748
749 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
750 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
751
752 if ((c | q) & 0x80808080)
753 break; // found a comma or a quote
754 }
755#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200756 for (; s < e; s++) {
757 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200758 else if (quoted) {
759 if (*s == '\\') qdpair = 1;
760 else if (*s == '"') quoted = 0;
761 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200762 else if (*s == '"') quoted = 1;
763 else if (*s == ',') return s;
764 }
765 return s;
766}
767
768/* Find the first or next occurrence of header <name> in message buffer <sol>
769 * using headers index <idx>, and return it in the <ctx> structure. This
770 * structure holds everything necessary to use the header and find next
771 * occurrence. If its <idx> member is 0, the header is searched from the
772 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100773 * 1 when it finds a value, and 0 when there is no more. It is designed to work
774 * with headers defined as comma-separated lists. As a special case, if ctx->val
775 * is NULL when searching for a new values of a header, the current header is
776 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200777 */
778int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100779 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200780 struct hdr_ctx *ctx)
781{
Willy Tarreau68085d82010-01-18 14:54:04 +0100782 char *eol, *sov;
783 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200784
Willy Tarreau68085d82010-01-18 14:54:04 +0100785 cur_idx = ctx->idx;
786 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200787 /* We have previously returned a value, let's search
788 * another one on the same line.
789 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200790 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200791 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100792 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200793 eol = sol + idx->v[cur_idx].len;
794
795 if (sov >= eol)
796 /* no more values in this header */
797 goto next_hdr;
798
Willy Tarreau68085d82010-01-18 14:54:04 +0100799 /* values remaining for this header, skip the comma but save it
800 * for later use (eg: for header deletion).
801 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200802 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100803 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200804 sov++;
805
806 goto return_hdr;
807 }
808
809 /* first request for this header */
810 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100811 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200812 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200813 while (cur_idx) {
814 eol = sol + idx->v[cur_idx].len;
815
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200816 if (len == 0) {
817 /* No argument was passed, we want any header.
818 * To achieve this, we simply build a fake request. */
819 while (sol + len < eol && sol[len] != ':')
820 len++;
821 name = sol;
822 }
823
Willy Tarreau33a7e692007-06-10 19:45:56 +0200824 if ((len < eol - sol) &&
825 (sol[len] == ':') &&
826 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100827 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200828 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100829 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200830 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100831
Willy Tarreau33a7e692007-06-10 19:45:56 +0200832 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100833 ctx->prev = old_idx;
834 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200835 ctx->idx = cur_idx;
836 ctx->val = sov - sol;
837
838 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200839 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100840 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200841 eol--;
842 ctx->tws++;
843 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200844 ctx->vlen = eol - sov;
845 return 1;
846 }
847 next_hdr:
848 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100849 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200850 cur_idx = idx->v[cur_idx].next;
851 }
852 return 0;
853}
854
855int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100856 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200857 struct hdr_ctx *ctx)
858{
859 return http_find_header2(name, strlen(name), sol, idx, ctx);
860}
861
Willy Tarreau68085d82010-01-18 14:54:04 +0100862/* Remove one value of a header. This only works on a <ctx> returned by one of
863 * the http_find_header functions. The value is removed, as well as surrounding
864 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100865 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100866 * message <msg>. The new index is returned. If it is zero, it means there is
867 * no more header, so any processing may stop. The ctx is always left in a form
868 * that can be handled by http_find_header2() to find next occurrence.
869 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100870int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100871{
872 int cur_idx = ctx->idx;
873 char *sol = ctx->line;
874 struct hdr_idx_elem *hdr;
875 int delta, skip_comma;
876
877 if (!cur_idx)
878 return 0;
879
880 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200881 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100882 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200883 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100884 http_msg_move_end(msg, delta);
885 idx->used--;
886 hdr->len = 0; /* unused entry */
887 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100888 if (idx->tail == ctx->idx)
889 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100890 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100891 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100892 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200893 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100894 return ctx->idx;
895 }
896
897 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200898 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
899 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100900 */
901
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200902 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200903 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200904 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100905 NULL, 0);
906 hdr->len += delta;
907 http_msg_move_end(msg, delta);
908 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200909 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100910 return ctx->idx;
911}
912
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100913/* This function handles a server error at the stream interface level. The
914 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100915 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100916 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100917 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200919static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +0100920 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200921{
Willy Tarreau2019f952017-03-14 11:07:31 +0100922 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100923 channel_auto_read(si_oc(si));
924 channel_abort(si_oc(si));
925 channel_auto_close(si_oc(si));
926 channel_erase(si_oc(si));
927 channel_auto_close(si_ic(si));
928 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200929 if (msg)
Willy Tarreau06d80a92017-10-19 14:32:15 +0200930 co_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200931 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200932 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200933 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200934 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935}
936
Willy Tarreau87b09662015-04-03 00:22:06 +0200937/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100938 * and message.
939 */
940
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200941struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100942{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200943 const int msgnum = http_get_status_idx(s->txn->status);
944
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200945 if (s->be->errmsg[msgnum].str)
946 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200947 else if (strm_fe(s)->errmsg[msgnum].str)
948 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100949 else
950 return &http_err_chunks[msgnum];
951}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200952
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100953void
954http_reply_and_close(struct stream *s, short status, struct chunk *msg)
955{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200956 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100957 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100958 stream_int_retnclose(&s->si[0], msg);
959}
960
Willy Tarreau53b6c742006-12-17 13:37:46 +0100961/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200962 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
963 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100964 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100965enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100966{
967 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100968 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100969
970 m = ((unsigned)*str - 'A');
971
972 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100973 for (h = http_methods[m]; h->len > 0; h++) {
974 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100975 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100976 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100977 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100978 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100979 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200980 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100981}
982
Willy Tarreau21d2af32008-02-14 20:25:24 +0100983/* Parse the URI from the given transaction (which is assumed to be in request
984 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
985 * It is returned otherwise.
986 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200987char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100988{
989 char *ptr, *end;
990
Willy Tarreauf37954d2018-06-15 18:31:02 +0200991 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100992 end = ptr + txn->req.sl.rq.u_l;
993
994 if (ptr >= end)
995 return NULL;
996
Lukas Tribus23953682017-04-28 13:24:30 +0000997 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +0100998 * Request-URI = "*" | absuri | abspath | authority
999 */
1000
1001 if (*ptr == '*')
1002 return NULL;
1003
1004 if (isalpha((unsigned char)*ptr)) {
1005 /* this is a scheme as described by RFC3986, par. 3.1 */
1006 ptr++;
1007 while (ptr < end &&
1008 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1009 ptr++;
1010 /* skip '://' */
1011 if (ptr == end || *ptr++ != ':')
1012 return NULL;
1013 if (ptr == end || *ptr++ != '/')
1014 return NULL;
1015 if (ptr == end || *ptr++ != '/')
1016 return NULL;
1017 }
1018 /* skip [user[:passwd]@]host[:[port]] */
1019
1020 while (ptr < end && *ptr != '/')
1021 ptr++;
1022
1023 if (ptr == end)
1024 return NULL;
1025
1026 /* OK, we got the '/' ! */
1027 return ptr;
1028}
1029
William Lallemand65ad6e12014-01-31 15:08:02 +01001030/* Parse the URI from the given string and look for the "/" beginning the PATH.
1031 * If not found, return NULL. It is returned otherwise.
1032 */
1033static char *
1034http_get_path_from_string(char *str)
1035{
1036 char *ptr = str;
1037
1038 /* RFC2616, par. 5.1.2 :
1039 * Request-URI = "*" | absuri | abspath | authority
1040 */
1041
1042 if (*ptr == '*')
1043 return NULL;
1044
1045 if (isalpha((unsigned char)*ptr)) {
1046 /* this is a scheme as described by RFC3986, par. 3.1 */
1047 ptr++;
1048 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1049 ptr++;
1050 /* skip '://' */
1051 if (*ptr == '\0' || *ptr++ != ':')
1052 return NULL;
1053 if (*ptr == '\0' || *ptr++ != '/')
1054 return NULL;
1055 if (*ptr == '\0' || *ptr++ != '/')
1056 return NULL;
1057 }
1058 /* skip [user[:passwd]@]host[:[port]] */
1059
1060 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1061 ptr++;
1062
1063 if (*ptr == '\0' || *ptr == ' ')
1064 return NULL;
1065
1066 /* OK, we got the '/' ! */
1067 return ptr;
1068}
1069
Willy Tarreau71241ab2012-12-27 11:30:54 +01001070/* Returns a 302 for a redirectable request that reaches a server working in
1071 * in redirect mode. This may only be called just after the stream interface
1072 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1073 * follow normal proxy processing. NOTE: this function is designed to support
1074 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001075 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001076void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001077{
1078 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001079 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001080 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001081 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001082
1083 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001084 trash.len = strlen(HTTP_302);
1085 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001087 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001088
Willy Tarreauefb453c2008-10-26 20:49:47 +01001089 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001090 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001091 return;
1092
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001093 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001094 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001095 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1096 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001097 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001098
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001099 /* 3: add the request URI. Since it was already forwarded, we need
1100 * to temporarily rewind the buffer.
1101 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001102 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001103 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001104
Willy Tarreauefb453c2008-10-26 20:49:47 +01001105 path = http_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02001106 len = b_dist(s->req.buf, path, c_ptr(&s->req, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001107
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001108 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001109
Willy Tarreauefb453c2008-10-26 20:49:47 +01001110 if (!path)
1111 return;
1112
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001113 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001114 return;
1115
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001116 memcpy(trash.str + trash.len, path, len);
1117 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001118
1119 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001120 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1121 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001122 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001123 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1124 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001125 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001126
1127 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001128 si_shutr(si);
1129 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001130 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001131 si->state = SI_ST_CLO;
1132
1133 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001134 txn->status = 302;
1135 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001136
1137 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001138 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001139 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001140}
1141
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001142/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001143 * that the server side is closed. Note that err_type is actually a
1144 * bitmask, where almost only aborts may be cumulated with other
1145 * values. We consider that aborted operations are more important
1146 * than timeouts or errors due to the fact that nobody else in the
1147 * logs might explain incomplete retries. All others should avoid
1148 * being cumulated. It should normally not be possible to have multiple
1149 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001150 * Note that connection errors appearing on the second request of a keep-alive
1151 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001152 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001153void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001154{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001155 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001156
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001157 /* set s->txn->status for http_error_message(s) */
1158 s->txn->status = 503;
1159
Willy Tarreauefb453c2008-10-26 20:49:47 +01001160 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001161 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001162 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001163 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001164 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001165 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001166 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001167 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001168 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001169 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001170 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001171 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001172 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001173 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001174 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001175 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001176 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001177 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001178 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001179 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001180 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001181 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001182 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001183 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001184 http_error_message(s));
1185 else { /* SI_ET_CONN_OTHER and others */
1186 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001187 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001188 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001189 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001190}
1191
Willy Tarreau42250582007-04-01 01:30:43 +02001192extern const char sess_term_cond[8];
1193extern const char sess_fin_state[8];
1194extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +01001195struct pool_head *pool_head_http_txn;
1196struct pool_head *pool_head_requri;
1197struct pool_head *pool_head_capture = NULL;
1198struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199
Willy Tarreau117f59e2007-03-04 18:17:17 +01001200/*
1201 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001202 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001203 */
1204void capture_headers(char *som, struct hdr_idx *idx,
1205 char **cap, struct cap_hdr *cap_hdr)
1206{
1207 char *eol, *sol, *col, *sov;
1208 int cur_idx;
1209 struct cap_hdr *h;
1210 int len;
1211
1212 sol = som + hdr_idx_first_pos(idx);
1213 cur_idx = hdr_idx_first_idx(idx);
1214
1215 while (cur_idx) {
1216 eol = sol + idx->v[cur_idx].len;
1217
1218 col = sol;
1219 while (col < eol && *col != ':')
1220 col++;
1221
1222 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001223 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001224 sov++;
1225
1226 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001227 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001228 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1229 if (cap[h->index] == NULL)
1230 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +01001231 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001232
1233 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001234 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +01001235 continue;
1236 }
1237
1238 len = eol - sov;
1239 if (len > h->len)
1240 len = h->len;
1241
1242 memcpy(cap[h->index], sov, len);
1243 cap[h->index][len]=0;
1244 }
1245 }
1246 sol = eol + idx->v[cur_idx].cr + 1;
1247 cur_idx = idx->v[cur_idx].next;
1248 }
1249}
1250
Willy Tarreaubaaee002006-06-26 02:48:02 +02001251/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001252 * Returns the data from Authorization header. Function may be called more
1253 * than once so data is stored in txn->auth_data. When no header is found
1254 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001255 * searching again for something we are unable to find anyway. However, if
1256 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001257 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001258 */
1259
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001260int
Willy Tarreau87b09662015-04-03 00:22:06 +02001261get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001262{
1263
Willy Tarreaueee5b512015-04-03 23:46:31 +02001264 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001265 struct chunk auth_method;
1266 struct hdr_ctx ctx;
1267 char *h, *p;
1268 int len;
1269
1270#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001271 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001272#endif
1273
1274 if (txn->auth.method == HTTP_AUTH_WRONG)
1275 return 0;
1276
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001277 txn->auth.method = HTTP_AUTH_WRONG;
1278
1279 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001280
1281 if (txn->flags & TX_USE_PX_CONN) {
1282 h = "Proxy-Authorization";
1283 len = strlen(h);
1284 } else {
1285 h = "Authorization";
1286 len = strlen(h);
1287 }
1288
Willy Tarreauf37954d2018-06-15 18:31:02 +02001289 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001290 return 0;
1291
1292 h = ctx.line + ctx.val;
1293
1294 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001295 len = p - h;
1296 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001297 return 0;
1298
David Carlier7365f7d2016-04-04 11:54:42 +01001299 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1300 return 0;
1301
Willy Tarreau5c557d12016-03-13 08:17:02 +01001302 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001303
1304 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
Christopher Faulet6988f672017-07-27 15:18:52 +02001305 struct chunk *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001306
1307 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Christopher Faulet6988f672017-07-27 15:18:52 +02001308 http_auth->str, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001309
1310 if (len < 0)
1311 return 0;
1312
1313
Christopher Faulet6988f672017-07-27 15:18:52 +02001314 http_auth->str[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001315
Christopher Faulet6988f672017-07-27 15:18:52 +02001316 p = strchr(http_auth->str, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001317
1318 if (!p)
1319 return 0;
1320
Christopher Faulet6988f672017-07-27 15:18:52 +02001321 txn->auth.user = http_auth->str;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001322 *p = '\0';
1323 txn->auth.pass = p+1;
1324
1325 txn->auth.method = HTTP_AUTH_BASIC;
1326 return 1;
1327 }
1328
1329 return 0;
1330}
1331
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001332
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001333/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1334 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1335 * nothing is done and 1 is returned.
1336 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001337static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001338{
1339 int delta;
1340 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001341 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001342
1343 if (msg->sl.rq.v_l != 0)
1344 return 1;
1345
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001346 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1347 if (txn->meth != HTTP_METH_GET)
1348 return 0;
1349
Willy Tarreauf37954d2018-06-15 18:31:02 +02001350 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001351
1352 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001353 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1354 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001355 }
1356 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001357 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001358 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001359 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001360 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001361 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001362 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001363 NULL, NULL);
1364 if (unlikely(!cur_end))
1365 return 0;
1366
1367 /* we have a full HTTP/1.0 request now and we know that
1368 * we have either a CR or an LF at <ptr>.
1369 */
1370 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1371 return 1;
1372}
1373
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001374/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001375 * and "keep-alive" values. If we already know that some headers may safely
1376 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001377 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1378 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001379 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001380 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1381 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1382 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001383 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001384 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001385void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001386{
Willy Tarreau5b154472009-12-21 20:11:07 +01001387 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001388 const char *hdr_val = "Connection";
1389 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001390
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001391 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001392 return;
1393
Willy Tarreau88d349d2010-01-25 12:15:43 +01001394 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1395 hdr_val = "Proxy-Connection";
1396 hdr_len = 16;
1397 }
1398
Willy Tarreau5b154472009-12-21 20:11:07 +01001399 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001400 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +02001401 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001402 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1403 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001404 if (to_del & 2)
1405 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001406 else
1407 txn->flags |= TX_CON_KAL_SET;
1408 }
1409 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1410 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001411 if (to_del & 1)
1412 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001413 else
1414 txn->flags |= TX_CON_CLO_SET;
1415 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001416 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1417 txn->flags |= TX_HDR_CONN_UPG;
1418 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001419 }
1420
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001421 txn->flags |= TX_HDR_CONN_PRS;
1422 return;
1423}
Willy Tarreau5b154472009-12-21 20:11:07 +01001424
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001425/* Apply desired changes on the Connection: header. Values may be removed and/or
1426 * added depending on the <wanted> flags, which are exclusively composed of
1427 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1428 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1429 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001430void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001431{
1432 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001433 const char *hdr_val = "Connection";
1434 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001435
1436 ctx.idx = 0;
1437
Willy Tarreau88d349d2010-01-25 12:15:43 +01001438
1439 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1440 hdr_val = "Proxy-Connection";
1441 hdr_len = 16;
1442 }
1443
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001444 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +02001445 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001446 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1447 if (wanted & TX_CON_KAL_SET)
1448 txn->flags |= TX_CON_KAL_SET;
1449 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001450 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001451 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001452 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1453 if (wanted & TX_CON_CLO_SET)
1454 txn->flags |= TX_CON_CLO_SET;
1455 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001456 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001457 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001458 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001459
1460 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1461 return;
1462
1463 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1464 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001465 hdr_val = "Connection: close";
1466 hdr_len = 17;
1467 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1468 hdr_val = "Proxy-Connection: close";
1469 hdr_len = 23;
1470 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001471 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001472 }
1473
1474 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1475 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001476 hdr_val = "Connection: keep-alive";
1477 hdr_len = 22;
1478 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1479 hdr_val = "Proxy-Connection: keep-alive";
1480 hdr_len = 28;
1481 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001482 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001483 }
1484 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001485}
1486
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001487/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1488 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1489 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1490 * Unparsable qvalues return 1000 as "q=1.000".
1491 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001492int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001493{
1494 int q = 1000;
1495
Willy Tarreau506c69a2014-07-08 00:59:48 +02001496 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001497 goto out;
1498 q = (*qvalue++ - '0') * 1000;
1499
1500 if (*qvalue++ != '.')
1501 goto out;
1502
Willy Tarreau506c69a2014-07-08 00:59:48 +02001503 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001504 goto out;
1505 q += (*qvalue++ - '0') * 100;
1506
Willy Tarreau506c69a2014-07-08 00:59:48 +02001507 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001508 goto out;
1509 q += (*qvalue++ - '0') * 10;
1510
Willy Tarreau506c69a2014-07-08 00:59:48 +02001511 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001512 goto out;
1513 q += (*qvalue++ - '0') * 1;
1514 out:
1515 if (q > 1000)
1516 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001517 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001518 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001519 return q;
1520}
William Lallemand82fe75c2012-10-23 10:25:10 +02001521
Willy Tarreau87b09662015-04-03 00:22:06 +02001522void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001523{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001524 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001525 int tmp = TX_CON_WANT_KAL;
1526
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001527 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1528 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001529 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1530 tmp = TX_CON_WANT_TUN;
1531
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001532 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001533 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1534 tmp = TX_CON_WANT_TUN;
1535 }
1536
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001537 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001538 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1539 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001540 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001541 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1542 tmp = TX_CON_WANT_CLO;
1543 else
1544 tmp = TX_CON_WANT_SCL;
1545 }
1546
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001547 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001548 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1549 tmp = TX_CON_WANT_CLO;
1550
1551 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1552 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1553
1554 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1555 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1556 /* parse the Connection header and possibly clean it */
1557 int to_del = 0;
1558 if ((msg->flags & HTTP_MSGF_VER_11) ||
1559 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001560 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001561 to_del |= 2; /* remove "keep-alive" */
1562 if (!(msg->flags & HTTP_MSGF_VER_11))
1563 to_del |= 1; /* remove "close" */
1564 http_parse_connection_header(txn, msg, to_del);
1565 }
1566
1567 /* check if client or config asks for explicit close in KAL/SCL */
1568 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1569 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1570 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1571 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1572 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001573 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001574 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1575}
William Lallemand82fe75c2012-10-23 10:25:10 +02001576
Willy Tarreaud787e662009-07-07 10:14:51 +02001577/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1578 * processing can continue on next analysers, or zero if it either needs more
1579 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001580 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001581 * when it has nothing left to do, and may remove any analyser when it wants to
1582 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001584int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585{
Willy Tarreau59234e92008-11-30 23:51:27 +01001586 /*
1587 * We will parse the partial (or complete) lines.
1588 * We will check the request syntax, and also join multi-line
1589 * headers. An index of all the lines will be elaborated while
1590 * parsing.
1591 *
1592 * For the parsing, we use a 28 states FSM.
1593 *
1594 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02001595 * ci_head(req) = beginning of request
1596 * ci_head(req) + msg->eoh = end of processed headers / start of current one
1597 * ci_tail(req) = end of input data
1598 * msg->eol = end of current header or line (LF or CRLF)
1599 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001600 *
1601 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001602 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001603 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1604 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001605 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001606
Willy Tarreau59234e92008-11-30 23:51:27 +01001607 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001608 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001609 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001610 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001611 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001612
Willy Tarreau87b09662015-04-03 00:22:06 +02001613 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01001614 now_ms, __FUNCTION__,
1615 s,
1616 req,
1617 req->rex, req->wex,
1618 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001619 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01001620 req->analysers);
1621
Willy Tarreau52a0c602009-08-16 22:45:38 +02001622 /* we're speaking HTTP here, so let's speak HTTP to the client */
1623 s->srv_error = http_return_srv_error;
1624
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001625 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001626 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001627 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1628
Willy Tarreau83e3af02009-12-28 17:39:57 +01001629 /* There's a protected area at the end of the buffer for rewriting
1630 * purposes. We don't want to start to parse the request if the
1631 * protected area is affected, because we may have to move processed
1632 * data later, which is much more complicated.
1633 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001634 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02001635 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001636 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001637 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001638 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001639 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001640 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001641 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001642 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001643 return 0;
1644 }
Willy Tarreau188e2302018-06-15 11:11:53 +02001645 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02001646 ci_tail(req) > b_wrap(req->buf) - global.tune.maxrewrite))
Willy Tarreaufd8d42f2018-07-12 10:57:15 +02001647 channel_slow_realign(req, trash.str);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001648 }
1649
Willy Tarreauf37954d2018-06-15 18:31:02 +02001650 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001651 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001652 }
1653
Willy Tarreau59234e92008-11-30 23:51:27 +01001654 /* 1: we might have to print this header in debug mode */
1655 if (unlikely((global.mode & MODE_DEBUG) &&
1656 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001657 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001658 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001659
Willy Tarreauf37954d2018-06-15 18:31:02 +02001660 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +02001661 /* this is a bit complex : in case of error on the request line,
1662 * we know that rq.l is still zero, so we display only the part
1663 * up to the end of the line (truncated by debug_hdr).
1664 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001665 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +01001666 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001667
Willy Tarreau59234e92008-11-30 23:51:27 +01001668 sol += hdr_idx_first_pos(&txn->hdr_idx);
1669 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001670
Willy Tarreau59234e92008-11-30 23:51:27 +01001671 while (cur_idx) {
1672 eol = sol + txn->hdr_idx.v[cur_idx].len;
1673 debug_hdr("clihdr", s, sol, eol);
1674 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1675 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001677 }
1678
Willy Tarreau58f10d72006-12-04 02:26:12 +01001679
Willy Tarreau59234e92008-11-30 23:51:27 +01001680 /*
1681 * Now we quickly check if we have found a full valid request.
1682 * If not so, we check the FD and buffer states before leaving.
1683 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001684 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001685 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001686 * on a keep-alive stream, if we encounter and error, close, t/o,
1687 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001688 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001689 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001690 * Last, we may increase some tracked counters' http request errors on
1691 * the cases that are deliberately the client's fault. For instance,
1692 * a timeout or connection reset is not counted as an error. However
1693 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001694 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001695
Willy Tarreau655dce92009-11-08 13:10:58 +01001696 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001697 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001698 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001699 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001700 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001701 stream_inc_http_req_ctr(s);
1702 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001703 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001704 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001705 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001706
Willy Tarreau59234e92008-11-30 23:51:27 +01001707 /* 1: Since we are in header mode, if there's no space
1708 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001709 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001710 * must terminate it now.
1711 */
Willy Tarreau23752332018-06-15 14:54:53 +02001712 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001713 /* FIXME: check if URI is set and return Status
1714 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001715 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001716 stream_inc_http_req_ctr(s);
1717 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001718 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001719 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02001720 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +01001721 goto return_bad_req;
1722 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001723
Willy Tarreau59234e92008-11-30 23:51:27 +01001724 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001725 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001726 if (!(s->flags & SF_ERR_MASK))
1727 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001728
Willy Tarreaufcffa692010-01-10 14:21:19 +01001729 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001730 goto failed_keep_alive;
1731
Willy Tarreau0f228a02015-05-01 15:37:53 +02001732 if (sess->fe->options & PR_O_IGNORE_PRB)
1733 goto failed_keep_alive;
1734
Willy Tarreau59234e92008-11-30 23:51:27 +01001735 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001736 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001737 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001738 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001739 }
1740
Willy Tarreaudc979f22012-12-04 10:39:01 +01001741 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001742 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001743 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001744 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001745 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001746 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001747 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001748 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001749 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001750 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001751
Willy Tarreaue7dff022015-04-03 01:14:29 +02001752 if (!(s->flags & SF_FINST_MASK))
1753 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001754 return 0;
1755 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001756
Willy Tarreau59234e92008-11-30 23:51:27 +01001757 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001758 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001759 if (!(s->flags & SF_ERR_MASK))
1760 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001761
Willy Tarreaufcffa692010-01-10 14:21:19 +01001762 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001763 goto failed_keep_alive;
1764
Willy Tarreau0f228a02015-05-01 15:37:53 +02001765 if (sess->fe->options & PR_O_IGNORE_PRB)
1766 goto failed_keep_alive;
1767
Willy Tarreau59234e92008-11-30 23:51:27 +01001768 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001769 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001770 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001771 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001772 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001773 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001774 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001775 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001776 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001777 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001778
Willy Tarreau87b09662015-04-03 00:22:06 +02001779 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001780 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001781 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001782 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001783 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001784
Willy Tarreaue7dff022015-04-03 01:14:29 +02001785 if (!(s->flags & SF_FINST_MASK))
1786 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001787 return 0;
1788 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001789
Willy Tarreau59234e92008-11-30 23:51:27 +01001790 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001791 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001792 if (!(s->flags & SF_ERR_MASK))
1793 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001794
Willy Tarreaufcffa692010-01-10 14:21:19 +01001795 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001796 goto failed_keep_alive;
1797
Willy Tarreau0f228a02015-05-01 15:37:53 +02001798 if (sess->fe->options & PR_O_IGNORE_PRB)
1799 goto failed_keep_alive;
1800
Willy Tarreau4076a152009-04-02 15:18:36 +02001801 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001802 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001803 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001804 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001805 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001806 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001807 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001808 stream_inc_http_err_ctr(s);
1809 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001810 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001811 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001812 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001813 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001814
Willy Tarreaue7dff022015-04-03 01:14:29 +02001815 if (!(s->flags & SF_FINST_MASK))
1816 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001817 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001818 }
1819
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001820 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001821 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001822 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001823#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001824 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001825 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001826 /* We need more data, we have to re-enable quick-ack in case we
1827 * previously disabled it, otherwise we might cause the client
1828 * to delay next data.
1829 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001830 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001831 }
1832#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001833
Willy Tarreaufcffa692010-01-10 14:21:19 +01001834 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1835 /* If the client starts to talk, let's fall back to
1836 * request timeout processing.
1837 */
1838 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001839 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001840 }
1841
Willy Tarreau59234e92008-11-30 23:51:27 +01001842 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001843 if (!tick_isset(req->analyse_exp)) {
1844 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1845 (txn->flags & TX_WAIT_NEXT_RQ) &&
1846 tick_isset(s->be->timeout.httpka))
1847 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1848 else
1849 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1850 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001851
Willy Tarreau59234e92008-11-30 23:51:27 +01001852 /* we're not ready yet */
1853 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001854
1855 failed_keep_alive:
1856 /* Here we process low-level errors for keep-alive requests. In
1857 * short, if the request is not the first one and it experiences
1858 * a timeout, read error or shutdown, we just silently close so
1859 * that the client can try again.
1860 */
1861 txn->status = 0;
1862 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001863 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001864 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001865 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001866 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001867 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001868 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001869 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001870
Willy Tarreaud787e662009-07-07 10:14:51 +02001871 /* OK now we have a complete HTTP request with indexed headers. Let's
1872 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02001873 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001874 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001875 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001876 * byte after the last LF. msg->sov points to the first byte of data.
1877 * msg->eol cannot be trusted because it may have been left uninitialized
1878 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001879 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001880
Willy Tarreau87b09662015-04-03 00:22:06 +02001881 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001882 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001883
Willy Tarreaub16a5742010-01-10 14:46:16 +01001884 if (txn->flags & TX_WAIT_NEXT_RQ) {
1885 /* kill the pending keep-alive timeout */
1886 txn->flags &= ~TX_WAIT_NEXT_RQ;
1887 req->analyse_exp = TICK_ETERNITY;
1888 }
1889
1890
Willy Tarreaud787e662009-07-07 10:14:51 +02001891 /* Maybe we found in invalid header name while we were configured not
1892 * to block on that, so we have to capture it now.
1893 */
1894 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001895 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001896
Willy Tarreau59234e92008-11-30 23:51:27 +01001897 /*
1898 * 1: identify the method
1899 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001900 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001901
1902 /* we can make use of server redirect on GET and HEAD */
1903 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001904 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001905 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001906 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001907 /* PRI is reserved for the HTTP/2 preface */
1908 msg->err_pos = 0;
1909 goto return_bad_req;
1910 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001911
Willy Tarreau59234e92008-11-30 23:51:27 +01001912 /*
1913 * 2: check if the URI matches the monitor_uri.
1914 * We have to do this for every request which gets in, because
1915 * the monitor-uri is defined by the frontend.
1916 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001917 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1918 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001919 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001920 sess->fe->monitor_uri,
1921 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001922 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001923 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001924 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001925 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001926
Willy Tarreaue7dff022015-04-03 01:14:29 +02001927 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001928 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001929
Willy Tarreau59234e92008-11-30 23:51:27 +01001930 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001931 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001932 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001933
Willy Tarreau59234e92008-11-30 23:51:27 +01001934 ret = acl_pass(ret);
1935 if (cond->pol == ACL_COND_UNLESS)
1936 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001937
Willy Tarreau59234e92008-11-30 23:51:27 +01001938 if (ret) {
1939 /* we fail this request, let's return 503 service unavail */
1940 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001941 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001942 if (!(s->flags & SF_ERR_MASK))
1943 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001944 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001945 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001946 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001947
Willy Tarreau59234e92008-11-30 23:51:27 +01001948 /* nothing to fail, let's reply normaly */
1949 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001950 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001951 if (!(s->flags & SF_ERR_MASK))
1952 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001953 goto return_prx_cond;
1954 }
1955
1956 /*
1957 * 3: Maybe we have to copy the original REQURI for the logs ?
1958 * Note: we cannot log anymore if the request has been
1959 * classified as invalid.
1960 */
1961 if (unlikely(s->logs.logwait & LW_REQ)) {
1962 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001963 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001964 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001965
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001966 if (urilen >= global.tune.requri_len )
1967 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001968 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001969 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001970
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001971 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001972 s->do_log(s);
1973 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001974 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001975 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001976 }
Willy Tarreau06619262006-12-17 08:37:22 +01001977
Willy Tarreau91852eb2015-05-01 13:26:00 +02001978 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1979 * exactly one digit "." one digit. This check may be disabled using
1980 * option accept-invalid-http-request.
1981 */
1982 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1983 if (msg->sl.rq.v_l != 8) {
1984 msg->err_pos = msg->sl.rq.v;
1985 goto return_bad_req;
1986 }
1987
Willy Tarreauf37954d2018-06-15 18:31:02 +02001988 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1989 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1990 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1991 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001992 msg->err_pos = msg->sl.rq.v + 4;
1993 goto return_bad_req;
1994 }
1995 }
Willy Tarreau13317662015-05-01 13:47:08 +02001996 else {
1997 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1998 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1999 goto return_bad_req;
2000 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002001
Willy Tarreau5b154472009-12-21 20:11:07 +01002002 /* ... and check if the request is HTTP/1.1 or above */
2003 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02002004 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
2005 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
2006 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002007 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002008
2009 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002010 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002011
Willy Tarreau88d349d2010-01-25 12:15:43 +01002012 /* if the frontend has "option http-use-proxy-header", we'll check if
2013 * we have what looks like a proxied connection instead of a connection,
2014 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2015 * Note that this is *not* RFC-compliant, however browsers and proxies
2016 * happen to do that despite being non-standard :-(
2017 * We consider that a request not beginning with either '/' or '*' is
2018 * a proxied connection, which covers both "scheme://location" and
2019 * CONNECT ip:port.
2020 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002021 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02002022 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002023 txn->flags |= TX_USE_PX_CONN;
2024
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002025 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002026 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002027
Willy Tarreau59234e92008-11-30 23:51:27 +01002028 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002029 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02002030 capture_headers(ci_head(req), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002031 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002032
Willy Tarreau557f1992015-05-01 10:05:17 +02002033 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2034 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002035 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002036 * The length of a message body is determined by one of the following
2037 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002038 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002039 * 1. Any response to a HEAD request and any response with a 1xx
2040 * (Informational), 204 (No Content), or 304 (Not Modified) status
2041 * code is always terminated by the first empty line after the
2042 * header fields, regardless of the header fields present in the
2043 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002044 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002045 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2046 * the connection will become a tunnel immediately after the empty
2047 * line that concludes the header fields. A client MUST ignore any
2048 * Content-Length or Transfer-Encoding header fields received in
2049 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002050 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002051 * 3. If a Transfer-Encoding header field is present and the chunked
2052 * transfer coding (Section 4.1) is the final encoding, the message
2053 * body length is determined by reading and decoding the chunked
2054 * data until the transfer coding indicates the data is complete.
2055 *
2056 * If a Transfer-Encoding header field is present in a response and
2057 * the chunked transfer coding is not the final encoding, the
2058 * message body length is determined by reading the connection until
2059 * it is closed by the server. If a Transfer-Encoding header field
2060 * is present in a request and the chunked transfer coding is not
2061 * the final encoding, the message body length cannot be determined
2062 * reliably; the server MUST respond with the 400 (Bad Request)
2063 * status code and then close the connection.
2064 *
2065 * If a message is received with both a Transfer-Encoding and a
2066 * Content-Length header field, the Transfer-Encoding overrides the
2067 * Content-Length. Such a message might indicate an attempt to
2068 * perform request smuggling (Section 9.5) or response splitting
2069 * (Section 9.4) and ought to be handled as an error. A sender MUST
2070 * remove the received Content-Length field prior to forwarding such
2071 * a message downstream.
2072 *
2073 * 4. If a message is received without Transfer-Encoding and with
2074 * either multiple Content-Length header fields having differing
2075 * field-values or a single Content-Length header field having an
2076 * invalid value, then the message framing is invalid and the
2077 * recipient MUST treat it as an unrecoverable error. If this is a
2078 * request message, the server MUST respond with a 400 (Bad Request)
2079 * status code and then close the connection. If this is a response
2080 * message received by a proxy, the proxy MUST close the connection
2081 * to the server, discard the received response, and send a 502 (Bad
2082 * Gateway) response to the client. If this is a response message
2083 * received by a user agent, the user agent MUST close the
2084 * connection to the server and discard the received response.
2085 *
2086 * 5. If a valid Content-Length header field is present without
2087 * Transfer-Encoding, its decimal value defines the expected message
2088 * body length in octets. If the sender closes the connection or
2089 * the recipient times out before the indicated number of octets are
2090 * received, the recipient MUST consider the message to be
2091 * incomplete and close the connection.
2092 *
2093 * 6. If this is a request message and none of the above are true, then
2094 * the message body length is zero (no message body is present).
2095 *
2096 * 7. Otherwise, this is a response message without a declared message
2097 * body length, so the message body length is determined by the
2098 * number of octets received prior to the server closing the
2099 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002100 */
2101
Willy Tarreau32b47f42009-10-18 20:55:02 +02002102 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002103 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02002104 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002105 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002106 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002107 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002108 /* chunked not last, return badreq */
2109 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002110 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002111 }
2112
Willy Tarreau1c913912015-04-30 10:57:51 +02002113 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002114 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002115 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002116 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02002117 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2118 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02002119 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002120 signed long long cl;
2121
Willy Tarreauad14f752011-09-02 20:33:27 +02002122 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002123 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002124 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002125 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002126
Willy Tarreauad14f752011-09-02 20:33:27 +02002127 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002128 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002129 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002130 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002131
Willy Tarreauad14f752011-09-02 20:33:27 +02002132 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002133 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002134 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002135 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002136
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002137 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002138 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002139 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002140 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002141
Christopher Fauletbe821b92017-03-30 11:21:53 +02002142 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002143 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002144 }
2145
Willy Tarreau34dfc602015-05-01 10:09:49 +02002146 /* even bodyless requests have a known length */
2147 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002148
Willy Tarreau179085c2014-04-28 16:48:56 +02002149 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2150 * only change if both the request and the config reference something else.
2151 * Option httpclose by itself sets tunnel mode where headers are mangled.
2152 * However, if another mode is set, it will affect it (eg: server-close/
2153 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2154 * if FE and BE have the same settings (common). The method consists in
2155 * checking if options changed between the two calls (implying that either
2156 * one is non-null, or one of them is non-null and we are there for the first
2157 * time.
2158 */
2159 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002160 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002161 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002162
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002163 /* we may have to wait for the request's body */
2164 if ((s->be->options & PR_O_WREQ_BODY) &&
2165 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2166 req->analysers |= AN_REQ_HTTP_BODY;
2167
Willy Tarreau83ece462017-12-21 15:13:09 +01002168 /*
2169 * RFC7234#4:
2170 * A cache MUST write through requests with methods
2171 * that are unsafe (Section 4.2.1 of [RFC7231]) to
2172 * the origin server; i.e., a cache is not allowed
2173 * to generate a reply to such a request before
2174 * having forwarded the request and having received
2175 * a corresponding response.
2176 *
2177 * RFC7231#4.2.1:
2178 * Of the request methods defined by this
2179 * specification, the GET, HEAD, OPTIONS, and TRACE
2180 * methods are defined to be safe.
2181 */
2182 if (likely(txn->meth == HTTP_METH_GET ||
2183 txn->meth == HTTP_METH_HEAD ||
2184 txn->meth == HTTP_METH_OPTIONS ||
2185 txn->meth == HTTP_METH_TRACE))
2186 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2187
Willy Tarreaud787e662009-07-07 10:14:51 +02002188 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002189 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002190 req->analyse_exp = TICK_ETERNITY;
2191 return 1;
2192
2193 return_bad_req:
2194 /* We centralize bad requests processing here */
2195 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2196 /* we detected a parsing error. We want to archive this request
2197 * in the dedicated proxy area for later troubleshooting.
2198 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002199 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002200 }
2201
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002202 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002203 txn->req.msg_state = HTTP_MSG_ERROR;
2204 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002205 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002206
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002207 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002208 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002209 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002210
2211 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002212 if (!(s->flags & SF_ERR_MASK))
2213 s->flags |= SF_ERR_PRXCOND;
2214 if (!(s->flags & SF_FINST_MASK))
2215 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002216
Christopher Faulet0184ea72017-01-05 14:06:34 +01002217 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002218 req->analyse_exp = TICK_ETERNITY;
2219 return 0;
2220}
2221
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002222
Willy Tarreau347a35d2013-11-22 17:51:09 +01002223/* This function prepares an applet to handle the stats. It can deal with the
2224 * "100-continue" expectation, check that admin rules are met for POST requests,
2225 * and program a response message if something was unexpected. It cannot fail
2226 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002227 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002228 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002229 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002230 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002231int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002232{
2233 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002234 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002235 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002236 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002237 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002238 struct uri_auth *uri_auth = s->be->uri_auth;
2239 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002240 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002241
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002242 appctx = si_appctx(si);
2243 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2244 appctx->st1 = appctx->st2 = 0;
2245 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2246 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002247 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002248 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002249
Willy Tarreauf37954d2018-06-15 18:31:02 +02002250 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002251 lookup = uri + uri_auth->uri_len;
2252
2253 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2254 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002255 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002256 break;
2257 }
2258 }
2259
2260 if (uri_auth->refresh) {
2261 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2262 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002263 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002264 break;
2265 }
2266 }
2267 }
2268
2269 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2270 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002271 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002272 break;
2273 }
2274 }
2275
Willy Tarreau1e62df92016-01-11 18:57:53 +01002276 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2277 if (memcmp(h, ";typed", 6) == 0) {
2278 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2279 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2280 break;
2281 }
2282 }
2283
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002284 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2285 if (memcmp(h, ";st=", 4) == 0) {
2286 int i;
2287 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002288 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002289 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2290 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002291 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002292 break;
2293 }
2294 }
2295 break;
2296 }
2297 }
2298
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002299 appctx->ctx.stats.scope_str = 0;
2300 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002301 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2302 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2303 int itx = 0;
2304 const char *h2;
2305 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2306 const char *err;
2307
2308 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2309 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002310 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002311 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2312 itx++;
2313 h++;
2314 }
2315
2316 if (itx > STAT_SCOPE_TXT_MAXLEN)
2317 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002318 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002319
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002320 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002321 memcpy(scope_txt, h2, itx);
2322 scope_txt[itx] = '\0';
2323 err = invalid_char(scope_txt);
2324 if (err) {
2325 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002326 appctx->ctx.stats.scope_str = 0;
2327 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002328 }
2329 break;
2330 }
2331 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002332
2333 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002334 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002335 int ret = 1;
2336
2337 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002338 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002339 ret = acl_pass(ret);
2340 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2341 ret = !ret;
2342 }
2343
2344 if (ret) {
2345 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002346 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002347 break;
2348 }
2349 }
2350
2351 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002352 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002353 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002354 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002355 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2356 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002357 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002358 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002359 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002360 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2361 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002362 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002363 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002364 else {
2365 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002366 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002367 }
2368
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002369 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002370 return 1;
2371}
2372
Lukas Tribus67db8df2013-06-23 17:37:13 +02002373/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2374 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2375 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002376void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002377{
2378#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002379 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002380 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2381#endif
2382#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002383 if (from->ss_family == AF_INET6) {
2384 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002385 /* v4-mapped addresses need IP_TOS */
2386 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2387 else
2388 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2389 }
2390#endif
2391}
2392
Willy Tarreau87b09662015-04-03 00:22:06 +02002393int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002394 const char* name, unsigned int name_len,
2395 const char *str, struct my_regex *re,
2396 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002397{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002398 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002399 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02002400 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002401 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2402 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002403 struct chunk *output = get_trash_chunk();
2404
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002405 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002406
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002407 /* Choose the header browsing function. */
2408 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002409 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002410 http_find_hdr_func = http_find_header2;
2411 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002412 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002413 http_find_hdr_func = http_find_full_header2;
2414 break;
2415 default: /* impossible */
2416 return -1;
2417 }
2418
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002419 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2420 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06002421 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002422 char *val = ctx.line + ctx.val;
2423 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002424
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002425 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2426 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002427
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002428 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002429 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002430 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002431
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002432 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002433
2434 hdr->len += delta;
2435 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002436
2437 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002438 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002439 }
2440
2441 return 0;
2442}
2443
Willy Tarreau87b09662015-04-03 00:22:06 +02002444static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002445 const char* name, unsigned int name_len,
2446 struct list *fmt, struct my_regex *re,
2447 int action)
2448{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002449 struct chunk *replace;
2450 int ret = -1;
2451
2452 replace = alloc_trash_chunk();
2453 if (!replace)
2454 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002455
2456 replace->len = build_logline(s, replace->str, replace->size, fmt);
2457 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002458 goto leave;
2459
2460 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002461
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002462 leave:
2463 free_trash_chunk(replace);
2464 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002465}
2466
Willy Tarreau87b09662015-04-03 00:22:06 +02002467/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002468 * transaction <txn>. Returns the verdict of the first rule that prevents
2469 * further processing of the request (auth, deny, ...), and defaults to
2470 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2471 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002472 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2473 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2474 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002475 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002476enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002477http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002478{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002479 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002480 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002481 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002482 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002483 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002484 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002485 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002486 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002487
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002488 /* If "the current_rule_list" match the executed rule list, we are in
2489 * resume condition. If a resume is needed it is always in the action
2490 * and never in the ACL or converters. In this case, we initialise the
2491 * current rule, and go to the action execution point.
2492 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002493 if (s->current_rule) {
2494 rule = s->current_rule;
2495 s->current_rule = NULL;
2496 if (s->current_rule_list == rules)
2497 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002498 }
2499 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002500
Willy Tarreauff011f22011-01-06 17:51:27 +01002501 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002502
Willy Tarreau96257ec2012-12-27 10:46:37 +01002503 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002504 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002505 int ret;
2506
Willy Tarreau192252e2015-04-04 01:47:55 +02002507 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002508 ret = acl_pass(ret);
2509
Willy Tarreauff011f22011-01-06 17:51:27 +01002510 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002511 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002512
2513 if (!ret) /* condition not matched */
2514 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002515 }
2516
Willy Tarreauacc98002015-09-27 23:34:39 +02002517 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002518resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002519 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002520 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002521 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002522
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002523 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002524 if (deny_status)
2525 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002526 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002527
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002528 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002529 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002530 if (deny_status)
2531 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002532 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002533
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002534 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002535 /* Auth might be performed on regular http-req rules as well as on stats */
2536 auth_realm = rule->arg.auth.realm;
2537 if (!auth_realm) {
2538 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2539 auth_realm = STATS_DEFAULT_REALM;
2540 else
2541 auth_realm = px->id;
2542 }
2543 /* send 401/407 depending on whether we use a proxy or not. We still
2544 * count one error, because normal browsing won't significantly
2545 * increase the counter but brute force attempts will.
2546 */
2547 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2548 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002549 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002550 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002551 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002552
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002553 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002554 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2555 return HTTP_RULE_RES_BADREQ;
2556 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002557
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002558 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002559 s->task->nice = rule->arg.nice;
2560 break;
2561
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002562 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002563 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002564 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002565 break;
2566
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002567 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002568#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002569 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002570 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002571#endif
2572 break;
2573
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002574 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002575 s->logs.level = rule->arg.loglevel;
2576 break;
2577
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002578 case ACT_HTTP_REPLACE_HDR:
2579 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002580 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2581 rule->arg.hdr_add.name_len,
2582 &rule->arg.hdr_add.fmt,
2583 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002584 return HTTP_RULE_RES_BADREQ;
2585 break;
2586
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002587 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002588 ctx.idx = 0;
2589 /* remove all occurrences of the header */
2590 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002591 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002592 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002593 }
Willy Tarreau85603282015-01-21 20:39:27 +01002594 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002595
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002596 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002597 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002598 /* The scope of the trash buffer must be limited to this function. The
2599 * build_logline() function can execute a lot of other function which
2600 * can use the trash buffer. So for limiting the scope of this global
2601 * buffer, we build first the header value using build_logline, and
2602 * after we store the header name.
2603 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002604 struct chunk *replace;
2605
2606 replace = alloc_trash_chunk();
2607 if (!replace)
2608 return HTTP_RULE_RES_BADREQ;
2609
Thierry Fournier4b788f72016-06-01 13:35:36 +02002610 len = rule->arg.hdr_add.name_len + 2,
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002611 len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt);
2612 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2613 replace->str[rule->arg.hdr_add.name_len] = ':';
2614 replace->str[rule->arg.hdr_add.name_len + 1] = ' ';
2615 replace->len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002616
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002617 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002618 /* remove all occurrences of the header */
2619 ctx.idx = 0;
2620 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002621 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002622 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2623 }
2624 }
2625
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002626 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len) < 0) {
2627 static unsigned char rate_limit = 0;
2628
2629 if ((rate_limit++ & 255) == 0) {
2630 replace->str[rule->arg.hdr_add.name_len] = 0;
2631 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the request header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id, replace->str, s->uniq_id);
2632 }
2633
2634 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2635 if (sess->fe != s->be)
2636 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2637 if (sess->listener->counters)
2638 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2639 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002640
2641 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002642 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002643 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002644
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002645 case ACT_HTTP_DEL_ACL:
2646 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002647 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002648 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002649
2650 /* collect reference */
2651 ref = pat_ref_lookup(rule->arg.map.ref);
2652 if (!ref)
2653 continue;
2654
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002655 /* allocate key */
2656 key = alloc_trash_chunk();
2657 if (!key)
2658 return HTTP_RULE_RES_BADREQ;
2659
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002660 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002661 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2662 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002663
2664 /* perform update */
2665 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002666 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002667 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002668 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002669
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002670 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002671 break;
2672 }
2673
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002674 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002675 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002676 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002677
2678 /* collect reference */
2679 ref = pat_ref_lookup(rule->arg.map.ref);
2680 if (!ref)
2681 continue;
2682
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002683 /* allocate key */
2684 key = alloc_trash_chunk();
2685 if (!key)
2686 return HTTP_RULE_RES_BADREQ;
2687
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002688 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002689 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2690 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002691
2692 /* perform update */
2693 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002694 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002695 if (pat_ref_find_elt(ref, key->str) == NULL)
2696 pat_ref_add(ref, key->str, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002697 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002698
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002699 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002700 break;
2701 }
2702
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002703 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002704 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002705 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002706
2707 /* collect reference */
2708 ref = pat_ref_lookup(rule->arg.map.ref);
2709 if (!ref)
2710 continue;
2711
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002712 /* allocate key */
2713 key = alloc_trash_chunk();
2714 if (!key)
2715 return HTTP_RULE_RES_BADREQ;
2716
2717 /* allocate value */
2718 value = alloc_trash_chunk();
2719 if (!value) {
2720 free_trash_chunk(key);
2721 return HTTP_RULE_RES_BADREQ;
2722 }
2723
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002724 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002725 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2726 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002727
2728 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002729 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2730 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002731
2732 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002733 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002734 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002735 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002736 else
2737 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002738 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002739
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002740 free_trash_chunk(key);
2741 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002742 break;
2743 }
William Lallemand73025dd2014-04-24 14:38:37 +02002744
Thierry FOURNIER42148732015-09-02 17:17:33 +02002745 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002746 if ((s->req.flags & CF_READ_ERROR) ||
2747 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2748 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2749 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002750 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002751
Willy Tarreauacc98002015-09-27 23:34:39 +02002752 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002753 case ACT_RET_ERR:
2754 case ACT_RET_CONT:
2755 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002756 case ACT_RET_STOP:
2757 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002758 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002759 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002760 return HTTP_RULE_RES_YIELD;
2761 }
William Lallemand73025dd2014-04-24 14:38:37 +02002762 break;
2763
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002764 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002765 /* Note: only the first valid tracking parameter of each
2766 * applies.
2767 */
2768
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002769 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002770 struct stktable *t;
2771 struct stksess *ts;
2772 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002773 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002774
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002775 t = rule->arg.trk_ctr.table.t;
2776 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02002777
2778 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002779 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002780
2781 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002782 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2783 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2784 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002785 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002786
Emeric Brun819fc6f2017-06-13 19:37:32 +02002787 if (ptr1)
2788 stktable_data_cast(ptr1, http_req_cnt)++;
2789
2790 if (ptr2)
2791 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2792 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2793
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002794 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002795
2796 /* If data was modified, we need to touch to re-schedule sync */
2797 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002798 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002799
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002800 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002801 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002802 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002803 }
2804 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002805 break;
2806
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002807 /* other flags exists, but normaly, they never be matched. */
2808 default:
2809 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002810 }
2811 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002812
2813 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002814 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002815}
2816
Willy Tarreau71241ab2012-12-27 11:30:54 +01002817
Willy Tarreau51d861a2015-05-22 17:30:48 +02002818/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2819 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2820 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2821 * is returned, the process can continue the evaluation of next rule list. If
2822 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2823 * is returned, it means the operation could not be processed and a server error
2824 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2825 * deny rule. If *YIELD is returned, the caller must call again the function
2826 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002827 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002828static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002829http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002830{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002831 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002832 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002833 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002834 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002835 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002836 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002837
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002838 /* If "the current_rule_list" match the executed rule list, we are in
2839 * resume condition. If a resume is needed it is always in the action
2840 * and never in the ACL or converters. In this case, we initialise the
2841 * current rule, and go to the action execution point.
2842 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002843 if (s->current_rule) {
2844 rule = s->current_rule;
2845 s->current_rule = NULL;
2846 if (s->current_rule_list == rules)
2847 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002848 }
2849 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002850
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002851 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002852
2853 /* check optional condition */
2854 if (rule->cond) {
2855 int ret;
2856
Willy Tarreau192252e2015-04-04 01:47:55 +02002857 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002858 ret = acl_pass(ret);
2859
2860 if (rule->cond->pol == ACL_COND_UNLESS)
2861 ret = !ret;
2862
2863 if (!ret) /* condition not matched */
2864 continue;
2865 }
2866
Willy Tarreauacc98002015-09-27 23:34:39 +02002867 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002868resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002869 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002870 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002871 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002872
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002873 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002874 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002875 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002876
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002877 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002878 s->task->nice = rule->arg.nice;
2879 break;
2880
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002881 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002882 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002883 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002884 break;
2885
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002886 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002887#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002888 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002889 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002890#endif
2891 break;
2892
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002893 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002894 s->logs.level = rule->arg.loglevel;
2895 break;
2896
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002897 case ACT_HTTP_REPLACE_HDR:
2898 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002899 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2900 rule->arg.hdr_add.name_len,
2901 &rule->arg.hdr_add.fmt,
2902 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002903 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002904 break;
2905
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002906 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002907 ctx.idx = 0;
2908 /* remove all occurrences of the header */
2909 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002910 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002911 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2912 }
Willy Tarreau85603282015-01-21 20:39:27 +01002913 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002914
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002915 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002916 case ACT_HTTP_ADD_HDR: {
2917 struct chunk *replace;
2918
2919 replace = alloc_trash_chunk();
2920 if (!replace)
2921 return HTTP_RULE_RES_BADREQ;
2922
2923 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2924 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2925 replace->len = rule->arg.hdr_add.name_len;
2926 replace->str[replace->len++] = ':';
2927 replace->str[replace->len++] = ' ';
2928 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2929 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002930
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002931 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002932 /* remove all occurrences of the header */
2933 ctx.idx = 0;
2934 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002935 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002936 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2937 }
2938 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002939
2940 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len) < 0) {
2941 static unsigned char rate_limit = 0;
2942
2943 if ((rate_limit++ & 255) == 0) {
2944 replace->str[rule->arg.hdr_add.name_len] = 0;
2945 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the response header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id, replace->str, s->uniq_id);
2946 }
2947
2948 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2949 if (sess->fe != s->be)
2950 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2951 if (sess->listener->counters)
2952 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2953 if (objt_server(s->target))
2954 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2955 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002956
2957 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002958 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002959 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002960
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002961 case ACT_HTTP_DEL_ACL:
2962 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002963 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002964 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002965
2966 /* collect reference */
2967 ref = pat_ref_lookup(rule->arg.map.ref);
2968 if (!ref)
2969 continue;
2970
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002971 /* allocate key */
2972 key = alloc_trash_chunk();
2973 if (!key)
2974 return HTTP_RULE_RES_BADREQ;
2975
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002976 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002977 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2978 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002979
2980 /* perform update */
2981 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002982 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002983 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002984 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002985
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002986 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002987 break;
2988 }
2989
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002990 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002991 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002992 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002993
2994 /* collect reference */
2995 ref = pat_ref_lookup(rule->arg.map.ref);
2996 if (!ref)
2997 continue;
2998
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002999 /* allocate key */
3000 key = alloc_trash_chunk();
3001 if (!key)
3002 return HTTP_RULE_RES_BADREQ;
3003
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003004 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003005 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
3006 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003007
3008 /* perform update */
3009 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003010 if (pat_ref_find_elt(ref, key->str) == NULL)
3011 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003012
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003013 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003014 break;
3015 }
3016
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003017 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003018 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003019 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003020
3021 /* collect reference */
3022 ref = pat_ref_lookup(rule->arg.map.ref);
3023 if (!ref)
3024 continue;
3025
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003026 /* allocate key */
3027 key = alloc_trash_chunk();
3028 if (!key)
3029 return HTTP_RULE_RES_BADREQ;
3030
3031 /* allocate value */
3032 value = alloc_trash_chunk();
3033 if (!value) {
3034 free_trash_chunk(key);
3035 return HTTP_RULE_RES_BADREQ;
3036 }
3037
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003038 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003039 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
3040 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003041
3042 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003043 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
3044 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003045
3046 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003047 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003048 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003049 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003050 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003051 else
3052 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003053 pat_ref_add(ref, key->str, value->str, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003054 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003055 free_trash_chunk(key);
3056 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003057 break;
3058 }
William Lallemand73025dd2014-04-24 14:38:37 +02003059
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003060 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003061 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3062 return HTTP_RULE_RES_BADREQ;
3063 return HTTP_RULE_RES_DONE;
3064
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003065 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3066 /* Note: only the first valid tracking parameter of each
3067 * applies.
3068 */
3069
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003070 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003071 struct stktable *t;
3072 struct stksess *ts;
3073 struct stktable_key *key;
3074 void *ptr;
3075
3076 t = rule->arg.trk_ctr.table.t;
3077 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3078
3079 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003080 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003081
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003082 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003083
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003084 /* let's count a new HTTP request as it's the first time we do it */
3085 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3086 if (ptr)
3087 stktable_data_cast(ptr, http_req_cnt)++;
3088
3089 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3090 if (ptr)
3091 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3092 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3093
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003094 /* When the client triggers a 4xx from the server, it's most often due
3095 * to a missing object or permission. These events should be tracked
3096 * because if they happen often, it may indicate a brute force or a
3097 * vulnerability scan. Normally this is done when receiving the response
3098 * but here we're tracking after this ought to have been done so we have
3099 * to do it on purpose.
3100 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003101 if ((unsigned)(txn->status - 400) < 100) {
3102 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3103 if (ptr)
3104 stktable_data_cast(ptr, http_err_cnt)++;
3105
3106 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3107 if (ptr)
3108 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3109 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3110 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003111
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003112 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003113
Emeric Brun0fed0b02017-11-29 16:15:07 +01003114 /* If data was modified, we need to touch to re-schedule sync */
3115 stktable_touch_local(t, ts, 0);
3116
Emeric Brun819fc6f2017-06-13 19:37:32 +02003117 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3118 if (sess->fe != s->be)
3119 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3120
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003121 }
3122 }
3123 break;
3124
Thierry FOURNIER42148732015-09-02 17:17:33 +02003125 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003126 if ((s->req.flags & CF_READ_ERROR) ||
3127 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3128 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3129 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02003130 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003131
Willy Tarreauacc98002015-09-27 23:34:39 +02003132 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003133 case ACT_RET_ERR:
3134 case ACT_RET_CONT:
3135 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003136 case ACT_RET_STOP:
3137 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003138 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003139 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003140 return HTTP_RULE_RES_YIELD;
3141 }
William Lallemand73025dd2014-04-24 14:38:37 +02003142 break;
3143
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003144 /* other flags exists, but normaly, they never be matched. */
3145 default:
3146 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003147 }
3148 }
3149
3150 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003151 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003152}
3153
3154
Willy Tarreau71241ab2012-12-27 11:30:54 +01003155/* Perform an HTTP redirect based on the information in <rule>. The function
3156 * returns non-zero on success, or zero in case of a, irrecoverable error such
3157 * as too large a request to build a valid response.
3158 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003159static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003160{
Willy Tarreaub329a312015-05-22 16:27:37 +02003161 struct http_msg *req = &txn->req;
3162 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003163 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003164 struct chunk *chunk;
3165 int ret = 0;
3166
3167 chunk = alloc_trash_chunk();
3168 if (!chunk)
3169 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003170
3171 /* build redirect message */
3172 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003173 case 308:
3174 msg_fmt = HTTP_308;
3175 break;
3176 case 307:
3177 msg_fmt = HTTP_307;
3178 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003179 case 303:
3180 msg_fmt = HTTP_303;
3181 break;
3182 case 301:
3183 msg_fmt = HTTP_301;
3184 break;
3185 case 302:
3186 default:
3187 msg_fmt = HTTP_302;
3188 break;
3189 }
3190
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003191 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3192 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003193
3194 switch(rule->type) {
3195 case REDIRECT_TYPE_SCHEME: {
3196 const char *path;
3197 const char *host;
3198 struct hdr_ctx ctx;
3199 int pathlen;
3200 int hostlen;
3201
3202 host = "";
3203 hostlen = 0;
3204 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02003205 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003206 host = ctx.line + ctx.val;
3207 hostlen = ctx.vlen;
3208 }
3209
3210 path = http_get_path(txn);
3211 /* build message using path */
3212 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003213 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003214 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3215 int qs = 0;
3216 while (qs < pathlen) {
3217 if (path[qs] == '?') {
3218 pathlen = qs;
3219 break;
3220 }
3221 qs++;
3222 }
3223 }
3224 } else {
3225 path = "/";
3226 pathlen = 1;
3227 }
3228
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003229 if (rule->rdr_str) { /* this is an old "redirect" rule */
3230 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003231 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3232 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003233
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003234 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003235 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3236 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003237 }
3238 else {
3239 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003240 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003241
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003242 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003243 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3244 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003245 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003246 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003247 memcpy(chunk->str + chunk->len, "://", 3);
3248 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003249
3250 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003251 memcpy(chunk->str + chunk->len, host, hostlen);
3252 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003253
3254 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003255 memcpy(chunk->str + chunk->len, path, pathlen);
3256 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003257
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003258 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003259 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003260 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003261 if (chunk->len > chunk->size - 5)
3262 goto leave;
3263 chunk->str[chunk->len] = '/';
3264 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003265 }
3266
3267 break;
3268 }
3269 case REDIRECT_TYPE_PREFIX: {
3270 const char *path;
3271 int pathlen;
3272
3273 path = http_get_path(txn);
3274 /* build message using path */
3275 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003276 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003277 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3278 int qs = 0;
3279 while (qs < pathlen) {
3280 if (path[qs] == '?') {
3281 pathlen = qs;
3282 break;
3283 }
3284 qs++;
3285 }
3286 }
3287 } else {
3288 path = "/";
3289 pathlen = 1;
3290 }
3291
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003292 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003293 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3294 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003295
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003296 /* add prefix. Note that if prefix == "/", we don't want to
3297 * add anything, otherwise it makes it hard for the user to
3298 * configure a self-redirection.
3299 */
3300 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003301 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3302 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003303 }
3304 }
3305 else {
3306 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003307 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003308
3309 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003310 if (chunk->len + pathlen > chunk->size - 4)
3311 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003312 }
3313
3314 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003315 memcpy(chunk->str + chunk->len, path, pathlen);
3316 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003317
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003318 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003319 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003320 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003321 if (chunk->len > chunk->size - 5)
3322 goto leave;
3323 chunk->str[chunk->len] = '/';
3324 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003325 }
3326
3327 break;
3328 }
3329 case REDIRECT_TYPE_LOCATION:
3330 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003331 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003332 if (chunk->len + rule->rdr_len > chunk->size - 4)
3333 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003334
3335 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003336 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3337 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003338 }
3339 else {
3340 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003341 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003342
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003343 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003344 if (chunk->len > chunk->size - 4)
3345 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003346 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003347 break;
3348 }
3349
3350 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003351 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3352 chunk->len += 14;
3353 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3354 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003355 }
3356
Willy Tarreau19b14122017-02-28 09:48:11 +01003357 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003358 txn->status = rule->code;
3359 /* let's log the request time */
3360 s->logs.tv_request = now;
3361
Christopher Fauletbe821b92017-03-30 11:21:53 +02003362 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003363 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3364 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3365 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003366 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003367 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003368 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3369 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003370 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003371 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3372 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003373 }
3374 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003375 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3376 chunk->len += 4;
3377 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003378 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003379 /* "eat" the request */
Willy Tarreau72a100b2018-07-10 09:59:31 +02003380 b_del(req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02003381 req->next -= req->sov;
3382 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003383 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003384 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003385 req->msg_state = HTTP_MSG_CLOSED;
3386 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003387 /* Trim any possible response */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003388 b_set_data(res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02003389 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003390 /* let the server side turn to SI_ST_CLO */
3391 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003392 } else {
3393 /* keep-alive not possible */
3394 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003395 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3396 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003397 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003398 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3399 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003400 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003401 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003402 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003403 }
3404
Willy Tarreaue7dff022015-04-03 01:14:29 +02003405 if (!(s->flags & SF_ERR_MASK))
3406 s->flags |= SF_ERR_LOCAL;
3407 if (!(s->flags & SF_FINST_MASK))
3408 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003409
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003410 ret = 1;
3411 leave:
3412 free_trash_chunk(chunk);
3413 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003414}
3415
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003416/* This stream analyser runs all HTTP request processing which is common to
3417 * frontends and backends, which means blocking ACLs, filters, connection-close,
3418 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003419 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003420 * either needs more data or wants to immediately abort the request (eg: deny,
3421 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003422 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003423int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003424{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003425 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003426 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003427 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003428 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003429 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003430 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003431 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003432 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003433
Willy Tarreau655dce92009-11-08 13:10:58 +01003434 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003435 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003436 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003437 }
3438
Willy Tarreau87b09662015-04-03 00:22:06 +02003439 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003440 now_ms, __FUNCTION__,
3441 s,
3442 req,
3443 req->rex, req->wex,
3444 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003445 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003446 req->analysers);
3447
Willy Tarreau65410832014-04-28 21:25:43 +02003448 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003449 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003450
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003451 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003452 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003453 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003454
Willy Tarreau0b748332014-04-29 00:13:29 +02003455 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003456 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3457 goto return_prx_yield;
3458
Willy Tarreau0b748332014-04-29 00:13:29 +02003459 case HTTP_RULE_RES_CONT:
3460 case HTTP_RULE_RES_STOP: /* nothing to do */
3461 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003462
Willy Tarreau0b748332014-04-29 00:13:29 +02003463 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3464 if (txn->flags & TX_CLTARPIT)
3465 goto tarpit;
3466 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003467
Willy Tarreau0b748332014-04-29 00:13:29 +02003468 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3469 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003470
Willy Tarreau0b748332014-04-29 00:13:29 +02003471 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003472 goto done;
3473
Willy Tarreau0b748332014-04-29 00:13:29 +02003474 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3475 goto return_bad_req;
3476 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003477 }
3478
Olivier Houchard25ae45a2017-11-29 19:51:19 +01003479 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
3480 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003481 struct hdr_ctx ctx;
3482
3483 ctx.idx = 0;
3484 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02003485 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003486 if (unlikely(http_header_add_tail2(&txn->req,
3487 &txn->hdr_idx, "Early-Data: 1",
3488 strlen("Early-Data: 1"))) < 0) {
3489 goto return_bad_req;
3490 }
3491 }
3492
3493 }
3494
Willy Tarreau52542592014-04-28 18:33:26 +02003495 /* OK at this stage, we know that the request was accepted according to
3496 * the http-request rules, we can check for the stats. Note that the
3497 * URI is detected *before* the req* rules in order not to be affected
3498 * by a possible reqrep, while they are processed *after* so that a
3499 * reqdeny can still block them. This clearly needs to change in 1.6!
3500 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003501 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003502 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003503 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003504 txn->status = 500;
3505 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003506 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003507
Willy Tarreaue7dff022015-04-03 01:14:29 +02003508 if (!(s->flags & SF_ERR_MASK))
3509 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003510 goto return_prx_cond;
3511 }
3512
3513 /* parse the whole stats request and extract the relevant information */
3514 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003515 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003516 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003517
Willy Tarreau0b748332014-04-29 00:13:29 +02003518 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3519 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003520
Willy Tarreau0b748332014-04-29 00:13:29 +02003521 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3522 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003523 }
3524
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003525 /* evaluate the req* rules except reqadd */
3526 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003527 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003528 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003529
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003530 if (txn->flags & TX_CLDENY)
3531 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003532
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003533 if (txn->flags & TX_CLTARPIT) {
3534 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003535 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003536 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003537 }
Willy Tarreau06619262006-12-17 08:37:22 +01003538
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003539 /* add request headers from the rule sets in the same order */
3540 list_for_each_entry(wl, &px->req_add, list) {
3541 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003542 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003543 ret = acl_pass(ret);
3544 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3545 ret = !ret;
3546 if (!ret)
3547 continue;
3548 }
3549
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003550 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003551 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003552 }
3553
Willy Tarreau52542592014-04-28 18:33:26 +02003554
3555 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003556 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3557 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003558 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003559 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003560 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003561
Willy Tarreaue7dff022015-04-03 01:14:29 +02003562 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3563 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3564 if (!(s->flags & SF_FINST_MASK))
3565 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003566
Willy Tarreau70730dd2014-04-24 18:06:27 +02003567 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003568 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3569 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003570 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003571 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003572 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003573
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003574 /* check whether we have some ACLs set to redirect this request */
3575 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003576 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003577 int ret;
3578
Willy Tarreau192252e2015-04-04 01:47:55 +02003579 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003580 ret = acl_pass(ret);
3581 if (rule->cond->pol == ACL_COND_UNLESS)
3582 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003583 if (!ret)
3584 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003585 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003586 if (!http_apply_redirect_rule(rule, s, txn))
3587 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003588 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003589 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003590
Willy Tarreau2be39392010-01-03 17:24:51 +01003591 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3592 * If this happens, then the data will not come immediately, so we must
3593 * send all what we have without waiting. Note that due to the small gain
3594 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003595 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003596 * itself once used.
3597 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003598 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003599
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003600 done: /* done with this analyser, continue with next ones that the calling
3601 * points will have set, if any.
3602 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003603 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003604 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3605 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003606 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003607
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003608 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003609 /* Allow cookie logging
3610 */
3611 if (s->be->cookie_name || sess->fe->capture_name)
3612 manage_client_side_cookies(s, req);
3613
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003614 /* When a connection is tarpitted, we use the tarpit timeout,
3615 * which may be the same as the connect timeout if unspecified.
3616 * If unset, then set it to zero because we really want it to
3617 * eventually expire. We build the tarpit as an analyser.
3618 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003619 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003620
3621 /* wipe the request out so that we can drop the connection early
3622 * if the client closes first.
3623 */
3624 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003625
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003626 txn->status = http_err_codes[deny_status];
3627
Christopher Faulet0184ea72017-01-05 14:06:34 +01003628 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003629 req->analysers |= AN_REQ_HTTP_TARPIT;
3630 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3631 if (!req->analyse_exp)
3632 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003633 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003634 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003635 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003636 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003637 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003638 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003639 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003640
3641 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003642
3643 /* Allow cookie logging
3644 */
3645 if (s->be->cookie_name || sess->fe->capture_name)
3646 manage_client_side_cookies(s, req);
3647
Willy Tarreau0b748332014-04-29 00:13:29 +02003648 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003649 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003650 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003651 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003652 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003653 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003654 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003655 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003656 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003657 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003658 goto return_prx_cond;
3659
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003660 return_bad_req:
3661 /* We centralize bad requests processing here */
3662 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3663 /* we detected a parsing error. We want to archive this request
3664 * in the dedicated proxy area for later troubleshooting.
3665 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003666 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003667 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003668
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003669 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003670 txn->req.msg_state = HTTP_MSG_ERROR;
3671 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003672 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003673
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003674 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003675 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003676 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003677
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003678 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003679 if (!(s->flags & SF_ERR_MASK))
3680 s->flags |= SF_ERR_PRXCOND;
3681 if (!(s->flags & SF_FINST_MASK))
3682 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003683
Christopher Faulet0184ea72017-01-05 14:06:34 +01003684 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003685 req->analyse_exp = TICK_ETERNITY;
3686 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003687
3688 return_prx_yield:
3689 channel_dont_connect(req);
3690 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003691}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003692
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003693/* This function performs all the processing enabled for the current request.
3694 * It returns 1 if the processing can continue on next analysers, or zero if it
3695 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003696 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003697 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003698int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003699{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003700 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003701 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003702 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003703 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003704
Willy Tarreau655dce92009-11-08 13:10:58 +01003705 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003706 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003707 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003708 return 0;
3709 }
3710
Willy Tarreau87b09662015-04-03 00:22:06 +02003711 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003712 now_ms, __FUNCTION__,
3713 s,
3714 req,
3715 req->rex, req->wex,
3716 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003717 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003718 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003719
Willy Tarreau59234e92008-11-30 23:51:27 +01003720 /*
3721 * Right now, we know that we have processed the entire headers
3722 * and that unwanted requests have been filtered out. We can do
3723 * whatever we want with the remaining request. Also, now we
3724 * may have separate values for ->fe, ->be.
3725 */
Willy Tarreau06619262006-12-17 08:37:22 +01003726
Willy Tarreau59234e92008-11-30 23:51:27 +01003727 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003728 * If HTTP PROXY is set we simply get remote server address parsing
3729 * incoming request. Note that this requires that a connection is
3730 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003731 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003732 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003733 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003734 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003735
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003736 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003737 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003738 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003739 txn->req.msg_state = HTTP_MSG_ERROR;
3740 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003741 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003742 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003743
Willy Tarreaue7dff022015-04-03 01:14:29 +02003744 if (!(s->flags & SF_ERR_MASK))
3745 s->flags |= SF_ERR_RESOURCE;
3746 if (!(s->flags & SF_FINST_MASK))
3747 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003748
3749 return 0;
3750 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003751
3752 path = http_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003753 if (url2sa(ci_head(req) + msg->sl.rq.u,
3754 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003755 &conn->addr.to, NULL) == -1)
3756 goto return_bad_req;
3757
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003758 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003759 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3760 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003761 * u_l characters by a single "/".
3762 */
3763 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003764 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003765 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3766 int delta;
3767
Willy Tarreauf37954d2018-06-15 18:31:02 +02003768 delta = buffer_replace2(req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003769 http_msg_move_end(&txn->req, delta);
3770 cur_end += delta;
3771 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3772 goto return_bad_req;
3773 }
3774 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003775 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003776 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3777 int delta;
3778
Willy Tarreauf37954d2018-06-15 18:31:02 +02003779 delta = buffer_replace2(req->buf, cur_ptr + msg->sl.rq.u,
3780 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003781 http_msg_move_end(&txn->req, delta);
3782 cur_end += delta;
3783 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3784 goto return_bad_req;
3785 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003786 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003787
Willy Tarreau59234e92008-11-30 23:51:27 +01003788 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003789 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003790 * Note that doing so might move headers in the request, but
3791 * the fields will stay coherent and the URI will not move.
3792 * This should only be performed in the backend.
3793 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003794 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003795 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003796
William Lallemanda73203e2012-03-12 12:48:57 +01003797 /* add unique-id if "header-unique-id" is specified */
3798
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003799 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003800 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003801 goto return_bad_req;
3802 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003803 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003804 }
William Lallemanda73203e2012-03-12 12:48:57 +01003805
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003806 if (sess->fe->header_unique_id && s->unique_id) {
3807 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003808 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003809 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003810 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003811 goto return_bad_req;
3812 }
3813
Cyril Bontéb21570a2009-11-29 20:04:48 +01003814 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003815 * 9: add X-Forwarded-For if either the frontend or the backend
3816 * asks for it.
3817 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003818 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003819 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003820 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3821 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3822 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003823 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003824 /* The header is set to be added only if none is present
3825 * and we found it, so don't do anything.
3826 */
3827 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003828 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003829 /* Add an X-Forwarded-For header unless the source IP is
3830 * in the 'except' network range.
3831 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003832 if ((!sess->fe->except_mask.s_addr ||
3833 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3834 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003835 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003836 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003837 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003838 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003839 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003840 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003841
3842 /* Note: we rely on the backend to get the header name to be used for
3843 * x-forwarded-for, because the header is really meant for the backends.
3844 * However, if the backend did not specify any option, we have to rely
3845 * on the frontend's header name.
3846 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003847 if (s->be->fwdfor_hdr_len) {
3848 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003849 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003850 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003851 len = sess->fe->fwdfor_hdr_len;
3852 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003853 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003854 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003855
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003856 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003857 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003858 }
3859 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003860 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003861 /* FIXME: for the sake of completeness, we should also support
3862 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003863 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003864 int len;
3865 char pn[INET6_ADDRSTRLEN];
3866 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003867 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003868 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003869
Willy Tarreau59234e92008-11-30 23:51:27 +01003870 /* Note: we rely on the backend to get the header name to be used for
3871 * x-forwarded-for, because the header is really meant for the backends.
3872 * However, if the backend did not specify any option, we have to rely
3873 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003874 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003875 if (s->be->fwdfor_hdr_len) {
3876 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003877 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003878 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003879 len = sess->fe->fwdfor_hdr_len;
3880 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003881 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003882 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003883
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003884 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003885 goto return_bad_req;
3886 }
3887 }
3888
3889 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003890 * 10: add X-Original-To if either the frontend or the backend
3891 * asks for it.
3892 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003893 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003894
3895 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003896 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003897 /* Add an X-Original-To header unless the destination IP is
3898 * in the 'except' network range.
3899 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003900 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003901
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003902 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003903 ((!sess->fe->except_mask_to.s_addr ||
3904 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3905 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003906 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003907 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003908 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003909 int len;
3910 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003911 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003912
3913 /* Note: we rely on the backend to get the header name to be used for
3914 * x-original-to, because the header is really meant for the backends.
3915 * However, if the backend did not specify any option, we have to rely
3916 * on the frontend's header name.
3917 */
3918 if (s->be->orgto_hdr_len) {
3919 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003920 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003921 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003922 len = sess->fe->orgto_hdr_len;
3923 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003924 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003925 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003926
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003927 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003928 goto return_bad_req;
3929 }
3930 }
3931 }
3932
Willy Tarreau50fc7772012-11-11 22:19:57 +01003933 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3934 * If an "Upgrade" token is found, the header is left untouched in order not to have
3935 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3936 * "Upgrade" is present in the Connection header.
3937 */
3938 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3939 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003940 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003941 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003942 unsigned int want_flags = 0;
3943
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003944 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003945 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003946 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003947 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003948 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003949 want_flags |= TX_CON_CLO_SET;
3950 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003951 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003952 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003953 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003954 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003955 want_flags |= TX_CON_KAL_SET;
3956 }
3957
3958 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003959 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003960 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003961
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003962
Willy Tarreau522d6c02009-12-06 18:49:18 +01003963 /* If we have no server assigned yet and we're balancing on url_param
3964 * with a POST request, we may be interested in checking the body for
3965 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003966 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003967 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003968 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003969 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003970 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003971 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003972 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003973
Christopher Fauletbe821b92017-03-30 11:21:53 +02003974 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3975 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003976#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003977 /* We expect some data from the client. Unless we know for sure
3978 * we already have a full request, we have to re-enable quick-ack
3979 * in case we previously disabled it, otherwise we might cause
3980 * the client to delay further data.
3981 */
3982 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3983 cli_conn && conn_ctrl_ready(cli_conn) &&
3984 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003985 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003986 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003987#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003988
Willy Tarreau59234e92008-11-30 23:51:27 +01003989 /*************************************************************
3990 * OK, that's finished for the headers. We have done what we *
3991 * could. Let's switch to the DATA state. *
3992 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003993 req->analyse_exp = TICK_ETERNITY;
3994 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003995
Willy Tarreau59234e92008-11-30 23:51:27 +01003996 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003997 /* OK let's go on with the BODY now */
3998 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003999
Willy Tarreau59234e92008-11-30 23:51:27 +01004000 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004001 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004002 /* we detected a parsing error. We want to archive this request
4003 * in the dedicated proxy area for later troubleshooting.
4004 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004005 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004006 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004007
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004008 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004009 txn->req.msg_state = HTTP_MSG_ERROR;
4010 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004011 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004012 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004013
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004014 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004015 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004016 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004017
Willy Tarreaue7dff022015-04-03 01:14:29 +02004018 if (!(s->flags & SF_ERR_MASK))
4019 s->flags |= SF_ERR_PRXCOND;
4020 if (!(s->flags & SF_FINST_MASK))
4021 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004022 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004023}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004024
Willy Tarreau60b85b02008-11-30 23:28:40 +01004025/* This function is an analyser which processes the HTTP tarpit. It always
4026 * returns zero, at the beginning because it prevents any other processing
4027 * from occurring, and at the end because it terminates the request.
4028 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004029int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004030{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004031 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004032
4033 /* This connection is being tarpitted. The CLIENT side has
4034 * already set the connect expiration date to the right
4035 * timeout. We just have to check that the client is still
4036 * there and that the timeout has not expired.
4037 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004038 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004039 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004040 !tick_is_expired(req->analyse_exp, now_ms))
4041 return 0;
4042
4043 /* We will set the queue timer to the time spent, just for
4044 * logging purposes. We fake a 500 server error, so that the
4045 * attacker will not suspect his connection has been tarpitted.
4046 * It will not cause trouble to the logs because we can exclude
4047 * the tarpitted connections by filtering on the 'PT' status flags.
4048 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004049 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4050
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004051 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004052 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004053
Christopher Faulet0184ea72017-01-05 14:06:34 +01004054 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004055 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004056
Willy Tarreaue7dff022015-04-03 01:14:29 +02004057 if (!(s->flags & SF_ERR_MASK))
4058 s->flags |= SF_ERR_PRXCOND;
4059 if (!(s->flags & SF_FINST_MASK))
4060 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004061 return 0;
4062}
4063
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004064/* This function is an analyser which waits for the HTTP request body. It waits
4065 * for either the buffer to be full, or the full advertised contents to have
4066 * reached the buffer. It must only be called after the standard HTTP request
4067 * processing has occurred, because it expects the request to be parsed and will
4068 * look for the Expect header. It may send a 100-Continue interim response. It
4069 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4070 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4071 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004072 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004073int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004074{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004075 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004076 struct http_txn *txn = s->txn;
4077 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004078
4079 /* We have to parse the HTTP request body to find any required data.
4080 * "balance url_param check_post" should have been the only way to get
4081 * into this. We were brought here after HTTP header analysis, so all
4082 * related structures are ready.
4083 */
4084
Willy Tarreau890988f2014-04-10 11:59:33 +02004085 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4086 /* This is the first call */
4087 if (msg->msg_state < HTTP_MSG_BODY)
4088 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004089
Willy Tarreau890988f2014-04-10 11:59:33 +02004090 if (msg->msg_state < HTTP_MSG_100_SENT) {
4091 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4092 * send an HTTP/1.1 100 Continue intermediate response.
4093 */
4094 if (msg->flags & HTTP_MSGF_VER_11) {
4095 struct hdr_ctx ctx;
4096 ctx.idx = 0;
4097 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004098 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02004099 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004100 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004101 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004102 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004103 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004104 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004105 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004106
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004107 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02004108 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02004109 * must save the body in msg->next because it survives buffer
4110 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004111 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004112 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004113
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004114 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004115 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4116 else
4117 msg->msg_state = HTTP_MSG_DATA;
4118 }
4119
Willy Tarreau890988f2014-04-10 11:59:33 +02004120 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4121 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004122 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004123 goto missing_data;
4124
4125 /* OK we have everything we need now */
4126 goto http_end;
4127 }
4128
4129 /* OK here we're parsing a chunked-encoded message */
4130
Willy Tarreau522d6c02009-12-06 18:49:18 +01004131 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004132 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004133 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004134 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004135 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004136 unsigned int chunk;
Willy Tarreaud760eec2018-07-10 09:50:25 +02004137 int ret = h1_parse_chunk_size(req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004138
Willy Tarreau115acb92009-12-26 13:56:06 +01004139 if (!ret)
4140 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004141 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004142 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004143 if (msg->err_pos < 0)
4144 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004145 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004146 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004147 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004148
4149 msg->chunk_len = chunk;
4150 msg->body_len += chunk;
4151
4152 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004153 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004154 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004155 }
4156
Willy Tarreaud98cf932009-12-27 22:54:55 +01004157 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004158 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4159 * for at least a whole chunk or the whole content length bytes after
4160 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004161 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004162 if (msg->msg_state == HTTP_MSG_TRAILERS)
4163 goto http_end;
4164
Willy Tarreaue115b492015-05-01 23:05:14 +02004165 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004166 goto http_end;
4167
4168 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004169 /* we get here if we need to wait for more data. If the buffer is full,
4170 * we have the maximum we can expect.
4171 */
Willy Tarreau23752332018-06-15 14:54:53 +02004172 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02004173 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004174
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004175 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004176 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004177 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004178
Willy Tarreaue7dff022015-04-03 01:14:29 +02004179 if (!(s->flags & SF_ERR_MASK))
4180 s->flags |= SF_ERR_CLITO;
4181 if (!(s->flags & SF_FINST_MASK))
4182 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004183 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004184 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004185
4186 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004187 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004188 /* Not enough data. We'll re-use the http-request
4189 * timeout here. Ideally, we should set the timeout
4190 * relative to the accept() date. We just set the
4191 * request timeout once at the beginning of the
4192 * request.
4193 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004194 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004195 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004196 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004197 return 0;
4198 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004199
4200 http_end:
4201 /* The situation will not evolve, so let's give up on the analysis. */
4202 s->logs.tv_request = now; /* update the request timer to reflect full request */
4203 req->analysers &= ~an_bit;
4204 req->analyse_exp = TICK_ETERNITY;
4205 return 1;
4206
4207 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004208 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004209 txn->req.msg_state = HTTP_MSG_ERROR;
4210 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004211 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004212
Willy Tarreaue7dff022015-04-03 01:14:29 +02004213 if (!(s->flags & SF_ERR_MASK))
4214 s->flags |= SF_ERR_PRXCOND;
4215 if (!(s->flags & SF_FINST_MASK))
4216 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004217
Willy Tarreau522d6c02009-12-06 18:49:18 +01004218 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004219 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004220 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004221 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004222 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004223 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004224}
4225
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004226/* send a server's name with an outgoing request over an established connection.
4227 * Note: this function is designed to be called once the request has been scheduled
4228 * for being forwarded. This is the reason why it rewinds the buffer before
4229 * proceeding.
4230 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004231int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004232
4233 struct hdr_ctx ctx;
4234
Mark Lamourinec2247f02012-01-04 13:02:01 -05004235 char *hdr_name = be->server_id_hdr_name;
4236 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004237 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004238 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004239 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004240
William Lallemandd9e90662012-01-30 17:27:17 +01004241 ctx.idx = 0;
4242
Willy Tarreau211cdec2014-04-17 20:18:08 +02004243 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004244 if (old_o) {
4245 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02004246 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004247 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004248 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004249 }
4250
Willy Tarreauf37954d2018-06-15 18:31:02 +02004251 old_i = ci_data(chn);
4252 while (http_find_header2(hdr_name, hdr_name_len, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004253 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004254 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004255 }
4256
4257 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004258 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004259 memcpy(hdr_val, hdr_name, hdr_name_len);
4260 hdr_val += hdr_name_len;
4261 *hdr_val++ = ':';
4262 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004263 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4264 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004265
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004266 if (old_o) {
4267 /* If this was a forwarded request, we must readjust the amount of
4268 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004269 * variations. Note that the current state is >= HTTP_MSG_BODY,
4270 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004271 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004272 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02004273 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004274 txn->req.next -= old_o;
4275 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004276 }
4277
Mark Lamourinec2247f02012-01-04 13:02:01 -05004278 return 0;
4279}
4280
Willy Tarreau610ecce2010-01-04 21:15:02 +01004281/* Terminate current transaction and prepare a new one. This is very tricky
4282 * right now but it works.
4283 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004284void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004285{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004286 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004287 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004288 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004289 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004290 struct connection *srv_conn;
4291 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004292 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004293
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294 /* FIXME: We need a more portable way of releasing a backend's and a
4295 * server's connections. We need a safer way to reinitialize buffer
4296 * flags. We also need a more accurate method for computing per-request
4297 * data.
4298 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004299 /*
4300 * XXX cognet: This is probably wrong, this is killing a whole
4301 * connection, in the new world order, we probably want to just kill
4302 * the stream, this is to be revisited the day we handle multiple
4303 * streams in one server connection.
4304 */
4305 cs = objt_cs(s->si[1].end);
4306 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004307
Willy Tarreau4213a112013-12-15 10:25:42 +01004308 /* unless we're doing keep-alive, we want to quickly close the connection
4309 * to the server.
4310 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004311 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004312 !si_conn_ready(&s->si[1])) {
4313 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4314 si_shutr(&s->si[1]);
4315 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004316 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004317
Willy Tarreaue7dff022015-04-03 01:14:29 +02004318 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004319 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004320 if (unlikely(s->srv_conn))
4321 sess_change_server(s, NULL);
4322 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004323
4324 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004325 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004326
Willy Tarreaueee5b512015-04-03 23:46:31 +02004327 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004328 int n;
4329
Willy Tarreaueee5b512015-04-03 23:46:31 +02004330 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331 if (n < 1 || n > 5)
4332 n = 0;
4333
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004334 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004335 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004336 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004337 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004338 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004339 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4340 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004341 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004342 }
4343
4344 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004345 s->logs.bytes_in -= ci_data(&s->req);
4346 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004347
4348 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004349 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004350 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004351 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004352 s->do_log(s);
4353 }
4354
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004355 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004356 stream_stop_content_counters(s);
4357 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004358
Willy Tarreau610ecce2010-01-04 21:15:02 +01004359 s->logs.accept_date = date; /* user-visible date for logging */
4360 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004361 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4362 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004363 tv_zero(&s->logs.tv_request);
4364 s->logs.t_queue = -1;
4365 s->logs.t_connect = -1;
4366 s->logs.t_data = -1;
4367 s->logs.t_close = 0;
4368 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4369 s->logs.srv_queue_size = 0; /* we will get this number soon */
4370
Willy Tarreauf37954d2018-06-15 18:31:02 +02004371 s->logs.bytes_in = s->req.total = ci_data(&s->req);
4372 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004373
4374 if (s->pend_pos)
4375 pendconn_free(s->pend_pos);
4376
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004377 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004378 if (s->flags & SF_CURR_SESS) {
4379 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004380 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004381 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004382 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004383 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004384 }
4385
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004386 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004387
Willy Tarreau4213a112013-12-15 10:25:42 +01004388 /* only release our endpoint if we don't intend to reuse the
4389 * connection.
4390 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004391 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004392 !si_conn_ready(&s->si[1])) {
4393 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004394 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004395 }
4396
Willy Tarreau350f4872014-11-28 14:42:25 +01004397 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4398 s->si[1].err_type = SI_ET_NONE;
4399 s->si[1].conn_retries = 0; /* used for logging too */
4400 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004401 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004402 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
Christopher Fauletc5a9d5b2017-11-09 09:36:43 +01004403 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA|CF_WRITE_EVENT);
Willy Tarreaue7dff022015-04-03 01:14:29 +02004404 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4405 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4406 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004407
Willy Tarreaueee5b512015-04-03 23:46:31 +02004408 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004409 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004410 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004411
4412 if (prev_status == 401 || prev_status == 407) {
4413 /* In HTTP keep-alive mode, if we receive a 401, we still have
4414 * a chance of being able to send the visitor again to the same
4415 * server over the same connection. This is required by some
4416 * broken protocols such as NTLM, and anyway whenever there is
4417 * an opportunity for sending the challenge to the proper place,
4418 * it's better to do it (at least it helps with debugging).
4419 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004420 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004421 if (srv_conn)
4422 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004423 }
4424
Willy Tarreau53f96852016-02-02 18:50:47 +01004425 /* Never ever allow to reuse a connection from a non-reuse backend */
4426 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4427 srv_conn->flags |= CO_FL_PRIVATE;
4428
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004429 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004430 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004431
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004432 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004433 s->req.flags |= CF_NEVER_WAIT;
4434 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004435 }
4436
Willy Tarreau714ea782015-11-25 20:11:11 +01004437 /* we're removing the analysers, we MUST re-enable events detection.
4438 * We don't enable close on the response channel since it's either
4439 * already closed, or in keep-alive with an idle connection handler.
4440 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004441 channel_auto_read(&s->req);
4442 channel_auto_close(&s->req);
4443 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004444
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004445 /* we're in keep-alive with an idle connection, monitor it if not already done */
4446 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004447 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004448 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004449 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004450 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004451 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004452 else if (prev_flags & TX_NOT_FIRST)
4453 /* note: we check the request, not the connection, but
4454 * this is valid for strategies SAFE and AGGR, and in
4455 * case of ALWS, we don't care anyway.
4456 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004457 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004458 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004459 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004460 }
Christopher Faulete6006242017-03-10 11:52:44 +01004461 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4462 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004463}
4464
4465
4466/* This function updates the request state machine according to the response
4467 * state machine and buffer flags. It returns 1 if it changes anything (flag
4468 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4469 * it is only used to find when a request/response couple is complete. Both
4470 * this function and its equivalent should loop until both return zero. It
4471 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4472 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004473int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004474{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004475 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004476 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004477 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004478 unsigned int old_state = txn->req.msg_state;
4479
Christopher Faulet4be98032017-07-18 10:48:24 +02004480 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004481 return 0;
4482
4483 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004484 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004485 * We can shut the read side unless we want to abort_on_close,
4486 * or we have a POST request. The issue with POST requests is
4487 * that some browsers still send a CRLF after the request, and
4488 * this CRLF must be read so that it does not remain in the kernel
4489 * buffers, otherwise a close could cause an RST on some systems
4490 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004491 * Note that if we're using keep-alive on the client side, we'd
4492 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004493 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004494 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004495 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004496 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4497 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004498 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4499 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01004500 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004501 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004502
Willy Tarreau40f151a2012-12-20 12:10:09 +01004503 /* if the server closes the connection, we want to immediately react
4504 * and close the socket to save packets and syscalls.
4505 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004506 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004507
Willy Tarreau7f876a12015-11-18 11:59:55 +01004508 /* In any case we've finished parsing the request so we must
4509 * disable Nagle when sending data because 1) we're not going
4510 * to shut this side, and 2) the server is waiting for us to
4511 * send pending data.
4512 */
4513 chn->flags |= CF_NEVER_WAIT;
4514
Willy Tarreau610ecce2010-01-04 21:15:02 +01004515 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4516 goto wait_other_side;
4517
4518 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4519 /* The server has not finished to respond, so we
4520 * don't want to move in order not to upset it.
4521 */
4522 goto wait_other_side;
4523 }
4524
Willy Tarreau610ecce2010-01-04 21:15:02 +01004525 /* When we get here, it means that both the request and the
4526 * response have finished receiving. Depending on the connection
4527 * mode, we'll have to wait for the last bytes to leave in either
4528 * direction, and sometimes for a close to be effective.
4529 */
4530
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004531 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4532 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004533 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4534 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004535 }
4536 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4537 /* Option forceclose is set, or either side wants to close,
4538 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004539 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004540 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004541 *
4542 * However, there is an exception if the response
4543 * length is undefined. In this case, we need to wait
4544 * the close from the server. The response will be
4545 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004546 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004547 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4548 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4549 goto check_channel_flags;
4550
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004551 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4552 channel_shutr_now(chn);
4553 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004554 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004555 }
4556 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004557 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4558 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004559 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004560 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4561 channel_auto_read(chn);
4562 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004563 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564 }
4565
Christopher Faulet4be98032017-07-18 10:48:24 +02004566 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567 }
4568
4569 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4570 http_msg_closing:
4571 /* nothing else to forward, just waiting for the output buffer
4572 * to be empty and for the shutw_now to take effect.
4573 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004574 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004575 txn->req.msg_state = HTTP_MSG_CLOSED;
4576 goto http_msg_closed;
4577 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004578 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004579 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004580 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004581 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004582 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004583 }
4584
4585 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4586 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004587 /* if we don't know whether the server will close, we need to hard close */
4588 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4589 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4590
Willy Tarreau3988d932013-12-27 23:03:08 +01004591 /* see above in MSG_DONE why we only do this in these states */
4592 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4593 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004594 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4595 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004596 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004597 goto wait_other_side;
4598 }
4599
Christopher Faulet4be98032017-07-18 10:48:24 +02004600 check_channel_flags:
4601 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4602 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4603 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004604 txn->req.msg_state = HTTP_MSG_CLOSING;
4605 goto http_msg_closing;
4606 }
4607
4608
Willy Tarreau610ecce2010-01-04 21:15:02 +01004609 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004610 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004611}
4612
4613
4614/* This function updates the response state machine according to the request
4615 * state machine and buffer flags. It returns 1 if it changes anything (flag
4616 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4617 * it is only used to find when a request/response couple is complete. Both
4618 * this function and its equivalent should loop until both return zero. It
4619 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4620 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004621int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004622{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004623 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004624 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004625 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004626 unsigned int old_state = txn->rsp.msg_state;
4627
Christopher Faulet4be98032017-07-18 10:48:24 +02004628 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004629 return 0;
4630
4631 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4632 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004633 * still monitor the server connection for a possible close
4634 * while the request is being uploaded, so we don't disable
4635 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004636 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004637 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004638
4639 if (txn->req.msg_state == HTTP_MSG_ERROR)
4640 goto wait_other_side;
4641
4642 if (txn->req.msg_state < HTTP_MSG_DONE) {
4643 /* The client seems to still be sending data, probably
4644 * because we got an error response during an upload.
4645 * We have the choice of either breaking the connection
4646 * or letting it pass through. Let's do the later.
4647 */
4648 goto wait_other_side;
4649 }
4650
Willy Tarreau610ecce2010-01-04 21:15:02 +01004651 /* When we get here, it means that both the request and the
4652 * response have finished receiving. Depending on the connection
4653 * mode, we'll have to wait for the last bytes to leave in either
4654 * direction, and sometimes for a close to be effective.
4655 */
4656
4657 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4658 /* Server-close mode : shut read and wait for the request
4659 * side to close its output buffer. The caller will detect
4660 * when we're in DONE and the other is in CLOSED and will
4661 * catch that for the final cleanup.
4662 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004663 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4664 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004665 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004666 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4667 /* Option forceclose is set, or either side wants to close,
4668 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004669 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004670 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004671 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004672 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004673 channel_shutr_now(chn);
4674 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004675 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 }
4677 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004678 /* The last possible modes are keep-alive and tunnel. Tunnel will
4679 * need to forward remaining data. Keep-alive will need to monitor
4680 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004681 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004682 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004683 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004684 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4685 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004686 }
4687
Christopher Faulet4be98032017-07-18 10:48:24 +02004688 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004689 }
4690
4691 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4692 http_msg_closing:
4693 /* nothing else to forward, just waiting for the output buffer
4694 * to be empty and for the shutw_now to take effect.
4695 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004696 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004697 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4698 goto http_msg_closed;
4699 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004700 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004701 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004702 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004703 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004704 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004705 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004706 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004707 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004708 }
4709
4710 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4711 http_msg_closed:
4712 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004713 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004714 channel_auto_close(chn);
4715 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004716 goto wait_other_side;
4717 }
4718
Christopher Faulet4be98032017-07-18 10:48:24 +02004719 check_channel_flags:
4720 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4721 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4722 /* if we've just closed an output, let's switch */
4723 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4724 goto http_msg_closing;
4725 }
4726
Willy Tarreau610ecce2010-01-04 21:15:02 +01004727 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004728 /* We force the response to leave immediately if we're waiting for the
4729 * other side, since there is no pending shutdown to push it out.
4730 */
4731 if (!channel_is_empty(chn))
4732 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004733 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004734}
4735
4736
Christopher Faulet894da4c2017-07-18 11:29:07 +02004737/* Resync the request and response state machines. */
4738void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004739{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004740 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004741#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004742 int old_req_state = txn->req.msg_state;
4743 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004744#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745
Willy Tarreau610ecce2010-01-04 21:15:02 +01004746 http_sync_req_state(s);
4747 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004748 if (!http_sync_res_state(s))
4749 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004750 if (!http_sync_req_state(s))
4751 break;
4752 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004753
Christopher Faulet894da4c2017-07-18 11:29:07 +02004754 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4755 "req->analysers=0x%08x res->analysers=0x%08x\n",
4756 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004757 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4758 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004759 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004760
4761
Willy Tarreau610ecce2010-01-04 21:15:02 +01004762 /* OK, both state machines agree on a compatible state.
4763 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004764 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4765 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004766 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4767 * means we must abort the request.
4768 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4769 * corresponding channel.
4770 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4771 * on the response with server-close mode means we've completed one
4772 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004773 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004774 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4775 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004776 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004777 channel_auto_close(&s->req);
4778 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004779 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004780 channel_auto_close(&s->res);
4781 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004782 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004783 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4784 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004785 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004786 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004787 channel_auto_close(&s->res);
4788 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004789 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004790 channel_abort(&s->req);
4791 channel_auto_close(&s->req);
4792 channel_auto_read(&s->req);
4793 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004794 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004795 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4796 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4797 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4798 s->req.analysers &= AN_REQ_FLT_END;
4799 if (HAS_REQ_DATA_FILTERS(s))
4800 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4801 }
4802 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4803 s->res.analysers &= AN_RES_FLT_END;
4804 if (HAS_RSP_DATA_FILTERS(s))
4805 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4806 }
4807 channel_auto_close(&s->req);
4808 channel_auto_read(&s->req);
4809 channel_auto_close(&s->res);
4810 channel_auto_read(&s->res);
4811 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004812 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4813 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004814 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004815 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4816 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4817 /* server-close/keep-alive: terminate this transaction,
4818 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004819 * a fresh-new transaction, but only once we're sure there's
4820 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004821 * another request. They must not hold any pending output data
4822 * and the response buffer must realigned
4823 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004824 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004825 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004826 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004827 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004828 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004829 else {
4830 s->req.analysers = AN_REQ_FLT_END;
4831 s->res.analysers = AN_RES_FLT_END;
4832 txn->flags |= TX_WAIT_CLEANUP;
4833 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004834 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004835}
4836
Willy Tarreaud98cf932009-12-27 22:54:55 +01004837/* This function is an analyser which forwards request body (including chunk
4838 * sizes if any). It is called as soon as we must forward, even if we forward
4839 * zero byte. The only situation where it must not be called is when we're in
4840 * tunnel mode and we want to forward till the close. It's used both to forward
4841 * remaining data and to resync after end of body. It expects the msg_state to
4842 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004843 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004844 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004845 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004846 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004847int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004848{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004849 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004850 struct http_txn *txn = s->txn;
4851 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004852 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004853
Christopher Faulet814d2702017-03-30 11:33:44 +02004854 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4855 now_ms, __FUNCTION__,
4856 s,
4857 req,
4858 req->rex, req->wex,
4859 req->flags,
4860 req->buf->i,
4861 req->analysers);
4862
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004863 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4864 return 0;
4865
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004866 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004867 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004868 /* Output closed while we were sending data. We must abort and
4869 * wake the other side up.
4870 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004871 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004872 msg->msg_state = HTTP_MSG_ERROR;
4873 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004874 return 1;
4875 }
4876
Willy Tarreaud98cf932009-12-27 22:54:55 +01004877 /* Note that we don't have to send 100-continue back because we don't
4878 * need the data to complete our job, and it's up to the server to
4879 * decide whether to return 100, 417 or anything else in return of
4880 * an "Expect: 100-continue" header.
4881 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004882 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004883 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4884 ? HTTP_MSG_CHUNK_SIZE
4885 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004886
4887 /* TODO/filters: when http-buffer-request option is set or if a
4888 * rule on url_param exists, the first chunk size could be
4889 * already parsed. In that case, msg->next is after the chunk
4890 * size (including the CRLF after the size). So this case should
4891 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004892 }
4893
Willy Tarreau7ba23542014-04-17 21:50:00 +02004894 /* Some post-connect processing might want us to refrain from starting to
4895 * forward data. Currently, the only reason for this is "balance url_param"
4896 * whichs need to parse/process the request after we've enabled forwarding.
4897 */
4898 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004899 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004900 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004901 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004902 channel_dont_close(req); /* don't fail on early shutr */
4903 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004904 }
4905 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4906 }
4907
Willy Tarreau80a92c02014-03-12 10:41:13 +01004908 /* in most states, we should abort in case of early close */
4909 channel_auto_close(req);
4910
Willy Tarreauefdf0942014-04-24 20:08:57 +02004911 if (req->to_forward) {
4912 /* We can't process the buffer's contents yet */
4913 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004914 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004915 }
4916
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004917 if (msg->msg_state < HTTP_MSG_DONE) {
4918 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4919 ? http_msg_forward_chunked_body(s, msg)
4920 : http_msg_forward_body(s, msg));
4921 if (!ret)
4922 goto missing_data_or_waiting;
4923 if (ret < 0)
4924 goto return_bad_req;
4925 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004926
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004927 /* other states, DONE...TUNNEL */
4928 /* we don't want to forward closes on DONE except in tunnel mode. */
4929 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4930 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004931
Christopher Faulet894da4c2017-07-18 11:29:07 +02004932 http_resync_states(s);
4933 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004934 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4935 if (req->flags & CF_SHUTW) {
4936 /* request errors are most likely due to the
4937 * server aborting the transfer. */
4938 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004939 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004940 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004941 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004942 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004943 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004944 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004945 }
4946
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004947 /* If "option abortonclose" is set on the backend, we want to monitor
4948 * the client's connection and forward any shutdown notification to the
4949 * server, which will decide whether to close or to go on processing the
4950 * request. We only do that in tunnel mode, and not in other modes since
4951 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004952 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004953 channel_auto_read(req);
4954 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4955 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4956 s->si[1].flags |= SI_FL_NOLINGER;
4957 channel_auto_close(req);
4958 }
4959 else if (s->txn->meth == HTTP_METH_POST) {
4960 /* POST requests may require to read extra CRLF sent by broken
4961 * browsers and which could cause an RST to be sent upon close
4962 * on some systems (eg: Linux). */
4963 channel_auto_read(req);
4964 }
4965 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004966
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004967 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004968 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004969 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004970 if (!(s->flags & SF_ERR_MASK))
4971 s->flags |= SF_ERR_CLICL;
4972 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004973 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004974 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004975 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004976 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004977 }
4978
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004979 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4980 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004981 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004982 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004983
4984 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004985 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004986
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004987 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004988 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004989 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004990 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004991
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004992 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004993 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004994 * And when content-length is used, we never want to let the possible
4995 * shutdown be forwarded to the other side, as the state machine will
4996 * take care of it once the client responds. It's also important to
4997 * prevent TIME_WAITs from accumulating on the backend side, and for
4998 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004999 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01005000 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005001 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005002
Willy Tarreau5c620922011-05-11 19:56:11 +02005003 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005004 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005005 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005006 * modes are already handled by the stream sock layer. We must not do
5007 * this in content-length mode because it could present the MSG_MORE
5008 * flag with the last block of forwarded data, which would cause an
5009 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005010 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005011 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005012 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005013
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005014 return 0;
5015
Willy Tarreaud98cf932009-12-27 22:54:55 +01005016 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005017 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005018 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005019 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02005020
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005021 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005022 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005023 txn->req.msg_state = HTTP_MSG_ERROR;
5024 if (txn->status) {
5025 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005026 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005027 } else {
5028 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005029 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005030 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005031 req->analysers &= AN_REQ_FLT_END;
5032 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005033
Willy Tarreaue7dff022015-04-03 01:14:29 +02005034 if (!(s->flags & SF_ERR_MASK))
5035 s->flags |= SF_ERR_PRXCOND;
5036 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005037 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005038 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005039 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005040 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005041 }
5042 return 0;
5043
5044 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005045 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005046 txn->req.msg_state = HTTP_MSG_ERROR;
5047 if (txn->status) {
5048 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005049 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005050 } else {
5051 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005052 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005053 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005054 req->analysers &= AN_REQ_FLT_END;
5055 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005056
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005057 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5058 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005059 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005060 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005061
Willy Tarreaue7dff022015-04-03 01:14:29 +02005062 if (!(s->flags & SF_ERR_MASK))
5063 s->flags |= SF_ERR_SRVCL;
5064 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005065 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005066 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005067 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005068 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005069 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005070 return 0;
5071}
5072
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005073/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5074 * processing can continue on next analysers, or zero if it either needs more
5075 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005076 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005077 * when it has nothing left to do, and may remove any analyser when it wants to
5078 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005079 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005080int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005081{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005082 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005083 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005084 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005085 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005086 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005087 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005088 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005089
Willy Tarreau87b09662015-04-03 00:22:06 +02005090 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005091 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005092 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005093 rep,
5094 rep->rex, rep->wex,
5095 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005096 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005097 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005098
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005099 /*
5100 * Now parse the partial (or complete) lines.
5101 * We will check the response syntax, and also join multi-line
5102 * headers. An index of all the lines will be elaborated while
5103 * parsing.
5104 *
5105 * For the parsing, we use a 28 states FSM.
5106 *
5107 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02005108 * ci_head(rep) = beginning of response
5109 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
5110 * ci_tail(rep) = end of input data
5111 * msg->eol = end of current header or line (LF or CRLF)
5112 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005113 */
5114
Willy Tarreau628c40c2014-04-24 19:11:26 +02005115 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005116 /* There's a protected area at the end of the buffer for rewriting
5117 * purposes. We don't want to start to parse the request if the
5118 * protected area is affected, because we may have to move processed
5119 * data later, which is much more complicated.
5120 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02005121 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005122 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005123 /* some data has still not left the buffer, wake us once that's done */
5124 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5125 goto abort_response;
5126 channel_dont_close(rep);
5127 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005128 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005129 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005130 }
5131
Willy Tarreau188e2302018-06-15 11:11:53 +02005132 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005133 ci_tail(rep) > b_wrap(rep->buf) - global.tune.maxrewrite))
Willy Tarreaufd8d42f2018-07-12 10:57:15 +02005134 channel_slow_realign(rep, trash.str);
Willy Tarreau379357a2013-06-08 12:55:46 +02005135
Willy Tarreauf37954d2018-06-15 18:31:02 +02005136 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01005137 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005138 }
5139
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005140 /* 1: we might have to print this header in debug mode */
5141 if (unlikely((global.mode & MODE_DEBUG) &&
5142 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005143 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005144 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005145
Willy Tarreauf37954d2018-06-15 18:31:02 +02005146 sol = ci_head(rep);
5147 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005148 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005149
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005150 sol += hdr_idx_first_pos(&txn->hdr_idx);
5151 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005152
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005153 while (cur_idx) {
5154 eol = sol + txn->hdr_idx.v[cur_idx].len;
5155 debug_hdr("srvhdr", s, sol, eol);
5156 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5157 cur_idx = txn->hdr_idx.v[cur_idx].next;
5158 }
5159 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005160
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005161 /*
5162 * Now we quickly check if we have found a full valid response.
5163 * If not so, we check the FD and buffer states before leaving.
5164 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005165 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005166 * responses are checked first.
5167 *
5168 * Depending on whether the client is still there or not, we
5169 * may send an error response back or not. Note that normally
5170 * we should only check for HTTP status there, and check I/O
5171 * errors somewhere else.
5172 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005173
Willy Tarreau655dce92009-11-08 13:10:58 +01005174 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005175 /* Invalid response */
5176 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5177 /* we detected a parsing error. We want to archive this response
5178 * in the dedicated proxy area for later troubleshooting.
5179 */
5180 hdr_response_bad:
5181 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005182 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005183
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005184 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005185 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005186 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005187 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005188 }
Willy Tarreau64648412010-03-05 10:41:54 +01005189 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005190 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005191 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005192 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005193 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005194 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005195 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005196
Willy Tarreaue7dff022015-04-03 01:14:29 +02005197 if (!(s->flags & SF_ERR_MASK))
5198 s->flags |= SF_ERR_PRXCOND;
5199 if (!(s->flags & SF_FINST_MASK))
5200 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005201
5202 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005203 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005204
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005205 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02005206 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005207 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02005208 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005209 goto hdr_response_bad;
5210 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005211
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005212 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005213 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005214 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005215 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005216 else if (txn->flags & TX_NOT_FIRST)
5217 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005218
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005219 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005220 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005221 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005222 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005223 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005224
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005225 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005226 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005227 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005228
5229 /* Check to see if the server refused the early data.
5230 * If so, just send a 425
5231 */
5232 if (objt_cs(s->si[1].end)) {
5233 struct connection *conn = objt_cs(s->si[1].end)->conn;
5234
5235 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5236 txn->status = 425;
5237 }
5238
Willy Tarreau350f4872014-11-28 14:42:25 +01005239 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005240 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005241 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005242
Willy Tarreaue7dff022015-04-03 01:14:29 +02005243 if (!(s->flags & SF_ERR_MASK))
5244 s->flags |= SF_ERR_SRVCL;
5245 if (!(s->flags & SF_FINST_MASK))
5246 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005247 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005248 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005249
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005250 /* read timeout : return a 504 to the client. */
5251 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005252 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005253 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005254
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005255 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005256 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005257 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005258 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005259 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005260
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005261 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005262 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005263 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005264 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005265 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005266 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005267
Willy Tarreaue7dff022015-04-03 01:14:29 +02005268 if (!(s->flags & SF_ERR_MASK))
5269 s->flags |= SF_ERR_SRVTO;
5270 if (!(s->flags & SF_FINST_MASK))
5271 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005272 return 0;
5273 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005274
Willy Tarreauf003d372012-11-26 13:35:37 +01005275 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005276 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005277 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5278 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005279 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005280 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005281
Christopher Faulet0184ea72017-01-05 14:06:34 +01005282 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005283 channel_auto_close(rep);
5284
5285 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005286 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005287 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005288
Willy Tarreaue7dff022015-04-03 01:14:29 +02005289 if (!(s->flags & SF_ERR_MASK))
5290 s->flags |= SF_ERR_CLICL;
5291 if (!(s->flags & SF_FINST_MASK))
5292 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005293
Willy Tarreau87b09662015-04-03 00:22:06 +02005294 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005295 return 0;
5296 }
5297
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005298 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005299 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005300 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005301 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005302 else if (txn->flags & TX_NOT_FIRST)
5303 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005304
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005305 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005306 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005307 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005308 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005309 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005310
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005311 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005312 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005313 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005314 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005315 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005316 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005317
Willy Tarreaue7dff022015-04-03 01:14:29 +02005318 if (!(s->flags & SF_ERR_MASK))
5319 s->flags |= SF_ERR_SRVCL;
5320 if (!(s->flags & SF_FINST_MASK))
5321 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005322 return 0;
5323 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005324
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005325 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005326 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005327 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005328 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005329 else if (txn->flags & TX_NOT_FIRST)
5330 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005331
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005332 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005333 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005334 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005335
Willy Tarreaue7dff022015-04-03 01:14:29 +02005336 if (!(s->flags & SF_ERR_MASK))
5337 s->flags |= SF_ERR_CLICL;
5338 if (!(s->flags & SF_FINST_MASK))
5339 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005340
Willy Tarreau87b09662015-04-03 00:22:06 +02005341 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005342 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005343 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005344
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005345 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005346 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005347 return 0;
5348 }
5349
5350 /* More interesting part now : we know that we have a complete
5351 * response which at least looks like HTTP. We have an indicator
5352 * of each header's length, so we can parse them quickly.
5353 */
5354
5355 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005356 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005357
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005358 /*
5359 * 1: get the status code
5360 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005361 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005362 if (n < 1 || n > 5)
5363 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005364 /* when the client triggers a 4xx from the server, it's most often due
5365 * to a missing object or permission. These events should be tracked
5366 * because if they happen often, it may indicate a brute force or a
5367 * vulnerability scan.
5368 */
5369 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005370 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005371
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005372 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005373 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005374
Willy Tarreau91852eb2015-05-01 13:26:00 +02005375 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5376 * exactly one digit "." one digit. This check may be disabled using
5377 * option accept-invalid-http-response.
5378 */
5379 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5380 if (msg->sl.st.v_l != 8) {
5381 msg->err_pos = 0;
5382 goto hdr_response_bad;
5383 }
5384
Willy Tarreauf37954d2018-06-15 18:31:02 +02005385 if (ci_head(rep)[4] != '/' ||
5386 !isdigit((unsigned char)ci_head(rep)[5]) ||
5387 ci_head(rep)[6] != '.' ||
5388 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02005389 msg->err_pos = 4;
5390 goto hdr_response_bad;
5391 }
5392 }
5393
Willy Tarreau5b154472009-12-21 20:11:07 +01005394 /* check if the response is HTTP/1.1 or above */
5395 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02005396 ((ci_head(rep)[5] > '1') ||
5397 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005398 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005399
5400 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005401 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01005402
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005403 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005404 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005405
Willy Tarreauf37954d2018-06-15 18:31:02 +02005406 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005407
Willy Tarreau39650402010-03-15 19:44:39 +01005408 /* Adjust server's health based on status code. Note: status codes 501
5409 * and 505 are triggered on demand by client request, so we must not
5410 * count them as server failures.
5411 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005412 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005413 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005414 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005415 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005416 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005417 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005418
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005419 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005420 * We may be facing a 100-continue response, or any other informational
5421 * 1xx response which is non-final, in which case this is not the right
5422 * response, and we're waiting for the next one. Let's allow this response
5423 * to go to the client and wait for the next one. There's an exception for
5424 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005425 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005426 if (txn->status < 200 &&
5427 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005428 hdr_idx_init(&txn->hdr_idx);
5429 msg->next -= channel_forward(rep, msg->next);
5430 msg->msg_state = HTTP_MSG_RPBEFORE;
5431 txn->status = 0;
5432 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005433 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005434 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005435 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005436
Willy Tarreaua14ad722017-07-07 11:36:32 +02005437 /*
5438 * 2: check for cacheability.
5439 */
5440
5441 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005442 case 200:
5443 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005444 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005445 case 206:
5446 case 300:
5447 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005448 case 404:
5449 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005450 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005451 case 414:
5452 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005453 break;
5454 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005455 /* RFC7231#6.1:
5456 * Responses with status codes that are defined as
5457 * cacheable by default (e.g., 200, 203, 204, 206,
5458 * 300, 301, 404, 405, 410, 414, and 501 in this
5459 * specification) can be reused by a cache with
5460 * heuristic expiration unless otherwise indicated
5461 * by the method definition or explicit cache
5462 * controls [RFC7234]; all other status codes are
5463 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005464 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005465 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005466 break;
5467 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005468
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005469 /*
5470 * 3: we may need to capture headers
5471 */
5472 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005473 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02005474 capture_headers(ci_head(rep), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005475 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005476
Willy Tarreau557f1992015-05-01 10:05:17 +02005477 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5478 * by RFC7230#3.3.3 :
5479 *
5480 * The length of a message body is determined by one of the following
5481 * (in order of precedence):
5482 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005483 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5484 * the connection will become a tunnel immediately after the empty
5485 * line that concludes the header fields. A client MUST ignore
5486 * any Content-Length or Transfer-Encoding header fields received
5487 * in such a message. Any 101 response (Switching Protocols) is
5488 * managed in the same manner.
5489 *
5490 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005491 * (Informational), 204 (No Content), or 304 (Not Modified) status
5492 * code is always terminated by the first empty line after the
5493 * header fields, regardless of the header fields present in the
5494 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005495 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005496 * 3. If a Transfer-Encoding header field is present and the chunked
5497 * transfer coding (Section 4.1) is the final encoding, the message
5498 * body length is determined by reading and decoding the chunked
5499 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005500 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005501 * If a Transfer-Encoding header field is present in a response and
5502 * the chunked transfer coding is not the final encoding, the
5503 * message body length is determined by reading the connection until
5504 * it is closed by the server. If a Transfer-Encoding header field
5505 * is present in a request and the chunked transfer coding is not
5506 * the final encoding, the message body length cannot be determined
5507 * reliably; the server MUST respond with the 400 (Bad Request)
5508 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005509 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005510 * If a message is received with both a Transfer-Encoding and a
5511 * Content-Length header field, the Transfer-Encoding overrides the
5512 * Content-Length. Such a message might indicate an attempt to
5513 * perform request smuggling (Section 9.5) or response splitting
5514 * (Section 9.4) and ought to be handled as an error. A sender MUST
5515 * remove the received Content-Length field prior to forwarding such
5516 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005517 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005518 * 4. If a message is received without Transfer-Encoding and with
5519 * either multiple Content-Length header fields having differing
5520 * field-values or a single Content-Length header field having an
5521 * invalid value, then the message framing is invalid and the
5522 * recipient MUST treat it as an unrecoverable error. If this is a
5523 * request message, the server MUST respond with a 400 (Bad Request)
5524 * status code and then close the connection. If this is a response
5525 * message received by a proxy, the proxy MUST close the connection
5526 * to the server, discard the received response, and send a 502 (Bad
5527 * Gateway) response to the client. If this is a response message
5528 * received by a user agent, the user agent MUST close the
5529 * connection to the server and discard the received response.
5530 *
5531 * 5. If a valid Content-Length header field is present without
5532 * Transfer-Encoding, its decimal value defines the expected message
5533 * body length in octets. If the sender closes the connection or
5534 * the recipient times out before the indicated number of octets are
5535 * received, the recipient MUST consider the message to be
5536 * incomplete and close the connection.
5537 *
5538 * 6. If this is a request message and none of the above are true, then
5539 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005540 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005541 * 7. Otherwise, this is a response message without a declared message
5542 * body length, so the message body length is determined by the
5543 * number of octets received prior to the server closing the
5544 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005545 */
5546
5547 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005548 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005549 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005550 * FIXME: should we parse anyway and return an error on chunked encoding ?
5551 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005552 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5553 txn->status == 101)) {
5554 /* Either we've established an explicit tunnel, or we're
5555 * switching the protocol. In both cases, we're very unlikely
5556 * to understand the next protocols. We have to switch to tunnel
5557 * mode, so that we transfer the request and responses then let
5558 * this protocol pass unmodified. When we later implement specific
5559 * parsers for such protocols, we'll want to check the Upgrade
5560 * header which contains information about that protocol for
5561 * responses with status 101 (eg: see RFC2817 about TLS).
5562 */
5563 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5564 msg->flags |= HTTP_MSGF_XFER_LEN;
5565 goto end;
5566 }
5567
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005568 if (txn->meth == HTTP_METH_HEAD ||
5569 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005570 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005571 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005572 goto skip_content_length;
5573 }
5574
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005575 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005576 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005577 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005578 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005579 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5580 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005581 /* bad transfer-encoding (chunked followed by something else) */
5582 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005583 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005584 break;
5585 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005586 }
5587
Willy Tarreau1c913912015-04-30 10:57:51 +02005588 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005589 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005590 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005591 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005592 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5593 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005594 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005595 signed long long cl;
5596
Willy Tarreauad14f752011-09-02 20:33:27 +02005597 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005598 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005599 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005600 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005601
Willy Tarreauad14f752011-09-02 20:33:27 +02005602 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005603 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005604 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005605 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005606
Willy Tarreauad14f752011-09-02 20:33:27 +02005607 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005608 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005609 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005610 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005611
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005612 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005613 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005614 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005615 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005616
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005617 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005618 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005619 }
5620
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005621 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005622 /* Now we have to check if we need to modify the Connection header.
5623 * This is more difficult on the response than it is on the request,
5624 * because we can have two different HTTP versions and we don't know
5625 * how the client will interprete a response. For instance, let's say
5626 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5627 * HTTP/1.1 response without any header. Maybe it will bound itself to
5628 * HTTP/1.0 because it only knows about it, and will consider the lack
5629 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5630 * the lack of header as a keep-alive. Thus we will use two flags
5631 * indicating how a request MAY be understood by the client. In case
5632 * of multiple possibilities, we'll fix the header to be explicit. If
5633 * ambiguous cases such as both close and keepalive are seen, then we
5634 * will fall back to explicit close. Note that we won't take risks with
5635 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005636 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005637 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005638 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5639 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5640 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5641 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005642 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005643
Willy Tarreau70dffda2014-01-30 03:07:23 +01005644 /* this situation happens when combining pretend-keepalive with httpclose. */
5645 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005646 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005647 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005648 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5649
Willy Tarreau60466522010-01-18 19:08:45 +01005650 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005651 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005652 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5653 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005654
Willy Tarreau60466522010-01-18 19:08:45 +01005655 /* now adjust header transformations depending on current state */
5656 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5657 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5658 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005659 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005660 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005661 }
Willy Tarreau60466522010-01-18 19:08:45 +01005662 else { /* SCL / KAL */
5663 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005664 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005665 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005666 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005667
Willy Tarreau60466522010-01-18 19:08:45 +01005668 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005669 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005670
Willy Tarreau60466522010-01-18 19:08:45 +01005671 /* Some keep-alive responses are converted to Server-close if
5672 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005673 */
Willy Tarreau60466522010-01-18 19:08:45 +01005674 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5675 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005676 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005677 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005678 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005679 }
5680
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005681 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005682 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005683 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005684
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005685 /* end of job, return OK */
5686 rep->analysers &= ~an_bit;
5687 rep->analyse_exp = TICK_ETERNITY;
5688 channel_auto_close(rep);
5689 return 1;
5690
5691 abort_keep_alive:
5692 /* A keep-alive request to the server failed on a network error.
5693 * The client is required to retry. We need to close without returning
5694 * any other information so that the client retries.
5695 */
5696 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005697 rep->analysers &= AN_RES_FLT_END;
5698 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005699 channel_auto_close(rep);
5700 s->logs.logwait = 0;
5701 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005702 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005703 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005704 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005705 return 0;
5706}
5707
5708/* This function performs all the processing enabled for the current response.
5709 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005710 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005711 * other functions. It works like process_request (see indications above).
5712 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005713int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005714{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005715 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005716 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005717 struct http_msg *msg = &txn->rsp;
5718 struct proxy *cur_proxy;
5719 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005720 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005721
Willy Tarreau87b09662015-04-03 00:22:06 +02005722 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005723 now_ms, __FUNCTION__,
5724 s,
5725 rep,
5726 rep->rex, rep->wex,
5727 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005728 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005729 rep->analysers);
5730
5731 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5732 return 0;
5733
Willy Tarreau70730dd2014-04-24 18:06:27 +02005734 /* The stats applet needs to adjust the Connection header but we don't
5735 * apply any filter there.
5736 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005737 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5738 rep->analysers &= ~an_bit;
5739 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005740 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005741 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005742
Willy Tarreau58975672014-04-24 21:13:57 +02005743 /*
5744 * We will have to evaluate the filters.
5745 * As opposed to version 1.2, now they will be evaluated in the
5746 * filters order and not in the header order. This means that
5747 * each filter has to be validated among all headers.
5748 *
5749 * Filters are tried with ->be first, then with ->fe if it is
5750 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005751 *
5752 * Maybe we are in resume condiion. In this case I choose the
5753 * "struct proxy" which contains the rule list matching the resume
5754 * pointer. If none of theses "struct proxy" match, I initialise
5755 * the process with the first one.
5756 *
5757 * In fact, I check only correspondance betwwen the current list
5758 * pointer and the ->fe rule list. If it doesn't match, I initialize
5759 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005760 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005761 if (s->current_rule_list == &sess->fe->http_res_rules)
5762 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005763 else
5764 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005765 while (1) {
5766 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005767
Willy Tarreau58975672014-04-24 21:13:57 +02005768 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005769 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005770 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005771
Willy Tarreau51d861a2015-05-22 17:30:48 +02005772 if (ret == HTTP_RULE_RES_BADREQ)
5773 goto return_srv_prx_502;
5774
5775 if (ret == HTTP_RULE_RES_DONE) {
5776 rep->analysers &= ~an_bit;
5777 rep->analyse_exp = TICK_ETERNITY;
5778 return 1;
5779 }
5780 }
5781
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005782 /* we need to be called again. */
5783 if (ret == HTTP_RULE_RES_YIELD) {
5784 channel_dont_close(rep);
5785 return 0;
5786 }
5787
Willy Tarreau58975672014-04-24 21:13:57 +02005788 /* try headers filters */
5789 if (rule_set->rsp_exp != NULL) {
5790 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5791 return_bad_resp:
5792 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005793 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005794 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005795 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005796 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005797 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005798 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005799 txn->status = 502;
5800 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005801 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005802 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005803 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005804 if (!(s->flags & SF_ERR_MASK))
5805 s->flags |= SF_ERR_PRXCOND;
5806 if (!(s->flags & SF_FINST_MASK))
5807 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005808 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005809 }
Willy Tarreau58975672014-04-24 21:13:57 +02005810 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005811
Willy Tarreau58975672014-04-24 21:13:57 +02005812 /* has the response been denied ? */
5813 if (txn->flags & TX_SVDENY) {
5814 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005815 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005816
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005817 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5818 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005819 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005820 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005821
Willy Tarreau58975672014-04-24 21:13:57 +02005822 goto return_srv_prx_502;
5823 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005824
Willy Tarreau58975672014-04-24 21:13:57 +02005825 /* add response headers from the rule sets in the same order */
5826 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005827 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005828 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005829 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005830 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005831 ret = acl_pass(ret);
5832 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5833 ret = !ret;
5834 if (!ret)
5835 continue;
5836 }
5837 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5838 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005839 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005840
Willy Tarreau58975672014-04-24 21:13:57 +02005841 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005842 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005843 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005844 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005845 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005846
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005847 /* After this point, this anayzer can't return yield, so we can
5848 * remove the bit corresponding to this analyzer from the list.
5849 *
5850 * Note that the intermediate returns and goto found previously
5851 * reset the analyzers.
5852 */
5853 rep->analysers &= ~an_bit;
5854 rep->analyse_exp = TICK_ETERNITY;
5855
Willy Tarreau58975672014-04-24 21:13:57 +02005856 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005857 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005858 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005859
Willy Tarreau58975672014-04-24 21:13:57 +02005860 /*
5861 * Now check for a server cookie.
5862 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005863 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005864 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005865
Willy Tarreau58975672014-04-24 21:13:57 +02005866 /*
5867 * Check for cache-control or pragma headers if required.
5868 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005869 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005870 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005871
Willy Tarreau58975672014-04-24 21:13:57 +02005872 /*
5873 * Add server cookie in the response if needed
5874 */
5875 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5876 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005877 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005878 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5879 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5880 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5881 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5882 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005883 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005884 /* the server is known, it's not the one the client requested, or the
5885 * cookie's last seen date needs to be refreshed. We have to
5886 * insert a set-cookie here, except if we want to insert only on POST
5887 * requests and this one isn't. Note that servers which don't have cookies
5888 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005889 */
Willy Tarreau58975672014-04-24 21:13:57 +02005890 if (!objt_server(s->target)->cookie) {
5891 chunk_printf(&trash,
5892 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5893 s->be->cookie_name);
5894 }
5895 else {
5896 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005897
Willy Tarreau58975672014-04-24 21:13:57 +02005898 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5899 /* emit last_date, which is mandatory */
5900 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5901 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5902 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005903
Willy Tarreau58975672014-04-24 21:13:57 +02005904 if (s->be->cookie_maxlife) {
5905 /* emit first_date, which is either the original one or
5906 * the current date.
5907 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005908 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005909 s30tob64(txn->cookie_first_date ?
5910 txn->cookie_first_date >> 2 :
5911 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005912 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005913 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005914 }
Willy Tarreau58975672014-04-24 21:13:57 +02005915 chunk_appendf(&trash, "; path=/");
5916 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005917
Willy Tarreau58975672014-04-24 21:13:57 +02005918 if (s->be->cookie_domain)
5919 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005920
Willy Tarreau58975672014-04-24 21:13:57 +02005921 if (s->be->ck_opts & PR_CK_HTTPONLY)
5922 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005923
Willy Tarreau58975672014-04-24 21:13:57 +02005924 if (s->be->ck_opts & PR_CK_SECURE)
5925 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005926
Willy Tarreau58975672014-04-24 21:13:57 +02005927 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5928 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005929
Willy Tarreau58975672014-04-24 21:13:57 +02005930 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005931 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005932 /* the server did not change, only the date was updated */
5933 txn->flags |= TX_SCK_UPDATED;
5934 else
5935 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005936
Willy Tarreau58975672014-04-24 21:13:57 +02005937 /* Here, we will tell an eventual cache on the client side that we don't
5938 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5939 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5940 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005941 */
Willy Tarreau58975672014-04-24 21:13:57 +02005942 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005943
Willy Tarreau58975672014-04-24 21:13:57 +02005944 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005945
Willy Tarreau58975672014-04-24 21:13:57 +02005946 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5947 "Cache-control: private", 22) < 0))
5948 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005949 }
Willy Tarreau58975672014-04-24 21:13:57 +02005950 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005951
Willy Tarreau58975672014-04-24 21:13:57 +02005952 /*
5953 * Check if result will be cacheable with a cookie.
5954 * We'll block the response if security checks have caught
5955 * nasty things such as a cacheable cookie.
5956 */
5957 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5958 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5959 (s->be->options & PR_O_CHK_CACHE)) {
5960 /* we're in presence of a cacheable response containing
5961 * a set-cookie header. We'll block it as requested by
5962 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005963 */
Willy Tarreau58975672014-04-24 21:13:57 +02005964 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005965 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005966
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005967 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5968 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005969 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005970 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005971
Christopher Faulet767a84b2017-11-24 16:50:31 +01005972 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5973 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005974 send_log(s->be, LOG_ALERT,
5975 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5976 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5977 goto return_srv_prx_502;
5978 }
Willy Tarreau03945942009-12-22 16:50:27 +01005979
Willy Tarreau70730dd2014-04-24 18:06:27 +02005980 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005981 /*
5982 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5983 * If an "Upgrade" token is found, the header is left untouched in order
5984 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005985 * if anything but "Upgrade" is present in the Connection header. We don't
5986 * want to touch any 101 response either since it's switching to another
5987 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005988 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005989 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005990 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005991 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005992 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5993 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005994
Willy Tarreau58975672014-04-24 21:13:57 +02005995 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5996 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5997 /* we want a keep-alive response here. Keep-alive header
5998 * required if either side is not 1.1.
5999 */
6000 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6001 want_flags |= TX_CON_KAL_SET;
6002 }
6003 else {
6004 /* we want a close response here. Close header required if
6005 * the server is 1.1, regardless of the client.
6006 */
6007 if (msg->flags & HTTP_MSGF_VER_11)
6008 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006009 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006010
Willy Tarreau58975672014-04-24 21:13:57 +02006011 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6012 http_change_connection_header(txn, msg, want_flags);
6013 }
6014
6015 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02006016 /* Always enter in the body analyzer */
6017 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
6018 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006019
Willy Tarreau58975672014-04-24 21:13:57 +02006020 /* if the user wants to log as soon as possible, without counting
6021 * bytes from the server, then this is the right moment. We have
6022 * to temporarily assign bytes_out to log what we currently have.
6023 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006024 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006025 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6026 s->logs.bytes_out = txn->rsp.eoh;
6027 s->do_log(s);
6028 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006029 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006030 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006031}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006032
Willy Tarreaud98cf932009-12-27 22:54:55 +01006033/* This function is an analyser which forwards response body (including chunk
6034 * sizes if any). It is called as soon as we must forward, even if we forward
6035 * zero byte. The only situation where it must not be called is when we're in
6036 * tunnel mode and we want to forward till the close. It's used both to forward
6037 * remaining data and to resync after end of body. It expects the msg_state to
6038 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006039 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006040 *
6041 * It is capable of compressing response data both in content-length mode and
6042 * in chunked mode. The state machines follows different flows depending on
6043 * whether content-length and chunked modes are used, since there are no
6044 * trailers in content-length :
6045 *
6046 * chk-mode cl-mode
6047 * ,----- BODY -----.
6048 * / \
6049 * V size > 0 V chk-mode
6050 * .--> SIZE -------------> DATA -------------> CRLF
6051 * | | size == 0 | last byte |
6052 * | v final crlf v inspected |
6053 * | TRAILERS -----------> DONE |
6054 * | |
6055 * `----------------------------------------------'
6056 *
6057 * Compression only happens in the DATA state, and must be flushed in final
6058 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6059 * is performed at once on final states for all bytes parsed, or when leaving
6060 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006061 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006062int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006063{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006064 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006065 struct http_txn *txn = s->txn;
6066 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006067 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006068
Christopher Faulet814d2702017-03-30 11:33:44 +02006069 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6070 now_ms, __FUNCTION__,
6071 s,
6072 res,
6073 res->rex, res->wex,
6074 res->flags,
6075 res->buf->i,
6076 res->analysers);
6077
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006078 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6079 return 0;
6080
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006081 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02006082 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006083 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006084 /* Output closed while we were sending data. We must abort and
6085 * wake the other side up.
6086 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006087 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006088 msg->msg_state = HTTP_MSG_ERROR;
6089 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006090 return 1;
6091 }
6092
Willy Tarreau4fe41902010-06-07 22:27:41 +02006093 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006094 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006095
Christopher Fauletd7c91962015-04-30 11:48:27 +02006096 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006097 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6098 ? HTTP_MSG_CHUNK_SIZE
6099 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006100 }
6101
Willy Tarreauefdf0942014-04-24 20:08:57 +02006102 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006103 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006104 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006105 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006106 }
6107
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006108 if (msg->msg_state < HTTP_MSG_DONE) {
6109 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6110 ? http_msg_forward_chunked_body(s, msg)
6111 : http_msg_forward_body(s, msg));
6112 if (!ret)
6113 goto missing_data_or_waiting;
6114 if (ret < 0)
6115 goto return_bad_res;
6116 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006117
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006118 /* other states, DONE...TUNNEL */
6119 /* for keep-alive we don't want to forward closes on DONE */
6120 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6121 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6122 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006123
Christopher Faulet894da4c2017-07-18 11:29:07 +02006124 http_resync_states(s);
6125 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006126 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6127 if (res->flags & CF_SHUTW) {
6128 /* response errors are most likely due to the
6129 * client aborting the transfer. */
6130 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006131 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006132 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006133 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006134 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006135 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006136 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006137 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006138 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006139
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006140 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006141 if (res->flags & CF_SHUTW)
6142 goto aborted_xfer;
6143
6144 /* stop waiting for data if the input is closed before the end. If the
6145 * client side was already closed, it means that the client has aborted,
6146 * so we don't want to count this as a server abort. Otherwise it's a
6147 * server abort.
6148 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006149 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006150 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006151 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006152 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02006153 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006154 if (!(s->flags & SF_ERR_MASK))
6155 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006156 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006157 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006158 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006159 goto return_bad_res_stats_ok;
6160 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006161 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006162
Willy Tarreau40dba092010-03-04 18:14:51 +01006163 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006164 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006165 goto return_bad_res;
6166
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006167 /* When TE: chunked is used, we need to get there again to parse
6168 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006169 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6170 * or if there are filters registered on the stream, we don't want to
6171 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006172 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006173 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006174 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006175 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6176 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006177 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006178
Willy Tarreau5c620922011-05-11 19:56:11 +02006179 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006180 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006181 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006182 * modes are already handled by the stream sock layer. We must not do
6183 * this in content-length mode because it could present the MSG_MORE
6184 * flag with the last block of forwarded data, which would cause an
6185 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006186 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006187 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006188 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006189
Willy Tarreau87b09662015-04-03 00:22:06 +02006190 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006191 return 0;
6192
Willy Tarreau40dba092010-03-04 18:14:51 +01006193 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006194 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006195 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006196 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006197
6198 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006199 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006200 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006201 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006202 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006203 res->analysers &= AN_RES_FLT_END;
6204 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006205 if (objt_server(s->target))
6206 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006207
Willy Tarreaue7dff022015-04-03 01:14:29 +02006208 if (!(s->flags & SF_ERR_MASK))
6209 s->flags |= SF_ERR_PRXCOND;
6210 if (!(s->flags & SF_FINST_MASK))
6211 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006212 return 0;
6213
6214 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006215 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006216 txn->rsp.msg_state = HTTP_MSG_ERROR;
6217 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006218 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006219 res->analysers &= AN_RES_FLT_END;
6220 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006221
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006222 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6223 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006224 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006225 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006226
Willy Tarreaue7dff022015-04-03 01:14:29 +02006227 if (!(s->flags & SF_ERR_MASK))
6228 s->flags |= SF_ERR_CLICL;
6229 if (!(s->flags & SF_FINST_MASK))
6230 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006231 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006232}
6233
6234
6235static inline int
6236http_msg_forward_body(struct stream *s, struct http_msg *msg)
6237{
6238 struct channel *chn = msg->chn;
6239 int ret;
6240
6241 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6242
6243 if (msg->msg_state == HTTP_MSG_ENDING)
6244 goto ending;
6245
6246 /* Neither content-length, nor transfer-encoding was found, so we must
6247 * read the body until the server connection is closed. In that case, we
6248 * eat data as they come. Of course, this happens for response only. */
6249 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006250 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006251 msg->chunk_len += len;
6252 msg->body_len += len;
6253 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006254 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02006255 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01006256 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006257 msg->next += ret;
6258 msg->chunk_len -= ret;
6259 if (msg->chunk_len) {
6260 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006261 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006262 chn->flags |= CF_WAKE_WRITE;
6263 goto missing_data_or_waiting;
6264 }
6265
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006266 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6267 * always set for a request. */
6268 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6269 /* The server still sending data that should be filtered */
6270 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6271 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006272 msg->msg_state = HTTP_MSG_TUNNEL;
6273 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006274 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006275
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006276 msg->msg_state = HTTP_MSG_ENDING;
6277
6278 ending:
6279 /* we may have some pending data starting at res->buf->p such as a last
6280 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006281 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6282 /* default_ret */ msg->next,
6283 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006284 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006285 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006286 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6287 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006288 if (msg->next)
6289 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006290
Christopher Fauletda02e172015-12-04 09:25:05 +01006291 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6292 /* default_ret */ 1,
6293 /* on_error */ goto error,
6294 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006295 if (msg->msg_state == HTTP_MSG_ENDING)
6296 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006297 return 1;
6298
6299 missing_data_or_waiting:
6300 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006301 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6302 /* default_ret */ msg->next,
6303 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006304 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006305 msg->next -= ret;
6306 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6307 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006308 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006309 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006310 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006311 return 0;
6312 error:
6313 return -1;
6314}
6315
6316static inline int
6317http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6318{
6319 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006320 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006321 int ret;
6322
6323 /* Here we have the guarantee to be in one of the following state:
6324 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6325 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6326
6327 switch_states:
6328 switch (msg->msg_state) {
6329 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006330 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02006331 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01006332 /* on_error */ goto error);
6333 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006334 msg->chunk_len -= ret;
6335 if (msg->chunk_len) {
6336 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006337 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006338 chn->flags |= CF_WAKE_WRITE;
6339 goto missing_data_or_waiting;
6340 }
6341
6342 /* nothing left to forward for this chunk*/
6343 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6344 /* fall through for HTTP_MSG_CHUNK_CRLF */
6345
6346 case HTTP_MSG_CHUNK_CRLF:
6347 /* we want the CRLF after the data */
Willy Tarreaud760eec2018-07-10 09:50:25 +02006348 ret = h1_skip_chunk_crlf(chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006349 if (ret == 0)
6350 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006351 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006352 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02006353 if (msg->err_pos < 0)
6354 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006355 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006356 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006357 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006358 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6359 /* fall through for HTTP_MSG_CHUNK_SIZE */
6360
6361 case HTTP_MSG_CHUNK_SIZE:
6362 /* read the chunk size and assign it to ->chunk_len,
6363 * then set ->next to point to the body and switch to
6364 * DATA or TRAILERS state.
6365 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02006366 ret = h1_parse_chunk_size(chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006367 if (ret == 0)
6368 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006369 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006370 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006371 if (msg->err_pos < 0)
6372 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006373 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006374 }
6375
6376 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006377 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006378 msg->chunk_len = chunk;
6379 msg->body_len += chunk;
6380
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006381 if (msg->chunk_len) {
6382 msg->msg_state = HTTP_MSG_DATA;
6383 goto switch_states;
6384 }
6385 msg->msg_state = HTTP_MSG_TRAILERS;
6386 /* fall through for HTTP_MSG_TRAILERS */
6387
6388 case HTTP_MSG_TRAILERS:
6389 ret = http_forward_trailers(msg);
6390 if (ret < 0)
6391 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006392 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6393 /* default_ret */ 1,
6394 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006395 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006396 if (!ret)
6397 goto missing_data_or_waiting;
6398 break;
6399
6400 case HTTP_MSG_ENDING:
6401 goto ending;
6402
6403 default:
6404 /* This should no happen in this function */
6405 goto error;
6406 }
6407
6408 msg->msg_state = HTTP_MSG_ENDING;
6409 ending:
6410 /* we may have some pending data starting at res->buf->p such as a last
6411 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006412 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006413 /* default_ret */ msg->next,
6414 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006415 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006416 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006417 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6418 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006419 if (msg->next)
6420 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006421
Christopher Fauletda02e172015-12-04 09:25:05 +01006422 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006423 /* default_ret */ 1,
6424 /* on_error */ goto error,
6425 /* on_wait */ goto waiting);
6426 msg->msg_state = HTTP_MSG_DONE;
6427 return 1;
6428
6429 missing_data_or_waiting:
6430 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006431 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006432 /* default_ret */ msg->next,
6433 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006434 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006435 msg->next -= ret;
6436 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6437 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006438 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006439 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006440 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006441 return 0;
6442
6443 chunk_parsing_error:
6444 if (msg->err_pos >= 0) {
6445 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006446 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006447 msg->msg_state, strm_fe(s));
6448 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006449 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006450 msg, msg->msg_state, s->be);
6451 }
6452 error:
6453 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006454}
6455
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006456
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006457/* Iterate the same filter through all request headers.
6458 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006459 * Since it can manage the switch to another backend, it updates the per-proxy
6460 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006461 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006462int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006463{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006464 char *cur_ptr, *cur_end, *cur_next;
6465 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006466 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006467 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006468 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006470 last_hdr = 0;
6471
Willy Tarreauf37954d2018-06-15 18:31:02 +02006472 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006473 old_idx = 0;
6474
6475 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006476 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006477 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006478 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006479 (exp->action == ACT_ALLOW ||
6480 exp->action == ACT_DENY ||
6481 exp->action == ACT_TARPIT))
6482 return 0;
6483
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006484 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006485 if (!cur_idx)
6486 break;
6487
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006488 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006489 cur_ptr = cur_next;
6490 cur_end = cur_ptr + cur_hdr->len;
6491 cur_next = cur_end + cur_hdr->cr + 1;
6492
6493 /* Now we have one header between cur_ptr and cur_end,
6494 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006495 */
6496
Willy Tarreau15a53a42015-01-21 13:39:42 +01006497 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006498 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006499 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006500 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006501 last_hdr = 1;
6502 break;
6503
6504 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006505 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006506 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006507 break;
6508
6509 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006510 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006511 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006512 break;
6513
6514 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006515 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6516 if (trash.len < 0)
6517 return -1;
6518
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006519 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006520 /* FIXME: if the user adds a newline in the replacement, the
6521 * index will not be recalculated for now, and the new line
6522 * will not be counted as a new header.
6523 */
6524
6525 cur_end += delta;
6526 cur_next += delta;
6527 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006528 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006529 break;
6530
6531 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006532 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006533 cur_next += delta;
6534
Willy Tarreaufa355d42009-11-29 18:12:29 +01006535 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006536 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6537 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006538 cur_hdr->len = 0;
6539 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006540 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541 break;
6542
6543 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006544 }
6545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006546 /* keep the link from this header to next one in case of later
6547 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006548 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006549 old_idx = cur_idx;
6550 }
6551 return 0;
6552}
6553
6554
6555/* Apply the filter to the request line.
6556 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6557 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006558 * Since it can manage the switch to another backend, it updates the per-proxy
6559 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006560 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006561int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006562{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006563 char *cur_ptr, *cur_end;
6564 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006565 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006566 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006567
Willy Tarreau3d300592007-03-18 18:34:41 +01006568 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006569 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006570 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006571 (exp->action == ACT_ALLOW ||
6572 exp->action == ACT_DENY ||
6573 exp->action == ACT_TARPIT))
6574 return 0;
6575 else if (exp->action == ACT_REMOVE)
6576 return 0;
6577
6578 done = 0;
6579
Willy Tarreauf37954d2018-06-15 18:31:02 +02006580 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006581 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006582
6583 /* Now we have the request line between cur_ptr and cur_end */
6584
Willy Tarreau15a53a42015-01-21 13:39:42 +01006585 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006586 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006587 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006588 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006589 done = 1;
6590 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006592 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006593 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006594 done = 1;
6595 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006597 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006598 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006599 done = 1;
6600 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006602 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006603 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6604 if (trash.len < 0)
6605 return -1;
6606
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006607 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006608 /* FIXME: if the user adds a newline in the replacement, the
6609 * index will not be recalculated for now, and the new line
6610 * will not be counted as a new header.
6611 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006612
Willy Tarreaufa355d42009-11-29 18:12:29 +01006613 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006614 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006615 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006616 HTTP_MSG_RQMETH,
6617 cur_ptr, cur_end + 1,
6618 NULL, NULL);
6619 if (unlikely(!cur_end))
6620 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006621
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006622 /* we have a full request and we know that we have either a CR
6623 * or an LF at <ptr>.
6624 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006625 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6626 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006627 /* there is no point trying this regex on headers */
6628 return 1;
6629 }
6630 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006631 return done;
6632}
Willy Tarreau97de6242006-12-27 17:18:38 +01006633
Willy Tarreau58f10d72006-12-04 02:26:12 +01006634
Willy Tarreau58f10d72006-12-04 02:26:12 +01006635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006636/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006637 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006638 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006639 * unparsable request. Since it can manage the switch to another backend, it
6640 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006641 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006642int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006643{
Willy Tarreau192252e2015-04-04 01:47:55 +02006644 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006645 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006646 struct hdr_exp *exp;
6647
6648 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006649 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006650
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006651 /*
6652 * The interleaving of transformations and verdicts
6653 * makes it difficult to decide to continue or stop
6654 * the evaluation.
6655 */
6656
Willy Tarreau6c123b12010-01-28 20:22:06 +01006657 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6658 break;
6659
Willy Tarreau3d300592007-03-18 18:34:41 +01006660 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006661 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006662 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006663 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006664
6665 /* if this filter had a condition, evaluate it now and skip to
6666 * next filter if the condition does not match.
6667 */
6668 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006669 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006670 ret = acl_pass(ret);
6671 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6672 ret = !ret;
6673
6674 if (!ret)
6675 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006676 }
6677
6678 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006679 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006680 if (unlikely(ret < 0))
6681 return -1;
6682
6683 if (likely(ret == 0)) {
6684 /* The filter did not match the request, it can be
6685 * iterated through all headers.
6686 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006687 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6688 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006689 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006690 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006691 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006692}
6693
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006694
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006695/* Find the end of a cookie value contained between <s> and <e>. It works the
6696 * same way as with headers above except that the semi-colon also ends a token.
6697 * See RFC2965 for more information. Note that it requires a valid header to
6698 * return a valid result.
6699 */
6700char *find_cookie_value_end(char *s, const char *e)
6701{
6702 int quoted, qdpair;
6703
6704 quoted = qdpair = 0;
6705 for (; s < e; s++) {
6706 if (qdpair) qdpair = 0;
6707 else if (quoted) {
6708 if (*s == '\\') qdpair = 1;
6709 else if (*s == '"') quoted = 0;
6710 }
6711 else if (*s == '"') quoted = 1;
6712 else if (*s == ',' || *s == ';') return s;
6713 }
6714 return s;
6715}
6716
6717/* Delete a value in a header between delimiters <from> and <next> in buffer
6718 * <buf>. The number of characters displaced is returned, and the pointer to
6719 * the first delimiter is updated if required. The function tries as much as
6720 * possible to respect the following principles :
6721 * - replace <from> delimiter by the <next> one unless <from> points to a
6722 * colon, in which case <next> is simply removed
6723 * - set exactly one space character after the new first delimiter, unless
6724 * there are not enough characters in the block being moved to do so.
6725 * - remove unneeded spaces before the previous delimiter and after the new
6726 * one.
6727 *
6728 * It is the caller's responsibility to ensure that :
6729 * - <from> points to a valid delimiter or the colon ;
6730 * - <next> points to a valid delimiter or the final CR/LF ;
6731 * - there are non-space chars before <from> ;
6732 * - there is a CR/LF at or after <next>.
6733 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006734int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006735{
6736 char *prev = *from;
6737
6738 if (*prev == ':') {
6739 /* We're removing the first value, preserve the colon and add a
6740 * space if possible.
6741 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006742 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006743 next++;
6744 prev++;
6745 if (prev < next)
6746 *prev++ = ' ';
6747
Willy Tarreau2235b262016-11-05 15:50:20 +01006748 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006749 next++;
6750 } else {
6751 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006752 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006753 prev--;
6754 *from = prev;
6755
6756 /* copy the delimiter and if possible a space if we're
6757 * not at the end of the line.
6758 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006759 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006760 *prev++ = *next++;
6761 if (prev + 1 < next)
6762 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006763 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006764 next++;
6765 }
6766 }
6767 return buffer_replace2(buf, prev, next, NULL, 0);
6768}
6769
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006770/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006771 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006772 * desirable to call it only when needed. This code is quite complex because
6773 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6774 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006775 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006776void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006777{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006778 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006779 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006780 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006781 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006782 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6783 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006784
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006785 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006786 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006787 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006788
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006789 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006790 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006791 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006792
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006793 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006794 hdr_beg = hdr_next;
6795 hdr_end = hdr_beg + cur_hdr->len;
6796 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006797
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006798 /* We have one full header between hdr_beg and hdr_end, and the
6799 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006800 * "Cookie:" headers.
6801 */
6802
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006803 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006804 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006805 old_idx = cur_idx;
6806 continue;
6807 }
6808
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006809 del_from = NULL; /* nothing to be deleted */
6810 preserve_hdr = 0; /* assume we may kill the whole header */
6811
Willy Tarreau58f10d72006-12-04 02:26:12 +01006812 /* Now look for cookies. Conforming to RFC2109, we have to support
6813 * attributes whose name begin with a '$', and associate them with
6814 * the right cookie, if we want to delete this cookie.
6815 * So there are 3 cases for each cookie read :
6816 * 1) it's a special attribute, beginning with a '$' : ignore it.
6817 * 2) it's a server id cookie that we *MAY* want to delete : save
6818 * some pointers on it (last semi-colon, beginning of cookie...)
6819 * 3) it's an application cookie : we *MAY* have to delete a previous
6820 * "special" cookie.
6821 * At the end of loop, if a "special" cookie remains, we may have to
6822 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006823 * *MUST* delete it.
6824 *
6825 * Note: RFC2965 is unclear about the processing of spaces around
6826 * the equal sign in the ATTR=VALUE form. A careful inspection of
6827 * the RFC explicitly allows spaces before it, and not within the
6828 * tokens (attrs or values). An inspection of RFC2109 allows that
6829 * too but section 10.1.3 lets one think that spaces may be allowed
6830 * after the equal sign too, resulting in some (rare) buggy
6831 * implementations trying to do that. So let's do what servers do.
6832 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6833 * allowed quoted strings in values, with any possible character
6834 * after a backslash, including control chars and delimitors, which
6835 * causes parsing to become ambiguous. Browsers also allow spaces
6836 * within values even without quotes.
6837 *
6838 * We have to keep multiple pointers in order to support cookie
6839 * removal at the beginning, middle or end of header without
6840 * corrupting the header. All of these headers are valid :
6841 *
6842 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6843 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6844 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6845 * | | | | | | | | |
6846 * | | | | | | | | hdr_end <--+
6847 * | | | | | | | +--> next
6848 * | | | | | | +----> val_end
6849 * | | | | | +-----------> val_beg
6850 * | | | | +--------------> equal
6851 * | | | +----------------> att_end
6852 * | | +---------------------> att_beg
6853 * | +--------------------------> prev
6854 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006855 */
6856
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006857 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6858 /* Iterate through all cookies on this line */
6859
6860 /* find att_beg */
6861 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006862 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006863 att_beg++;
6864
6865 /* find att_end : this is the first character after the last non
6866 * space before the equal. It may be equal to hdr_end.
6867 */
6868 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006869
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006870 while (equal < hdr_end) {
6871 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006872 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006873 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006874 continue;
6875 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006876 }
6877
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006878 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6879 * is between <att_beg> and <equal>, both may be identical.
6880 */
6881
6882 /* look for end of cookie if there is an equal sign */
6883 if (equal < hdr_end && *equal == '=') {
6884 /* look for the beginning of the value */
6885 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006886 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006887 val_beg++;
6888
6889 /* find the end of the value, respecting quotes */
6890 next = find_cookie_value_end(val_beg, hdr_end);
6891
6892 /* make val_end point to the first white space or delimitor after the value */
6893 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006894 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006895 val_end--;
6896 } else {
6897 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006898 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006899
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006900 /* We have nothing to do with attributes beginning with '$'. However,
6901 * they will automatically be removed if a header before them is removed,
6902 * since they're supposed to be linked together.
6903 */
6904 if (*att_beg == '$')
6905 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006906
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006907 /* Ignore cookies with no equal sign */
6908 if (equal == next) {
6909 /* This is not our cookie, so we must preserve it. But if we already
6910 * scheduled another cookie for removal, we cannot remove the
6911 * complete header, but we can remove the previous block itself.
6912 */
6913 preserve_hdr = 1;
6914 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006915 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006916 val_end += delta;
6917 next += delta;
6918 hdr_end += delta;
6919 hdr_next += delta;
6920 cur_hdr->len += delta;
6921 http_msg_move_end(&txn->req, delta);
6922 prev = del_from;
6923 del_from = NULL;
6924 }
6925 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006926 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006927
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006928 /* if there are spaces around the equal sign, we need to
6929 * strip them otherwise we'll get trouble for cookie captures,
6930 * or even for rewrites. Since this happens extremely rarely,
6931 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006932 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006933 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6934 int stripped_before = 0;
6935 int stripped_after = 0;
6936
6937 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006938 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006939 equal += stripped_before;
6940 val_beg += stripped_before;
6941 }
6942
6943 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006944 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006945 val_beg += stripped_after;
6946 stripped_before += stripped_after;
6947 }
6948
6949 val_end += stripped_before;
6950 next += stripped_before;
6951 hdr_end += stripped_before;
6952 hdr_next += stripped_before;
6953 cur_hdr->len += stripped_before;
6954 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006955 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006956 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006957
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006958 /* First, let's see if we want to capture this cookie. We check
6959 * that we don't already have a client side cookie, because we
6960 * can only capture one. Also as an optimisation, we ignore
6961 * cookies shorter than the declared name.
6962 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006963 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6964 (val_end - att_beg >= sess->fe->capture_namelen) &&
6965 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006966 int log_len = val_end - att_beg;
6967
Willy Tarreaubafbe012017-11-24 17:34:44 +01006968 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006969 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006970 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006971 if (log_len > sess->fe->capture_len)
6972 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006973 memcpy(txn->cli_cookie, att_beg, log_len);
6974 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006975 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006976 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006977
Willy Tarreaubca99692010-10-06 19:25:55 +02006978 /* Persistence cookies in passive, rewrite or insert mode have the
6979 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006980 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006981 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006982 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006983 * For cookies in prefix mode, the form is :
6984 *
6985 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006986 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006987 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6988 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6989 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006990 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006991
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006992 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6993 * have the server ID between val_beg and delim, and the original cookie between
6994 * delim+1 and val_end. Otherwise, delim==val_end :
6995 *
6996 * Cookie: NAME=SRV; # in all but prefix modes
6997 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6998 * | || || | |+-> next
6999 * | || || | +--> val_end
7000 * | || || +---------> delim
7001 * | || |+------------> val_beg
7002 * | || +-------------> att_end = equal
7003 * | |+-----------------> att_beg
7004 * | +------------------> prev
7005 * +-------------------------> hdr_beg
7006 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007007
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007008 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007009 for (delim = val_beg; delim < val_end; delim++)
7010 if (*delim == COOKIE_DELIM)
7011 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007012 } else {
7013 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007014 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007015 /* Now check if the cookie contains a date field, which would
7016 * appear after a vertical bar ('|') just after the server name
7017 * and before the delimiter.
7018 */
7019 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7020 if (vbar1) {
7021 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007022 * right is the last seen date. It is a base64 encoded
7023 * 30-bit value representing the UNIX date since the
7024 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007025 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007026 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007027 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007028 if (val_end - vbar1 >= 5) {
7029 val = b64tos30(vbar1);
7030 if (val > 0)
7031 txn->cookie_last_date = val << 2;
7032 }
7033 /* look for a second vertical bar */
7034 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7035 if (vbar1 && (val_end - vbar1 > 5)) {
7036 val = b64tos30(vbar1 + 1);
7037 if (val > 0)
7038 txn->cookie_first_date = val << 2;
7039 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007040 }
7041 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007042
Willy Tarreauf64d1412010-10-07 20:06:11 +02007043 /* if the cookie has an expiration date and the proxy wants to check
7044 * it, then we do that now. We first check if the cookie is too old,
7045 * then only if it has expired. We detect strict overflow because the
7046 * time resolution here is not great (4 seconds). Cookies with dates
7047 * in the future are ignored if their offset is beyond one day. This
7048 * allows an admin to fix timezone issues without expiring everyone
7049 * and at the same time avoids keeping unwanted side effects for too
7050 * long.
7051 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007052 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7053 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007054 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007055 txn->flags &= ~TX_CK_MASK;
7056 txn->flags |= TX_CK_OLD;
7057 delim = val_beg; // let's pretend we have not found the cookie
7058 txn->cookie_first_date = 0;
7059 txn->cookie_last_date = 0;
7060 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007061 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7062 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007063 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007064 txn->flags &= ~TX_CK_MASK;
7065 txn->flags |= TX_CK_EXPIRED;
7066 delim = val_beg; // let's pretend we have not found the cookie
7067 txn->cookie_first_date = 0;
7068 txn->cookie_last_date = 0;
7069 }
7070
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007071 /* Here, we'll look for the first running server which supports the cookie.
7072 * This allows to share a same cookie between several servers, for example
7073 * to dedicate backup servers to specific servers only.
7074 * However, to prevent clients from sticking to cookie-less backup server
7075 * when they have incidentely learned an empty cookie, we simply ignore
7076 * empty cookies and mark them as invalid.
7077 * The same behaviour is applied when persistence must be ignored.
7078 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007079 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007080 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007082 while (srv) {
7083 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7084 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007085 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007086 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007087 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 /* we found the server and we can use it */
7089 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007090 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007091 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007092 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007093 break;
7094 } else {
7095 /* we found a server, but it's down,
7096 * mark it as such and go on in case
7097 * another one is available.
7098 */
7099 txn->flags &= ~TX_CK_MASK;
7100 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007101 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007102 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 srv = srv->next;
7104 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007105
Willy Tarreauf64d1412010-10-07 20:06:11 +02007106 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007107 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007108 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007109 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007110 txn->flags |= TX_CK_UNUSED;
7111 else
7112 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007113 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007114
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007115 /* depending on the cookie mode, we may have to either :
7116 * - delete the complete cookie if we're in insert+indirect mode, so that
7117 * the server never sees it ;
7118 * - remove the server id from the cookie value, and tag the cookie as an
7119 * application cookie so that it does not get accidentely removed later,
7120 * if we're in cookie prefix mode
7121 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007122 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007123 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007124
Willy Tarreau9b28e032012-10-12 23:49:43 +02007125 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007126 val_end += delta;
7127 next += delta;
7128 hdr_end += delta;
7129 hdr_next += delta;
7130 cur_hdr->len += delta;
7131 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007132
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007133 del_from = NULL;
7134 preserve_hdr = 1; /* we want to keep this cookie */
7135 }
7136 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007137 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007138 del_from = prev;
7139 }
7140 } else {
7141 /* This is not our cookie, so we must preserve it. But if we already
7142 * scheduled another cookie for removal, we cannot remove the
7143 * complete header, but we can remove the previous block itself.
7144 */
7145 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007146
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007147 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007148 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007149 if (att_beg >= del_from)
7150 att_beg += delta;
7151 if (att_end >= del_from)
7152 att_end += delta;
7153 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007154 val_end += delta;
7155 next += delta;
7156 hdr_end += delta;
7157 hdr_next += delta;
7158 cur_hdr->len += delta;
7159 http_msg_move_end(&txn->req, delta);
7160 prev = del_from;
7161 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007162 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007163 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007164
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007165 /* continue with next cookie on this header line */
7166 att_beg = next;
7167 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007168
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007169 /* There are no more cookies on this line.
7170 * We may still have one (or several) marked for deletion at the
7171 * end of the line. We must do this now in two ways :
7172 * - if some cookies must be preserved, we only delete from the
7173 * mark to the end of line ;
7174 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007175 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007176 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007177 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007178 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007179 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007180 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007181 cur_hdr->len += delta;
7182 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007183 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007184
7185 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007186 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7187 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007188 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007189 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007190 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007191 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007192 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007193 }
7194
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007195 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007196 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007197 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007198}
7199
7200
Willy Tarreaua15645d2007-03-18 16:22:39 +01007201/* Iterate the same filter through all response headers contained in <rtr>.
7202 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7203 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007204int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007205{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 char *cur_ptr, *cur_end, *cur_next;
7207 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007208 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007209 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007210 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007211
7212 last_hdr = 0;
7213
Willy Tarreauf37954d2018-06-15 18:31:02 +02007214 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007215 old_idx = 0;
7216
7217 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007218 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007219 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007220 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007221 (exp->action == ACT_ALLOW ||
7222 exp->action == ACT_DENY))
7223 return 0;
7224
7225 cur_idx = txn->hdr_idx.v[old_idx].next;
7226 if (!cur_idx)
7227 break;
7228
7229 cur_hdr = &txn->hdr_idx.v[cur_idx];
7230 cur_ptr = cur_next;
7231 cur_end = cur_ptr + cur_hdr->len;
7232 cur_next = cur_end + cur_hdr->cr + 1;
7233
7234 /* Now we have one header between cur_ptr and cur_end,
7235 * and the next header starts at cur_next.
7236 */
7237
Willy Tarreau15a53a42015-01-21 13:39:42 +01007238 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007239 switch (exp->action) {
7240 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007241 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242 last_hdr = 1;
7243 break;
7244
7245 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007246 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247 last_hdr = 1;
7248 break;
7249
7250 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007251 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7252 if (trash.len < 0)
7253 return -1;
7254
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007255 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007256 /* FIXME: if the user adds a newline in the replacement, the
7257 * index will not be recalculated for now, and the new line
7258 * will not be counted as a new header.
7259 */
7260
7261 cur_end += delta;
7262 cur_next += delta;
7263 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007264 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007265 break;
7266
7267 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007268 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269 cur_next += delta;
7270
Willy Tarreaufa355d42009-11-29 18:12:29 +01007271 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007272 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7273 txn->hdr_idx.used--;
7274 cur_hdr->len = 0;
7275 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007276 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007277 break;
7278
7279 }
7280 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281
7282 /* keep the link from this header to next one in case of later
7283 * removal of next header.
7284 */
7285 old_idx = cur_idx;
7286 }
7287 return 0;
7288}
7289
7290
7291/* Apply the filter to the status line in the response buffer <rtr>.
7292 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7293 * or -1 if a replacement resulted in an invalid status line.
7294 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007295int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007296{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007297 char *cur_ptr, *cur_end;
7298 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007299 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007300 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301
7302
Willy Tarreau3d300592007-03-18 18:34:41 +01007303 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007304 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007305 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007306 (exp->action == ACT_ALLOW ||
7307 exp->action == ACT_DENY))
7308 return 0;
7309 else if (exp->action == ACT_REMOVE)
7310 return 0;
7311
7312 done = 0;
7313
Willy Tarreauf37954d2018-06-15 18:31:02 +02007314 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007315 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007316
7317 /* Now we have the status line between cur_ptr and cur_end */
7318
Willy Tarreau15a53a42015-01-21 13:39:42 +01007319 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007320 switch (exp->action) {
7321 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007322 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007323 done = 1;
7324 break;
7325
7326 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007327 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007328 done = 1;
7329 break;
7330
7331 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007332 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7333 if (trash.len < 0)
7334 return -1;
7335
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007336 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337 /* FIXME: if the user adds a newline in the replacement, the
7338 * index will not be recalculated for now, and the new line
7339 * will not be counted as a new header.
7340 */
7341
Willy Tarreaufa355d42009-11-29 18:12:29 +01007342 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007344 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007345 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007346 cur_ptr, cur_end + 1,
7347 NULL, NULL);
7348 if (unlikely(!cur_end))
7349 return -1;
7350
7351 /* we have a full respnse and we know that we have either a CR
7352 * or an LF at <ptr>.
7353 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007354 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007355 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007356 /* there is no point trying this regex on headers */
7357 return 1;
7358 }
7359 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007360 return done;
7361}
7362
7363
7364
7365/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007366 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007367 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7368 * unparsable response.
7369 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007370int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007371{
Willy Tarreau192252e2015-04-04 01:47:55 +02007372 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007373 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007374 struct hdr_exp *exp;
7375
7376 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007377 int ret;
7378
7379 /*
7380 * The interleaving of transformations and verdicts
7381 * makes it difficult to decide to continue or stop
7382 * the evaluation.
7383 */
7384
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007385 if (txn->flags & TX_SVDENY)
7386 break;
7387
Willy Tarreau3d300592007-03-18 18:34:41 +01007388 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007389 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7390 exp->action == ACT_PASS)) {
7391 exp = exp->next;
7392 continue;
7393 }
7394
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007395 /* if this filter had a condition, evaluate it now and skip to
7396 * next filter if the condition does not match.
7397 */
7398 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007399 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007400 ret = acl_pass(ret);
7401 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7402 ret = !ret;
7403 if (!ret)
7404 continue;
7405 }
7406
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007408 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007409 if (unlikely(ret < 0))
7410 return -1;
7411
7412 if (likely(ret == 0)) {
7413 /* The filter did not match the response, it can be
7414 * iterated through all headers.
7415 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007416 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7417 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007418 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007419 }
7420 return 0;
7421}
7422
7423
Willy Tarreaua15645d2007-03-18 16:22:39 +01007424/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007425 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007426 * desirable to call it only when needed. This function is also used when we
7427 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007428 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007429void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007430{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007431 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007432 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007433 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007434 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007435 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007436 char *hdr_beg, *hdr_end, *hdr_next;
7437 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007438
Willy Tarreaua15645d2007-03-18 16:22:39 +01007439 /* Iterate through the headers.
7440 * we start with the start line.
7441 */
7442 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007443 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007444
7445 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7446 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007447 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007448
7449 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007450 hdr_beg = hdr_next;
7451 hdr_end = hdr_beg + cur_hdr->len;
7452 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007453
Willy Tarreau24581ba2010-08-31 22:39:35 +02007454 /* We have one full header between hdr_beg and hdr_end, and the
7455 * next header starts at hdr_next. We're only interested in
7456 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007457 */
7458
Willy Tarreau24581ba2010-08-31 22:39:35 +02007459 is_cookie2 = 0;
7460 prev = hdr_beg + 10;
7461 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007462 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007463 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7464 if (!val) {
7465 old_idx = cur_idx;
7466 continue;
7467 }
7468 is_cookie2 = 1;
7469 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007470 }
7471
Willy Tarreau24581ba2010-08-31 22:39:35 +02007472 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7473 * <prev> points to the colon.
7474 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007475 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007476
Willy Tarreau24581ba2010-08-31 22:39:35 +02007477 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7478 * check-cache is enabled) and we are not interested in checking
7479 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007480 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007481 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007482 return;
7483
Willy Tarreau24581ba2010-08-31 22:39:35 +02007484 /* OK so now we know we have to process this response cookie.
7485 * The format of the Set-Cookie header is slightly different
7486 * from the format of the Cookie header in that it does not
7487 * support the comma as a cookie delimiter (thus the header
7488 * cannot be folded) because the Expires attribute described in
7489 * the original Netscape's spec may contain an unquoted date
7490 * with a comma inside. We have to live with this because
7491 * many browsers don't support Max-Age and some browsers don't
7492 * support quoted strings. However the Set-Cookie2 header is
7493 * clean.
7494 *
7495 * We have to keep multiple pointers in order to support cookie
7496 * removal at the beginning, middle or end of header without
7497 * corrupting the header (in case of set-cookie2). A special
7498 * pointer, <scav> points to the beginning of the set-cookie-av
7499 * fields after the first semi-colon. The <next> pointer points
7500 * either to the end of line (set-cookie) or next unquoted comma
7501 * (set-cookie2). All of these headers are valid :
7502 *
7503 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7504 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7505 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7506 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7507 * | | | | | | | | | |
7508 * | | | | | | | | +-> next hdr_end <--+
7509 * | | | | | | | +------------> scav
7510 * | | | | | | +--------------> val_end
7511 * | | | | | +--------------------> val_beg
7512 * | | | | +----------------------> equal
7513 * | | | +------------------------> att_end
7514 * | | +----------------------------> att_beg
7515 * | +------------------------------> prev
7516 * +-----------------------------------------> hdr_beg
7517 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007518
Willy Tarreau24581ba2010-08-31 22:39:35 +02007519 for (; prev < hdr_end; prev = next) {
7520 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007521
Willy Tarreau24581ba2010-08-31 22:39:35 +02007522 /* find att_beg */
7523 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007524 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007525 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007526
Willy Tarreau24581ba2010-08-31 22:39:35 +02007527 /* find att_end : this is the first character after the last non
7528 * space before the equal. It may be equal to hdr_end.
7529 */
7530 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007531
Willy Tarreau24581ba2010-08-31 22:39:35 +02007532 while (equal < hdr_end) {
7533 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7534 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007535 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007536 continue;
7537 att_end = equal;
7538 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007539
Willy Tarreau24581ba2010-08-31 22:39:35 +02007540 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7541 * is between <att_beg> and <equal>, both may be identical.
7542 */
7543
7544 /* look for end of cookie if there is an equal sign */
7545 if (equal < hdr_end && *equal == '=') {
7546 /* look for the beginning of the value */
7547 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007548 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007549 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007550
Willy Tarreau24581ba2010-08-31 22:39:35 +02007551 /* find the end of the value, respecting quotes */
7552 next = find_cookie_value_end(val_beg, hdr_end);
7553
7554 /* make val_end point to the first white space or delimitor after the value */
7555 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007556 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007557 val_end--;
7558 } else {
7559 /* <equal> points to next comma, semi-colon or EOL */
7560 val_beg = val_end = next = equal;
7561 }
7562
7563 if (next < hdr_end) {
7564 /* Set-Cookie2 supports multiple cookies, and <next> points to
7565 * a colon or semi-colon before the end. So skip all attr-value
7566 * pairs and look for the next comma. For Set-Cookie, since
7567 * commas are permitted in values, skip to the end.
7568 */
7569 if (is_cookie2)
7570 next = find_hdr_value_end(next, hdr_end);
7571 else
7572 next = hdr_end;
7573 }
7574
7575 /* Now everything is as on the diagram above */
7576
7577 /* Ignore cookies with no equal sign */
7578 if (equal == val_end)
7579 continue;
7580
7581 /* If there are spaces around the equal sign, we need to
7582 * strip them otherwise we'll get trouble for cookie captures,
7583 * or even for rewrites. Since this happens extremely rarely,
7584 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007585 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007586 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7587 int stripped_before = 0;
7588 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007589
Willy Tarreau24581ba2010-08-31 22:39:35 +02007590 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007591 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007592 equal += stripped_before;
7593 val_beg += stripped_before;
7594 }
7595
7596 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007597 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007598 val_beg += stripped_after;
7599 stripped_before += stripped_after;
7600 }
7601
7602 val_end += stripped_before;
7603 next += stripped_before;
7604 hdr_end += stripped_before;
7605 hdr_next += stripped_before;
7606 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007607 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007608 }
7609
7610 /* First, let's see if we want to capture this cookie. We check
7611 * that we don't already have a server side cookie, because we
7612 * can only capture one. Also as an optimisation, we ignore
7613 * cookies shorter than the declared name.
7614 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007615 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007616 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007617 (val_end - att_beg >= sess->fe->capture_namelen) &&
7618 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007619 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007620 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007621 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007622 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007623 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007624 if (log_len > sess->fe->capture_len)
7625 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007626 memcpy(txn->srv_cookie, att_beg, log_len);
7627 txn->srv_cookie[log_len] = 0;
7628 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007629 }
7630
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007631 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007632 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007633 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007634 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7635 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007636 /* assume passive cookie by default */
7637 txn->flags &= ~TX_SCK_MASK;
7638 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007639
7640 /* If the cookie is in insert mode on a known server, we'll delete
7641 * this occurrence because we'll insert another one later.
7642 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007643 * a direct access.
7644 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007645 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007646 /* The "preserve" flag was set, we don't want to touch the
7647 * server's cookie.
7648 */
7649 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007650 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007651 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007652 /* this cookie must be deleted */
7653 if (*prev == ':' && next == hdr_end) {
7654 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007655 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007656 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7657 txn->hdr_idx.used--;
7658 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007659 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007660 hdr_next += delta;
7661 http_msg_move_end(&txn->rsp, delta);
7662 /* note: while both invalid now, <next> and <hdr_end>
7663 * are still equal, so the for() will stop as expected.
7664 */
7665 } else {
7666 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007667 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007668 next = prev;
7669 hdr_end += delta;
7670 hdr_next += delta;
7671 cur_hdr->len += delta;
7672 http_msg_move_end(&txn->rsp, delta);
7673 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007674 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007675 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007676 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007677 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007678 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007679 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007680 * with this server since we know it.
7681 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007682 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007683 next += delta;
7684 hdr_end += delta;
7685 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007686 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007687 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007688
Willy Tarreauf1348312010-10-07 15:54:11 +02007689 txn->flags &= ~TX_SCK_MASK;
7690 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007691 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007692 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007693 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007694 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007695 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007696 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007697 next += delta;
7698 hdr_end += delta;
7699 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007700 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007701 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007702
Willy Tarreau827aee92011-03-10 16:55:02 +01007703 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007704 txn->flags &= ~TX_SCK_MASK;
7705 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007706 }
7707 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007708 /* that's done for this cookie, check the next one on the same
7709 * line when next != hdr_end (only if is_cookie2).
7710 */
7711 }
7712 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007713 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007714 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007715}
7716
7717
Willy Tarreaua15645d2007-03-18 16:22:39 +01007718/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007719 * Parses the Cache-Control and Pragma request header fields to determine if
7720 * the request may be served from the cache and/or if it is cacheable. Updates
7721 * s->txn->flags.
7722 */
7723void check_request_for_cacheability(struct stream *s, struct channel *chn)
7724{
7725 struct http_txn *txn = s->txn;
7726 char *p1, *p2;
7727 char *cur_ptr, *cur_end, *cur_next;
7728 int pragma_found;
7729 int cc_found;
7730 int cur_idx;
7731
7732 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7733 return; /* nothing more to do here */
7734
7735 cur_idx = 0;
7736 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007737 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007738
7739 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7740 struct hdr_idx_elem *cur_hdr;
7741 int val;
7742
7743 cur_hdr = &txn->hdr_idx.v[cur_idx];
7744 cur_ptr = cur_next;
7745 cur_end = cur_ptr + cur_hdr->len;
7746 cur_next = cur_end + cur_hdr->cr + 1;
7747
7748 /* We have one full header between cur_ptr and cur_end, and the
7749 * next header starts at cur_next.
7750 */
7751
7752 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7753 if (val) {
7754 if ((cur_end - (cur_ptr + val) >= 8) &&
7755 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7756 pragma_found = 1;
7757 continue;
7758 }
7759 }
7760
William Lallemand8a16fe02018-05-22 11:04:33 +02007761 /* Don't use the cache and don't try to store if we found the
7762 * Authorization header */
7763 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7764 if (val) {
7765 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7766 txn->flags |= TX_CACHE_IGNORE;
7767 continue;
7768 }
7769
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007770 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7771 if (!val)
7772 continue;
7773
7774 /* OK, right now we know we have a cache-control header at cur_ptr */
7775 cc_found = 1;
7776 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7777
7778 if (p1 >= cur_end) /* no more info */
7779 continue;
7780
7781 /* p1 is at the beginning of the value */
7782 p2 = p1;
7783 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7784 p2++;
7785
7786 /* we have a complete value between p1 and p2. We don't check the
7787 * values after max-age, max-stale nor min-fresh, we simply don't
7788 * use the cache when they're specified.
7789 */
7790 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7791 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7792 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7793 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7794 txn->flags |= TX_CACHE_IGNORE;
7795 continue;
7796 }
7797
7798 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7799 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7800 continue;
7801 }
7802 }
7803
7804 /* RFC7234#5.4:
7805 * When the Cache-Control header field is also present and
7806 * understood in a request, Pragma is ignored.
7807 * When the Cache-Control header field is not present in a
7808 * request, caches MUST consider the no-cache request
7809 * pragma-directive as having the same effect as if
7810 * "Cache-Control: no-cache" were present.
7811 */
7812 if (!cc_found && pragma_found)
7813 txn->flags |= TX_CACHE_IGNORE;
7814}
7815
7816/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007817 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007818 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007819void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007820{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007821 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007822 char *p1, *p2;
7823
7824 char *cur_ptr, *cur_end, *cur_next;
7825 int cur_idx;
7826
Willy Tarreau12b32f22017-12-21 16:08:09 +01007827 if (txn->status < 200) {
7828 /* do not try to cache interim responses! */
7829 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007830 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007831 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007832
7833 /* Iterate through the headers.
7834 * we start with the start line.
7835 */
7836 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007837 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007838
7839 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7840 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007841 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007842
7843 cur_hdr = &txn->hdr_idx.v[cur_idx];
7844 cur_ptr = cur_next;
7845 cur_end = cur_ptr + cur_hdr->len;
7846 cur_next = cur_end + cur_hdr->cr + 1;
7847
7848 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007849 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007850 */
7851
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007852 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7853 if (val) {
7854 if ((cur_end - (cur_ptr + val) >= 8) &&
7855 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7856 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7857 return;
7858 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007859 }
7860
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007861 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7862 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007863 continue;
7864
7865 /* OK, right now we know we have a cache-control header at cur_ptr */
7866
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007867 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007868
7869 if (p1 >= cur_end) /* no more info */
7870 continue;
7871
7872 /* p1 is at the beginning of the value */
7873 p2 = p1;
7874
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007875 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007876 p2++;
7877
7878 /* we have a complete value between p1 and p2 */
7879 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007880 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7881 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7882 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7883 continue;
7884 }
7885
Willy Tarreaua15645d2007-03-18 16:22:39 +01007886 /* we have something of the form no-cache="set-cookie" */
7887 if ((cur_end - p1 >= 21) &&
7888 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7889 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007890 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007891 continue;
7892 }
7893
7894 /* OK, so we know that either p2 points to the end of string or to a comma */
7895 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007896 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007897 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007898 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007899 return;
7900 }
7901
7902 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007903 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007904 continue;
7905 }
7906 }
7907}
7908
Willy Tarreau58f10d72006-12-04 02:26:12 +01007909
Willy Tarreaub2513902006-12-17 14:52:38 +01007910/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007911 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007912 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007913 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007914 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007915 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007916 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007917 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007918 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007919int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007920{
7921 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007922 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007923 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007924
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007925 if (!uri_auth)
7926 return 0;
7927
Cyril Bonté70be45d2010-10-12 00:14:35 +02007928 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007929 return 0;
7930
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007931 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007932 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007933 return 0;
7934
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007935 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007936 return 0;
7937
Willy Tarreaub2513902006-12-17 14:52:38 +01007938 return 1;
7939}
7940
Willy Tarreau4076a152009-04-02 15:18:36 +02007941/*
7942 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007943 * By default it tries to report the error position as msg->err_pos. However if
7944 * this one is not set, it will then report msg->next, which is the last known
7945 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007946 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007947 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007948void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007949 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007950 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007951{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007952 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007953 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007954 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007955
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007956 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf37954d2018-06-15 18:31:02 +02007957 es->len = MIN(ci_data(chn), global.tune.bufsize);
7958 len1 = b_wrap(chn->buf) - ci_head(chn);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007959 len1 = MIN(len1, es->len);
7960 len2 = es->len - len1; /* remaining data if buffer wraps */
7961
Willy Tarreauf3764b72016-03-31 13:45:10 +02007962 if (!es->buf)
7963 es->buf = malloc(global.tune.bufsize);
7964
7965 if (es->buf) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02007966 memcpy(es->buf, ci_head(chn), len1);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007967 if (len2)
Willy Tarreauf37954d2018-06-15 18:31:02 +02007968 memcpy(es->buf + len1, b_orig(chn->buf), len2);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007969 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007970
Willy Tarreau4076a152009-04-02 15:18:36 +02007971 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007972 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007973 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007974 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007975
Willy Tarreau4076a152009-04-02 15:18:36 +02007976 es->when = date; // user-visible date
7977 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007978 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007979 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007980 if (objt_conn(sess->origin))
7981 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007982 else
7983 memset(&es->src, 0, sizeof(es->src));
7984
Willy Tarreau078272e2010-12-12 12:46:33 +01007985 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007986 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007987 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007988 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007989 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007990 es->m_flags = msg->flags;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007991 es->b_out = co_data(chn);
7992 es->b_wrap = b_wrap(chn->buf) - ci_head(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007993 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007994 es->m_clen = msg->chunk_len;
7995 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007996 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007997}
Willy Tarreaub2513902006-12-17 14:52:38 +01007998
Willy Tarreau294c4732011-12-16 21:35:50 +01007999/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8000 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8001 * performed over the whole headers. Otherwise it must contain a valid header
8002 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8003 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8004 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8005 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008006 * -1. The value fetch stops at commas, so this function is suited for use with
8007 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008008 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008009 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008010unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008011 struct hdr_idx *idx, int occ,
8012 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008013{
Willy Tarreau294c4732011-12-16 21:35:50 +01008014 struct hdr_ctx local_ctx;
8015 char *ptr_hist[MAX_HDR_HISTORY];
8016 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008017 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008018 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008019
Willy Tarreau294c4732011-12-16 21:35:50 +01008020 if (!ctx) {
8021 local_ctx.idx = 0;
8022 ctx = &local_ctx;
8023 }
8024
Willy Tarreaubce70882009-09-07 11:51:47 +02008025 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008026 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008027 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008028 occ--;
8029 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008030 *vptr = ctx->line + ctx->val;
8031 *vlen = ctx->vlen;
8032 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008033 }
8034 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008035 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008036 }
8037
8038 /* negative occurrence, we scan all the list then walk back */
8039 if (-occ > MAX_HDR_HISTORY)
8040 return 0;
8041
Willy Tarreau294c4732011-12-16 21:35:50 +01008042 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02008043 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008044 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8045 len_hist[hist_ptr] = ctx->vlen;
8046 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008047 hist_ptr = 0;
8048 found++;
8049 }
8050 if (-occ > found)
8051 return 0;
8052 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008053 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8054 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8055 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008056 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008057 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008058 if (hist_ptr >= MAX_HDR_HISTORY)
8059 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008060 *vptr = ptr_hist[hist_ptr];
8061 *vlen = len_hist[hist_ptr];
8062 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008063}
8064
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008065/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8066 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8067 * performed over the whole headers. Otherwise it must contain a valid header
8068 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8069 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8070 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8071 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8072 * -1. This function differs from http_get_hdr() in that it only returns full
8073 * line header values and does not stop at commas.
8074 * The return value is 0 if nothing was found, or non-zero otherwise.
8075 */
8076unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8077 struct hdr_idx *idx, int occ,
8078 struct hdr_ctx *ctx, char **vptr, int *vlen)
8079{
8080 struct hdr_ctx local_ctx;
8081 char *ptr_hist[MAX_HDR_HISTORY];
8082 int len_hist[MAX_HDR_HISTORY];
8083 unsigned int hist_ptr;
8084 int found;
8085
8086 if (!ctx) {
8087 local_ctx.idx = 0;
8088 ctx = &local_ctx;
8089 }
8090
8091 if (occ >= 0) {
8092 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008093 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008094 occ--;
8095 if (occ <= 0) {
8096 *vptr = ctx->line + ctx->val;
8097 *vlen = ctx->vlen;
8098 return 1;
8099 }
8100 }
8101 return 0;
8102 }
8103
8104 /* negative occurrence, we scan all the list then walk back */
8105 if (-occ > MAX_HDR_HISTORY)
8106 return 0;
8107
8108 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02008109 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008110 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8111 len_hist[hist_ptr] = ctx->vlen;
8112 if (++hist_ptr >= MAX_HDR_HISTORY)
8113 hist_ptr = 0;
8114 found++;
8115 }
8116 if (-occ > found)
8117 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008118
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008119 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008120 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8121 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8122 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008123 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008124 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008125 if (hist_ptr >= MAX_HDR_HISTORY)
8126 hist_ptr -= MAX_HDR_HISTORY;
8127 *vptr = ptr_hist[hist_ptr];
8128 *vlen = len_hist[hist_ptr];
8129 return 1;
8130}
8131
Willy Tarreaubaaee002006-06-26 02:48:02 +02008132/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008133 * Print a debug line with a header. Always stop at the first CR or LF char,
8134 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8135 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008136 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008137void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008138{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008139 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008140 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008141
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008142 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008143 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008144 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02008145 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008146
8147 for (max = 0; start + max < end; max++)
8148 if (start[max] == '\r' || start[max] == '\n')
8149 break;
8150
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008151 UBOUND(max, trash.size - trash.len - 3);
8152 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8153 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008154 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008155}
8156
Willy Tarreaueee5b512015-04-03 23:46:31 +02008157
8158/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8159 * The hdr_idx is allocated as well. In case of allocation failure, everything
8160 * allocated is freed and NULL is returned. Otherwise the new transaction is
8161 * assigned to the stream and returned.
8162 */
8163struct http_txn *http_alloc_txn(struct stream *s)
8164{
8165 struct http_txn *txn = s->txn;
8166
8167 if (txn)
8168 return txn;
8169
Willy Tarreaubafbe012017-11-24 17:34:44 +01008170 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008171 if (!txn)
8172 return txn;
8173
8174 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008175 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008176 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008177 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008178 return NULL;
8179 }
8180
8181 s->txn = txn;
8182 return txn;
8183}
8184
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008185void http_txn_reset_req(struct http_txn *txn)
8186{
8187 txn->req.flags = 0;
8188 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8189 txn->req.next = 0;
8190 txn->req.chunk_len = 0LL;
8191 txn->req.body_len = 0LL;
8192 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8193}
8194
8195void http_txn_reset_res(struct http_txn *txn)
8196{
8197 txn->rsp.flags = 0;
8198 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8199 txn->rsp.next = 0;
8200 txn->rsp.chunk_len = 0LL;
8201 txn->rsp.body_len = 0LL;
8202 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8203}
8204
Willy Tarreau0937bc42009-12-22 15:03:09 +01008205/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008206 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008207 * the required fields are properly allocated and that we only need to (re)init
8208 * them. This should be used before processing any new request.
8209 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008210void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008211{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008212 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008213 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008214
8215 txn->flags = 0;
8216 txn->status = -1;
8217
Willy Tarreauf64d1412010-10-07 20:06:11 +02008218 txn->cookie_first_date = 0;
8219 txn->cookie_last_date = 0;
8220
Willy Tarreaueee5b512015-04-03 23:46:31 +02008221 txn->srv_cookie = NULL;
8222 txn->cli_cookie = NULL;
8223 txn->uri = NULL;
8224
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008225 http_txn_reset_req(txn);
8226 http_txn_reset_res(txn);
8227
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008228 txn->req.chn = &s->req;
8229 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008230
8231 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008232
8233 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8234 if (fe->options2 & PR_O2_REQBUG_OK)
8235 txn->req.err_pos = -1; /* let buggy requests pass */
8236
Willy Tarreau0937bc42009-12-22 15:03:09 +01008237 if (txn->hdr_idx.v)
8238 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008239
8240 vars_init(&s->vars_txn, SCOPE_TXN);
8241 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008242}
8243
8244/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008245void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008246{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008247 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008248 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008249
8250 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008251 pool_free(pool_head_requri, txn->uri);
8252 pool_free(pool_head_capture, txn->cli_cookie);
8253 pool_free(pool_head_capture, txn->srv_cookie);
8254 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008255
William Lallemanda73203e2012-03-12 12:48:57 +01008256 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008257 txn->uri = NULL;
8258 txn->srv_cookie = NULL;
8259 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008260
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008261 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008262 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008263 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008264 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008265 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008266 }
8267
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008268 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008269 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008270 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008271 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008272 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008273 }
8274
Willy Tarreau6204cd92016-03-10 16:33:04 +01008275 vars_prune(&s->vars_txn, s->sess, s);
8276 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008277}
8278
8279/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008280void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008281{
8282 http_end_txn(s);
8283 http_init_txn(s);
8284
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008285 /* reinitialise the current rule list pointer to NULL. We are sure that
8286 * any rulelist match the NULL pointer.
8287 */
8288 s->current_rule_list = NULL;
8289
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008290 s->be = strm_fe(s);
8291 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008292 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008293 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008294 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008295 /* re-init store persistence */
8296 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008297 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008298
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008299 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008300
Willy Tarreau739cfba2010-01-25 23:11:14 +01008301 /* We must trim any excess data from the response buffer, because we
8302 * may have blocked an invalid response from a server that we don't
8303 * want to accidentely forward once we disable the analysers, nor do
8304 * we want those data to come along with next response. A typical
8305 * example of such data would be from a buggy server responding to
8306 * a HEAD with some data, or sending more than the advertised
8307 * content-length.
8308 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008309 if (unlikely(ci_data(&s->res)))
8310 b_set_data(s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01008311
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008312 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02008313 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008314
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008315 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008316 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008317
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008318 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008319 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008320
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008321 s->req.rex = TICK_ETERNITY;
8322 s->req.wex = TICK_ETERNITY;
8323 s->req.analyse_exp = TICK_ETERNITY;
8324 s->res.rex = TICK_ETERNITY;
8325 s->res.wex = TICK_ETERNITY;
8326 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008327 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008328}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008329
Sasha Pachev218f0642014-06-16 12:05:59 -06008330void free_http_res_rules(struct list *r)
8331{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008332 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008333
8334 list_for_each_entry_safe(pr, tr, r, list) {
8335 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008336 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008337 free(pr);
8338 }
8339}
8340
8341void free_http_req_rules(struct list *r)
8342{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008343 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008344
8345 list_for_each_entry_safe(pr, tr, r, list) {
8346 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008347 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008348 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008349
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008350 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008351 free(pr);
8352 }
8353}
8354
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008355/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008356struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008357{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008358 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008359 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008360 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008361 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008362
Vincent Bernat02779b62016-04-03 13:48:43 +02008363 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008364 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008365 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008366 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008367 }
8368
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008369 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008370 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008371 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008372 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008373 int code;
8374 int hc;
8375
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008376 if (!strcmp(args[0], "tarpit")) {
8377 rule->action = ACT_HTTP_REQ_TARPIT;
8378 rule->deny_status = HTTP_ERR_500;
8379 }
8380 else {
8381 rule->action = ACT_ACTION_DENY;
8382 rule->deny_status = HTTP_ERR_403;
8383 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008384 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008385 if (strcmp(args[cur_arg], "deny_status") == 0) {
8386 cur_arg++;
8387 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008388 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8389 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008390 goto out_err;
8391 }
8392
8393 code = atol(args[cur_arg]);
8394 cur_arg++;
8395 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8396 if (http_err_codes[hc] == code) {
8397 rule->deny_status = hc;
8398 break;
8399 }
8400 }
8401
8402 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008403 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8404 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008405 }
8406 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008407 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008408 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008409 cur_arg = 1;
8410
8411 while(*args[cur_arg]) {
8412 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008413 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008414 cur_arg+=2;
8415 continue;
8416 } else
8417 break;
8418 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008419 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008420 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008421 cur_arg = 1;
8422
8423 if (!*args[cur_arg] ||
8424 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008425 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8426 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008427 goto out_err;
8428 }
8429 rule->arg.nice = atoi(args[cur_arg]);
8430 if (rule->arg.nice < -1024)
8431 rule->arg.nice = -1024;
8432 else if (rule->arg.nice > 1024)
8433 rule->arg.nice = 1024;
8434 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008435 } else if (!strcmp(args[0], "set-tos")) {
8436#ifdef IP_TOS
8437 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008438 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008439 cur_arg = 1;
8440
8441 if (!*args[cur_arg] ||
8442 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008443 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8444 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008445 goto out_err;
8446 }
8447
8448 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8449 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008450 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8451 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008452 goto out_err;
8453 }
8454 cur_arg++;
8455#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008456 ha_alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008457 goto out_err;
8458#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008459 } else if (!strcmp(args[0], "set-mark")) {
8460#ifdef SO_MARK
8461 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008462 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008463 cur_arg = 1;
8464
8465 if (!*args[cur_arg] ||
8466 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008467 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8468 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008469 goto out_err;
8470 }
8471
8472 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8473 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008474 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8475 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008476 goto out_err;
8477 }
8478 cur_arg++;
8479 global.last_checks |= LSTCHK_NETADM;
8480#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008481 ha_alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008482 goto out_err;
8483#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008484 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008485 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008486 cur_arg = 1;
8487
8488 if (!*args[cur_arg] ||
8489 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8490 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008491 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8492 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008493 goto out_err;
8494 }
8495 if (strcmp(args[cur_arg], "silent") == 0)
8496 rule->arg.loglevel = -1;
8497 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8498 goto bad_log_level;
8499 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008500 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008501 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008502 cur_arg = 1;
8503
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008504 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8505 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008506 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8507 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008508 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008509 }
8510
8511 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8512 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8513 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008514
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008515 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008516 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008517 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008518 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008519 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8520 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008521 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008522 goto out_err;
8523 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008524 free(proxy->conf.lfs_file);
8525 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8526 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008527 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008528 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008529 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008530 cur_arg = 1;
8531
8532 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008533 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008534 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8535 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008536 goto out_err;
8537 }
8538
8539 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8540 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8541 LIST_INIT(&rule->arg.hdr_add.fmt);
8542
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008543 error = NULL;
8544 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008545 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8546 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008547 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008548 goto out_err;
8549 }
8550
8551 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008552 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008553 if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008554 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008555 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8556 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008557 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008558 goto out_err;
8559 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008560
8561 free(proxy->conf.lfs_file);
8562 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8563 proxy->conf.lfs_line = proxy->conf.args.line;
8564 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008565 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008566 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008567 cur_arg = 1;
8568
8569 if (!*args[cur_arg] ||
8570 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008571 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8572 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008573 goto out_err;
8574 }
8575
8576 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8577 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8578
8579 proxy->conf.args.ctx = ARGC_HRQ;
8580 free(proxy->conf.lfs_file);
8581 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8582 proxy->conf.lfs_line = proxy->conf.args.line;
8583 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008584 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008585 struct sample_expr *expr;
8586 unsigned int where;
8587 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008588 unsigned int tsc_num;
8589 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008590
8591 cur_arg = 1;
8592 proxy->conf.args.ctx = ARGC_TRK;
8593
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008594 tsc_num_str = &args[0][8];
8595 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8596 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8597 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8598 free(err);
8599 goto out_err;
8600 }
8601
Willy Tarreau09448f72014-06-25 18:12:15 +02008602 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8603 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008604 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8605 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008606 free(err);
8607 goto out_err;
8608 }
8609
8610 where = 0;
8611 if (proxy->cap & PR_CAP_FE)
8612 where |= SMP_VAL_FE_HRQ_HDR;
8613 if (proxy->cap & PR_CAP_BE)
8614 where |= SMP_VAL_BE_HRQ_HDR;
8615
8616 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008617 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8618 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8619 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8620 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008621 free(expr);
8622 goto out_err;
8623 }
8624
8625 if (strcmp(args[cur_arg], "table") == 0) {
8626 cur_arg++;
8627 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008628 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8629 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008630 free(expr);
8631 goto out_err;
8632 }
8633 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008634 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008635 cur_arg++;
8636 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008637 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008638 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008639 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008640 } else if (strcmp(args[0], "redirect") == 0) {
8641 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008642 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008643
Willy Tarreaube4653b2015-05-28 15:26:58 +02008644 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008645 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8646 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008647 goto out_err;
8648 }
8649
8650 /* this redirect rule might already contain a parsed condition which
8651 * we'll pass to the http-request rule.
8652 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008653 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008654 rule->arg.redir = redir;
8655 rule->cond = redir->cond;
8656 redir->cond = NULL;
8657 cur_arg = 2;
8658 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008659 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8660 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008661 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008662 /*
8663 * '+ 8' for 'add-acl('
8664 * '- 9' for 'add-acl(' + trailing ')'
8665 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008666 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008667
8668 cur_arg = 1;
8669
8670 if (!*args[cur_arg] ||
8671 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008672 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8673 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008674 goto out_err;
8675 }
8676
8677 LIST_INIT(&rule->arg.map.key);
8678 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008679 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008680 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008681 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008682 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8683 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008684 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008685 goto out_err;
8686 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008687 free(proxy->conf.lfs_file);
8688 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8689 proxy->conf.lfs_line = proxy->conf.args.line;
8690 cur_arg += 1;
8691 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8692 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008693 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008694 /*
8695 * '+ 8' for 'del-acl('
8696 * '- 9' for 'del-acl(' + trailing ')'
8697 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008698 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008699
8700 cur_arg = 1;
8701
8702 if (!*args[cur_arg] ||
8703 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008704 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8705 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008706 goto out_err;
8707 }
8708
8709 LIST_INIT(&rule->arg.map.key);
8710 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008711 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008712 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008713 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008714 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8715 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008716 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008717 goto out_err;
8718 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008719 free(proxy->conf.lfs_file);
8720 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8721 proxy->conf.lfs_line = proxy->conf.args.line;
8722 cur_arg += 1;
8723 } else if (strncmp(args[0], "del-map", 7) == 0) {
8724 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008725 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008726 /*
8727 * '+ 8' for 'del-map('
8728 * '- 9' for 'del-map(' + trailing ')'
8729 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008730 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008731
8732 cur_arg = 1;
8733
8734 if (!*args[cur_arg] ||
8735 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008736 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8737 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008738 goto out_err;
8739 }
8740
8741 LIST_INIT(&rule->arg.map.key);
8742 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008743 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008744 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008745 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008746 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8747 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008748 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008749 goto out_err;
8750 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008751 free(proxy->conf.lfs_file);
8752 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8753 proxy->conf.lfs_line = proxy->conf.args.line;
8754 cur_arg += 1;
8755 } else if (strncmp(args[0], "set-map", 7) == 0) {
8756 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008757 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008758 /*
8759 * '+ 8' for 'set-map('
8760 * '- 9' for 'set-map(' + trailing ')'
8761 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008762 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008763
8764 cur_arg = 1;
8765
8766 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8767 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008768 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8769 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008770 goto out_err;
8771 }
8772
8773 LIST_INIT(&rule->arg.map.key);
8774 LIST_INIT(&rule->arg.map.value);
8775 proxy->conf.args.ctx = ARGC_HRQ;
8776
8777 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008778 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008779 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008780 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008781 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8782 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008783 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008784 goto out_err;
8785 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008786
8787 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008788 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008789 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008790 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008791 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8792 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008793 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008794 goto out_err;
8795 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008796 free(proxy->conf.lfs_file);
8797 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8798 proxy->conf.lfs_line = proxy->conf.args.line;
8799
8800 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008801 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8802 char *errmsg = NULL;
8803 cur_arg = 1;
8804 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008805 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008806 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008807 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008808 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8809 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008810 free(errmsg);
8811 goto out_err;
8812 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008813 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008814 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008815 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8816 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8817 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8818 "%s%s, but got '%s'%s.\n",
8819 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008820 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008821 }
8822
8823 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8824 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008825 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008826
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008827 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008828 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8829 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008830 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008831 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008832 }
8833 rule->cond = cond;
8834 }
8835 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008836 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8837 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8838 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008839 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008840 }
8841
8842 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008843 out_err:
8844 free(rule);
8845 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008846}
8847
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008848/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008849struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008850{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008851 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008852 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008853 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008854 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008855
8856 rule = calloc(1, sizeof(*rule));
8857 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008858 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008859 goto out_err;
8860 }
8861
8862 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008863 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008864 cur_arg = 1;
8865 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008866 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008867 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008868 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008869 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008870 cur_arg = 1;
8871
8872 if (!*args[cur_arg] ||
8873 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008874 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8875 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008876 goto out_err;
8877 }
8878 rule->arg.nice = atoi(args[cur_arg]);
8879 if (rule->arg.nice < -1024)
8880 rule->arg.nice = -1024;
8881 else if (rule->arg.nice > 1024)
8882 rule->arg.nice = 1024;
8883 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008884 } else if (!strcmp(args[0], "set-tos")) {
8885#ifdef IP_TOS
8886 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008887 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008888 cur_arg = 1;
8889
8890 if (!*args[cur_arg] ||
8891 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008892 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8893 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008894 goto out_err;
8895 }
8896
8897 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8898 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008899 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8900 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008901 goto out_err;
8902 }
8903 cur_arg++;
8904#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008905 ha_alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008906 goto out_err;
8907#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008908 } else if (!strcmp(args[0], "set-mark")) {
8909#ifdef SO_MARK
8910 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008911 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008912 cur_arg = 1;
8913
8914 if (!*args[cur_arg] ||
8915 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008916 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8917 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008918 goto out_err;
8919 }
8920
8921 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8922 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008923 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8924 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008925 goto out_err;
8926 }
8927 cur_arg++;
8928 global.last_checks |= LSTCHK_NETADM;
8929#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008930 ha_alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008931 goto out_err;
8932#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008933 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008934 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008935 cur_arg = 1;
8936
8937 if (!*args[cur_arg] ||
8938 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8939 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008940 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8941 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008942 goto out_err;
8943 }
8944 if (strcmp(args[cur_arg], "silent") == 0)
8945 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008946 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008947 goto bad_log_level;
8948 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008949 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008950 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008951 cur_arg = 1;
8952
8953 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8954 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008955 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8956 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008957 goto out_err;
8958 }
8959
8960 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8961 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8962 LIST_INIT(&rule->arg.hdr_add.fmt);
8963
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008964 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008965 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008966 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008967 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008968 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8969 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008970 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008971 goto out_err;
8972 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008973 free(proxy->conf.lfs_file);
8974 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8975 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008976 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008977 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008978 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008979 cur_arg = 1;
8980
8981 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008982 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008983 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8984 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008985 goto out_err;
8986 }
8987
8988 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8989 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8990 LIST_INIT(&rule->arg.hdr_add.fmt);
8991
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008992 error = NULL;
8993 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008994 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8995 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008996 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008997 goto out_err;
8998 }
8999
9000 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009001 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009002 if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009003 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009004 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9005 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009006 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009007 goto out_err;
9008 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009009
9010 free(proxy->conf.lfs_file);
9011 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9012 proxy->conf.lfs_line = proxy->conf.args.line;
9013 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009014 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009015 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009016 cur_arg = 1;
9017
9018 if (!*args[cur_arg] ||
9019 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009020 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9021 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009022 goto out_err;
9023 }
9024
9025 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9026 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9027
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009028 proxy->conf.args.ctx = ARGC_HRS;
9029 free(proxy->conf.lfs_file);
9030 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9031 proxy->conf.lfs_line = proxy->conf.args.line;
9032 cur_arg += 1;
9033 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9034 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009035 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009036 /*
9037 * '+ 8' for 'add-acl('
9038 * '- 9' for 'add-acl(' + trailing ')'
9039 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009040 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009041
9042 cur_arg = 1;
9043
9044 if (!*args[cur_arg] ||
9045 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009046 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9047 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009048 goto out_err;
9049 }
9050
9051 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009052 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009053 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009054 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009055 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009056 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9057 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009058 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009059 goto out_err;
9060 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009061 free(proxy->conf.lfs_file);
9062 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9063 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009064
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009065 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009066 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9067 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009068 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009069 /*
9070 * '+ 8' for 'del-acl('
9071 * '- 9' for 'del-acl(' + trailing ')'
9072 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009073 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009074
9075 cur_arg = 1;
9076
9077 if (!*args[cur_arg] ||
9078 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009079 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9080 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009081 goto out_err;
9082 }
9083
9084 LIST_INIT(&rule->arg.map.key);
9085 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009086 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009087 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009088 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009089 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9090 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009091 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009092 goto out_err;
9093 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009094 free(proxy->conf.lfs_file);
9095 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9096 proxy->conf.lfs_line = proxy->conf.args.line;
9097 cur_arg += 1;
9098 } else if (strncmp(args[0], "del-map", 7) == 0) {
9099 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009100 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009101 /*
9102 * '+ 8' for 'del-map('
9103 * '- 9' for 'del-map(' + trailing ')'
9104 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009105 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009106
9107 cur_arg = 1;
9108
9109 if (!*args[cur_arg] ||
9110 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009111 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9112 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009113 goto out_err;
9114 }
9115
9116 LIST_INIT(&rule->arg.map.key);
9117 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009118 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009119 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009120 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009121 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9122 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009123 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009124 goto out_err;
9125 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009126 free(proxy->conf.lfs_file);
9127 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9128 proxy->conf.lfs_line = proxy->conf.args.line;
9129 cur_arg += 1;
9130 } else if (strncmp(args[0], "set-map", 7) == 0) {
9131 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009132 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009133 /*
9134 * '+ 8' for 'set-map('
9135 * '- 9' for 'set-map(' + trailing ')'
9136 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009137 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009138
9139 cur_arg = 1;
9140
9141 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9142 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009143 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9144 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009145 goto out_err;
9146 }
9147
9148 LIST_INIT(&rule->arg.map.key);
9149 LIST_INIT(&rule->arg.map.value);
9150
9151 proxy->conf.args.ctx = ARGC_HRS;
9152
9153 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009154 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009155 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009156 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009157 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9158 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009159 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009160 goto out_err;
9161 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009162
9163 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009164 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009165 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009166 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009167 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9168 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009169 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009170 goto out_err;
9171 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009172
9173 free(proxy->conf.lfs_file);
9174 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9175 proxy->conf.lfs_line = proxy->conf.args.line;
9176
9177 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009178 } else if (strcmp(args[0], "redirect") == 0) {
9179 struct redirect_rule *redir;
9180 char *errmsg = NULL;
9181
9182 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009183 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9184 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009185 goto out_err;
9186 }
9187
9188 /* this redirect rule might already contain a parsed condition which
9189 * we'll pass to the http-request rule.
9190 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009191 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009192 rule->arg.redir = redir;
9193 rule->cond = redir->cond;
9194 redir->cond = NULL;
9195 cur_arg = 2;
9196 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009197 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009198 struct sample_expr *expr;
9199 unsigned int where;
9200 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009201 unsigned int tsc_num;
9202 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009203
9204 cur_arg = 1;
9205 proxy->conf.args.ctx = ARGC_TRK;
9206
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009207 tsc_num_str = &args[0][8];
9208 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
9209 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9210 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9211 free(err);
9212 goto out_err;
9213 }
9214
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009215 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9216 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009217 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9218 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009219 free(err);
9220 goto out_err;
9221 }
9222
9223 where = 0;
9224 if (proxy->cap & PR_CAP_FE)
9225 where |= SMP_VAL_FE_HRS_HDR;
9226 if (proxy->cap & PR_CAP_BE)
9227 where |= SMP_VAL_BE_HRS_HDR;
9228
9229 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009230 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9231 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9232 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9233 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009234 free(expr);
9235 goto out_err;
9236 }
9237
9238 if (strcmp(args[cur_arg], "table") == 0) {
9239 cur_arg++;
9240 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009241 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9242 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009243 free(expr);
9244 goto out_err;
9245 }
9246 /* we copy the table name for now, it will be resolved later */
9247 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9248 cur_arg++;
9249 }
9250 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009251 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02009252 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009253 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9254 char *errmsg = NULL;
9255 cur_arg = 1;
9256 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009257 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009258 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009259 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009260 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9261 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009262 free(errmsg);
9263 goto out_err;
9264 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009265 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009266 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009267 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9268 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9269 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9270 "%s%s, but got '%s'%s.\n",
9271 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009272 goto out_err;
9273 }
9274
9275 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9276 struct acl_cond *cond;
9277 char *errmsg = NULL;
9278
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009279 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009280 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9281 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009282 free(errmsg);
9283 goto out_err;
9284 }
9285 rule->cond = cond;
9286 }
9287 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009288 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9289 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9290 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009291 goto out_err;
9292 }
9293
9294 return rule;
9295 out_err:
9296 free(rule);
9297 return NULL;
9298}
9299
Willy Tarreau4baae242012-12-27 12:00:31 +01009300/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009301 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009302 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9303 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009304 */
9305struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009306 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009307{
9308 struct redirect_rule *rule;
9309 int cur_arg;
9310 int type = REDIRECT_TYPE_NONE;
9311 int code = 302;
9312 const char *destination = NULL;
9313 const char *cookie = NULL;
9314 int cookie_set = 0;
9315 unsigned int flags = REDIRECT_FLAG_NONE;
9316 struct acl_cond *cond = NULL;
9317
9318 cur_arg = 0;
9319 while (*(args[cur_arg])) {
9320 if (strcmp(args[cur_arg], "location") == 0) {
9321 if (!*args[cur_arg + 1])
9322 goto missing_arg;
9323
9324 type = REDIRECT_TYPE_LOCATION;
9325 cur_arg++;
9326 destination = args[cur_arg];
9327 }
9328 else if (strcmp(args[cur_arg], "prefix") == 0) {
9329 if (!*args[cur_arg + 1])
9330 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009331 type = REDIRECT_TYPE_PREFIX;
9332 cur_arg++;
9333 destination = args[cur_arg];
9334 }
9335 else if (strcmp(args[cur_arg], "scheme") == 0) {
9336 if (!*args[cur_arg + 1])
9337 goto missing_arg;
9338
9339 type = REDIRECT_TYPE_SCHEME;
9340 cur_arg++;
9341 destination = args[cur_arg];
9342 }
9343 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9344 if (!*args[cur_arg + 1])
9345 goto missing_arg;
9346
9347 cur_arg++;
9348 cookie = args[cur_arg];
9349 cookie_set = 1;
9350 }
9351 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9352 if (!*args[cur_arg + 1])
9353 goto missing_arg;
9354
9355 cur_arg++;
9356 cookie = args[cur_arg];
9357 cookie_set = 0;
9358 }
9359 else if (strcmp(args[cur_arg], "code") == 0) {
9360 if (!*args[cur_arg + 1])
9361 goto missing_arg;
9362
9363 cur_arg++;
9364 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009365 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009366 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009367 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009368 args[cur_arg - 1], args[cur_arg]);
9369 return NULL;
9370 }
9371 }
9372 else if (!strcmp(args[cur_arg],"drop-query")) {
9373 flags |= REDIRECT_FLAG_DROP_QS;
9374 }
9375 else if (!strcmp(args[cur_arg],"append-slash")) {
9376 flags |= REDIRECT_FLAG_APPEND_SLASH;
9377 }
9378 else if (strcmp(args[cur_arg], "if") == 0 ||
9379 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009380 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009381 if (!cond) {
9382 memprintf(errmsg, "error in condition: %s", *errmsg);
9383 return NULL;
9384 }
9385 break;
9386 }
9387 else {
9388 memprintf(errmsg,
9389 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9390 args[cur_arg]);
9391 return NULL;
9392 }
9393 cur_arg++;
9394 }
9395
9396 if (type == REDIRECT_TYPE_NONE) {
9397 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9398 return NULL;
9399 }
9400
Willy Tarreaube4653b2015-05-28 15:26:58 +02009401 if (dir && type != REDIRECT_TYPE_LOCATION) {
9402 memprintf(errmsg, "response only supports redirect type 'location'");
9403 return NULL;
9404 }
9405
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009406 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009407 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009408 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009409
9410 if (!use_fmt) {
9411 /* old-style static redirect rule */
9412 rule->rdr_str = strdup(destination);
9413 rule->rdr_len = strlen(destination);
9414 }
9415 else {
9416 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009417
9418 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9419 * if prefix == "/", we don't want to add anything, otherwise it
9420 * makes it hard for the user to configure a self-redirection.
9421 */
Godbachd9722032014-12-18 15:44:58 +08009422 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009423 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009424 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9425 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009426 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9427 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009428 return NULL;
9429 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009430 free(curproxy->conf.lfs_file);
9431 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9432 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009433 }
9434 }
9435
Willy Tarreau4baae242012-12-27 12:00:31 +01009436 if (cookie) {
9437 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9438 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9439 */
9440 rule->cookie_len = strlen(cookie);
9441 if (cookie_set) {
9442 rule->cookie_str = malloc(rule->cookie_len + 10);
9443 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9444 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9445 rule->cookie_len += 9;
9446 } else {
9447 rule->cookie_str = malloc(rule->cookie_len + 21);
9448 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9449 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9450 rule->cookie_len += 20;
9451 }
9452 }
9453 rule->type = type;
9454 rule->code = code;
9455 rule->flags = flags;
9456 LIST_INIT(&rule->list);
9457 return rule;
9458
9459 missing_arg:
9460 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9461 return NULL;
9462}
9463
Willy Tarreau8797c062007-05-07 00:55:35 +02009464/************************************************************************/
9465/* The code below is dedicated to ACL parsing and matching */
9466/************************************************************************/
9467
9468
Willy Tarreau14174bc2012-04-16 14:34:04 +02009469/* This function ensures that the prerequisites for an L7 fetch are ready,
9470 * which means that a request or response is ready. If some data is missing,
9471 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009472 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9473 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009474 *
9475 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009476 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9477 * decide whether or not an HTTP message is present ;
9478 * 0 if the requested data cannot be fetched or if it is certain that
9479 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009480 * 1 if an HTTP message is ready
9481 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009482int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009483 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009484{
Willy Tarreau192252e2015-04-04 01:47:55 +02009485 struct http_txn *txn;
9486 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009487
Willy Tarreaube508f12016-03-10 11:47:01 +01009488 /* Note: it is possible that <s> is NULL when called before stream
9489 * initialization (eg: tcp-request connection), so this function is the
9490 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009491 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009492 if (!s)
9493 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009494
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009495 if (!s->txn) {
9496 if (unlikely(!http_alloc_txn(s)))
9497 return 0; /* not enough memory */
9498 http_init_txn(s);
9499 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009500 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009501 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009502
9503 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009504 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009505
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009506 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009507 /* If the buffer does not leave enough free space at the end,
9508 * we must first realign it.
9509 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009510 if (ci_head(&s->req) > b_orig(s->req.buf) &&
9511 ci_head(&s->req) + ci_data(&s->req) > b_wrap(s->req.buf) - global.tune.maxrewrite)
Willy Tarreaufd8d42f2018-07-12 10:57:15 +02009512 channel_slow_realign(&s->req, trash.str);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009513
Willy Tarreau14174bc2012-04-16 14:34:04 +02009514 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009515 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009516 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009517
9518 /* Try to decode HTTP request */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009519 if (likely(msg->next < ci_data(&s->req)))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009520 http_msg_analyzer(msg, &txn->hdr_idx);
9521
9522 /* Still no valid request ? */
9523 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009524 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau23752332018-06-15 14:54:53 +02009525 channel_full(&s->req, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009526 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009527 }
9528 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009529 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009530 return 0;
9531 }
9532
9533 /* OK we just got a valid HTTP request. We have some minor
9534 * preparation to perform so that further checks can rely
9535 * on HTTP tests.
9536 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009537
9538 /* If the request was parsed but was too large, we must absolutely
9539 * return an error so that it is not processed. At the moment this
9540 * cannot happen, but if the parsers are to change in the future,
9541 * we want this check to be maintained.
9542 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009543 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
9544 b_wrap(s->req.buf) - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009545 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009546 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009547 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009548 return 1;
9549 }
9550
Willy Tarreauf37954d2018-06-15 18:31:02 +02009551 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009552 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009553 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009554
Willy Tarreau506d0502013-07-06 13:29:24 +02009555 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9556 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009557 }
9558
Willy Tarreau506d0502013-07-06 13:29:24 +02009559 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009560 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009561 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009562
9563 /* otherwise everything's ready for the request */
9564 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009565 else {
9566 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009567 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9568 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009569 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009570 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009571 }
9572
9573 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009574 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009575 return 1;
9576}
Willy Tarreau8797c062007-05-07 00:55:35 +02009577
Willy Tarreau8797c062007-05-07 00:55:35 +02009578/* 1. Check on METHOD
9579 * We use the pre-parsed method if it is known, and store its number as an
9580 * integer. If it is unknown, we use the pointer and the length.
9581 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009582static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009583{
9584 int len, meth;
9585
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009586 len = strlen(text);
9587 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009588
9589 pattern->val.i = meth;
9590 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009591 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009592 pattern->len = len;
9593 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009594 else {
9595 pattern->ptr.str = NULL;
9596 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009597 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009598 return 1;
9599}
9600
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009601/* This function fetches the method of current HTTP request and stores
9602 * it in the global pattern struct as a chunk. There are two possibilities :
9603 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9604 * in <len> and <ptr> is NULL ;
9605 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9606 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009607 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009608 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009609static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009610smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009611{
9612 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009613 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009614
Willy Tarreau24e32d82012-04-23 23:55:44 +02009615 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009616
Willy Tarreaube508f12016-03-10 11:47:01 +01009617 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009618 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009619 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009620 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009621 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009622 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9623 /* ensure the indexes are not affected */
9624 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009625 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009626 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009627 smp->data.u.meth.str.str = ci_head(txn->req.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009628 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009629 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009630 return 1;
9631}
9632
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009633/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009634static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009635{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009636 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009637 struct pattern_list *lst;
9638 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009639
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009640 list_for_each_entry(lst, &expr->patterns, list) {
9641 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009642
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009643 /* well-known method */
9644 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009645 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009646 return pattern;
9647 else
9648 continue;
9649 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009650
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009651 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009652 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009653 continue;
9654
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009655 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009656 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9657 (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009658 return pattern;
9659 }
9660 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009661}
9662
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009663static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009664smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009665{
Willy Tarreaube508f12016-03-10 11:47:01 +01009666 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009667 char *ptr;
9668 int len;
9669
Willy Tarreauc0239e02012-04-16 14:42:55 +02009670 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009671
Willy Tarreaube508f12016-03-10 11:47:01 +01009672 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009673 len = txn->req.sl.rq.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009674 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009675
9676 while ((len-- > 0) && (*ptr++ != '/'));
9677 if (len <= 0)
9678 return 0;
9679
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009680 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009681 smp->data.u.str.str = ptr;
9682 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009683
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009684 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009685 return 1;
9686}
9687
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009688static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009689smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009690{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009691 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009692 char *ptr;
9693 int len;
9694
Willy Tarreauc0239e02012-04-16 14:42:55 +02009695 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009696
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009697 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009698 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9699 return 0;
9700
Willy Tarreau8797c062007-05-07 00:55:35 +02009701 len = txn->rsp.sl.st.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009702 ptr = ci_head(txn->rsp.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009703
9704 while ((len-- > 0) && (*ptr++ != '/'));
9705 if (len <= 0)
9706 return 0;
9707
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009708 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009709 smp->data.u.str.str = ptr;
9710 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009711
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009712 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009713 return 1;
9714}
9715
9716/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009717static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009718smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009719{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009720 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009721 char *ptr;
9722 int len;
9723
Willy Tarreauc0239e02012-04-16 14:42:55 +02009724 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009725
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009726 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009727 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9728 return 0;
9729
Willy Tarreau8797c062007-05-07 00:55:35 +02009730 len = txn->rsp.sl.st.c_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009731 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009732
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009733 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009734 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009735 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009736 return 1;
9737}
9738
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009739static int
9740smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9741{
9742 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9743 return 0;
9744
9745 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009746 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009747 return 0;
9748 smp->strm->unique_id[0] = '\0';
9749 }
9750 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9751 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9752
9753 smp->data.type = SMP_T_STR;
9754 smp->data.u.str.str = smp->strm->unique_id;
9755 smp->flags = SMP_F_CONST;
9756 return 1;
9757}
9758
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009759/* Returns a string block containing all headers including the
9760 * empty line wich separes headers from the body. This is useful
9761 * form some headers analysis.
9762 */
9763static int
9764smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9765{
9766 struct http_msg *msg;
9767 struct hdr_idx *idx;
9768 struct http_txn *txn;
9769
9770 CHECK_HTTP_MESSAGE_FIRST();
9771
9772 txn = smp->strm->txn;
9773 idx = &txn->hdr_idx;
9774 msg = &txn->req;
9775
9776 smp->data.type = SMP_T_STR;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009777 smp->data.u.str.str = ci_head(msg->chn) + hdr_idx_first_pos(idx);
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009778 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Willy Tarreauf37954d2018-06-15 18:31:02 +02009779 (ci_head(msg->chn)[msg->eoh] == '\r');
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009780
9781 return 1;
9782}
9783
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009784/* Returns the header request in a length/value encoded format.
9785 * This is useful for exchanges with the SPOE.
9786 *
9787 * A "length value" is a multibyte code encoding numbers. It uses the
9788 * SPOE format. The encoding is the following:
9789 *
9790 * Each couple "header name" / "header value" is composed
9791 * like this:
9792 * "length value" "header name bytes"
9793 * "length value" "header value bytes"
9794 * When the last header is reached, the header name and the header
9795 * value are empty. Their length are 0
9796 */
9797static int
9798smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9799{
9800 struct http_msg *msg;
9801 struct chunk *temp;
9802 struct hdr_idx *idx;
9803 const char *cur_ptr, *cur_next, *p;
9804 int old_idx, cur_idx;
9805 struct hdr_idx_elem *cur_hdr;
9806 const char *hn, *hv;
9807 int hnl, hvl;
9808 int ret;
9809 struct http_txn *txn;
9810 char *buf;
9811 char *end;
9812
9813 CHECK_HTTP_MESSAGE_FIRST();
9814
9815 temp = get_trash_chunk();
9816 buf = temp->str;
9817 end = temp->str + temp->size;
9818
9819 txn = smp->strm->txn;
9820 idx = &txn->hdr_idx;
9821 msg = &txn->req;
9822
9823 /* Build array of headers. */
9824 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009825 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009826 while (1) {
9827 cur_idx = idx->v[old_idx].next;
9828 if (!cur_idx)
9829 break;
9830 old_idx = cur_idx;
9831
9832 cur_hdr = &idx->v[cur_idx];
9833 cur_ptr = cur_next;
9834 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9835
9836 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9837 * and the next header starts at cur_next. We'll check
9838 * this header in the list as well as against the default
9839 * rule.
9840 */
9841
9842 /* look for ': *'. */
9843 hn = cur_ptr;
9844 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9845 if (p >= cur_ptr+cur_hdr->len)
9846 continue;
9847 hnl = p - hn;
9848 p++;
9849 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9850 p++;
9851 if (p >= cur_ptr + cur_hdr->len)
9852 continue;
9853 hv = p;
9854 hvl = cur_ptr + cur_hdr->len-p;
9855
9856 /* encode the header name. */
9857 ret = encode_varint(hnl, &buf, end);
9858 if (ret == -1)
9859 return 0;
9860 if (buf + hnl > end)
9861 return 0;
9862 memcpy(buf, hn, hnl);
9863 buf += hnl;
9864
9865 /* encode and copy the value. */
9866 ret = encode_varint(hvl, &buf, end);
9867 if (ret == -1)
9868 return 0;
9869 if (buf + hvl > end)
9870 return 0;
9871 memcpy(buf, hv, hvl);
9872 buf += hvl;
9873 }
9874
9875 /* encode the end of the header list with empty
9876 * header name and header value.
9877 */
9878 ret = encode_varint(0, &buf, end);
9879 if (ret == -1)
9880 return 0;
9881 ret = encode_varint(0, &buf, end);
9882 if (ret == -1)
9883 return 0;
9884
9885 /* Initialise sample data which will be filled. */
9886 smp->data.type = SMP_T_BIN;
9887 smp->data.u.str.str = temp->str;
9888 smp->data.u.str.len = buf - temp->str;
9889 smp->data.u.str.size = temp->size;
9890
9891 return 1;
9892}
9893
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009894/* returns the longest available part of the body. This requires that the body
9895 * has been waited for using http-buffer-request.
9896 */
9897static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009898smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009899{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009900 struct http_msg *msg;
9901 unsigned long len;
9902 unsigned long block1;
9903 char *body;
9904 struct chunk *temp;
9905
9906 CHECK_HTTP_MESSAGE_FIRST();
9907
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009908 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009909 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009910 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009911 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009912
9913 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +02009914 body = c_ptr(msg->chn, -http_data_rewind(msg));
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009915
9916 block1 = len;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009917 if (block1 > b_wrap(msg->chn->buf) - body)
9918 block1 = b_wrap(msg->chn->buf) - body;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009919
9920 if (block1 == len) {
9921 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009922 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009923 smp->data.u.str.str = body;
9924 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009925 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9926 }
9927 else {
9928 /* buffer is wrapped, we need to defragment it */
9929 temp = get_trash_chunk();
9930 memcpy(temp->str, body, block1);
Willy Tarreauf37954d2018-06-15 18:31:02 +02009931 memcpy(temp->str + block1, b_orig(msg->chn->buf), len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009932 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009933 smp->data.u.str.str = temp->str;
9934 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009935 smp->flags = SMP_F_VOL_TEST;
9936 }
9937 return 1;
9938}
9939
9940
9941/* returns the available length of the body. This requires that the body
9942 * has been waited for using http-buffer-request.
9943 */
9944static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009945smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009946{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009947 struct http_msg *msg;
9948
9949 CHECK_HTTP_MESSAGE_FIRST();
9950
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009951 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009952 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009953 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009954 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009955
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009956 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009957 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009958
9959 smp->flags = SMP_F_VOL_TEST;
9960 return 1;
9961}
9962
9963
9964/* returns the advertised length of the body, or the advertised size of the
9965 * chunks available in the buffer. This requires that the body has been waited
9966 * for using http-buffer-request.
9967 */
9968static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009969smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009970{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009971 struct http_msg *msg;
9972
9973 CHECK_HTTP_MESSAGE_FIRST();
9974
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009975 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009976 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009977 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009978 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009979
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009980 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009981 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009982
9983 smp->flags = SMP_F_VOL_TEST;
9984 return 1;
9985}
9986
9987
Willy Tarreau8797c062007-05-07 00:55:35 +02009988/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009989static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009990smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009991{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009992 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009993
Willy Tarreauc0239e02012-04-16 14:42:55 +02009994 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009995
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009996 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009997 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009998 smp->data.u.str.len = txn->req.sl.rq.u_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009999 smp->data.u.str.str = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010000 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010001 return 1;
10002}
10003
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010004static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010005smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010006{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010007 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010008 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010009
Willy Tarreauc0239e02012-04-16 14:42:55 +020010010 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010011
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010012 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010013 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010014 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010015 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010016
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010017 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010018 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010019 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010020 return 1;
10021}
10022
10023static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010024smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010025{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010026 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010027 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010028
Willy Tarreauc0239e02012-04-16 14:42:55 +020010029 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010030
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010031 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010032 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010033 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10034 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010035
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010036 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010037 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010038 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010039 return 1;
10040}
10041
Willy Tarreau185b5c42012-04-26 15:11:51 +020010042/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10043 * Accepts an optional argument of type string containing the header field name,
10044 * and an optional argument of type signed or unsigned integer to request an
10045 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010046 * headers are considered from the first one. It does not stop on commas and
10047 * returns full lines instead (useful for User-Agent or Date for example).
10048 */
10049static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010050smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010051{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010052 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010053 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010054 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010055 int occ = 0;
10056 const char *name_str = NULL;
10057 int name_len = 0;
10058
10059 if (!ctx) {
10060 /* first call */
10061 ctx = &static_hdr_ctx;
10062 ctx->idx = 0;
10063 smp->ctx.a[0] = ctx;
10064 }
10065
10066 if (args) {
10067 if (args[0].type != ARGT_STR)
10068 return 0;
10069 name_str = args[0].data.str.str;
10070 name_len = args[0].data.str.len;
10071
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010072 if (args[1].type == ARGT_SINT)
10073 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010074 }
10075
10076 CHECK_HTTP_MESSAGE_FIRST();
10077
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010078 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010079 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010080
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010081 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10082 /* search for header from the beginning */
10083 ctx->idx = 0;
10084
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010085 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010086 /* no explicit occurrence and single fetch => last header by default */
10087 occ = -1;
10088
10089 if (!occ)
10090 /* prepare to report multiple occurrences for ACL fetches */
10091 smp->flags |= SMP_F_NOT_LAST;
10092
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010093 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010094 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010095 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010096 return 1;
10097
10098 smp->flags &= ~SMP_F_NOT_LAST;
10099 return 0;
10100}
10101
10102/* 6. Check on HTTP header count. The number of occurrences is returned.
10103 * Accepts exactly 1 argument of type string. It does not stop on commas and
10104 * returns full lines instead (useful for User-Agent or Date for example).
10105 */
10106static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010107smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010108{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010109 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010110 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010111 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010112 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010113 const char *name = NULL;
10114 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010115
Willy Tarreau601a4d12015-04-01 19:16:09 +020010116 if (args && args->type == ARGT_STR) {
10117 name = args->data.str.str;
10118 len = args->data.str.len;
10119 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010120
10121 CHECK_HTTP_MESSAGE_FIRST();
10122
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010123 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010124 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010125
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010126 ctx.idx = 0;
10127 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010128 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010129 cnt++;
10130
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010131 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010132 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010133 smp->flags = SMP_F_VOL_HDR;
10134 return 1;
10135}
10136
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010137static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010138smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010139{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010140 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010141 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010142 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010143 struct chunk *temp;
10144 char del = ',';
10145
10146 if (args && args->type == ARGT_STR)
10147 del = *args[0].data.str.str;
10148
10149 CHECK_HTTP_MESSAGE_FIRST();
10150
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010151 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010152 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010153
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010154 temp = get_trash_chunk();
10155
10156 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010157 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010158 if (temp->len)
10159 temp->str[temp->len++] = del;
10160 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10161 temp->len += ctx.del;
10162 }
10163
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010164 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010165 smp->data.u.str.str = temp->str;
10166 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010167 smp->flags = SMP_F_VOL_HDR;
10168 return 1;
10169}
10170
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010171/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10172 * Accepts an optional argument of type string containing the header field name,
10173 * and an optional argument of type signed or unsigned integer to request an
10174 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010175 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010176 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010177static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010178smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010179{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010180 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010181 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010182 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010183 int occ = 0;
10184 const char *name_str = NULL;
10185 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010186
Willy Tarreaua890d072013-04-02 12:01:06 +020010187 if (!ctx) {
10188 /* first call */
10189 ctx = &static_hdr_ctx;
10190 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010191 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010192 }
10193
Willy Tarreau185b5c42012-04-26 15:11:51 +020010194 if (args) {
10195 if (args[0].type != ARGT_STR)
10196 return 0;
10197 name_str = args[0].data.str.str;
10198 name_len = args[0].data.str.len;
10199
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010200 if (args[1].type == ARGT_SINT)
10201 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010202 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010203
Willy Tarreaue333ec92012-04-16 16:26:40 +020010204 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010205
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010206 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010207 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010208
Willy Tarreau185b5c42012-04-26 15:11:51 +020010209 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010210 /* search for header from the beginning */
10211 ctx->idx = 0;
10212
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010213 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010214 /* no explicit occurrence and single fetch => last header by default */
10215 occ = -1;
10216
10217 if (!occ)
10218 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010219 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010220
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010221 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010222 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010223 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010224 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010225
Willy Tarreau37406352012-04-23 16:16:37 +020010226 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010227 return 0;
10228}
10229
Willy Tarreauc11416f2007-06-17 16:58:38 +020010230/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010231 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010232 */
10233static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010234smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010235{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010236 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010237 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010238 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010239 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010240 const char *name = NULL;
10241 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010242
Willy Tarreau601a4d12015-04-01 19:16:09 +020010243 if (args && args->type == ARGT_STR) {
10244 name = args->data.str.str;
10245 len = args->data.str.len;
10246 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010247
Willy Tarreaue333ec92012-04-16 16:26:40 +020010248 CHECK_HTTP_MESSAGE_FIRST();
10249
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010250 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010251 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010252
Willy Tarreau33a7e692007-06-10 19:45:56 +020010253 ctx.idx = 0;
10254 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010255 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010256 cnt++;
10257
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010258 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010259 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010260 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010261 return 1;
10262}
10263
Willy Tarreau185b5c42012-04-26 15:11:51 +020010264/* Fetch an HTTP header's integer value. The integer value is returned. It
10265 * takes a mandatory argument of type string and an optional one of type int
10266 * to designate a specific occurrence. It returns an unsigned integer, which
10267 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010268 */
10269static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010270smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010271{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010272 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010273
Willy Tarreauf853c462012-04-23 18:53:56 +020010274 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010275 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010276 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010277 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010278
Willy Tarreaud53e2422012-04-16 17:21:11 +020010279 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010280}
10281
Cyril Bonté69fa9922012-10-25 00:01:06 +020010282/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10283 * and an optional one of type int to designate a specific occurrence.
10284 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010285 */
10286static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010287smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010288{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010289 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010290
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010291 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010292 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010293 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010294 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010295 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010296 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010297 if (smp->data.u.str.len < temp->size - 1) {
10298 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10299 temp->str[smp->data.u.str.len] = '\0';
10300 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010301 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010302 break;
10303 }
10304 }
10305 }
10306
Willy Tarreaud53e2422012-04-16 17:21:11 +020010307 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010308 if (!(smp->flags & SMP_F_NOT_LAST))
10309 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010310 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010311 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010312}
10313
Willy Tarreau737b0c12007-06-10 21:28:46 +020010314/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10315 * the first '/' after the possible hostname, and ends before the possible '?'.
10316 */
10317static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010318smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010319{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010320 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010321 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010322
Willy Tarreauc0239e02012-04-16 14:42:55 +020010323 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010324
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010325 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010326 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010327 ptr = http_get_path(txn);
10328 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010329 return 0;
10330
10331 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010332 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010333 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010334
10335 while (ptr < end && *ptr != '?')
10336 ptr++;
10337
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010338 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010339 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010340 return 1;
10341}
10342
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010343/* This produces a concatenation of the first occurrence of the Host header
10344 * followed by the path component if it begins with a slash ('/'). This means
10345 * that '*' will not be added, resulting in exactly the first Host entry.
10346 * If no Host header is found, then the path is returned as-is. The returned
10347 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010348 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010349 */
10350static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010351smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010352{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010353 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010354 char *ptr, *end, *beg;
10355 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010356 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010357
10358 CHECK_HTTP_MESSAGE_FIRST();
10359
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010360 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010361 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010362 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010363 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010364
10365 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010366 temp = get_trash_chunk();
10367 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010368 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010369 smp->data.u.str.str = temp->str;
10370 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010371
10372 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020010373 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010374 beg = http_get_path(txn);
10375 if (!beg)
10376 beg = end;
10377
10378 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10379
10380 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010381 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10382 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010383 }
10384
10385 smp->flags = SMP_F_VOL_1ST;
10386 return 1;
10387}
10388
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010389/* This produces a 32-bit hash of the concatenation of the first occurrence of
10390 * the Host header followed by the path component if it begins with a slash ('/').
10391 * This means that '*' will not be added, resulting in exactly the first Host
10392 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010393 * is hashed using the path hash followed by a full avalanche hash and provides a
10394 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010395 * high-traffic sites without having to store whole paths.
10396 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010397int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010398smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010399{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010400 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010401 struct hdr_ctx ctx;
10402 unsigned int hash = 0;
10403 char *ptr, *beg, *end;
10404 int len;
10405
10406 CHECK_HTTP_MESSAGE_FIRST();
10407
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010408 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010409 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010410 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010411 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10412 ptr = ctx.line + ctx.val;
10413 len = ctx.vlen;
10414 while (len--)
10415 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10416 }
10417
10418 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020010419 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010420 beg = http_get_path(txn);
10421 if (!beg)
10422 beg = end;
10423
10424 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10425
10426 if (beg < ptr && *beg == '/') {
10427 while (beg < ptr)
10428 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10429 }
10430 hash = full_hash(hash);
10431
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010432 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010433 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010434 smp->flags = SMP_F_VOL_1ST;
10435 return 1;
10436}
10437
Willy Tarreau4a550602012-12-09 14:53:32 +010010438/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010439 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10440 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10441 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010442 * that in environments where IPv6 is insignificant, truncating the output to
10443 * 8 bytes would still work.
10444 */
10445static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010446smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010447{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010448 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010449 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010450
10451 if (!cli_conn)
10452 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010453
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010454 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010455 return 0;
10456
Willy Tarreau47ca5452012-12-23 20:22:19 +010010457 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010458 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010459 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010460
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010461 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010462 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010463 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010464 temp->len += 4;
10465 break;
10466 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010467 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010468 temp->len += 16;
10469 break;
10470 default:
10471 return 0;
10472 }
10473
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010474 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010475 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010476 return 1;
10477}
10478
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010479/* Extracts the query string, which comes after the question mark '?'. If no
10480 * question mark is found, nothing is returned. Otherwise it returns a sample
10481 * of type string carrying the whole query string.
10482 */
10483static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010484smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010485{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010486 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010487 char *ptr, *end;
10488
10489 CHECK_HTTP_MESSAGE_FIRST();
10490
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010491 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010492 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010493 end = ptr + txn->req.sl.rq.u_l;
10494
10495 /* look up the '?' */
10496 do {
10497 if (ptr == end)
10498 return 0;
10499 } while (*ptr++ != '?');
10500
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010501 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010502 smp->data.u.str.str = ptr;
10503 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010504 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10505 return 1;
10506}
10507
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010508static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010509smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010510{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010511 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10512 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10513 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010514
Willy Tarreau24e32d82012-04-23 23:55:44 +020010515 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010516
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010517 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010518 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010519 return 1;
10520}
10521
Willy Tarreau7f18e522010-10-22 20:04:13 +020010522/* return a valid test if the current request is the first one on the connection */
10523static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010524smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010525{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010526 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010527 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010528 return 1;
10529}
10530
Willy Tarreau34db1082012-04-19 17:16:54 +020010531/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010532static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010533smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010534{
10535
Willy Tarreau24e32d82012-04-23 23:55:44 +020010536 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010537 return 0;
10538
Willy Tarreauc0239e02012-04-16 14:42:55 +020010539 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010540
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010541 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010542 return 0;
10543
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010544 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010545 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010546 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010547 return 1;
10548}
Willy Tarreau8797c062007-05-07 00:55:35 +020010549
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010550/* Accepts exactly 1 argument of type userlist */
10551static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010552smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010553{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010554 if (!args || args->type != ARGT_USR)
10555 return 0;
10556
10557 CHECK_HTTP_MESSAGE_FIRST();
10558
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010559 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010560 return 0;
10561
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010562 /* if the user does not belong to the userlist or has a wrong password,
10563 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010564 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010565 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010566 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10567 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010568 return 0;
10569
10570 /* pat_match_auth() will need the user list */
10571 smp->ctx.a[0] = args->data.usr;
10572
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010573 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010574 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010575 smp->data.u.str.str = smp->strm->txn->auth.user;
10576 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010577
10578 return 1;
10579}
10580
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010581/* Try to find the next occurrence of a cookie name in a cookie header value.
10582 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10583 * the cookie value is returned into *value and *value_l, and the function
10584 * returns a pointer to the next pointer to search from if the value was found.
10585 * Otherwise if the cookie was not found, NULL is returned and neither value
10586 * nor value_l are touched. The input <hdr> string should first point to the
10587 * header's value, and the <hdr_end> pointer must point to the first character
10588 * not part of the value. <list> must be non-zero if value may represent a list
10589 * of values (cookie headers). This makes it faster to abort parsing when no
10590 * list is expected.
10591 */
David Carlier4686f792015-09-25 14:10:50 +010010592char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010593extract_cookie_value(char *hdr, const char *hdr_end,
10594 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010595 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010596{
10597 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10598 char *next;
10599
10600 /* we search at least a cookie name followed by an equal, and more
10601 * generally something like this :
10602 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10603 */
10604 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10605 /* Iterate through all cookies on this line */
10606
Willy Tarreau2235b262016-11-05 15:50:20 +010010607 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010608 att_beg++;
10609
10610 /* find att_end : this is the first character after the last non
10611 * space before the equal. It may be equal to hdr_end.
10612 */
10613 equal = att_end = att_beg;
10614
10615 while (equal < hdr_end) {
10616 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10617 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010618 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010619 continue;
10620 att_end = equal;
10621 }
10622
10623 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10624 * is between <att_beg> and <equal>, both may be identical.
10625 */
10626
10627 /* look for end of cookie if there is an equal sign */
10628 if (equal < hdr_end && *equal == '=') {
10629 /* look for the beginning of the value */
10630 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010631 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010632 val_beg++;
10633
10634 /* find the end of the value, respecting quotes */
10635 next = find_cookie_value_end(val_beg, hdr_end);
10636
10637 /* make val_end point to the first white space or delimitor after the value */
10638 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010639 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010640 val_end--;
10641 } else {
10642 val_beg = val_end = next = equal;
10643 }
10644
10645 /* We have nothing to do with attributes beginning with '$'. However,
10646 * they will automatically be removed if a header before them is removed,
10647 * since they're supposed to be linked together.
10648 */
10649 if (*att_beg == '$')
10650 continue;
10651
10652 /* Ignore cookies with no equal sign */
10653 if (equal == next)
10654 continue;
10655
10656 /* Now we have the cookie name between att_beg and att_end, and
10657 * its value between val_beg and val_end.
10658 */
10659
10660 if (att_end - att_beg == cookie_name_l &&
10661 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10662 /* let's return this value and indicate where to go on from */
10663 *value = val_beg;
10664 *value_l = val_end - val_beg;
10665 return next + 1;
10666 }
10667
10668 /* Set-Cookie headers only have the name in the first attr=value part */
10669 if (!list)
10670 break;
10671 }
10672
10673 return NULL;
10674}
10675
William Lallemanda43ba4e2014-01-28 18:14:25 +010010676/* Fetch a captured HTTP request header. The index is the position of
10677 * the "capture" option in the configuration file
10678 */
10679static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010680smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010681{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010682 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010683 int idx;
10684
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010685 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010686 return 0;
10687
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010688 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010689
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010690 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010691 return 0;
10692
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010693 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010694 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010695 smp->data.u.str.str = smp->strm->req_cap[idx];
10696 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010697
10698 return 1;
10699}
10700
10701/* Fetch a captured HTTP response header. The index is the position of
10702 * the "capture" option in the configuration file
10703 */
10704static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010705smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010706{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010707 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010708 int idx;
10709
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010710 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010711 return 0;
10712
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010713 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010714
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010715 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010716 return 0;
10717
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010718 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010719 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010720 smp->data.u.str.str = smp->strm->res_cap[idx];
10721 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010722
10723 return 1;
10724}
10725
William Lallemand65ad6e12014-01-31 15:08:02 +010010726/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10727static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010728smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010729{
10730 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010731 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010732 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010733
Willy Tarreau15e91e12015-04-04 00:52:09 +020010734 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010735 return 0;
10736
William Lallemand96a77852014-02-05 00:30:02 +010010737 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010738
William Lallemand96a77852014-02-05 00:30:02 +010010739 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10740 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010741
William Lallemand96a77852014-02-05 00:30:02 +010010742 temp = get_trash_chunk();
10743 temp->str = txn->uri;
10744 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010745 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010746 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010747 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010748
10749 return 1;
10750
10751}
10752
10753/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10754static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010755smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010756{
10757 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010758 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010759 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010760
Willy Tarreau15e91e12015-04-04 00:52:09 +020010761 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010762 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010763
William Lallemand65ad6e12014-01-31 15:08:02 +010010764 ptr = txn->uri;
10765
10766 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10767 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010768
William Lallemand65ad6e12014-01-31 15:08:02 +010010769 if (!*ptr)
10770 return 0;
10771
10772 ptr++; /* skip the space */
10773
10774 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010775 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010776 if (!ptr)
10777 return 0;
10778 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10779 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010780
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010781 smp->data.u.str = *temp;
10782 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010783 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010784 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010785
10786 return 1;
10787}
10788
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010789/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10790 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10791 */
10792static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010793smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010794{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010795 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010796
Willy Tarreau15e91e12015-04-04 00:52:09 +020010797 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010798 return 0;
10799
10800 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010801 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010802 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010803 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010804
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010805 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010806 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010807 smp->flags = SMP_F_CONST;
10808 return 1;
10809
10810}
10811
10812/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10813 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10814 */
10815static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010816smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010817{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010818 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010819
Willy Tarreau15e91e12015-04-04 00:52:09 +020010820 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010821 return 0;
10822
10823 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010824 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010825 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010826 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010827
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010828 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010829 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010830 smp->flags = SMP_F_CONST;
10831 return 1;
10832
10833}
10834
William Lallemand65ad6e12014-01-31 15:08:02 +010010835
Willy Tarreaue333ec92012-04-16 16:26:40 +020010836/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010837 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010838 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010839 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010840 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010841 * Accepts exactly 1 argument of type string. If the input options indicate
10842 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010843 * The returned sample is of type CSTR. Can be used to parse cookies in other
10844 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010845 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010846int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010847{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010848 struct http_txn *txn;
10849 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010850 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010851 const struct http_msg *msg;
10852 const char *hdr_name;
10853 int hdr_name_len;
10854 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010855 int occ = 0;
10856 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010857
Willy Tarreau24e32d82012-04-23 23:55:44 +020010858 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010859 return 0;
10860
Willy Tarreaua890d072013-04-02 12:01:06 +020010861 if (!ctx) {
10862 /* first call */
10863 ctx = &static_hdr_ctx;
10864 ctx->idx = 0;
10865 smp->ctx.a[2] = ctx;
10866 }
10867
Willy Tarreaue333ec92012-04-16 16:26:40 +020010868 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010869
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010870 txn = smp->strm->txn;
10871 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010872
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010873 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010874 msg = &txn->req;
10875 hdr_name = "Cookie";
10876 hdr_name_len = 6;
10877 } else {
10878 msg = &txn->rsp;
10879 hdr_name = "Set-Cookie";
10880 hdr_name_len = 10;
10881 }
10882
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010883 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010884 /* no explicit occurrence and single fetch => last cookie by default */
10885 occ = -1;
10886
10887 /* OK so basically here, either we want only one value and it's the
10888 * last one, or we want to iterate over all of them and we fetch the
10889 * next one.
10890 */
10891
Willy Tarreauf37954d2018-06-15 18:31:02 +020010892 sol = ci_head(msg->chn);
Willy Tarreau37406352012-04-23 16:16:37 +020010893 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010894 /* search for the header from the beginning, we must first initialize
10895 * the search parameters.
10896 */
Willy Tarreau37406352012-04-23 16:16:37 +020010897 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010898 ctx->idx = 0;
10899 }
10900
Willy Tarreau28376d62012-04-26 21:26:10 +020010901 smp->flags |= SMP_F_VOL_HDR;
10902
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010903 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010904 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10905 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010906 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10907 goto out;
10908
Willy Tarreau24e32d82012-04-23 23:55:44 +020010909 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010910 continue;
10911
Willy Tarreau37406352012-04-23 16:16:37 +020010912 smp->ctx.a[0] = ctx->line + ctx->val;
10913 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010914 }
10915
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010916 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010917 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010918 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010919 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010920 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010921 &smp->data.u.str.str,
10922 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010923 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010924 found = 1;
10925 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010926 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010927 smp->flags |= SMP_F_NOT_LAST;
10928 return 1;
10929 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010930 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010931 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010932 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010933 /* all cookie headers and values were scanned. If we're looking for the
10934 * last occurrence, we may return it now.
10935 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010936 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010937 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010938 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010939}
10940
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010941/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010942 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010943 * multiple cookies may be parsed on the same line. The returned sample is of
10944 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010945 */
10946static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010947smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010948{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010949 struct http_txn *txn;
10950 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010951 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010952 const struct http_msg *msg;
10953 const char *hdr_name;
10954 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010955 int cnt;
10956 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010957 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010958
Willy Tarreau24e32d82012-04-23 23:55:44 +020010959 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010960 return 0;
10961
Willy Tarreaue333ec92012-04-16 16:26:40 +020010962 CHECK_HTTP_MESSAGE_FIRST();
10963
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010964 txn = smp->strm->txn;
10965 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010966
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010967 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010968 msg = &txn->req;
10969 hdr_name = "Cookie";
10970 hdr_name_len = 6;
10971 } else {
10972 msg = &txn->rsp;
10973 hdr_name = "Set-Cookie";
10974 hdr_name_len = 10;
10975 }
10976
Willy Tarreauf37954d2018-06-15 18:31:02 +020010977 sol = ci_head(msg->chn);
Willy Tarreau46787ed2012-04-11 17:28:40 +020010978 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010979 ctx.idx = 0;
10980 cnt = 0;
10981
10982 while (1) {
10983 /* Note: val_beg == NULL every time we need to fetch a new header */
10984 if (!val_beg) {
10985 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10986 break;
10987
Willy Tarreau24e32d82012-04-23 23:55:44 +020010988 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010989 continue;
10990
10991 val_beg = ctx.line + ctx.val;
10992 val_end = val_beg + ctx.vlen;
10993 }
10994
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010995 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010996 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010997 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010998 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010999 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011000 &smp->data.u.str.str,
11001 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011002 cnt++;
11003 }
11004 }
11005
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011006 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011007 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011008 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011009 return 1;
11010}
11011
Willy Tarreau51539362012-05-08 12:46:28 +020011012/* Fetch an cookie's integer value. The integer value is returned. It
11013 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11014 */
11015static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011016smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011017{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011018 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011019
11020 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011021 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011022 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011023 }
11024
11025 return ret;
11026}
11027
Willy Tarreau8797c062007-05-07 00:55:35 +020011028/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011029/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011030/************************************************************************/
11031
David Cournapeau16023ee2010-12-23 20:55:41 +090011032/*
11033 * Given a path string and its length, find the position of beginning of the
11034 * query string. Returns NULL if no query string is found in the path.
11035 *
11036 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11037 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011038 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011039 */
bedis4c75cca2012-10-05 08:38:24 +020011040static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011041{
11042 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011043
bedis4c75cca2012-10-05 08:38:24 +020011044 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011045 return p ? p + 1 : NULL;
11046}
11047
bedis4c75cca2012-10-05 08:38:24 +020011048static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011049{
bedis4c75cca2012-10-05 08:38:24 +020011050 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011051}
11052
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011053/* after increasing a pointer value, it can exceed the first buffer
11054 * size. This function transform the value of <ptr> according with
11055 * the expected position. <chunks> is an array of the one or two
11056 * avalaible chunks. The first value is the start of the first chunk,
11057 * the second value if the end+1 of the first chunks. The third value
11058 * is NULL or the start of the second chunk and the fourth value is
11059 * the end+1 of the second chunk. The function returns 1 if does a
11060 * wrap, else returns 0.
11061 */
11062static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11063{
11064 if (*ptr < chunks[1])
11065 return 0;
11066 if (!chunks[2])
11067 return 0;
11068 *ptr = chunks[2] + ( *ptr - chunks[1] );
11069 return 1;
11070}
11071
David Cournapeau16023ee2010-12-23 20:55:41 +090011072/*
11073 * Given a url parameter, find the starting position of the first occurence,
11074 * or NULL if the parameter is not found.
11075 *
11076 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11077 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011078 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011079 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011080 * or the second chunk. The caller must be check the position before using the
11081 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011082 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011083static const char *
11084find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011085 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011086 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011087{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011088 const char *pos, *last, *equal;
11089 const char **bufs = chunks;
11090 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011091
David Cournapeau16023ee2010-12-23 20:55:41 +090011092
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011093 pos = bufs[0];
11094 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011095 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011096 /* Check the equal. */
11097 equal = pos + url_param_name_l;
11098 if (fix_pointer_if_wrap(chunks, &equal)) {
11099 if (equal >= chunks[3])
11100 return NULL;
11101 } else {
11102 if (equal >= chunks[1])
11103 return NULL;
11104 }
11105 if (*equal == '=') {
11106 if (pos + url_param_name_l > last) {
11107 /* process wrap case, we detect a wrap. In this case, the
11108 * comparison is performed in two parts.
11109 */
11110
11111 /* This is the end, we dont have any other chunk. */
11112 if (bufs != chunks || !bufs[2])
11113 return NULL;
11114
11115 /* Compute the length of each part of the comparison. */
11116 l1 = last - pos;
11117 l2 = url_param_name_l - l1;
11118
11119 /* The second buffer is too short to contain the compared string. */
11120 if (bufs[2] + l2 > bufs[3])
11121 return NULL;
11122
11123 if (memcmp(pos, url_param_name, l1) == 0 &&
11124 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11125 return pos;
11126
11127 /* Perform wrapping and jump the string who fail the comparison. */
11128 bufs += 2;
11129 pos = bufs[0] + l2;
11130 last = bufs[1];
11131
11132 } else {
11133 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011134 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11135 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011136 pos += url_param_name_l + 1;
11137 if (fix_pointer_if_wrap(chunks, &pos))
11138 last = bufs[2];
11139 }
11140 }
11141
11142 while (1) {
11143 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011144 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011145 pos++;
11146 if (pos < last)
11147 break;
11148 /* process buffer wrapping. */
11149 if (bufs != chunks || !bufs[2])
11150 return NULL;
11151 bufs += 2;
11152 pos = bufs[0];
11153 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011154 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011155 pos++;
11156 }
11157 return NULL;
11158}
11159
11160/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011161 * Given a url parameter name and a query string, find the next value.
11162 * An empty url_param_name matches the first available parameter.
11163 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11164 * respectively provide a pointer to the value and its end.
11165 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011166 */
11167static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011168find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011169 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011170 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011171{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011172 const char *arg_start, *qs_end;
11173 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011174
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011175 arg_start = chunks[0];
11176 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011177 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011178 /* Looks for an argument name. */
11179 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011180 url_param_name, url_param_name_l,
11181 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011182 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011183 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011184 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011185 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011186 if (!arg_start)
11187 return 0;
11188
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011189 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011190 while (1) {
11191 /* looks for the first argument. */
11192 value_start = memchr(arg_start, '=', qs_end - arg_start);
11193 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011194 /* Check for wrapping. */
11195 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011196 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011197 chunks[2]) {
11198 arg_start = chunks[2];
11199 qs_end = chunks[3];
11200 continue;
11201 }
11202 return 0;
11203 }
11204 break;
11205 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011206 value_start++;
11207 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011208 else {
11209 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011210 value_start = arg_start + url_param_name_l + 1;
11211
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011212 /* Check for pointer wrapping. */
11213 if (fix_pointer_if_wrap(chunks, &value_start)) {
11214 /* Update the end pointer. */
11215 qs_end = chunks[3];
11216
11217 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011218 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011219 return 0;
11220 }
11221 }
11222
David Cournapeau16023ee2010-12-23 20:55:41 +090011223 value_end = value_start;
11224
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011225 while (1) {
11226 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11227 value_end++;
11228 if (value_end < qs_end)
11229 break;
11230 /* process buffer wrapping. */
11231 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011232 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011233 chunks[2]) {
11234 value_end = chunks[2];
11235 qs_end = chunks[3];
11236 continue;
11237 }
11238 break;
11239 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011240
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011241 *vstart = value_start;
11242 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011243 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011244}
11245
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011246/* This scans a URL-encoded query string. It takes an optionally wrapping
11247 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11248 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11249 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011250 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011251static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011252smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011253{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011254 const char *vstart, *vend;
11255 struct chunk *temp;
11256 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011257
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011258 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011259 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011260 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011261 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011262 return 0;
11263
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011264 /* Create sample. If the value is contiguous, return the pointer as CONST,
11265 * if the value is wrapped, copy-it in a buffer.
11266 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011267 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011268 if (chunks[2] &&
11269 vstart >= chunks[0] && vstart <= chunks[1] &&
11270 vend >= chunks[2] && vend <= chunks[3]) {
11271 /* Wrapped case. */
11272 temp = get_trash_chunk();
11273 memcpy(temp->str, vstart, chunks[1] - vstart);
11274 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011275 smp->data.u.str.str = temp->str;
11276 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011277 } else {
11278 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011279 smp->data.u.str.str = (char *)vstart;
11280 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011281 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11282 }
11283
11284 /* Update context, check wrapping. */
11285 chunks[0] = vend;
11286 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11287 chunks[1] = chunks[3];
11288 chunks[2] = NULL;
11289 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011290
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011291 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011292 smp->flags |= SMP_F_NOT_LAST;
11293
David Cournapeau16023ee2010-12-23 20:55:41 +090011294 return 1;
11295}
11296
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011297/* This function iterates over each parameter of the query string. It uses
11298 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011299 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11300 * An optional parameter name is passed in args[0], otherwise any parameter is
11301 * considered. It supports an optional delimiter argument for the beginning of
11302 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011303 */
11304static int
11305smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11306{
11307 struct http_msg *msg;
11308 char delim = '?';
11309 const char *name;
11310 int name_len;
11311
Dragan Dosen26f77e52015-05-25 10:02:11 +020011312 if (!args ||
11313 (args[0].type && args[0].type != ARGT_STR) ||
11314 (args[1].type && args[1].type != ARGT_STR))
11315 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011316
Dragan Dosen26f77e52015-05-25 10:02:11 +020011317 name = "";
11318 name_len = 0;
11319 if (args->type == ARGT_STR) {
11320 name = args->data.str.str;
11321 name_len = args->data.str.len;
11322 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011323
Dragan Dosen26f77e52015-05-25 10:02:11 +020011324 if (args[1].type)
11325 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011326
Dragan Dosen26f77e52015-05-25 10:02:11 +020011327 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011328 CHECK_HTTP_MESSAGE_FIRST();
11329
11330 msg = &smp->strm->txn->req;
11331
Willy Tarreauf37954d2018-06-15 18:31:02 +020011332 smp->ctx.a[0] = find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011333 msg->sl.rq.u_l, delim);
11334 if (!smp->ctx.a[0])
11335 return 0;
11336
Willy Tarreauf37954d2018-06-15 18:31:02 +020011337 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011338
11339 /* Assume that the context is filled with NULL pointer
11340 * before the first call.
11341 * smp->ctx.a[2] = NULL;
11342 * smp->ctx.a[3] = NULL;
11343 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011344 }
11345
11346 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11347}
11348
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011349/* This function iterates over each parameter of the body. This requires
11350 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011351 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11352 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11353 * optional second part if the body wraps at the end of the buffer. An optional
11354 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011355 */
11356static int
11357smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11358{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011359 struct http_msg *msg;
11360 unsigned long len;
11361 unsigned long block1;
11362 char *body;
11363 const char *name;
11364 int name_len;
11365
11366 if (!args || (args[0].type && args[0].type != ARGT_STR))
11367 return 0;
11368
11369 name = "";
11370 name_len = 0;
11371 if (args[0].type == ARGT_STR) {
11372 name = args[0].data.str.str;
11373 name_len = args[0].data.str.len;
11374 }
11375
11376 if (!smp->ctx.a[0]) { // first call, find the query string
11377 CHECK_HTTP_MESSAGE_FIRST();
11378
11379 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011380 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011381 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011382 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011383
11384 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +020011385 body = c_ptr(msg->chn, -http_data_rewind(msg));
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011386
11387 block1 = len;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011388 if (block1 > b_wrap(msg->chn->buf) - body)
11389 block1 = b_wrap(msg->chn->buf) - body;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011390
11391 if (block1 == len) {
11392 /* buffer is not wrapped (or empty) */
11393 smp->ctx.a[0] = body;
11394 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011395
11396 /* Assume that the context is filled with NULL pointer
11397 * before the first call.
11398 * smp->ctx.a[2] = NULL;
11399 * smp->ctx.a[3] = NULL;
11400 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011401 }
11402 else {
11403 /* buffer is wrapped, we need to defragment it */
11404 smp->ctx.a[0] = body;
11405 smp->ctx.a[1] = body + block1;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011406 smp->ctx.a[2] = b_orig(msg->chn->buf);
11407 smp->ctx.a[3] = b_orig(msg->chn->buf) + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011408 }
11409 }
11410 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11411}
11412
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011413/* Return the signed integer value for the specified url parameter (see url_param
11414 * above).
11415 */
11416static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011417smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011418{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011419 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011420
11421 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011422 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011423 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011424 }
11425
11426 return ret;
11427}
11428
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011429/* This produces a 32-bit hash of the concatenation of the first occurrence of
11430 * the Host header followed by the path component if it begins with a slash ('/').
11431 * This means that '*' will not be added, resulting in exactly the first Host
11432 * entry. If no Host header is found, then the path is used. The resulting value
11433 * is hashed using the url hash followed by a full avalanche hash and provides a
11434 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11435 * high-traffic sites without having to store whole paths.
11436 * this differs from the base32 functions in that it includes the url parameters
11437 * as well as the path
11438 */
11439static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011440smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011441{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011442 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011443 struct hdr_ctx ctx;
11444 unsigned int hash = 0;
11445 char *ptr, *beg, *end;
11446 int len;
11447
11448 CHECK_HTTP_MESSAGE_FIRST();
11449
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011450 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011451 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011452 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011453 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11454 ptr = ctx.line + ctx.val;
11455 len = ctx.vlen;
11456 while (len--)
11457 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11458 }
11459
11460 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020011461 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011462 beg = http_get_path(txn);
11463 if (!beg)
11464 beg = end;
11465
11466 for (ptr = beg; ptr < end ; ptr++);
11467
11468 if (beg < ptr && *beg == '/') {
11469 while (beg < ptr)
11470 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11471 }
11472 hash = full_hash(hash);
11473
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011474 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011475 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011476 smp->flags = SMP_F_VOL_1ST;
11477 return 1;
11478}
11479
11480/* This concatenates the source address with the 32-bit hash of the Host and
11481 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11482 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11483 * on the source address length. The URL hash is stored before the address so
11484 * that in environments where IPv6 is insignificant, truncating the output to
11485 * 8 bytes would still work.
11486 */
11487static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011488smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011489{
11490 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011491 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011492
Dragan Dosendb5af612016-06-16 11:23:01 +020011493 if (!cli_conn)
11494 return 0;
11495
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011496 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011497 return 0;
11498
11499 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011500 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11501 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011502
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011503 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011504 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011505 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011506 temp->len += 4;
11507 break;
11508 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011509 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011510 temp->len += 16;
11511 break;
11512 default:
11513 return 0;
11514 }
11515
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011516 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011517 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011518 return 1;
11519}
11520
Willy Tarreau185b5c42012-04-26 15:11:51 +020011521/* This function is used to validate the arguments passed to any "hdr" fetch
11522 * keyword. These keywords support an optional positive or negative occurrence
11523 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11524 * is assumed that the types are already the correct ones. Returns 0 on error,
11525 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11526 * error message in case of error, that the caller is responsible for freeing.
11527 * The initial location must either be freeable or NULL.
11528 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011529int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011530{
11531 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011532 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011533 return 0;
11534 }
11535 return 1;
11536}
11537
Willy Tarreau276fae92013-07-25 14:36:01 +020011538/* takes an UINT value on input supposed to represent the time since EPOCH,
11539 * adds an optional offset found in args[0] and emits a string representing
11540 * the date in RFC-1123/5322 format.
11541 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011542static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011543{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011544 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011545 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11546 struct chunk *temp;
11547 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011548 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011549 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011550
11551 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011552 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011553 curr_date += args[0].data.sint;
11554
11555 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011556 if (!tm)
11557 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011558
11559 temp = get_trash_chunk();
11560 temp->len = snprintf(temp->str, temp->size - temp->len,
11561 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11562 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11563 tm->tm_hour, tm->tm_min, tm->tm_sec);
11564
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011565 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011566 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011567 return 1;
11568}
11569
Thierry FOURNIERad903512014-04-11 17:51:01 +020011570/* Match language range with language tag. RFC2616 14.4:
11571 *
11572 * A language-range matches a language-tag if it exactly equals
11573 * the tag, or if it exactly equals a prefix of the tag such
11574 * that the first tag character following the prefix is "-".
11575 *
11576 * Return 1 if the strings match, else return 0.
11577 */
11578static inline int language_range_match(const char *range, int range_len,
11579 const char *tag, int tag_len)
11580{
11581 const char *end = range + range_len;
11582 const char *tend = tag + tag_len;
11583 while (range < end) {
11584 if (*range == '-' && tag == tend)
11585 return 1;
11586 if (*range != *tag || tag == tend)
11587 return 0;
11588 range++;
11589 tag++;
11590 }
11591 /* Return true only if the last char of the tag is matched. */
11592 return tag == tend;
11593}
11594
11595/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011596static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011597{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011598 const char *al = smp->data.u.str.str;
11599 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011600 const char *token;
11601 int toklen;
11602 int qvalue;
11603 const char *str;
11604 const char *w;
11605 int best_q = 0;
11606
11607 /* Set the constant to the sample, because the output of the
11608 * function will be peek in the constant configuration string.
11609 */
11610 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011611 smp->data.u.str.size = 0;
11612 smp->data.u.str.str = "";
11613 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011614
11615 /* Parse the accept language */
11616 while (1) {
11617
11618 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011619 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011620 al++;
11621 if (al >= end)
11622 break;
11623
11624 /* Start of the fisrt word. */
11625 token = al;
11626
11627 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011628 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011629 al++;
11630 if (al == token)
11631 goto expect_comma;
11632
11633 /* Length of the token. */
11634 toklen = al - token;
11635 qvalue = 1000;
11636
11637 /* Check if the token exists in the list. If the token not exists,
11638 * jump to the next token.
11639 */
11640 str = args[0].data.str.str;
11641 w = str;
11642 while (1) {
11643 if (*str == ';' || *str == '\0') {
11644 if (language_range_match(token, toklen, w, str-w))
11645 goto look_for_q;
11646 if (*str == '\0')
11647 goto expect_comma;
11648 w = str + 1;
11649 }
11650 str++;
11651 }
11652 goto expect_comma;
11653
11654look_for_q:
11655
11656 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011657 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011658 al++;
11659 if (al >= end)
11660 goto process_value;
11661
11662 /* If ',' is found, process the result */
11663 if (*al == ',')
11664 goto process_value;
11665
11666 /* If the character is different from ';', look
11667 * for the end of the header part in best effort.
11668 */
11669 if (*al != ';')
11670 goto expect_comma;
11671
11672 /* Assumes that the char is ';', now expect "q=". */
11673 al++;
11674
11675 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011676 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011677 al++;
11678 if (al >= end)
11679 goto process_value;
11680
11681 /* Expect 'q'. If no 'q', continue in best effort */
11682 if (*al != 'q')
11683 goto process_value;
11684 al++;
11685
11686 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011687 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011688 al++;
11689 if (al >= end)
11690 goto process_value;
11691
11692 /* Expect '='. If no '=', continue in best effort */
11693 if (*al != '=')
11694 goto process_value;
11695 al++;
11696
11697 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011698 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011699 al++;
11700 if (al >= end)
11701 goto process_value;
11702
11703 /* Parse the q value. */
11704 qvalue = parse_qvalue(al, &al);
11705
11706process_value:
11707
11708 /* If the new q value is the best q value, then store the associated
11709 * language in the response. If qvalue is the biggest value (1000),
11710 * break the process.
11711 */
11712 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011713 smp->data.u.str.str = (char *)w;
11714 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011715 if (qvalue >= 1000)
11716 break;
11717 best_q = qvalue;
11718 }
11719
11720expect_comma:
11721
11722 /* Expect comma or end. If the end is detected, quit the loop. */
11723 while (al < end && *al != ',')
11724 al++;
11725 if (al >= end)
11726 break;
11727
11728 /* Comma is found, jump it and restart the analyzer. */
11729 al++;
11730 }
11731
11732 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011733 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11734 smp->data.u.str.str = args[1].data.str.str;
11735 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011736 }
11737
11738 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011739 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011740}
11741
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011742/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011743static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011744{
11745 /* If the constant flag is set or if not size is avalaible at
11746 * the end of the buffer, copy the string in other buffer
11747 * before decoding.
11748 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011749 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011750 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011751 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11752 smp->data.u.str.str = str->str;
11753 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011754 smp->flags &= ~SMP_F_CONST;
11755 }
11756
11757 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011758 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11759 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011760 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011761}
11762
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011763static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11764{
11765 struct proxy *fe = strm_fe(smp->strm);
11766 int idx, i;
11767 struct cap_hdr *hdr;
11768 int len;
11769
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011770 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011771 return 0;
11772
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011773 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011774
11775 /* Check the availibity of the capture id. */
11776 if (idx > fe->nb_req_cap - 1)
11777 return 0;
11778
11779 /* Look for the original configuration. */
11780 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11781 hdr != NULL && i != idx ;
11782 i--, hdr = hdr->next);
11783 if (!hdr)
11784 return 0;
11785
11786 /* check for the memory allocation */
11787 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011788 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011789 if (smp->strm->req_cap[hdr->index] == NULL)
11790 return 0;
11791
11792 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011793 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011794 if (len > hdr->len)
11795 len = hdr->len;
11796
11797 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011798 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011799 smp->strm->req_cap[idx][len] = '\0';
11800
11801 return 1;
11802}
11803
11804static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11805{
11806 struct proxy *fe = strm_fe(smp->strm);
11807 int idx, i;
11808 struct cap_hdr *hdr;
11809 int len;
11810
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011811 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011812 return 0;
11813
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011814 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011815
11816 /* Check the availibity of the capture id. */
11817 if (idx > fe->nb_rsp_cap - 1)
11818 return 0;
11819
11820 /* Look for the original configuration. */
11821 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11822 hdr != NULL && i != idx ;
11823 i--, hdr = hdr->next);
11824 if (!hdr)
11825 return 0;
11826
11827 /* check for the memory allocation */
11828 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011829 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011830 if (smp->strm->res_cap[hdr->index] == NULL)
11831 return 0;
11832
11833 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011834 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011835 if (len > hdr->len)
11836 len = hdr->len;
11837
11838 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011839 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011840 smp->strm->res_cap[idx][len] = '\0';
11841
11842 return 1;
11843}
11844
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011845/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011846 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011847 * the relevant part of the request line accordingly. Then it updates various
11848 * pointers to the next elements which were moved, and the total buffer length.
11849 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011850 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11851 * error, though this can be revisited when this code is finally exploited.
11852 *
11853 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11854 * query string and 3 to replace uri.
11855 *
11856 * In query string case, the mark question '?' must be set at the start of the
11857 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011858 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011859int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011860 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011861{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011862 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011863 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011864 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011865 int delta;
11866
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011867 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011868 case 0: // method
Willy Tarreauf37954d2018-06-15 18:31:02 +020011869 cur_ptr = ci_head(&s->req);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011870 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11871
11872 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011873 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011874 txn->req.sl.rq.m_l += delta;
11875 txn->req.sl.rq.u += delta;
11876 txn->req.sl.rq.v += delta;
11877 break;
11878
11879 case 1: // path
11880 cur_ptr = http_get_path(txn);
11881 if (!cur_ptr)
Willy Tarreauf37954d2018-06-15 18:31:02 +020011882 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011883
11884 cur_end = cur_ptr;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011885 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011886 cur_end++;
11887
11888 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011889 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011890 txn->req.sl.rq.u_l += delta;
11891 txn->req.sl.rq.v += delta;
11892 break;
11893
11894 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011895 offset = 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011896 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011897 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11898 while (cur_ptr < cur_end && *cur_ptr != '?')
11899 cur_ptr++;
11900
11901 /* skip the question mark or indicate that we must insert it
11902 * (but only if the format string is not empty then).
11903 */
11904 if (cur_ptr < cur_end)
11905 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011906 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011907 offset = 0;
11908
11909 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011910 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011911 txn->req.sl.rq.u_l += delta;
11912 txn->req.sl.rq.v += delta;
11913 break;
11914
11915 case 3: // uri
Willy Tarreauf37954d2018-06-15 18:31:02 +020011916 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011917 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11918
11919 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011920 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011921 txn->req.sl.rq.u_l += delta;
11922 txn->req.sl.rq.v += delta;
11923 break;
11924
11925 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011926 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011927 }
11928
11929 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011930 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011931 txn->req.sl.rq.l += delta;
11932 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011933 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011934 return 0;
11935}
11936
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011937/* This function replace the HTTP status code and the associated message. The
11938 * variable <status> contains the new status code. This function never fails.
11939 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011940void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011941{
11942 struct http_txn *txn = s->txn;
11943 char *cur_ptr, *cur_end;
11944 int delta;
11945 char *res;
11946 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011947 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011948 int msg_len;
11949
11950 chunk_reset(&trash);
11951
11952 res = ultoa_o(status, trash.str, trash.size);
11953 c_l = res - trash.str;
11954
11955 trash.str[c_l] = ' ';
11956 trash.len = c_l + 1;
11957
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011958 /* Do we have a custom reason format string? */
11959 if (msg == NULL)
11960 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011961 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011962 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11963 trash.len += msg_len;
11964
Willy Tarreauf37954d2018-06-15 18:31:02 +020011965 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
11966 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011967
11968 /* commit changes and adjust message */
11969 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11970
11971 /* adjust res line offsets and lengths */
11972 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11973 txn->rsp.sl.st.c_l = c_l;
11974 txn->rsp.sl.st.r_l = msg_len;
11975
11976 delta = trash.len - (cur_end - cur_ptr);
11977 txn->rsp.sl.st.l += delta;
11978 txn->hdr_idx.v[0].len += delta;
11979 http_msg_move_end(&txn->rsp, delta);
11980}
11981
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011982/* This function executes one of the set-{method,path,query,uri} actions. It
11983 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011984 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011985 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011986 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11987 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011988 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011989enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011990 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011991{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011992 struct chunk *replace;
11993 enum act_return ret = ACT_RET_ERR;
11994
11995 replace = alloc_trash_chunk();
11996 if (!replace)
11997 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011998
11999 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012000 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020012001 replace->str[replace->len++] = '?';
12002 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
12003 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012004
Dragan Dosen2ae327e2017-10-26 11:25:10 +020012005 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
12006
12007 ret = ACT_RET_CONT;
12008
12009leave:
12010 free_trash_chunk(replace);
12011 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012012}
12013
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012014/* This function is just a compliant action wrapper for "set-status". */
12015enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012016 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012017{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012018 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012019 return ACT_RET_CONT;
12020}
12021
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012022/* parse an http-request action among :
12023 * set-method
12024 * set-path
12025 * set-query
12026 * set-uri
12027 *
12028 * All of them accept a single argument of type string representing a log-format.
12029 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12030 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012031 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012032 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012033enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12034 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012035{
12036 int cur_arg = *orig_arg;
12037
Thierry FOURNIER42148732015-09-02 17:17:33 +020012038 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012039
12040 switch (args[0][4]) {
12041 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012042 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012043 rule->action_ptr = http_action_set_req_line;
12044 break;
12045 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012046 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012047 rule->action_ptr = http_action_set_req_line;
12048 break;
12049 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012050 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012051 rule->action_ptr = http_action_set_req_line;
12052 break;
12053 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012054 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012055 rule->action_ptr = http_action_set_req_line;
12056 break;
12057 default:
12058 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012059 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012060 }
12061
12062 if (!*args[cur_arg] ||
12063 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12064 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012065 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012066 }
12067
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012068 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012069 px->conf.args.ctx = ARGC_HRQ;
12070 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
12071 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012072 return ACT_RET_PRS_ERR;
12073 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012074
12075 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012076 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012077}
12078
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012079/* parse set-status action:
12080 * This action accepts a single argument of type int representing
12081 * an http status code. It returns ACT_RET_PRS_OK on success,
12082 * ACT_RET_PRS_ERR on error.
12083 */
12084enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12085 struct act_rule *rule, char **err)
12086{
12087 char *error;
12088
Thierry FOURNIER42148732015-09-02 17:17:33 +020012089 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012090 rule->action_ptr = action_http_set_status;
12091
12092 /* Check if an argument is available */
12093 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012094 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012095 return ACT_RET_PRS_ERR;
12096 }
12097
12098 /* convert status code as integer */
12099 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12100 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12101 memprintf(err, "expects an integer status code between 100 and 999");
12102 return ACT_RET_PRS_ERR;
12103 }
12104
12105 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012106
12107 /* set custom reason string */
12108 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12109 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12110 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12111 (*orig_arg)++;
12112 rule->arg.status.reason = strdup(args[*orig_arg]);
12113 (*orig_arg)++;
12114 }
12115
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012116 return ACT_RET_PRS_OK;
12117}
12118
Willy Tarreau53275e82017-11-24 07:52:01 +010012119/* This function executes the "reject" HTTP action. It clears the request and
12120 * response buffer without sending any response. It can be useful as an HTTP
12121 * alternative to the silent-drop action to defend against DoS attacks, and may
12122 * also be used with HTTP/2 to close a connection instead of just a stream.
12123 * The txn status is unchanged, indicating no response was sent. The termination
12124 * flags will indicate "PR". It always returns ACT_RET_STOP.
12125 */
12126enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
12127 struct session *sess, struct stream *s, int flags)
12128{
12129 channel_abort(&s->req);
12130 channel_abort(&s->res);
12131 s->req.analysers = 0;
12132 s->res.analysers = 0;
12133
12134 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
12135 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
12136 if (sess->listener && sess->listener->counters)
12137 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
12138
12139 if (!(s->flags & SF_ERR_MASK))
12140 s->flags |= SF_ERR_PRXCOND;
12141 if (!(s->flags & SF_FINST_MASK))
12142 s->flags |= SF_FINST_R;
12143
12144 return ACT_RET_CONT;
12145}
12146
12147/* parse the "reject" action:
12148 * This action takes no argument and returns ACT_RET_PRS_OK on success,
12149 * ACT_RET_PRS_ERR on error.
12150 */
12151enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
12152 struct act_rule *rule, char **err)
12153{
12154 rule->action = ACT_CUSTOM;
12155 rule->action_ptr = http_action_reject;
12156 return ACT_RET_PRS_OK;
12157}
12158
Willy Tarreaua9083d02015-05-08 15:27:59 +020012159/* This function executes the "capture" action. It executes a fetch expression,
12160 * turns the result into a string and puts it in a capture slot. It always
12161 * returns 1. If an error occurs the action is cancelled, but the rule
12162 * processing continues.
12163 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012164enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012165 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012166{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012167 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012168 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012169 char **cap = s->req_cap;
12170 int len;
12171
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012172 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012173 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012174 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012175
12176 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012177 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012178
12179 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012180 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012181
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012182 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012183 if (len > h->len)
12184 len = h->len;
12185
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012186 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012187 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012188 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012189}
12190
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012191/* This function executes the "capture" action and store the result in a
12192 * capture slot if exists. It executes a fetch expression, turns the result
12193 * into a string and puts it in a capture slot. It always returns 1. If an
12194 * error occurs the action is cancelled, but the rule processing continues.
12195 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012196enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012197 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012198{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012199 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012200 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012201 char **cap = s->req_cap;
12202 struct proxy *fe = strm_fe(s);
12203 int len;
12204 int i;
12205
12206 /* Look for the original configuration. */
12207 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012208 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012209 i--, h = h->next);
12210 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012211 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012212
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012213 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012214 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012215 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012216
12217 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012218 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012219
12220 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012221 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012222
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012223 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012224 if (len > h->len)
12225 len = h->len;
12226
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012227 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012228 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012229 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012230}
12231
Christopher Faulet29730ba2017-09-18 15:26:32 +020012232/* Check an "http-request capture" action.
12233 *
12234 * The function returns 1 in success case, otherwise, it returns 0 and err is
12235 * filled.
12236 */
12237int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12238{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012239 if (rule->action_ptr != http_action_req_capture_by_id)
12240 return 1;
12241
Christopher Faulet29730ba2017-09-18 15:26:32 +020012242 if (rule->arg.capid.idx >= px->nb_req_cap) {
12243 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12244 rule->arg.capid.idx);
12245 return 0;
12246 }
12247
12248 return 1;
12249}
12250
Willy Tarreaua9083d02015-05-08 15:27:59 +020012251/* parse an "http-request capture" action. It takes a single argument which is
12252 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012253 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012254 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012255 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012256enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12257 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012258{
12259 struct sample_expr *expr;
12260 struct cap_hdr *hdr;
12261 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012262 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012263
12264 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12265 if (strcmp(args[cur_arg], "if") == 0 ||
12266 strcmp(args[cur_arg], "unless") == 0)
12267 break;
12268
12269 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012270 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012271 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012272 }
12273
Willy Tarreaua9083d02015-05-08 15:27:59 +020012274 cur_arg = *orig_arg;
12275 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12276 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012277 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012278
12279 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12280 memprintf(err,
12281 "fetch method '%s' extracts information from '%s', none of which is available here",
12282 args[cur_arg-1], sample_src_names(expr->fetch->use));
12283 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012284 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012285 }
12286
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012287 if (!args[cur_arg] || !*args[cur_arg]) {
12288 memprintf(err, "expects 'len or 'id'");
12289 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012290 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012291 }
12292
Willy Tarreaua9083d02015-05-08 15:27:59 +020012293 if (strcmp(args[cur_arg], "len") == 0) {
12294 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012295
12296 if (!(px->cap & PR_CAP_FE)) {
12297 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012298 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012299 }
12300
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012301 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012302
Willy Tarreaua9083d02015-05-08 15:27:59 +020012303 if (!args[cur_arg]) {
12304 memprintf(err, "missing length value");
12305 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012306 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012307 }
12308 /* we copy the table name for now, it will be resolved later */
12309 len = atoi(args[cur_arg]);
12310 if (len <= 0) {
12311 memprintf(err, "length must be > 0");
12312 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012313 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012314 }
12315 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012316
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012317 if (!len) {
12318 memprintf(err, "a positive 'len' argument is mandatory");
12319 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012320 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012321 }
12322
Vincent Bernat02779b62016-04-03 13:48:43 +020012323 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012324 hdr->next = px->req_cap;
12325 hdr->name = NULL; /* not a header capture */
12326 hdr->namelen = 0;
12327 hdr->len = len;
12328 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12329 hdr->index = px->nb_req_cap++;
12330
12331 px->req_cap = hdr;
12332 px->to_log |= LW_REQHDR;
12333
Thierry FOURNIER42148732015-09-02 17:17:33 +020012334 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012335 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012336 rule->arg.cap.expr = expr;
12337 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012338 }
12339
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012340 else if (strcmp(args[cur_arg], "id") == 0) {
12341 int id;
12342 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012343
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012344 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012345
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012346 if (!args[cur_arg]) {
12347 memprintf(err, "missing id value");
12348 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012349 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012350 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012351
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012352 id = strtol(args[cur_arg], &error, 10);
12353 if (*error != '\0') {
12354 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12355 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012356 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012357 }
12358 cur_arg++;
12359
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012360 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012361
Thierry FOURNIER42148732015-09-02 17:17:33 +020012362 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012363 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012364 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012365 rule->arg.capid.expr = expr;
12366 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012367 }
12368
12369 else {
12370 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12371 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012372 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012373 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012374
12375 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012376 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012377}
12378
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012379/* This function executes the "capture" action and store the result in a
12380 * capture slot if exists. It executes a fetch expression, turns the result
12381 * into a string and puts it in a capture slot. It always returns 1. If an
12382 * error occurs the action is cancelled, but the rule processing continues.
12383 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012384enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012385 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012386{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012387 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012388 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012389 char **cap = s->res_cap;
12390 struct proxy *fe = strm_fe(s);
12391 int len;
12392 int i;
12393
12394 /* Look for the original configuration. */
12395 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012396 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012397 i--, h = h->next);
12398 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012399 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012400
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012401 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012402 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012403 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012404
12405 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012406 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012407
12408 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012409 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012410
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012411 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012412 if (len > h->len)
12413 len = h->len;
12414
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012415 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012416 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012417 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012418}
12419
Christopher Faulet29730ba2017-09-18 15:26:32 +020012420/* Check an "http-response capture" action.
12421 *
12422 * The function returns 1 in success case, otherwise, it returns 0 and err is
12423 * filled.
12424 */
12425int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12426{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012427 if (rule->action_ptr != http_action_res_capture_by_id)
12428 return 1;
12429
Christopher Faulet29730ba2017-09-18 15:26:32 +020012430 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12431 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12432 rule->arg.capid.idx);
12433 return 0;
12434 }
12435
12436 return 1;
12437}
12438
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012439/* parse an "http-response capture" action. It takes a single argument which is
12440 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12441 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012442 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012443 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012444enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12445 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012446{
12447 struct sample_expr *expr;
12448 int cur_arg;
12449 int id;
12450 char *error;
12451
12452 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12453 if (strcmp(args[cur_arg], "if") == 0 ||
12454 strcmp(args[cur_arg], "unless") == 0)
12455 break;
12456
12457 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012458 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012459 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012460 }
12461
12462 cur_arg = *orig_arg;
12463 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12464 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012465 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012466
12467 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12468 memprintf(err,
12469 "fetch method '%s' extracts information from '%s', none of which is available here",
12470 args[cur_arg-1], sample_src_names(expr->fetch->use));
12471 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012472 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012473 }
12474
12475 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012476 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012477 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012478 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012479 }
12480
12481 if (strcmp(args[cur_arg], "id") != 0) {
12482 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12483 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012484 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012485 }
12486
12487 cur_arg++;
12488
12489 if (!args[cur_arg]) {
12490 memprintf(err, "missing id value");
12491 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012492 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012493 }
12494
12495 id = strtol(args[cur_arg], &error, 10);
12496 if (*error != '\0') {
12497 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12498 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012499 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012500 }
12501 cur_arg++;
12502
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012503 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012504
Thierry FOURNIER42148732015-09-02 17:17:33 +020012505 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012506 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012507 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012508 rule->arg.capid.expr = expr;
12509 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012510
12511 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012512 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012513}
12514
William Lallemand73025dd2014-04-24 14:38:37 +020012515/*
12516 * Return the struct http_req_action_kw associated to a keyword.
12517 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012518struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012519{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012520 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012521}
12522
12523/*
12524 * Return the struct http_res_action_kw associated to a keyword.
12525 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012526struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012527{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012528 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012529}
12530
Willy Tarreau12207b32016-11-22 19:48:51 +010012531
12532/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12533 * now.
12534 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012535static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau12207b32016-11-22 19:48:51 +010012536{
12537 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12538 return 1;
12539
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012540 if (*args[2]) {
12541 struct proxy *px;
12542
12543 px = proxy_find_by_name(args[2], 0, 0);
12544 if (px)
12545 appctx->ctx.errors.iid = px->uuid;
12546 else
12547 appctx->ctx.errors.iid = atoi(args[2]);
12548
12549 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012550 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012551 appctx->ctx.cli.msg = "No such proxy.\n";
12552 appctx->st0 = CLI_ST_PRINT;
12553 return 1;
12554 }
12555 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012556 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012557 appctx->ctx.errors.iid = -1; // dump all proxies
12558
Willy Tarreau35069f82016-11-25 09:16:37 +010012559 appctx->ctx.errors.flag = 0;
12560 if (strcmp(args[3], "request") == 0)
12561 appctx->ctx.errors.flag |= 4; // ignore response
12562 else if (strcmp(args[3], "response") == 0)
12563 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012564 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012565 return 0;
12566}
12567
12568/* This function dumps all captured errors onto the stream interface's
12569 * read buffer. It returns 0 if the output buffer is full and it needs
12570 * to be called again, otherwise non-zero.
12571 */
12572static int cli_io_handler_show_errors(struct appctx *appctx)
12573{
12574 struct stream_interface *si = appctx->owner;
12575 extern const char *monthname[12];
12576
12577 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12578 return 1;
12579
12580 chunk_reset(&trash);
12581
12582 if (!appctx->ctx.errors.px) {
12583 /* the function had not been called yet, let's prepare the
12584 * buffer for a response.
12585 */
12586 struct tm tm;
12587
12588 get_localtime(date.tv_sec, &tm);
12589 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12590 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12591 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12592 error_snapshot_id);
12593
Willy Tarreau06d80a92017-10-19 14:32:15 +020012594 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012595 /* Socket buffer full. Let's try again later from the same point */
12596 si_applet_cant_put(si);
12597 return 0;
12598 }
12599
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012600 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012601 appctx->ctx.errors.bol = 0;
12602 appctx->ctx.errors.ptr = -1;
12603 }
12604
12605 /* we have two inner loops here, one for the proxy, the other one for
12606 * the buffer.
12607 */
12608 while (appctx->ctx.errors.px) {
12609 struct error_snapshot *es;
12610
Willy Tarreau35069f82016-11-25 09:16:37 +010012611 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012612 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012613 if (appctx->ctx.errors.flag & 2) // skip req
12614 goto next;
12615 }
12616 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012617 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012618 if (appctx->ctx.errors.flag & 4) // skip resp
12619 goto next;
12620 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012621
12622 if (!es->when.tv_sec)
12623 goto next;
12624
12625 if (appctx->ctx.errors.iid >= 0 &&
12626 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12627 es->oe->uuid != appctx->ctx.errors.iid)
12628 goto next;
12629
12630 if (appctx->ctx.errors.ptr < 0) {
12631 /* just print headers now */
12632
12633 char pn[INET6_ADDRSTRLEN];
12634 struct tm tm;
12635 int port;
12636
12637 get_localtime(es->when.tv_sec, &tm);
12638 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12639 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12640 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12641
12642 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12643 case AF_INET:
12644 case AF_INET6:
12645 port = get_host_port(&es->src);
12646 break;
12647 default:
12648 port = 0;
12649 }
12650
Willy Tarreau35069f82016-11-25 09:16:37 +010012651 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012652 case 0:
12653 chunk_appendf(&trash,
12654 " frontend %s (#%d): invalid request\n"
12655 " backend %s (#%d)",
12656 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12657 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12658 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12659 break;
12660 case 1:
12661 chunk_appendf(&trash,
12662 " backend %s (#%d): invalid response\n"
12663 " frontend %s (#%d)",
12664 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12665 es->oe->id, es->oe->uuid);
12666 break;
12667 }
12668
12669 chunk_appendf(&trash,
12670 ", server %s (#%d), event #%u\n"
12671 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012672 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012673 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12674 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12675 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12676 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12677 es->ev_id,
12678 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012679 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012680 es->m_clen, es->m_blen,
12681 es->b_flags, es->b_out, es->b_tot,
12682 es->len, es->b_wrap, es->pos);
12683
Willy Tarreau06d80a92017-10-19 14:32:15 +020012684 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012685 /* Socket buffer full. Let's try again later from the same point */
12686 si_applet_cant_put(si);
12687 return 0;
12688 }
12689 appctx->ctx.errors.ptr = 0;
12690 appctx->ctx.errors.sid = es->sid;
12691 }
12692
12693 if (appctx->ctx.errors.sid != es->sid) {
12694 /* the snapshot changed while we were dumping it */
12695 chunk_appendf(&trash,
12696 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012697 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012698 si_applet_cant_put(si);
12699 return 0;
12700 }
12701 goto next;
12702 }
12703
12704 /* OK, ptr >= 0, so we have to dump the current line */
12705 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12706 int newptr;
12707 int newline;
12708
12709 newline = appctx->ctx.errors.bol;
12710 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12711 if (newptr == appctx->ctx.errors.ptr)
12712 return 0;
12713
Willy Tarreau06d80a92017-10-19 14:32:15 +020012714 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012715 /* Socket buffer full. Let's try again later from the same point */
12716 si_applet_cant_put(si);
12717 return 0;
12718 }
12719 appctx->ctx.errors.ptr = newptr;
12720 appctx->ctx.errors.bol = newline;
12721 };
12722 next:
12723 appctx->ctx.errors.bol = 0;
12724 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012725 appctx->ctx.errors.flag ^= 1;
12726 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012727 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012728 }
12729
12730 /* dump complete */
12731 return 1;
12732}
12733
12734/* register cli keywords */
12735static struct cli_kw_list cli_kws = {{ },{
12736 { { "show", "errors", NULL },
12737 "show errors : report last request and response errors for each proxy",
12738 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12739 },
12740 {{},}
12741}};
12742
Willy Tarreau4a568972010-05-12 08:08:50 +020012743/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012744/* All supported ACL keywords must be declared here. */
12745/************************************************************************/
12746
12747/* Note: must not be declared <const> as its list will be overwritten.
12748 * Please take care of keeping this list alphabetically sorted.
12749 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012750static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012751 { "base", "base", PAT_MATCH_STR },
12752 { "base_beg", "base", PAT_MATCH_BEG },
12753 { "base_dir", "base", PAT_MATCH_DIR },
12754 { "base_dom", "base", PAT_MATCH_DOM },
12755 { "base_end", "base", PAT_MATCH_END },
12756 { "base_len", "base", PAT_MATCH_LEN },
12757 { "base_reg", "base", PAT_MATCH_REG },
12758 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012759
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012760 { "cook", "req.cook", PAT_MATCH_STR },
12761 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12762 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12763 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12764 { "cook_end", "req.cook", PAT_MATCH_END },
12765 { "cook_len", "req.cook", PAT_MATCH_LEN },
12766 { "cook_reg", "req.cook", PAT_MATCH_REG },
12767 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012768
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012769 { "hdr", "req.hdr", PAT_MATCH_STR },
12770 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12771 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12772 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12773 { "hdr_end", "req.hdr", PAT_MATCH_END },
12774 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12775 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12776 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012777
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012778 /* these two declarations uses strings with list storage (in place
12779 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12780 * and delete functions are relative to the list management. The parse
12781 * and match method are related to the corresponding fetch methods. This
12782 * is very particular ACL declaration mode.
12783 */
12784 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12785 { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012786
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012787 { "path", "path", PAT_MATCH_STR },
12788 { "path_beg", "path", PAT_MATCH_BEG },
12789 { "path_dir", "path", PAT_MATCH_DIR },
12790 { "path_dom", "path", PAT_MATCH_DOM },
12791 { "path_end", "path", PAT_MATCH_END },
12792 { "path_len", "path", PAT_MATCH_LEN },
12793 { "path_reg", "path", PAT_MATCH_REG },
12794 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012795
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012796 { "req_ver", "req.ver", PAT_MATCH_STR },
12797 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012798
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012799 { "scook", "res.cook", PAT_MATCH_STR },
12800 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12801 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12802 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12803 { "scook_end", "res.cook", PAT_MATCH_END },
12804 { "scook_len", "res.cook", PAT_MATCH_LEN },
12805 { "scook_reg", "res.cook", PAT_MATCH_REG },
12806 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012807
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012808 { "shdr", "res.hdr", PAT_MATCH_STR },
12809 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12810 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12811 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12812 { "shdr_end", "res.hdr", PAT_MATCH_END },
12813 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12814 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12815 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012816
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012817 { "url", "url", PAT_MATCH_STR },
12818 { "url_beg", "url", PAT_MATCH_BEG },
12819 { "url_dir", "url", PAT_MATCH_DIR },
12820 { "url_dom", "url", PAT_MATCH_DOM },
12821 { "url_end", "url", PAT_MATCH_END },
12822 { "url_len", "url", PAT_MATCH_LEN },
12823 { "url_reg", "url", PAT_MATCH_REG },
12824 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012825
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012826 { "urlp", "urlp", PAT_MATCH_STR },
12827 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12828 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12829 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12830 { "urlp_end", "urlp", PAT_MATCH_END },
12831 { "urlp_len", "urlp", PAT_MATCH_LEN },
12832 { "urlp_reg", "urlp", PAT_MATCH_REG },
12833 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012834
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012835 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012836}};
12837
12838/************************************************************************/
12839/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012840/************************************************************************/
12841/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012842static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012843 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012844 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012845 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12846
Willy Tarreau87b09662015-04-03 00:22:06 +020012847 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012848 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012849
12850 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012851 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12852 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12853 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012854
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012855 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12856 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012857
Willy Tarreau409bcde2013-01-08 00:31:00 +010012858 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12859 * are only here to match the ACL's name, are request-only and are used
12860 * for ACL compatibility only.
12861 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012862 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12863 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012864 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12865 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012866
12867 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12868 * only here to match the ACL's name, are request-only and are used for
12869 * ACL compatibility only.
12870 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012871 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012872 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012873 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012874 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012875
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012876 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012877 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012878 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012879 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012880 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012881 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012882
12883 /* HTTP protocol on the request path */
12884 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012885 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012886
12887 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012888 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12889 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012890
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012891 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012892 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12893 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012894 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012895
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012896 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012897 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12898
Willy Tarreau18ed2562013-01-14 15:56:36 +010012899 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012900 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12901 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012902
Willy Tarreau18ed2562013-01-14 15:56:36 +010012903 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012904 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012905 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12906 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012907
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012908 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012909 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012910 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012911 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012912 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010012913 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012914 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012915
12916 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012917 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012918 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12919 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012920
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012921 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012922 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012923 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012924 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012925 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010012926 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012927 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012928
Willy Tarreau409bcde2013-01-08 00:31:00 +010012929 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012930 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012931 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12932 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012933 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012934
12935 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012936 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012937 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012938 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012939 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012940
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012941 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012942 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012943 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012944 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012945 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012946 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012947 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012948 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12949 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012950 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012951 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012952}};
12953
Willy Tarreau8797c062007-05-07 00:55:35 +020012954
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012955/************************************************************************/
12956/* All supported converter keywords must be declared here. */
12957/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012958/* Note: must not be declared <const> as its list will be overwritten */
12959static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012960 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012961 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012962 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12963 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012964 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012965 { NULL, NULL, 0, 0, 0 },
12966}};
12967
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012968
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012969/************************************************************************/
12970/* All supported http-request action keywords must be declared here. */
12971/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012972struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012973 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012974 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012975 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012976 { "set-method", parse_set_req_line },
12977 { "set-path", parse_set_req_line },
12978 { "set-query", parse_set_req_line },
12979 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012980 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012981 }
12982};
12983
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012984struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012985 .kw = {
12986 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012987 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012988 { NULL, NULL }
12989 }
12990};
12991
Willy Tarreau8797c062007-05-07 00:55:35 +020012992__attribute__((constructor))
12993static void __http_protocol_init(void)
12994{
12995 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012996 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012997 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012998 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012999 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013000 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013001}
13002
13003
Willy Tarreau58f10d72006-12-04 02:26:12 +010013004/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013005 * Local variables:
13006 * c-indent-level: 8
13007 * c-basic-offset: 8
13008 * End:
13009 */