blob: a7a8dadd67bbc42e7dff2d38adf00f70475e1d2e [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
Willy Tarreau83061a82018-07-13 11:56:34 +020081const struct buffer http_100_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020082 .area = (char *)&HTTP_100,
83 .data = sizeof(HTTP_100)-1
Willy Tarreau522d6c02009-12-06 18:49:18 +010084};
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 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200278struct buffer http_err_chunks[HTTP_ERR_SIZE];
Willy Tarreau80587432006-12-24 17:47:20 +0100279
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
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200425 http_err_chunks[msg].area = (char *)http_err_msgs[msg];
426 http_err_chunks[msg].data = strlen(http_err_msgs[msg]);
Willy Tarreau80587432006-12-24 17:47:20 +0100427 }
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 Tarreauc9fa0482018-07-10 17:43:27 +0200883 delta = b_rep_blk(&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 Tarreauc9fa0482018-07-10 17:43:27 +0200903 delta = b_rep_blk(&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 Tarreau83061a82018-07-13 11:56:34 +0200920 int err, int finst, const struct buffer *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 Tarreau843b7cb2018-07-13 10:54:26 +0200930 co_inject(si_ic(si), msg->area, msg->data);
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
Willy Tarreau83061a82018-07-13 11:56:34 +0200941struct buffer *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 Tarreau843b7cb2018-07-13 10:54:26 +0200945 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200946 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200947 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200948 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
Willy Tarreau83061a82018-07-13 11:56:34 +0200954http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100955{
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 Tarreau843b7cb2018-07-13 10:54:26 +02001084 trash.data = strlen(HTTP_302);
1085 memcpy(trash.area, HTTP_302, trash.data);
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 Tarreau843b7cb2018-07-13 10:54:26 +02001090 if (trash.data + 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 Tarreau843b7cb2018-07-13 10:54:26 +02001095 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
1096 trash.data += 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 Tarreauc9fa0482018-07-10 17:43:27 +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 Tarreau843b7cb2018-07-13 10:54:26 +02001113 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001114 return;
1115
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001116 memcpy(trash.area + trash.data, path, len);
1117 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001118
1119 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001120 memcpy(trash.area + trash.data,
1121 "\r\nProxy-Connection: close\r\n\r\n", 29);
1122 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001123 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001124 memcpy(trash.area + trash.data,
1125 "\r\nConnection: close\r\n\r\n", 23);
1126 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001127 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001128
1129 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001130 si_shutr(si);
1131 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001132 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001133 si->state = SI_ST_CLO;
1134
1135 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001136 txn->status = 302;
1137 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001138
1139 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001140 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001141 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001142}
1143
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001144/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001145 * that the server side is closed. Note that err_type is actually a
1146 * bitmask, where almost only aborts may be cumulated with other
1147 * values. We consider that aborted operations are more important
1148 * than timeouts or errors due to the fact that nobody else in the
1149 * logs might explain incomplete retries. All others should avoid
1150 * being cumulated. It should normally not be possible to have multiple
1151 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001152 * Note that connection errors appearing on the second request of a keep-alive
1153 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001154 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001155void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001156{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001157 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001158
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001159 /* set s->txn->status for http_error_message(s) */
1160 s->txn->status = 503;
1161
Willy Tarreauefb453c2008-10-26 20:49:47 +01001162 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001163 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001164 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001165 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001166 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001167 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001168 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001169 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001170 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001171 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001172 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001173 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001174 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001175 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001176 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001177 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001178 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001179 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001180 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001181 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001182 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001183 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001184 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001185 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001186 http_error_message(s));
1187 else { /* SI_ET_CONN_OTHER and others */
1188 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001189 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001190 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001191 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001192}
1193
Willy Tarreau42250582007-04-01 01:30:43 +02001194extern const char sess_term_cond[8];
1195extern const char sess_fin_state[8];
1196extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +01001197struct pool_head *pool_head_http_txn;
1198struct pool_head *pool_head_requri;
1199struct pool_head *pool_head_capture = NULL;
1200struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001201
Willy Tarreau117f59e2007-03-04 18:17:17 +01001202/*
1203 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001204 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001205 */
1206void capture_headers(char *som, struct hdr_idx *idx,
1207 char **cap, struct cap_hdr *cap_hdr)
1208{
1209 char *eol, *sol, *col, *sov;
1210 int cur_idx;
1211 struct cap_hdr *h;
1212 int len;
1213
1214 sol = som + hdr_idx_first_pos(idx);
1215 cur_idx = hdr_idx_first_idx(idx);
1216
1217 while (cur_idx) {
1218 eol = sol + idx->v[cur_idx].len;
1219
1220 col = sol;
1221 while (col < eol && *col != ':')
1222 col++;
1223
1224 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001225 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001226 sov++;
1227
1228 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001229 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001230 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1231 if (cap[h->index] == NULL)
1232 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +01001233 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001234
1235 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001236 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +01001237 continue;
1238 }
1239
1240 len = eol - sov;
1241 if (len > h->len)
1242 len = h->len;
1243
1244 memcpy(cap[h->index], sov, len);
1245 cap[h->index][len]=0;
1246 }
1247 }
1248 sol = eol + idx->v[cur_idx].cr + 1;
1249 cur_idx = idx->v[cur_idx].next;
1250 }
1251}
1252
Willy Tarreaubaaee002006-06-26 02:48:02 +02001253/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001254 * Returns the data from Authorization header. Function may be called more
1255 * than once so data is stored in txn->auth_data. When no header is found
1256 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001257 * searching again for something we are unable to find anyway. However, if
1258 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001259 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001260 */
1261
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001262int
Willy Tarreau87b09662015-04-03 00:22:06 +02001263get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001264{
1265
Willy Tarreaueee5b512015-04-03 23:46:31 +02001266 struct http_txn *txn = s->txn;
Willy Tarreau83061a82018-07-13 11:56:34 +02001267 struct buffer auth_method;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001268 struct hdr_ctx ctx;
1269 char *h, *p;
1270 int len;
1271
1272#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001273 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001274#endif
1275
1276 if (txn->auth.method == HTTP_AUTH_WRONG)
1277 return 0;
1278
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001279 txn->auth.method = HTTP_AUTH_WRONG;
1280
1281 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001282
1283 if (txn->flags & TX_USE_PX_CONN) {
1284 h = "Proxy-Authorization";
1285 len = strlen(h);
1286 } else {
1287 h = "Authorization";
1288 len = strlen(h);
1289 }
1290
Willy Tarreauf37954d2018-06-15 18:31:02 +02001291 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001292 return 0;
1293
1294 h = ctx.line + ctx.val;
1295
1296 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001297 len = p - h;
1298 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001299 return 0;
1300
David Carlier7365f7d2016-04-04 11:54:42 +01001301 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1302 return 0;
1303
Willy Tarreau5c557d12016-03-13 08:17:02 +01001304 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001305
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001306 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001307 struct buffer *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001308
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001309 len = base64dec(txn->auth.method_data.area,
1310 txn->auth.method_data.data,
1311 http_auth->area, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001312
1313 if (len < 0)
1314 return 0;
1315
1316
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001317 http_auth->area[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001318
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001319 p = strchr(http_auth->area, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001320
1321 if (!p)
1322 return 0;
1323
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001324 txn->auth.user = http_auth->area;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001325 *p = '\0';
1326 txn->auth.pass = p+1;
1327
1328 txn->auth.method = HTTP_AUTH_BASIC;
1329 return 1;
1330 }
1331
1332 return 0;
1333}
1334
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001335
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001336/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1337 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1338 * nothing is done and 1 is returned.
1339 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001340static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001341{
1342 int delta;
1343 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001344 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001345
1346 if (msg->sl.rq.v_l != 0)
1347 return 1;
1348
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001349 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1350 if (txn->meth != HTTP_METH_GET)
1351 return 0;
1352
Willy Tarreauf37954d2018-06-15 18:31:02 +02001353 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001354
1355 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001356 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1357 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001358 }
1359 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001360 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001361 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001362 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001363 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001364 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001365 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001366 NULL, NULL);
1367 if (unlikely(!cur_end))
1368 return 0;
1369
1370 /* we have a full HTTP/1.0 request now and we know that
1371 * we have either a CR or an LF at <ptr>.
1372 */
1373 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1374 return 1;
1375}
1376
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001377/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001378 * and "keep-alive" values. If we already know that some headers may safely
1379 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001380 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1381 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001382 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001383 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1384 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1385 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001386 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001387 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001388void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001389{
Willy Tarreau5b154472009-12-21 20:11:07 +01001390 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001391 const char *hdr_val = "Connection";
1392 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001393
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001394 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001395 return;
1396
Willy Tarreau88d349d2010-01-25 12:15:43 +01001397 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1398 hdr_val = "Proxy-Connection";
1399 hdr_len = 16;
1400 }
1401
Willy Tarreau5b154472009-12-21 20:11:07 +01001402 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001403 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +02001404 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001405 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1406 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001407 if (to_del & 2)
1408 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001409 else
1410 txn->flags |= TX_CON_KAL_SET;
1411 }
1412 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1413 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001414 if (to_del & 1)
1415 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001416 else
1417 txn->flags |= TX_CON_CLO_SET;
1418 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001419 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1420 txn->flags |= TX_HDR_CONN_UPG;
1421 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001422 }
1423
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001424 txn->flags |= TX_HDR_CONN_PRS;
1425 return;
1426}
Willy Tarreau5b154472009-12-21 20:11:07 +01001427
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001428/* Apply desired changes on the Connection: header. Values may be removed and/or
1429 * added depending on the <wanted> flags, which are exclusively composed of
1430 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1431 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1432 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001433void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001434{
1435 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001436 const char *hdr_val = "Connection";
1437 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001438
1439 ctx.idx = 0;
1440
Willy Tarreau88d349d2010-01-25 12:15:43 +01001441
1442 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1443 hdr_val = "Proxy-Connection";
1444 hdr_len = 16;
1445 }
1446
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001447 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +02001448 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001449 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1450 if (wanted & TX_CON_KAL_SET)
1451 txn->flags |= TX_CON_KAL_SET;
1452 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001453 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001454 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001455 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1456 if (wanted & TX_CON_CLO_SET)
1457 txn->flags |= TX_CON_CLO_SET;
1458 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001459 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001460 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001461 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001462
1463 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1464 return;
1465
1466 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1467 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001468 hdr_val = "Connection: close";
1469 hdr_len = 17;
1470 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1471 hdr_val = "Proxy-Connection: close";
1472 hdr_len = 23;
1473 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001474 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001475 }
1476
1477 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1478 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001479 hdr_val = "Connection: keep-alive";
1480 hdr_len = 22;
1481 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1482 hdr_val = "Proxy-Connection: keep-alive";
1483 hdr_len = 28;
1484 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001485 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001486 }
1487 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001488}
1489
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001490/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1491 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1492 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1493 * Unparsable qvalues return 1000 as "q=1.000".
1494 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001495int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001496{
1497 int q = 1000;
1498
Willy Tarreau506c69a2014-07-08 00:59:48 +02001499 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001500 goto out;
1501 q = (*qvalue++ - '0') * 1000;
1502
1503 if (*qvalue++ != '.')
1504 goto out;
1505
Willy Tarreau506c69a2014-07-08 00:59:48 +02001506 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001507 goto out;
1508 q += (*qvalue++ - '0') * 100;
1509
Willy Tarreau506c69a2014-07-08 00:59:48 +02001510 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001511 goto out;
1512 q += (*qvalue++ - '0') * 10;
1513
Willy Tarreau506c69a2014-07-08 00:59:48 +02001514 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001515 goto out;
1516 q += (*qvalue++ - '0') * 1;
1517 out:
1518 if (q > 1000)
1519 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001520 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001521 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001522 return q;
1523}
William Lallemand82fe75c2012-10-23 10:25:10 +02001524
Willy Tarreau87b09662015-04-03 00:22:06 +02001525void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001526{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001527 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001528 int tmp = TX_CON_WANT_KAL;
1529
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001530 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1531 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001532 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1533 tmp = TX_CON_WANT_TUN;
1534
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001535 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001536 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1537 tmp = TX_CON_WANT_TUN;
1538 }
1539
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001540 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001541 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1542 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001543 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001544 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1545 tmp = TX_CON_WANT_CLO;
1546 else
1547 tmp = TX_CON_WANT_SCL;
1548 }
1549
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001550 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001551 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1552 tmp = TX_CON_WANT_CLO;
1553
1554 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1555 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1556
1557 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1558 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1559 /* parse the Connection header and possibly clean it */
1560 int to_del = 0;
1561 if ((msg->flags & HTTP_MSGF_VER_11) ||
1562 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001563 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001564 to_del |= 2; /* remove "keep-alive" */
1565 if (!(msg->flags & HTTP_MSGF_VER_11))
1566 to_del |= 1; /* remove "close" */
1567 http_parse_connection_header(txn, msg, to_del);
1568 }
1569
1570 /* check if client or config asks for explicit close in KAL/SCL */
1571 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1572 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1573 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1574 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1575 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001576 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001577 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1578}
William Lallemand82fe75c2012-10-23 10:25:10 +02001579
Willy Tarreaud787e662009-07-07 10:14:51 +02001580/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1581 * processing can continue on next analysers, or zero if it either needs more
1582 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001583 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001584 * when it has nothing left to do, and may remove any analyser when it wants to
1585 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001587int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588{
Willy Tarreau59234e92008-11-30 23:51:27 +01001589 /*
1590 * We will parse the partial (or complete) lines.
1591 * We will check the request syntax, and also join multi-line
1592 * headers. An index of all the lines will be elaborated while
1593 * parsing.
1594 *
1595 * For the parsing, we use a 28 states FSM.
1596 *
1597 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02001598 * ci_head(req) = beginning of request
1599 * ci_head(req) + msg->eoh = end of processed headers / start of current one
1600 * ci_tail(req) = end of input data
1601 * msg->eol = end of current header or line (LF or CRLF)
1602 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001603 *
1604 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001605 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001606 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1607 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001608 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001609
Willy Tarreau59234e92008-11-30 23:51:27 +01001610 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001611 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001612 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001613 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001614 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001615
Christopher Faulet45073512018-07-20 10:16:29 +02001616 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01001617 now_ms, __FUNCTION__,
1618 s,
1619 req,
1620 req->rex, req->wex,
1621 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001622 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +01001623 req->analysers);
1624
Willy Tarreau52a0c602009-08-16 22:45:38 +02001625 /* we're speaking HTTP here, so let's speak HTTP to the client */
1626 s->srv_error = http_return_srv_error;
1627
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001628 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001629 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001630 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1631
Willy Tarreau83e3af02009-12-28 17:39:57 +01001632 /* There's a protected area at the end of the buffer for rewriting
1633 * purposes. We don't want to start to parse the request if the
1634 * protected area is affected, because we may have to move processed
1635 * data later, which is much more complicated.
1636 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001637 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02001638 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001639 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001640 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001641 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001642 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001643 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001644 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001645 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001646 return 0;
1647 }
Willy Tarreau188e2302018-06-15 11:11:53 +02001648 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001649 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001650 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001651 }
1652
Willy Tarreauf37954d2018-06-15 18:31:02 +02001653 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001654 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001655 }
1656
Willy Tarreau59234e92008-11-30 23:51:27 +01001657 /* 1: we might have to print this header in debug mode */
1658 if (unlikely((global.mode & MODE_DEBUG) &&
1659 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001660 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001661 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001662
Willy Tarreauf37954d2018-06-15 18:31:02 +02001663 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +02001664 /* this is a bit complex : in case of error on the request line,
1665 * we know that rq.l is still zero, so we display only the part
1666 * up to the end of the line (truncated by debug_hdr).
1667 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001668 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +01001669 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001670
Willy Tarreau59234e92008-11-30 23:51:27 +01001671 sol += hdr_idx_first_pos(&txn->hdr_idx);
1672 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001673
Willy Tarreau59234e92008-11-30 23:51:27 +01001674 while (cur_idx) {
1675 eol = sol + txn->hdr_idx.v[cur_idx].len;
1676 debug_hdr("clihdr", s, sol, eol);
1677 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1678 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001680 }
1681
Willy Tarreau58f10d72006-12-04 02:26:12 +01001682
Willy Tarreau59234e92008-11-30 23:51:27 +01001683 /*
1684 * Now we quickly check if we have found a full valid request.
1685 * If not so, we check the FD and buffer states before leaving.
1686 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001687 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001688 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001689 * on a keep-alive stream, if we encounter and error, close, t/o,
1690 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001691 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001692 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001693 * Last, we may increase some tracked counters' http request errors on
1694 * the cases that are deliberately the client's fault. For instance,
1695 * a timeout or connection reset is not counted as an error. However
1696 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001697 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001698
Willy Tarreau655dce92009-11-08 13:10:58 +01001699 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001700 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001701 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001702 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001703 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001704 stream_inc_http_req_ctr(s);
1705 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001706 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001707 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001708 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001709
Willy Tarreau59234e92008-11-30 23:51:27 +01001710 /* 1: Since we are in header mode, if there's no space
1711 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001712 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001713 * must terminate it now.
1714 */
Willy Tarreau23752332018-06-15 14:54:53 +02001715 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001716 /* FIXME: check if URI is set and return Status
1717 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001718 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001719 stream_inc_http_req_ctr(s);
1720 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001721 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001722 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02001723 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +01001724 goto return_bad_req;
1725 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001726
Willy Tarreau59234e92008-11-30 23:51:27 +01001727 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001728 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001729 if (!(s->flags & SF_ERR_MASK))
1730 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001731
Willy Tarreaufcffa692010-01-10 14:21:19 +01001732 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001733 goto failed_keep_alive;
1734
Willy Tarreau0f228a02015-05-01 15:37:53 +02001735 if (sess->fe->options & PR_O_IGNORE_PRB)
1736 goto failed_keep_alive;
1737
Willy Tarreau59234e92008-11-30 23:51:27 +01001738 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001739 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001740 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001741 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001742 }
1743
Willy Tarreaudc979f22012-12-04 10:39:01 +01001744 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001745 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001746 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001747 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001748 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001749 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001750 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001751 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001752 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001753 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001754
Willy Tarreaue7dff022015-04-03 01:14:29 +02001755 if (!(s->flags & SF_FINST_MASK))
1756 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001757 return 0;
1758 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001759
Willy Tarreau59234e92008-11-30 23:51:27 +01001760 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001761 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001762 if (!(s->flags & SF_ERR_MASK))
1763 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001764
Willy Tarreaufcffa692010-01-10 14:21:19 +01001765 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001766 goto failed_keep_alive;
1767
Willy Tarreau0f228a02015-05-01 15:37:53 +02001768 if (sess->fe->options & PR_O_IGNORE_PRB)
1769 goto failed_keep_alive;
1770
Willy Tarreau59234e92008-11-30 23:51:27 +01001771 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001772 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001773 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001774 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001775 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001776 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001777 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001778 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001779 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001780 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001781
Willy Tarreau87b09662015-04-03 00:22:06 +02001782 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001783 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001784 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001785 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001786 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001787
Willy Tarreaue7dff022015-04-03 01:14:29 +02001788 if (!(s->flags & SF_FINST_MASK))
1789 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001790 return 0;
1791 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001792
Willy Tarreau59234e92008-11-30 23:51:27 +01001793 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001794 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001795 if (!(s->flags & SF_ERR_MASK))
1796 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001797
Willy Tarreaufcffa692010-01-10 14:21:19 +01001798 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001799 goto failed_keep_alive;
1800
Willy Tarreau0f228a02015-05-01 15:37:53 +02001801 if (sess->fe->options & PR_O_IGNORE_PRB)
1802 goto failed_keep_alive;
1803
Willy Tarreau4076a152009-04-02 15:18:36 +02001804 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001805 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001806 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001807 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001808 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001809 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001810 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001811 stream_inc_http_err_ctr(s);
1812 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001813 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001814 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001815 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001816 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001817
Willy Tarreaue7dff022015-04-03 01:14:29 +02001818 if (!(s->flags & SF_FINST_MASK))
1819 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001820 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001821 }
1822
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001823 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001824 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001825 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001826#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001827 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001828 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001829 /* We need more data, we have to re-enable quick-ack in case we
1830 * previously disabled it, otherwise we might cause the client
1831 * to delay next data.
1832 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001833 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001834 }
1835#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001836
Willy Tarreaufcffa692010-01-10 14:21:19 +01001837 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1838 /* If the client starts to talk, let's fall back to
1839 * request timeout processing.
1840 */
1841 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001842 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001843 }
1844
Willy Tarreau59234e92008-11-30 23:51:27 +01001845 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001846 if (!tick_isset(req->analyse_exp)) {
1847 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1848 (txn->flags & TX_WAIT_NEXT_RQ) &&
1849 tick_isset(s->be->timeout.httpka))
1850 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1851 else
1852 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1853 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001854
Willy Tarreau59234e92008-11-30 23:51:27 +01001855 /* we're not ready yet */
1856 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001857
1858 failed_keep_alive:
1859 /* Here we process low-level errors for keep-alive requests. In
1860 * short, if the request is not the first one and it experiences
1861 * a timeout, read error or shutdown, we just silently close so
1862 * that the client can try again.
1863 */
1864 txn->status = 0;
1865 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001866 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001867 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001868 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001869 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001870 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001871 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001872 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001873
Willy Tarreaud787e662009-07-07 10:14:51 +02001874 /* OK now we have a complete HTTP request with indexed headers. Let's
1875 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001876 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001877 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001878 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001879 * byte after the last LF. msg->sov points to the first byte of data.
1880 * msg->eol cannot be trusted because it may have been left uninitialized
1881 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001882 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001883
Willy Tarreau87b09662015-04-03 00:22:06 +02001884 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001885 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001886
Willy Tarreaub16a5742010-01-10 14:46:16 +01001887 if (txn->flags & TX_WAIT_NEXT_RQ) {
1888 /* kill the pending keep-alive timeout */
1889 txn->flags &= ~TX_WAIT_NEXT_RQ;
1890 req->analyse_exp = TICK_ETERNITY;
1891 }
1892
1893
Willy Tarreaud787e662009-07-07 10:14:51 +02001894 /* Maybe we found in invalid header name while we were configured not
1895 * to block on that, so we have to capture it now.
1896 */
1897 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001898 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001899
Willy Tarreau59234e92008-11-30 23:51:27 +01001900 /*
1901 * 1: identify the method
1902 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001903 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001904
1905 /* we can make use of server redirect on GET and HEAD */
1906 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001907 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001908 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001909 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001910 /* PRI is reserved for the HTTP/2 preface */
1911 msg->err_pos = 0;
1912 goto return_bad_req;
1913 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001914
Willy Tarreau59234e92008-11-30 23:51:27 +01001915 /*
1916 * 2: check if the URI matches the monitor_uri.
1917 * We have to do this for every request which gets in, because
1918 * the monitor-uri is defined by the frontend.
1919 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001920 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1921 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001922 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001923 sess->fe->monitor_uri,
1924 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001925 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001926 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001927 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001928 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001929
Willy Tarreaue7dff022015-04-03 01:14:29 +02001930 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001931 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001932
Willy Tarreau59234e92008-11-30 23:51:27 +01001933 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001934 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001935 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001936
Willy Tarreau59234e92008-11-30 23:51:27 +01001937 ret = acl_pass(ret);
1938 if (cond->pol == ACL_COND_UNLESS)
1939 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001940
Willy Tarreau59234e92008-11-30 23:51:27 +01001941 if (ret) {
1942 /* we fail this request, let's return 503 service unavail */
1943 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001944 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001945 if (!(s->flags & SF_ERR_MASK))
1946 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001947 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001948 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001949 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001950
Willy Tarreau59234e92008-11-30 23:51:27 +01001951 /* nothing to fail, let's reply normaly */
1952 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001953 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001954 if (!(s->flags & SF_ERR_MASK))
1955 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001956 goto return_prx_cond;
1957 }
1958
1959 /*
1960 * 3: Maybe we have to copy the original REQURI for the logs ?
1961 * Note: we cannot log anymore if the request has been
1962 * classified as invalid.
1963 */
1964 if (unlikely(s->logs.logwait & LW_REQ)) {
1965 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001966 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001967 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001968
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001969 if (urilen >= global.tune.requri_len )
1970 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001971 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001972 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001973
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001974 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001975 s->do_log(s);
1976 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001977 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001978 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001979 }
Willy Tarreau06619262006-12-17 08:37:22 +01001980
Willy Tarreau91852eb2015-05-01 13:26:00 +02001981 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1982 * exactly one digit "." one digit. This check may be disabled using
1983 * option accept-invalid-http-request.
1984 */
1985 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1986 if (msg->sl.rq.v_l != 8) {
1987 msg->err_pos = msg->sl.rq.v;
1988 goto return_bad_req;
1989 }
1990
Willy Tarreauf37954d2018-06-15 18:31:02 +02001991 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1992 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1993 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1994 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001995 msg->err_pos = msg->sl.rq.v + 4;
1996 goto return_bad_req;
1997 }
1998 }
Willy Tarreau13317662015-05-01 13:47:08 +02001999 else {
2000 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2001 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2002 goto return_bad_req;
2003 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002004
Willy Tarreau5b154472009-12-21 20:11:07 +01002005 /* ... and check if the request is HTTP/1.1 or above */
2006 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02002007 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
2008 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
2009 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002010 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002011
2012 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002013 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 +01002014
Willy Tarreau88d349d2010-01-25 12:15:43 +01002015 /* if the frontend has "option http-use-proxy-header", we'll check if
2016 * we have what looks like a proxied connection instead of a connection,
2017 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2018 * Note that this is *not* RFC-compliant, however browsers and proxies
2019 * happen to do that despite being non-standard :-(
2020 * We consider that a request not beginning with either '/' or '*' is
2021 * a proxied connection, which covers both "scheme://location" and
2022 * CONNECT ip:port.
2023 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002024 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02002025 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002026 txn->flags |= TX_USE_PX_CONN;
2027
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002028 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002029 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002030
Willy Tarreau59234e92008-11-30 23:51:27 +01002031 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002032 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02002033 capture_headers(ci_head(req), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002034 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002035
Willy Tarreau557f1992015-05-01 10:05:17 +02002036 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2037 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002038 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002039 * The length of a message body is determined by one of the following
2040 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002041 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002042 * 1. Any response to a HEAD request and any response with a 1xx
2043 * (Informational), 204 (No Content), or 304 (Not Modified) status
2044 * code is always terminated by the first empty line after the
2045 * header fields, regardless of the header fields present in the
2046 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002047 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002048 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2049 * the connection will become a tunnel immediately after the empty
2050 * line that concludes the header fields. A client MUST ignore any
2051 * Content-Length or Transfer-Encoding header fields received in
2052 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002053 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002054 * 3. If a Transfer-Encoding header field is present and the chunked
2055 * transfer coding (Section 4.1) is the final encoding, the message
2056 * body length is determined by reading and decoding the chunked
2057 * data until the transfer coding indicates the data is complete.
2058 *
2059 * If a Transfer-Encoding header field is present in a response and
2060 * the chunked transfer coding is not the final encoding, the
2061 * message body length is determined by reading the connection until
2062 * it is closed by the server. If a Transfer-Encoding header field
2063 * is present in a request and the chunked transfer coding is not
2064 * the final encoding, the message body length cannot be determined
2065 * reliably; the server MUST respond with the 400 (Bad Request)
2066 * status code and then close the connection.
2067 *
2068 * If a message is received with both a Transfer-Encoding and a
2069 * Content-Length header field, the Transfer-Encoding overrides the
2070 * Content-Length. Such a message might indicate an attempt to
2071 * perform request smuggling (Section 9.5) or response splitting
2072 * (Section 9.4) and ought to be handled as an error. A sender MUST
2073 * remove the received Content-Length field prior to forwarding such
2074 * a message downstream.
2075 *
2076 * 4. If a message is received without Transfer-Encoding and with
2077 * either multiple Content-Length header fields having differing
2078 * field-values or a single Content-Length header field having an
2079 * invalid value, then the message framing is invalid and the
2080 * recipient MUST treat it as an unrecoverable error. If this is a
2081 * request message, the server MUST respond with a 400 (Bad Request)
2082 * status code and then close the connection. If this is a response
2083 * message received by a proxy, the proxy MUST close the connection
2084 * to the server, discard the received response, and send a 502 (Bad
2085 * Gateway) response to the client. If this is a response message
2086 * received by a user agent, the user agent MUST close the
2087 * connection to the server and discard the received response.
2088 *
2089 * 5. If a valid Content-Length header field is present without
2090 * Transfer-Encoding, its decimal value defines the expected message
2091 * body length in octets. If the sender closes the connection or
2092 * the recipient times out before the indicated number of octets are
2093 * received, the recipient MUST consider the message to be
2094 * incomplete and close the connection.
2095 *
2096 * 6. If this is a request message and none of the above are true, then
2097 * the message body length is zero (no message body is present).
2098 *
2099 * 7. Otherwise, this is a response message without a declared message
2100 * body length, so the message body length is determined by the
2101 * number of octets received prior to the server closing the
2102 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002103 */
2104
Willy Tarreau32b47f42009-10-18 20:55:02 +02002105 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002106 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02002107 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002108 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002109 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002110 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002111 /* chunked not last, return badreq */
2112 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002113 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002114 }
2115
Willy Tarreau1c913912015-04-30 10:57:51 +02002116 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002117 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002118 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002119 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02002120 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2121 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02002122 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002123 signed long long cl;
2124
Willy Tarreauad14f752011-09-02 20:33:27 +02002125 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002126 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002127 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002128 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002129
Willy Tarreauad14f752011-09-02 20:33:27 +02002130 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002131 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002132 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002133 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002134
Willy Tarreauad14f752011-09-02 20:33:27 +02002135 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002136 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002137 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002138 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002139
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002140 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002141 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002142 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002143 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002144
Christopher Fauletbe821b92017-03-30 11:21:53 +02002145 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002146 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002147 }
2148
Willy Tarreau34dfc602015-05-01 10:09:49 +02002149 /* even bodyless requests have a known length */
2150 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002151
Willy Tarreau179085c2014-04-28 16:48:56 +02002152 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2153 * only change if both the request and the config reference something else.
2154 * Option httpclose by itself sets tunnel mode where headers are mangled.
2155 * However, if another mode is set, it will affect it (eg: server-close/
2156 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2157 * if FE and BE have the same settings (common). The method consists in
2158 * checking if options changed between the two calls (implying that either
2159 * one is non-null, or one of them is non-null and we are there for the first
2160 * time.
2161 */
2162 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002163 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002164 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002165
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002166 /* we may have to wait for the request's body */
2167 if ((s->be->options & PR_O_WREQ_BODY) &&
2168 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2169 req->analysers |= AN_REQ_HTTP_BODY;
2170
Willy Tarreau83ece462017-12-21 15:13:09 +01002171 /*
2172 * RFC7234#4:
2173 * A cache MUST write through requests with methods
2174 * that are unsafe (Section 4.2.1 of [RFC7231]) to
2175 * the origin server; i.e., a cache is not allowed
2176 * to generate a reply to such a request before
2177 * having forwarded the request and having received
2178 * a corresponding response.
2179 *
2180 * RFC7231#4.2.1:
2181 * Of the request methods defined by this
2182 * specification, the GET, HEAD, OPTIONS, and TRACE
2183 * methods are defined to be safe.
2184 */
2185 if (likely(txn->meth == HTTP_METH_GET ||
2186 txn->meth == HTTP_METH_HEAD ||
2187 txn->meth == HTTP_METH_OPTIONS ||
2188 txn->meth == HTTP_METH_TRACE))
2189 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2190
Willy Tarreaud787e662009-07-07 10:14:51 +02002191 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002192 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002193 req->analyse_exp = TICK_ETERNITY;
2194 return 1;
2195
2196 return_bad_req:
2197 /* We centralize bad requests processing here */
2198 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2199 /* we detected a parsing error. We want to archive this request
2200 * in the dedicated proxy area for later troubleshooting.
2201 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002202 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002203 }
2204
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002205 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002206 txn->req.msg_state = HTTP_MSG_ERROR;
2207 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002208 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002209
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002210 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002211 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002212 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002213
2214 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002215 if (!(s->flags & SF_ERR_MASK))
2216 s->flags |= SF_ERR_PRXCOND;
2217 if (!(s->flags & SF_FINST_MASK))
2218 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002219
Christopher Faulet0184ea72017-01-05 14:06:34 +01002220 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002221 req->analyse_exp = TICK_ETERNITY;
2222 return 0;
2223}
2224
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002225
Willy Tarreau347a35d2013-11-22 17:51:09 +01002226/* This function prepares an applet to handle the stats. It can deal with the
2227 * "100-continue" expectation, check that admin rules are met for POST requests,
2228 * and program a response message if something was unexpected. It cannot fail
2229 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002230 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002231 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002232 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002233 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002234int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002235{
2236 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002237 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002238 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002239 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002240 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002241 struct uri_auth *uri_auth = s->be->uri_auth;
2242 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002243 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002244
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002245 appctx = si_appctx(si);
2246 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2247 appctx->st1 = appctx->st2 = 0;
2248 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2249 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002250 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002251 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002252
Willy Tarreauf37954d2018-06-15 18:31:02 +02002253 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002254 lookup = uri + uri_auth->uri_len;
2255
2256 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2257 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002258 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002259 break;
2260 }
2261 }
2262
2263 if (uri_auth->refresh) {
2264 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2265 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002266 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002267 break;
2268 }
2269 }
2270 }
2271
2272 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2273 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002274 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002275 break;
2276 }
2277 }
2278
Willy Tarreau1e62df92016-01-11 18:57:53 +01002279 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2280 if (memcmp(h, ";typed", 6) == 0) {
2281 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2282 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2283 break;
2284 }
2285 }
2286
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002287 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2288 if (memcmp(h, ";st=", 4) == 0) {
2289 int i;
2290 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002291 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002292 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2293 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002294 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002295 break;
2296 }
2297 }
2298 break;
2299 }
2300 }
2301
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002302 appctx->ctx.stats.scope_str = 0;
2303 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002304 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2305 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2306 int itx = 0;
2307 const char *h2;
2308 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2309 const char *err;
2310
2311 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2312 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002313 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002314 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2315 itx++;
2316 h++;
2317 }
2318
2319 if (itx > STAT_SCOPE_TXT_MAXLEN)
2320 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002321 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002322
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002323 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002324 memcpy(scope_txt, h2, itx);
2325 scope_txt[itx] = '\0';
2326 err = invalid_char(scope_txt);
2327 if (err) {
2328 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002329 appctx->ctx.stats.scope_str = 0;
2330 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002331 }
2332 break;
2333 }
2334 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002335
2336 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002337 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002338 int ret = 1;
2339
2340 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002341 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 +01002342 ret = acl_pass(ret);
2343 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2344 ret = !ret;
2345 }
2346
2347 if (ret) {
2348 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002349 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002350 break;
2351 }
2352 }
2353
2354 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002355 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002356 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002357 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002358 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2359 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002360 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002361 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002362 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002363 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2364 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002365 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002366 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002367 else {
2368 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002369 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002370 }
2371
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002372 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002373 return 1;
2374}
2375
Lukas Tribus67db8df2013-06-23 17:37:13 +02002376/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2377 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2378 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002379void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002380{
2381#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002382 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002383 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2384#endif
2385#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002386 if (from->ss_family == AF_INET6) {
2387 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002388 /* v4-mapped addresses need IP_TOS */
2389 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2390 else
2391 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2392 }
2393#endif
2394}
2395
Willy Tarreau87b09662015-04-03 00:22:06 +02002396int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002397 const char* name, unsigned int name_len,
2398 const char *str, struct my_regex *re,
2399 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002400{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002401 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002402 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02002403 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002404 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2405 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02002406 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002407
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002408 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002409
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002410 /* Choose the header browsing function. */
2411 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002412 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002413 http_find_hdr_func = http_find_header2;
2414 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002415 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002416 http_find_hdr_func = http_find_full_header2;
2417 break;
2418 default: /* impossible */
2419 return -1;
2420 }
2421
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002422 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2423 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02002424 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002425 char *val = ctx.line + ctx.val;
2426 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002427
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002428 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2429 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002430
Willy Tarreau6e27be12018-08-22 04:46:47 +02002431 len = exp_replace(output->area, output->size, val, str, pmatch);
2432 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002433 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002434
Willy Tarreau6e27be12018-08-22 04:46:47 +02002435 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002436
2437 hdr->len += delta;
2438 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002439
2440 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002441 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002442 }
2443
2444 return 0;
2445}
2446
Willy Tarreau87b09662015-04-03 00:22:06 +02002447static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002448 const char* name, unsigned int name_len,
2449 struct list *fmt, struct my_regex *re,
2450 int action)
2451{
Willy Tarreau83061a82018-07-13 11:56:34 +02002452 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002453 int ret = -1;
2454
2455 replace = alloc_trash_chunk();
2456 if (!replace)
2457 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002458
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002459 replace->data = build_logline(s, replace->area, replace->size, fmt);
2460 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002461 goto leave;
2462
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002463 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
2464 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002465
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002466 leave:
2467 free_trash_chunk(replace);
2468 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002469}
2470
Willy Tarreau87b09662015-04-03 00:22:06 +02002471/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002472 * transaction <txn>. Returns the verdict of the first rule that prevents
2473 * further processing of the request (auth, deny, ...), and defaults to
2474 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2475 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002476 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2477 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2478 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002479 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002480enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002481http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002482{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002483 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002484 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002485 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002486 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002487 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002488 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002489 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002490 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002491
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002492 /* If "the current_rule_list" match the executed rule list, we are in
2493 * resume condition. If a resume is needed it is always in the action
2494 * and never in the ACL or converters. In this case, we initialise the
2495 * current rule, and go to the action execution point.
2496 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002497 if (s->current_rule) {
2498 rule = s->current_rule;
2499 s->current_rule = NULL;
2500 if (s->current_rule_list == rules)
2501 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002502 }
2503 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002504
Willy Tarreauff011f22011-01-06 17:51:27 +01002505 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002506
Willy Tarreau96257ec2012-12-27 10:46:37 +01002507 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002508 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002509 int ret;
2510
Willy Tarreau192252e2015-04-04 01:47:55 +02002511 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002512 ret = acl_pass(ret);
2513
Willy Tarreauff011f22011-01-06 17:51:27 +01002514 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002515 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002516
2517 if (!ret) /* condition not matched */
2518 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002519 }
2520
Willy Tarreauacc98002015-09-27 23:34:39 +02002521 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002522resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002523 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002524 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002525 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002526
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002527 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002528 if (deny_status)
2529 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002530 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002531
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002532 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002533 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002534 if (deny_status)
2535 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002536 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002537
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002538 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002539 /* Auth might be performed on regular http-req rules as well as on stats */
2540 auth_realm = rule->arg.auth.realm;
2541 if (!auth_realm) {
2542 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2543 auth_realm = STATS_DEFAULT_REALM;
2544 else
2545 auth_realm = px->id;
2546 }
2547 /* send 401/407 depending on whether we use a proxy or not. We still
2548 * count one error, because normal browsing won't significantly
2549 * increase the counter but brute force attempts will.
2550 */
2551 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2552 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002553 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002554 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002555 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002556
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002557 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002558 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2559 return HTTP_RULE_RES_BADREQ;
2560 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002561
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002562 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002563 s->task->nice = rule->arg.nice;
2564 break;
2565
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002566 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002567 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002568 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002569 break;
2570
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002571 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002572#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002573 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002574 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002575#endif
2576 break;
2577
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002578 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002579 s->logs.level = rule->arg.loglevel;
2580 break;
2581
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002582 case ACT_HTTP_REPLACE_HDR:
2583 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002584 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2585 rule->arg.hdr_add.name_len,
2586 &rule->arg.hdr_add.fmt,
2587 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002588 return HTTP_RULE_RES_BADREQ;
2589 break;
2590
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002591 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002592 ctx.idx = 0;
2593 /* remove all occurrences of the header */
2594 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002595 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002596 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002597 }
Willy Tarreau85603282015-01-21 20:39:27 +01002598 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002599
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002600 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002601 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002602 /* The scope of the trash buffer must be limited to this function. The
2603 * build_logline() function can execute a lot of other function which
2604 * can use the trash buffer. So for limiting the scope of this global
2605 * buffer, we build first the header value using build_logline, and
2606 * after we store the header name.
2607 */
Willy Tarreau83061a82018-07-13 11:56:34 +02002608 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002609
2610 replace = alloc_trash_chunk();
2611 if (!replace)
2612 return HTTP_RULE_RES_BADREQ;
2613
Thierry Fournier4b788f72016-06-01 13:35:36 +02002614 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002615 len += build_logline(s, replace->area + len,
2616 replace->size - len,
2617 &rule->arg.hdr_add.fmt);
2618 memcpy(replace->area, rule->arg.hdr_add.name,
2619 rule->arg.hdr_add.name_len);
2620 replace->area[rule->arg.hdr_add.name_len] = ':';
2621 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
2622 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002623
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002624 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002625 /* remove all occurrences of the header */
2626 ctx.idx = 0;
2627 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002628 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002629 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2630 }
2631 }
2632
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002633 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002634 static unsigned char rate_limit = 0;
2635
2636 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002637 replace->area[rule->arg.hdr_add.name_len] = 0;
2638 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,
2639 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002640 }
2641
2642 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2643 if (sess->fe != s->be)
2644 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2645 if (sess->listener->counters)
2646 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2647 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002648
2649 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002650 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002651 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002652
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002653 case ACT_HTTP_DEL_ACL:
2654 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002655 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002656 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002657
2658 /* collect reference */
2659 ref = pat_ref_lookup(rule->arg.map.ref);
2660 if (!ref)
2661 continue;
2662
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002663 /* allocate key */
2664 key = alloc_trash_chunk();
2665 if (!key)
2666 return HTTP_RULE_RES_BADREQ;
2667
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002668 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002669 key->data = build_logline(s, key->area, key->size,
2670 &rule->arg.map.key);
2671 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002672
2673 /* perform update */
2674 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002675 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002676 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002677 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002678
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002679 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002680 break;
2681 }
2682
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002683 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002684 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002685 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002686
2687 /* collect reference */
2688 ref = pat_ref_lookup(rule->arg.map.ref);
2689 if (!ref)
2690 continue;
2691
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002692 /* allocate key */
2693 key = alloc_trash_chunk();
2694 if (!key)
2695 return HTTP_RULE_RES_BADREQ;
2696
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002697 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002698 key->data = build_logline(s, key->area, key->size,
2699 &rule->arg.map.key);
2700 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002701
2702 /* perform update */
2703 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002704 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002705 if (pat_ref_find_elt(ref, key->area) == NULL)
2706 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002707 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002708
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002709 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002710 break;
2711 }
2712
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002713 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002714 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002715 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002716
2717 /* collect reference */
2718 ref = pat_ref_lookup(rule->arg.map.ref);
2719 if (!ref)
2720 continue;
2721
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002722 /* allocate key */
2723 key = alloc_trash_chunk();
2724 if (!key)
2725 return HTTP_RULE_RES_BADREQ;
2726
2727 /* allocate value */
2728 value = alloc_trash_chunk();
2729 if (!value) {
2730 free_trash_chunk(key);
2731 return HTTP_RULE_RES_BADREQ;
2732 }
2733
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002734 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002735 key->data = build_logline(s, key->area, key->size,
2736 &rule->arg.map.key);
2737 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002738
2739 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002740 value->data = build_logline(s, value->area,
2741 value->size,
2742 &rule->arg.map.value);
2743 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002744
2745 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002746 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002747 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002748 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002749 else
2750 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002751 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002752
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002753 free_trash_chunk(key);
2754 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002755 break;
2756 }
William Lallemand73025dd2014-04-24 14:38:37 +02002757
Thierry FOURNIER42148732015-09-02 17:17:33 +02002758 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002759 if ((s->req.flags & CF_READ_ERROR) ||
2760 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2761 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2762 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002763 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002764
Willy Tarreauacc98002015-09-27 23:34:39 +02002765 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002766 case ACT_RET_ERR:
2767 case ACT_RET_CONT:
2768 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002769 case ACT_RET_STOP:
2770 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002771 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002772 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002773 return HTTP_RULE_RES_YIELD;
2774 }
William Lallemand73025dd2014-04-24 14:38:37 +02002775 break;
2776
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002777 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002778 /* Note: only the first valid tracking parameter of each
2779 * applies.
2780 */
2781
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002782 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002783 struct stktable *t;
2784 struct stksess *ts;
2785 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002786 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002787
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002788 t = rule->arg.trk_ctr.table.t;
2789 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 +02002790
2791 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002792 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002793
2794 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002795 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2796 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2797 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002798 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002799
Emeric Brun819fc6f2017-06-13 19:37:32 +02002800 if (ptr1)
2801 stktable_data_cast(ptr1, http_req_cnt)++;
2802
2803 if (ptr2)
2804 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2805 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2806
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002807 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002808
2809 /* If data was modified, we need to touch to re-schedule sync */
2810 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002811 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002812
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002813 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002814 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002815 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002816 }
2817 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002818 break;
2819
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002820 /* other flags exists, but normaly, they never be matched. */
2821 default:
2822 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002823 }
2824 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002825
2826 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002827 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002828}
2829
Willy Tarreau71241ab2012-12-27 11:30:54 +01002830
Willy Tarreau51d861a2015-05-22 17:30:48 +02002831/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2832 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2833 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2834 * is returned, the process can continue the evaluation of next rule list. If
2835 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2836 * is returned, it means the operation could not be processed and a server error
2837 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2838 * deny rule. If *YIELD is returned, the caller must call again the function
2839 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002840 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002841static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002842http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002843{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002844 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002845 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002846 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002847 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002848 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002849 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002850
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002851 /* If "the current_rule_list" match the executed rule list, we are in
2852 * resume condition. If a resume is needed it is always in the action
2853 * and never in the ACL or converters. In this case, we initialise the
2854 * current rule, and go to the action execution point.
2855 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002856 if (s->current_rule) {
2857 rule = s->current_rule;
2858 s->current_rule = NULL;
2859 if (s->current_rule_list == rules)
2860 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002861 }
2862 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002863
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002864 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002865
2866 /* check optional condition */
2867 if (rule->cond) {
2868 int ret;
2869
Willy Tarreau192252e2015-04-04 01:47:55 +02002870 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002871 ret = acl_pass(ret);
2872
2873 if (rule->cond->pol == ACL_COND_UNLESS)
2874 ret = !ret;
2875
2876 if (!ret) /* condition not matched */
2877 continue;
2878 }
2879
Willy Tarreauacc98002015-09-27 23:34:39 +02002880 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002881resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002882 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002883 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002884 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002885
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002886 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002887 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002888 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002889
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002890 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002891 s->task->nice = rule->arg.nice;
2892 break;
2893
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002894 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002895 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002896 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002897 break;
2898
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002899 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002900#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002901 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002902 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002903#endif
2904 break;
2905
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002906 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002907 s->logs.level = rule->arg.loglevel;
2908 break;
2909
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002910 case ACT_HTTP_REPLACE_HDR:
2911 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002912 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2913 rule->arg.hdr_add.name_len,
2914 &rule->arg.hdr_add.fmt,
2915 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002916 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002917 break;
2918
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002919 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002920 ctx.idx = 0;
2921 /* remove all occurrences of the header */
2922 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002923 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002924 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2925 }
Willy Tarreau85603282015-01-21 20:39:27 +01002926 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002927
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002928 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002929 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002930 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002931
2932 replace = alloc_trash_chunk();
2933 if (!replace)
2934 return HTTP_RULE_RES_BADREQ;
2935
2936 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002937 memcpy(replace->area, rule->arg.hdr_add.name,
2938 rule->arg.hdr_add.name_len);
2939 replace->data = rule->arg.hdr_add.name_len;
2940 replace->area[replace->data++] = ':';
2941 replace->area[replace->data++] = ' ';
2942 replace->data += build_logline(s,
2943 replace->area + replace->data,
2944 replace->size - replace->data,
2945 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002946
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002947 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002948 /* remove all occurrences of the header */
2949 ctx.idx = 0;
2950 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002951 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002952 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2953 }
2954 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002955
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002956 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002957 static unsigned char rate_limit = 0;
2958
2959 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002960 replace->area[rule->arg.hdr_add.name_len] = 0;
2961 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,
2962 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002963 }
2964
2965 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2966 if (sess->fe != s->be)
2967 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2968 if (sess->listener->counters)
2969 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2970 if (objt_server(s->target))
2971 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2972 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002973
2974 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002975 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002976 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002977
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002978 case ACT_HTTP_DEL_ACL:
2979 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002980 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002981 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002982
2983 /* collect reference */
2984 ref = pat_ref_lookup(rule->arg.map.ref);
2985 if (!ref)
2986 continue;
2987
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002988 /* allocate key */
2989 key = alloc_trash_chunk();
2990 if (!key)
2991 return HTTP_RULE_RES_BADREQ;
2992
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002993 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002994 key->data = build_logline(s, key->area, key->size,
2995 &rule->arg.map.key);
2996 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002997
2998 /* perform update */
2999 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003000 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003001 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003002 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003003
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003004 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003005 break;
3006 }
3007
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003008 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003009 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02003010 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003011
3012 /* collect reference */
3013 ref = pat_ref_lookup(rule->arg.map.ref);
3014 if (!ref)
3015 continue;
3016
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003017 /* allocate key */
3018 key = alloc_trash_chunk();
3019 if (!key)
3020 return HTTP_RULE_RES_BADREQ;
3021
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003022 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003023 key->data = build_logline(s, key->area, key->size,
3024 &rule->arg.map.key);
3025 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003026
3027 /* perform update */
3028 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003029 if (pat_ref_find_elt(ref, key->area) == NULL)
3030 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003031
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003032 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003033 break;
3034 }
3035
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003036 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003037 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02003038 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003039
3040 /* collect reference */
3041 ref = pat_ref_lookup(rule->arg.map.ref);
3042 if (!ref)
3043 continue;
3044
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003045 /* allocate key */
3046 key = alloc_trash_chunk();
3047 if (!key)
3048 return HTTP_RULE_RES_BADREQ;
3049
3050 /* allocate value */
3051 value = alloc_trash_chunk();
3052 if (!value) {
3053 free_trash_chunk(key);
3054 return HTTP_RULE_RES_BADREQ;
3055 }
3056
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003057 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003058 key->data = build_logline(s, key->area, key->size,
3059 &rule->arg.map.key);
3060 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003061
3062 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003063 value->data = build_logline(s, value->area,
3064 value->size,
3065 &rule->arg.map.value);
3066 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003067
3068 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003069 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003070 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003071 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003072 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003073 else
3074 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003075 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003076 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003077 free_trash_chunk(key);
3078 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003079 break;
3080 }
William Lallemand73025dd2014-04-24 14:38:37 +02003081
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003082 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003083 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3084 return HTTP_RULE_RES_BADREQ;
3085 return HTTP_RULE_RES_DONE;
3086
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003087 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3088 /* Note: only the first valid tracking parameter of each
3089 * applies.
3090 */
3091
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003092 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003093 struct stktable *t;
3094 struct stksess *ts;
3095 struct stktable_key *key;
3096 void *ptr;
3097
3098 t = rule->arg.trk_ctr.table.t;
3099 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3100
3101 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003102 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003103
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003104 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003105
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003106 /* let's count a new HTTP request as it's the first time we do it */
3107 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3108 if (ptr)
3109 stktable_data_cast(ptr, http_req_cnt)++;
3110
3111 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3112 if (ptr)
3113 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3114 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3115
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003116 /* When the client triggers a 4xx from the server, it's most often due
3117 * to a missing object or permission. These events should be tracked
3118 * because if they happen often, it may indicate a brute force or a
3119 * vulnerability scan. Normally this is done when receiving the response
3120 * but here we're tracking after this ought to have been done so we have
3121 * to do it on purpose.
3122 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003123 if ((unsigned)(txn->status - 400) < 100) {
3124 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3125 if (ptr)
3126 stktable_data_cast(ptr, http_err_cnt)++;
3127
3128 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3129 if (ptr)
3130 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3131 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3132 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003133
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003134 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003135
Emeric Brun0fed0b02017-11-29 16:15:07 +01003136 /* If data was modified, we need to touch to re-schedule sync */
3137 stktable_touch_local(t, ts, 0);
3138
Emeric Brun819fc6f2017-06-13 19:37:32 +02003139 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3140 if (sess->fe != s->be)
3141 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3142
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003143 }
3144 }
3145 break;
3146
Thierry FOURNIER42148732015-09-02 17:17:33 +02003147 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003148 if ((s->req.flags & CF_READ_ERROR) ||
3149 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3150 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3151 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02003152 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003153
Willy Tarreauacc98002015-09-27 23:34:39 +02003154 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003155 case ACT_RET_ERR:
3156 case ACT_RET_CONT:
3157 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003158 case ACT_RET_STOP:
3159 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003160 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003161 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003162 return HTTP_RULE_RES_YIELD;
3163 }
William Lallemand73025dd2014-04-24 14:38:37 +02003164 break;
3165
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003166 /* other flags exists, but normaly, they never be matched. */
3167 default:
3168 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003169 }
3170 }
3171
3172 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003173 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003174}
3175
3176
Willy Tarreau71241ab2012-12-27 11:30:54 +01003177/* Perform an HTTP redirect based on the information in <rule>. The function
3178 * returns non-zero on success, or zero in case of a, irrecoverable error such
3179 * as too large a request to build a valid response.
3180 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003181static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003182{
Willy Tarreaub329a312015-05-22 16:27:37 +02003183 struct http_msg *req = &txn->req;
3184 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003185 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02003186 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003187 int ret = 0;
3188
3189 chunk = alloc_trash_chunk();
3190 if (!chunk)
3191 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003192
3193 /* build redirect message */
3194 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003195 case 308:
3196 msg_fmt = HTTP_308;
3197 break;
3198 case 307:
3199 msg_fmt = HTTP_307;
3200 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003201 case 303:
3202 msg_fmt = HTTP_303;
3203 break;
3204 case 301:
3205 msg_fmt = HTTP_301;
3206 break;
3207 case 302:
3208 default:
3209 msg_fmt = HTTP_302;
3210 break;
3211 }
3212
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003213 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3214 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003215
3216 switch(rule->type) {
3217 case REDIRECT_TYPE_SCHEME: {
3218 const char *path;
3219 const char *host;
3220 struct hdr_ctx ctx;
3221 int pathlen;
3222 int hostlen;
3223
3224 host = "";
3225 hostlen = 0;
3226 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02003227 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003228 host = ctx.line + ctx.val;
3229 hostlen = ctx.vlen;
3230 }
3231
3232 path = http_get_path(txn);
3233 /* build message using path */
3234 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003235 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003236 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3237 int qs = 0;
3238 while (qs < pathlen) {
3239 if (path[qs] == '?') {
3240 pathlen = qs;
3241 break;
3242 }
3243 qs++;
3244 }
3245 }
3246 } else {
3247 path = "/";
3248 pathlen = 1;
3249 }
3250
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003251 if (rule->rdr_str) { /* this is an old "redirect" rule */
3252 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003253 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003254 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003255
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003256 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003257 memcpy(chunk->area + chunk->data, rule->rdr_str,
3258 rule->rdr_len);
3259 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003260 }
3261 else {
3262 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003263 chunk->data += build_logline(s,
3264 chunk->area + chunk->data,
3265 chunk->size - chunk->data,
3266 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003267
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003268 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003269 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003270 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003271 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003272 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003273 memcpy(chunk->area + chunk->data, "://", 3);
3274 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003275
3276 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003277 memcpy(chunk->area + chunk->data, host, hostlen);
3278 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003279
3280 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003281 memcpy(chunk->area + chunk->data, path, pathlen);
3282 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003283
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003284 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003285 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003286 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003287 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003288 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003289 chunk->area[chunk->data] = '/';
3290 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003291 }
3292
3293 break;
3294 }
3295 case REDIRECT_TYPE_PREFIX: {
3296 const char *path;
3297 int pathlen;
3298
3299 path = http_get_path(txn);
3300 /* build message using path */
3301 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003302 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003303 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3304 int qs = 0;
3305 while (qs < pathlen) {
3306 if (path[qs] == '?') {
3307 pathlen = qs;
3308 break;
3309 }
3310 qs++;
3311 }
3312 }
3313 } else {
3314 path = "/";
3315 pathlen = 1;
3316 }
3317
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003318 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003319 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003320 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003321
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003322 /* add prefix. Note that if prefix == "/", we don't want to
3323 * add anything, otherwise it makes it hard for the user to
3324 * configure a self-redirection.
3325 */
3326 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003327 memcpy(chunk->area + chunk->data,
3328 rule->rdr_str, rule->rdr_len);
3329 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003330 }
3331 }
3332 else {
3333 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003334 chunk->data += build_logline(s,
3335 chunk->area + chunk->data,
3336 chunk->size - chunk->data,
3337 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003338
3339 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003340 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003341 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003342 }
3343
3344 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003345 memcpy(chunk->area + chunk->data, path, pathlen);
3346 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003347
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003348 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003349 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003350 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003351 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003352 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003353 chunk->area[chunk->data] = '/';
3354 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003355 }
3356
3357 break;
3358 }
3359 case REDIRECT_TYPE_LOCATION:
3360 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003361 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003362 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003363 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003364
3365 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003366 memcpy(chunk->area + chunk->data, rule->rdr_str,
3367 rule->rdr_len);
3368 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003369 }
3370 else {
3371 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003372 chunk->data += build_logline(s,
3373 chunk->area + chunk->data,
3374 chunk->size - chunk->data,
3375 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003376
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003377 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003378 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003379 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003380 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003381 break;
3382 }
3383
3384 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003385 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
3386 chunk->data += 14;
3387 memcpy(chunk->area + chunk->data, rule->cookie_str,
3388 rule->cookie_len);
3389 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003390 }
3391
Willy Tarreau19b14122017-02-28 09:48:11 +01003392 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003393 txn->status = rule->code;
3394 /* let's log the request time */
3395 s->logs.tv_request = now;
3396
Christopher Fauletbe821b92017-03-30 11:21:53 +02003397 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003398 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3399 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3400 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003401 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003402 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003403 memcpy(chunk->area + chunk->data,
3404 "\r\nProxy-Connection: keep-alive", 30);
3405 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003406 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003407 memcpy(chunk->area + chunk->data,
3408 "\r\nConnection: keep-alive", 24);
3409 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003410 }
3411 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003412 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
3413 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003414 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003415 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003416 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003417 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02003418 req->next -= req->sov;
3419 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003420 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003421 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003422 req->msg_state = HTTP_MSG_CLOSED;
3423 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003424 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003425 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02003426 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003427 /* let the server side turn to SI_ST_CLO */
3428 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003429 } else {
3430 /* keep-alive not possible */
3431 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003432 memcpy(chunk->area + chunk->data,
3433 "\r\nProxy-Connection: close\r\n\r\n", 29);
3434 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003435 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003436 memcpy(chunk->area + chunk->data,
3437 "\r\nConnection: close\r\n\r\n", 23);
3438 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003439 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003440 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003441 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003442 }
3443
Willy Tarreaue7dff022015-04-03 01:14:29 +02003444 if (!(s->flags & SF_ERR_MASK))
3445 s->flags |= SF_ERR_LOCAL;
3446 if (!(s->flags & SF_FINST_MASK))
3447 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003448
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003449 ret = 1;
3450 leave:
3451 free_trash_chunk(chunk);
3452 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003453}
3454
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003455/* This stream analyser runs all HTTP request processing which is common to
3456 * frontends and backends, which means blocking ACLs, filters, connection-close,
3457 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003458 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003459 * either needs more data or wants to immediately abort the request (eg: deny,
3460 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003461 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003462int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003463{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003464 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003465 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003466 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003467 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003468 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003469 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003470 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003471 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003472
Willy Tarreau655dce92009-11-08 13:10:58 +01003473 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003474 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003475 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003476 }
3477
Christopher Faulet45073512018-07-20 10:16:29 +02003478 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003479 now_ms, __FUNCTION__,
3480 s,
3481 req,
3482 req->rex, req->wex,
3483 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003484 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02003485 req->analysers);
3486
Willy Tarreau65410832014-04-28 21:25:43 +02003487 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003488 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003489
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003490 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003491 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003492 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003493
Willy Tarreau0b748332014-04-29 00:13:29 +02003494 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003495 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3496 goto return_prx_yield;
3497
Willy Tarreau0b748332014-04-29 00:13:29 +02003498 case HTTP_RULE_RES_CONT:
3499 case HTTP_RULE_RES_STOP: /* nothing to do */
3500 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003501
Willy Tarreau0b748332014-04-29 00:13:29 +02003502 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3503 if (txn->flags & TX_CLTARPIT)
3504 goto tarpit;
3505 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003506
Willy Tarreau0b748332014-04-29 00:13:29 +02003507 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3508 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003509
Willy Tarreau0b748332014-04-29 00:13:29 +02003510 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003511 goto done;
3512
Willy Tarreau0b748332014-04-29 00:13:29 +02003513 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3514 goto return_bad_req;
3515 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003516 }
3517
Olivier Houchard25ae45a2017-11-29 19:51:19 +01003518 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
3519 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003520 struct hdr_ctx ctx;
3521
3522 ctx.idx = 0;
3523 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02003524 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003525 if (unlikely(http_header_add_tail2(&txn->req,
3526 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02003527 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003528 goto return_bad_req;
3529 }
3530 }
3531
3532 }
3533
Willy Tarreau52542592014-04-28 18:33:26 +02003534 /* OK at this stage, we know that the request was accepted according to
3535 * the http-request rules, we can check for the stats. Note that the
3536 * URI is detected *before* the req* rules in order not to be affected
3537 * by a possible reqrep, while they are processed *after* so that a
3538 * reqdeny can still block them. This clearly needs to change in 1.6!
3539 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003540 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003541 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003542 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003543 txn->status = 500;
3544 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003545 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003546
Willy Tarreaue7dff022015-04-03 01:14:29 +02003547 if (!(s->flags & SF_ERR_MASK))
3548 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003549 goto return_prx_cond;
3550 }
3551
3552 /* parse the whole stats request and extract the relevant information */
3553 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003554 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003555 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003556
Willy Tarreau0b748332014-04-29 00:13:29 +02003557 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3558 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003559
Willy Tarreau0b748332014-04-29 00:13:29 +02003560 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3561 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003562 }
3563
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003564 /* evaluate the req* rules except reqadd */
3565 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003566 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003567 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003568
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003569 if (txn->flags & TX_CLDENY)
3570 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003571
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003572 if (txn->flags & TX_CLTARPIT) {
3573 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003574 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003575 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003576 }
Willy Tarreau06619262006-12-17 08:37:22 +01003577
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003578 /* add request headers from the rule sets in the same order */
3579 list_for_each_entry(wl, &px->req_add, list) {
3580 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003581 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003582 ret = acl_pass(ret);
3583 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3584 ret = !ret;
3585 if (!ret)
3586 continue;
3587 }
3588
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003589 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003590 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003591 }
3592
Willy Tarreau52542592014-04-28 18:33:26 +02003593
3594 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003595 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3596 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003597 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003598 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003599 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003600
Willy Tarreaue7dff022015-04-03 01:14:29 +02003601 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3602 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3603 if (!(s->flags & SF_FINST_MASK))
3604 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003605
Willy Tarreau70730dd2014-04-24 18:06:27 +02003606 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003607 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3608 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003609 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003610 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003611 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003612
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003613 /* check whether we have some ACLs set to redirect this request */
3614 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003615 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003616 int ret;
3617
Willy Tarreau192252e2015-04-04 01:47:55 +02003618 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003619 ret = acl_pass(ret);
3620 if (rule->cond->pol == ACL_COND_UNLESS)
3621 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003622 if (!ret)
3623 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003624 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003625 if (!http_apply_redirect_rule(rule, s, txn))
3626 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003627 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003628 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003629
Willy Tarreau2be39392010-01-03 17:24:51 +01003630 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3631 * If this happens, then the data will not come immediately, so we must
3632 * send all what we have without waiting. Note that due to the small gain
3633 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003634 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003635 * itself once used.
3636 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003637 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003638
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003639 done: /* done with this analyser, continue with next ones that the calling
3640 * points will have set, if any.
3641 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003642 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003643 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3644 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003645 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003646
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003647 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003648 /* Allow cookie logging
3649 */
3650 if (s->be->cookie_name || sess->fe->capture_name)
3651 manage_client_side_cookies(s, req);
3652
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003653 /* When a connection is tarpitted, we use the tarpit timeout,
3654 * which may be the same as the connect timeout if unspecified.
3655 * If unset, then set it to zero because we really want it to
3656 * eventually expire. We build the tarpit as an analyser.
3657 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003658 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003659
3660 /* wipe the request out so that we can drop the connection early
3661 * if the client closes first.
3662 */
3663 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003664
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003665 txn->status = http_err_codes[deny_status];
3666
Christopher Faulet0184ea72017-01-05 14:06:34 +01003667 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003668 req->analysers |= AN_REQ_HTTP_TARPIT;
3669 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3670 if (!req->analyse_exp)
3671 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003672 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003673 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003674 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003675 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003676 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003677 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003678 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003679
3680 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003681
3682 /* Allow cookie logging
3683 */
3684 if (s->be->cookie_name || sess->fe->capture_name)
3685 manage_client_side_cookies(s, req);
3686
Willy Tarreau0b748332014-04-29 00:13:29 +02003687 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003688 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003689 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003690 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003691 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003692 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003693 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003694 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003695 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003696 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003697 goto return_prx_cond;
3698
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003699 return_bad_req:
3700 /* We centralize bad requests processing here */
3701 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3702 /* we detected a parsing error. We want to archive this request
3703 * in the dedicated proxy area for later troubleshooting.
3704 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003705 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003706 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003707
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003708 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003709 txn->req.msg_state = HTTP_MSG_ERROR;
3710 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003711 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003712
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003713 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003714 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003715 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003716
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003717 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003718 if (!(s->flags & SF_ERR_MASK))
3719 s->flags |= SF_ERR_PRXCOND;
3720 if (!(s->flags & SF_FINST_MASK))
3721 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003722
Christopher Faulet0184ea72017-01-05 14:06:34 +01003723 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003724 req->analyse_exp = TICK_ETERNITY;
3725 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003726
3727 return_prx_yield:
3728 channel_dont_connect(req);
3729 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003730}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003731
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003732/* This function performs all the processing enabled for the current request.
3733 * It returns 1 if the processing can continue on next analysers, or zero if it
3734 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003735 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003736 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003737int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003738{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003739 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003740 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003741 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003742 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003743
Willy Tarreau655dce92009-11-08 13:10:58 +01003744 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003745 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003746 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003747 return 0;
3748 }
3749
Christopher Faulet45073512018-07-20 10:16:29 +02003750 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003751 now_ms, __FUNCTION__,
3752 s,
3753 req,
3754 req->rex, req->wex,
3755 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003756 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003757 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003758
Willy Tarreau59234e92008-11-30 23:51:27 +01003759 /*
3760 * Right now, we know that we have processed the entire headers
3761 * and that unwanted requests have been filtered out. We can do
3762 * whatever we want with the remaining request. Also, now we
3763 * may have separate values for ->fe, ->be.
3764 */
Willy Tarreau06619262006-12-17 08:37:22 +01003765
Willy Tarreau59234e92008-11-30 23:51:27 +01003766 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003767 * If HTTP PROXY is set we simply get remote server address parsing
3768 * incoming request. Note that this requires that a connection is
3769 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003770 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003771 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003772 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003773 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003774
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003775 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003776 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003777 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003778 txn->req.msg_state = HTTP_MSG_ERROR;
3779 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003780 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003781 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003782
Willy Tarreaue7dff022015-04-03 01:14:29 +02003783 if (!(s->flags & SF_ERR_MASK))
3784 s->flags |= SF_ERR_RESOURCE;
3785 if (!(s->flags & SF_FINST_MASK))
3786 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003787
3788 return 0;
3789 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003790
3791 path = http_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003792 if (url2sa(ci_head(req) + msg->sl.rq.u,
3793 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003794 &conn->addr.to, NULL) == -1)
3795 goto return_bad_req;
3796
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003797 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003798 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3799 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003800 * u_l characters by a single "/".
3801 */
3802 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003803 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003804 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3805 int delta;
3806
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003807 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003808 http_msg_move_end(&txn->req, delta);
3809 cur_end += delta;
3810 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3811 goto return_bad_req;
3812 }
3813 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003814 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003815 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3816 int delta;
3817
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003818 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003819 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003820 http_msg_move_end(&txn->req, delta);
3821 cur_end += delta;
3822 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3823 goto return_bad_req;
3824 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003825 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003826
Willy Tarreau59234e92008-11-30 23:51:27 +01003827 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003828 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003829 * Note that doing so might move headers in the request, but
3830 * the fields will stay coherent and the URI will not move.
3831 * This should only be performed in the backend.
3832 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003833 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003834 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003835
William Lallemanda73203e2012-03-12 12:48:57 +01003836 /* add unique-id if "header-unique-id" is specified */
3837
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003838 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003839 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003840 goto return_bad_req;
3841 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003842 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003843 }
William Lallemanda73203e2012-03-12 12:48:57 +01003844
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003845 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003846 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003847 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003848 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003849 goto return_bad_req;
3850 }
3851
Cyril Bontéb21570a2009-11-29 20:04:48 +01003852 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003853 * 9: add X-Forwarded-For if either the frontend or the backend
3854 * asks for it.
3855 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003856 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003857 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003858 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3859 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3860 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003861 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003862 /* The header is set to be added only if none is present
3863 * and we found it, so don't do anything.
3864 */
3865 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003866 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003867 /* Add an X-Forwarded-For header unless the source IP is
3868 * in the 'except' network range.
3869 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003870 if ((!sess->fe->except_mask.s_addr ||
3871 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3872 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003873 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003874 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003875 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003876 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003877 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003878 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003879
3880 /* Note: we rely on the backend to get the header name to be used for
3881 * x-forwarded-for, because the header is really meant for the backends.
3882 * However, if the backend did not specify any option, we have to rely
3883 * on the frontend's header name.
3884 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003885 if (s->be->fwdfor_hdr_len) {
3886 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003887 memcpy(trash.area,
3888 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003889 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003890 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003891 memcpy(trash.area,
3892 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003893 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003894 len += snprintf(trash.area + len,
3895 trash.size - len,
3896 ": %d.%d.%d.%d", pn[0], pn[1],
3897 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003898
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003899 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003900 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003901 }
3902 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003903 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003904 /* FIXME: for the sake of completeness, we should also support
3905 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003906 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003907 int len;
3908 char pn[INET6_ADDRSTRLEN];
3909 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003910 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003911 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003912
Willy Tarreau59234e92008-11-30 23:51:27 +01003913 /* Note: we rely on the backend to get the header name to be used for
3914 * x-forwarded-for, 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.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003917 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003918 if (s->be->fwdfor_hdr_len) {
3919 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003920 memcpy(trash.area, s->be->fwdfor_hdr_name,
3921 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003922 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003923 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003924 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3925 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003926 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003927 len += snprintf(trash.area + len, trash.size - len,
3928 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003929
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003930 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003931 goto return_bad_req;
3932 }
3933 }
3934
3935 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003936 * 10: add X-Original-To if either the frontend or the backend
3937 * asks for it.
3938 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003939 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003940
3941 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003942 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003943 /* Add an X-Original-To header unless the destination IP is
3944 * in the 'except' network range.
3945 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003946 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003947
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003948 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003949 ((!sess->fe->except_mask_to.s_addr ||
3950 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3951 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003952 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003953 (((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 +02003954 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003955 int len;
3956 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003957 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003958
3959 /* Note: we rely on the backend to get the header name to be used for
3960 * x-original-to, because the header is really meant for the backends.
3961 * However, if the backend did not specify any option, we have to rely
3962 * on the frontend's header name.
3963 */
3964 if (s->be->orgto_hdr_len) {
3965 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003966 memcpy(trash.area,
3967 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003968 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003969 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003970 memcpy(trash.area,
3971 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003972 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003973 len += snprintf(trash.area + len,
3974 trash.size - len,
3975 ": %d.%d.%d.%d", pn[0], pn[1],
3976 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003977
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003978 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003979 goto return_bad_req;
3980 }
3981 }
3982 }
3983
Willy Tarreau50fc7772012-11-11 22:19:57 +01003984 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3985 * If an "Upgrade" token is found, the header is left untouched in order not to have
3986 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3987 * "Upgrade" is present in the Connection header.
3988 */
3989 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3990 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003991 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003992 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003993 unsigned int want_flags = 0;
3994
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003995 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003996 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003997 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003998 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003999 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004000 want_flags |= TX_CON_CLO_SET;
4001 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004002 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004003 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004004 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004005 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004006 want_flags |= TX_CON_KAL_SET;
4007 }
4008
4009 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004010 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004011 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004012
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004013
Willy Tarreau522d6c02009-12-06 18:49:18 +01004014 /* If we have no server assigned yet and we're balancing on url_param
4015 * with a POST request, we may be interested in checking the body for
4016 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004017 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004018 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004019 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004020 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004021 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004022 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004023 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004024
Christopher Fauletbe821b92017-03-30 11:21:53 +02004025 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
4026 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004027#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02004028 /* We expect some data from the client. Unless we know for sure
4029 * we already have a full request, we have to re-enable quick-ack
4030 * in case we previously disabled it, otherwise we might cause
4031 * the client to delay further data.
4032 */
4033 if ((sess->listener->options & LI_O_NOQUICKACK) &&
4034 cli_conn && conn_ctrl_ready(cli_conn) &&
4035 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004036 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02004037 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004038#endif
Willy Tarreau03945942009-12-22 16:50:27 +01004039
Willy Tarreau59234e92008-11-30 23:51:27 +01004040 /*************************************************************
4041 * OK, that's finished for the headers. We have done what we *
4042 * could. Let's switch to the DATA state. *
4043 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004044 req->analyse_exp = TICK_ETERNITY;
4045 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004046
Willy Tarreau59234e92008-11-30 23:51:27 +01004047 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004048 /* OK let's go on with the BODY now */
4049 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004050
Willy Tarreau59234e92008-11-30 23:51:27 +01004051 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004052 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004053 /* we detected a parsing error. We want to archive this request
4054 * in the dedicated proxy area for later troubleshooting.
4055 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004056 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004057 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004058
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004059 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004060 txn->req.msg_state = HTTP_MSG_ERROR;
4061 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004062 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004063 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004064
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004065 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004066 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004067 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004068
Willy Tarreaue7dff022015-04-03 01:14:29 +02004069 if (!(s->flags & SF_ERR_MASK))
4070 s->flags |= SF_ERR_PRXCOND;
4071 if (!(s->flags & SF_FINST_MASK))
4072 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004073 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004074}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004075
Willy Tarreau60b85b02008-11-30 23:28:40 +01004076/* This function is an analyser which processes the HTTP tarpit. It always
4077 * returns zero, at the beginning because it prevents any other processing
4078 * from occurring, and at the end because it terminates the request.
4079 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004080int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004081{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004082 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004083
4084 /* This connection is being tarpitted. The CLIENT side has
4085 * already set the connect expiration date to the right
4086 * timeout. We just have to check that the client is still
4087 * there and that the timeout has not expired.
4088 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004089 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004090 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004091 !tick_is_expired(req->analyse_exp, now_ms))
4092 return 0;
4093
4094 /* We will set the queue timer to the time spent, just for
4095 * logging purposes. We fake a 500 server error, so that the
4096 * attacker will not suspect his connection has been tarpitted.
4097 * It will not cause trouble to the logs because we can exclude
4098 * the tarpitted connections by filtering on the 'PT' status flags.
4099 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004100 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4101
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004102 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004103 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004104
Christopher Faulet0184ea72017-01-05 14:06:34 +01004105 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004106 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004107
Willy Tarreaue7dff022015-04-03 01:14:29 +02004108 if (!(s->flags & SF_ERR_MASK))
4109 s->flags |= SF_ERR_PRXCOND;
4110 if (!(s->flags & SF_FINST_MASK))
4111 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004112 return 0;
4113}
4114
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004115/* This function is an analyser which waits for the HTTP request body. It waits
4116 * for either the buffer to be full, or the full advertised contents to have
4117 * reached the buffer. It must only be called after the standard HTTP request
4118 * processing has occurred, because it expects the request to be parsed and will
4119 * look for the Expect header. It may send a 100-Continue interim response. It
4120 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4121 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4122 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004123 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004124int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004125{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004126 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004127 struct http_txn *txn = s->txn;
4128 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004129
4130 /* We have to parse the HTTP request body to find any required data.
4131 * "balance url_param check_post" should have been the only way to get
4132 * into this. We were brought here after HTTP header analysis, so all
4133 * related structures are ready.
4134 */
4135
Willy Tarreau890988f2014-04-10 11:59:33 +02004136 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4137 /* This is the first call */
4138 if (msg->msg_state < HTTP_MSG_BODY)
4139 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004140
Willy Tarreau890988f2014-04-10 11:59:33 +02004141 if (msg->msg_state < HTTP_MSG_100_SENT) {
4142 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4143 * send an HTTP/1.1 100 Continue intermediate response.
4144 */
4145 if (msg->flags & HTTP_MSGF_VER_11) {
4146 struct hdr_ctx ctx;
4147 ctx.idx = 0;
4148 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004149 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02004150 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004151 co_inject(&s->res,
4152 http_100_chunk.area,
4153 http_100_chunk.data);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004154 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004155 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004156 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004157 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004158 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004159
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004160 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02004161 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02004162 * must save the body in msg->next because it survives buffer
4163 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004164 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004165 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004166
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004167 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004168 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4169 else
4170 msg->msg_state = HTTP_MSG_DATA;
4171 }
4172
Willy Tarreau890988f2014-04-10 11:59:33 +02004173 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4174 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004175 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004176 goto missing_data;
4177
4178 /* OK we have everything we need now */
4179 goto http_end;
4180 }
4181
4182 /* OK here we're parsing a chunked-encoded message */
4183
Willy Tarreau522d6c02009-12-06 18:49:18 +01004184 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004185 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004186 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004187 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004188 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004189 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004190 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004191
Willy Tarreau115acb92009-12-26 13:56:06 +01004192 if (!ret)
4193 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004194 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004195 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004196 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004197 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004198 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004199 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004200 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004201
4202 msg->chunk_len = chunk;
4203 msg->body_len += chunk;
4204
4205 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004206 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004207 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004208 }
4209
Willy Tarreaud98cf932009-12-27 22:54:55 +01004210 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004211 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4212 * for at least a whole chunk or the whole content length bytes after
4213 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004214 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004215 if (msg->msg_state == HTTP_MSG_TRAILERS)
4216 goto http_end;
4217
Willy Tarreaue115b492015-05-01 23:05:14 +02004218 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004219 goto http_end;
4220
4221 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004222 /* we get here if we need to wait for more data. If the buffer is full,
4223 * we have the maximum we can expect.
4224 */
Willy Tarreau23752332018-06-15 14:54:53 +02004225 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02004226 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004227
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004228 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004229 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004230 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004231
Willy Tarreaue7dff022015-04-03 01:14:29 +02004232 if (!(s->flags & SF_ERR_MASK))
4233 s->flags |= SF_ERR_CLITO;
4234 if (!(s->flags & SF_FINST_MASK))
4235 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004236 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004237 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004238
4239 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004240 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004241 /* Not enough data. We'll re-use the http-request
4242 * timeout here. Ideally, we should set the timeout
4243 * relative to the accept() date. We just set the
4244 * request timeout once at the beginning of the
4245 * request.
4246 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004247 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004248 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004249 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004250 return 0;
4251 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004252
4253 http_end:
4254 /* The situation will not evolve, so let's give up on the analysis. */
4255 s->logs.tv_request = now; /* update the request timer to reflect full request */
4256 req->analysers &= ~an_bit;
4257 req->analyse_exp = TICK_ETERNITY;
4258 return 1;
4259
4260 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004261 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004262 txn->req.msg_state = HTTP_MSG_ERROR;
4263 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004264 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004265
Willy Tarreaue7dff022015-04-03 01:14:29 +02004266 if (!(s->flags & SF_ERR_MASK))
4267 s->flags |= SF_ERR_PRXCOND;
4268 if (!(s->flags & SF_FINST_MASK))
4269 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004270
Willy Tarreau522d6c02009-12-06 18:49:18 +01004271 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004272 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004273 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004274 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004275 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004276 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004277}
4278
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004279/* send a server's name with an outgoing request over an established connection.
4280 * Note: this function is designed to be called once the request has been scheduled
4281 * for being forwarded. This is the reason why it rewinds the buffer before
4282 * proceeding.
4283 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004284int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004285
4286 struct hdr_ctx ctx;
4287
Mark Lamourinec2247f02012-01-04 13:02:01 -05004288 char *hdr_name = be->server_id_hdr_name;
4289 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004290 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004291 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004292 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004293
William Lallemandd9e90662012-01-30 17:27:17 +01004294 ctx.idx = 0;
4295
Willy Tarreau211cdec2014-04-17 20:18:08 +02004296 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004297 if (old_o) {
4298 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02004299 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004300 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004301 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004302 }
4303
Willy Tarreauf37954d2018-06-15 18:31:02 +02004304 old_i = ci_data(chn);
4305 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 -05004306 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004307 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004308 }
4309
4310 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004311 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004312 memcpy(hdr_val, hdr_name, hdr_name_len);
4313 hdr_val += hdr_name_len;
4314 *hdr_val++ = ':';
4315 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004316 hdr_val += strlcpy2(hdr_val, srv_name,
4317 trash.area + trash.size - hdr_val);
4318 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
4319 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004320
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004321 if (old_o) {
4322 /* If this was a forwarded request, we must readjust the amount of
4323 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004324 * variations. Note that the current state is >= HTTP_MSG_BODY,
4325 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004326 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004327 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02004328 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004329 txn->req.next -= old_o;
4330 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004331 }
4332
Mark Lamourinec2247f02012-01-04 13:02:01 -05004333 return 0;
4334}
4335
Willy Tarreau610ecce2010-01-04 21:15:02 +01004336/* Terminate current transaction and prepare a new one. This is very tricky
4337 * right now but it works.
4338 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004339void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004340{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004341 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004342 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004343 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004344 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004345 struct connection *srv_conn;
4346 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004347 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004348
Willy Tarreau610ecce2010-01-04 21:15:02 +01004349 /* FIXME: We need a more portable way of releasing a backend's and a
4350 * server's connections. We need a safer way to reinitialize buffer
4351 * flags. We also need a more accurate method for computing per-request
4352 * data.
4353 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004354 /*
4355 * XXX cognet: This is probably wrong, this is killing a whole
4356 * connection, in the new world order, we probably want to just kill
4357 * the stream, this is to be revisited the day we handle multiple
4358 * streams in one server connection.
4359 */
4360 cs = objt_cs(s->si[1].end);
4361 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004362
Willy Tarreau4213a112013-12-15 10:25:42 +01004363 /* unless we're doing keep-alive, we want to quickly close the connection
4364 * to the server.
4365 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004366 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004367 !si_conn_ready(&s->si[1])) {
4368 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4369 si_shutr(&s->si[1]);
4370 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004371 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004372
Willy Tarreaue7dff022015-04-03 01:14:29 +02004373 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004374 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004375 if (unlikely(s->srv_conn))
4376 sess_change_server(s, NULL);
4377 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004378
4379 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004380 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004381
Willy Tarreaueee5b512015-04-03 23:46:31 +02004382 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004383 int n;
4384
Willy Tarreaueee5b512015-04-03 23:46:31 +02004385 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004386 if (n < 1 || n > 5)
4387 n = 0;
4388
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004389 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004390 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004391 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004392 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004393 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004394 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4395 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004396 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004397 }
4398
4399 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004400 s->logs.bytes_in -= ci_data(&s->req);
4401 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004402
Willy Tarreau66425e32018-07-25 06:55:12 +02004403 /* we may need to know the position in the queue */
4404 pendconn_free(s);
4405
Willy Tarreau610ecce2010-01-04 21:15:02 +01004406 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004407 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004408 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004409 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410 s->do_log(s);
4411 }
4412
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004413 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004414 stream_stop_content_counters(s);
4415 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004416
Willy Tarreau610ecce2010-01-04 21:15:02 +01004417 s->logs.accept_date = date; /* user-visible date for logging */
4418 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004419 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4420 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004421 tv_zero(&s->logs.tv_request);
4422 s->logs.t_queue = -1;
4423 s->logs.t_connect = -1;
4424 s->logs.t_data = -1;
4425 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04004426 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
4427 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004428
Willy Tarreauf37954d2018-06-15 18:31:02 +02004429 s->logs.bytes_in = s->req.total = ci_data(&s->req);
4430 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004431
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004432 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004433 if (s->flags & SF_CURR_SESS) {
4434 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004435 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004436 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004437 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004438 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004439 }
4440
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004441 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004442
Willy Tarreau4213a112013-12-15 10:25:42 +01004443 /* only release our endpoint if we don't intend to reuse the
4444 * connection.
4445 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004446 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004447 !si_conn_ready(&s->si[1])) {
4448 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004449 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004450 }
4451
Willy Tarreau350f4872014-11-28 14:42:25 +01004452 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4453 s->si[1].err_type = SI_ET_NONE;
4454 s->si[1].conn_retries = 0; /* used for logging too */
4455 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004456 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 +01004457 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 +01004458 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 +02004459 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4460 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4461 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004462
Willy Tarreaueee5b512015-04-03 23:46:31 +02004463 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004464 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004465 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004466
4467 if (prev_status == 401 || prev_status == 407) {
4468 /* In HTTP keep-alive mode, if we receive a 401, we still have
4469 * a chance of being able to send the visitor again to the same
4470 * server over the same connection. This is required by some
4471 * broken protocols such as NTLM, and anyway whenever there is
4472 * an opportunity for sending the challenge to the proper place,
4473 * it's better to do it (at least it helps with debugging).
4474 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004475 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004476 if (srv_conn)
4477 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004478 }
4479
Willy Tarreau53f96852016-02-02 18:50:47 +01004480 /* Never ever allow to reuse a connection from a non-reuse backend */
4481 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4482 srv_conn->flags |= CO_FL_PRIVATE;
4483
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004484 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004485 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004486
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004487 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004488 s->req.flags |= CF_NEVER_WAIT;
4489 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004490 }
4491
Willy Tarreau714ea782015-11-25 20:11:11 +01004492 /* we're removing the analysers, we MUST re-enable events detection.
4493 * We don't enable close on the response channel since it's either
4494 * already closed, or in keep-alive with an idle connection handler.
4495 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004496 channel_auto_read(&s->req);
4497 channel_auto_close(&s->req);
4498 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004499
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004500 /* we're in keep-alive with an idle connection, monitor it if not already done */
4501 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004502 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004503 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004504 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004505 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004506 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004507 else if (prev_flags & TX_NOT_FIRST)
4508 /* note: we check the request, not the connection, but
4509 * this is valid for strategies SAFE and AGGR, and in
4510 * case of ALWS, we don't care anyway.
4511 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004512 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004513 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004514 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004515 }
Christopher Faulete6006242017-03-10 11:52:44 +01004516 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4517 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004518}
4519
4520
4521/* This function updates the request state machine according to the response
4522 * state machine and buffer flags. It returns 1 if it changes anything (flag
4523 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4524 * it is only used to find when a request/response couple is complete. Both
4525 * this function and its equivalent should loop until both return zero. It
4526 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4527 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004528int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004529{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004530 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004531 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004532 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004533 unsigned int old_state = txn->req.msg_state;
4534
Christopher Faulet4be98032017-07-18 10:48:24 +02004535 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004536 return 0;
4537
4538 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004539 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004540 * We can shut the read side unless we want to abort_on_close,
4541 * or we have a POST request. The issue with POST requests is
4542 * that some browsers still send a CRLF after the request, and
4543 * this CRLF must be read so that it does not remain in the kernel
4544 * buffers, otherwise a close could cause an RST on some systems
4545 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004546 * Note that if we're using keep-alive on the client side, we'd
4547 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004548 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004549 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004550 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004551 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4552 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004553 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4554 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01004555 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004556 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004557
Willy Tarreau40f151a2012-12-20 12:10:09 +01004558 /* if the server closes the connection, we want to immediately react
4559 * and close the socket to save packets and syscalls.
4560 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004561 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004562
Willy Tarreau7f876a12015-11-18 11:59:55 +01004563 /* In any case we've finished parsing the request so we must
4564 * disable Nagle when sending data because 1) we're not going
4565 * to shut this side, and 2) the server is waiting for us to
4566 * send pending data.
4567 */
4568 chn->flags |= CF_NEVER_WAIT;
4569
Willy Tarreau610ecce2010-01-04 21:15:02 +01004570 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4571 goto wait_other_side;
4572
4573 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4574 /* The server has not finished to respond, so we
4575 * don't want to move in order not to upset it.
4576 */
4577 goto wait_other_side;
4578 }
4579
Willy Tarreau610ecce2010-01-04 21:15:02 +01004580 /* When we get here, it means that both the request and the
4581 * response have finished receiving. Depending on the connection
4582 * mode, we'll have to wait for the last bytes to leave in either
4583 * direction, and sometimes for a close to be effective.
4584 */
4585
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004586 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4587 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004588 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4589 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004590 }
4591 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4592 /* Option forceclose is set, or either side wants to close,
4593 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004594 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004595 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004596 *
4597 * However, there is an exception if the response
4598 * length is undefined. In this case, we need to wait
4599 * the close from the server. The response will be
4600 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004601 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004602 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4603 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4604 goto check_channel_flags;
4605
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004606 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4607 channel_shutr_now(chn);
4608 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004609 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004610 }
4611 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004612 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4613 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004614 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004615 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4616 channel_auto_read(chn);
4617 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004618 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619 }
4620
Christopher Faulet4be98032017-07-18 10:48:24 +02004621 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004622 }
4623
4624 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4625 http_msg_closing:
4626 /* nothing else to forward, just waiting for the output buffer
4627 * to be empty and for the shutw_now to take effect.
4628 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004629 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004630 txn->req.msg_state = HTTP_MSG_CLOSED;
4631 goto http_msg_closed;
4632 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004633 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004634 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004636 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004637 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004638 }
4639
4640 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4641 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004642 /* if we don't know whether the server will close, we need to hard close */
4643 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4644 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4645
Willy Tarreau3988d932013-12-27 23:03:08 +01004646 /* see above in MSG_DONE why we only do this in these states */
4647 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4648 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004649 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4650 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004651 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004652 goto wait_other_side;
4653 }
4654
Christopher Faulet4be98032017-07-18 10:48:24 +02004655 check_channel_flags:
4656 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4657 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4658 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004659 txn->req.msg_state = HTTP_MSG_CLOSING;
4660 goto http_msg_closing;
4661 }
4662
4663
Willy Tarreau610ecce2010-01-04 21:15:02 +01004664 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004665 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004666}
4667
4668
4669/* This function updates the response state machine according to the request
4670 * state machine and buffer flags. It returns 1 if it changes anything (flag
4671 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4672 * it is only used to find when a request/response couple is complete. Both
4673 * this function and its equivalent should loop until both return zero. It
4674 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4675 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004676int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004677{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004678 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004679 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004680 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004681 unsigned int old_state = txn->rsp.msg_state;
4682
Christopher Faulet4be98032017-07-18 10:48:24 +02004683 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004684 return 0;
4685
4686 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4687 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004688 * still monitor the server connection for a possible close
4689 * while the request is being uploaded, so we don't disable
4690 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004691 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004692 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004693
4694 if (txn->req.msg_state == HTTP_MSG_ERROR)
4695 goto wait_other_side;
4696
4697 if (txn->req.msg_state < HTTP_MSG_DONE) {
4698 /* The client seems to still be sending data, probably
4699 * because we got an error response during an upload.
4700 * We have the choice of either breaking the connection
4701 * or letting it pass through. Let's do the later.
4702 */
4703 goto wait_other_side;
4704 }
4705
Willy Tarreau610ecce2010-01-04 21:15:02 +01004706 /* When we get here, it means that both the request and the
4707 * response have finished receiving. Depending on the connection
4708 * mode, we'll have to wait for the last bytes to leave in either
4709 * direction, and sometimes for a close to be effective.
4710 */
4711
4712 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4713 /* Server-close mode : shut read and wait for the request
4714 * side to close its output buffer. The caller will detect
4715 * when we're in DONE and the other is in CLOSED and will
4716 * catch that for the final cleanup.
4717 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004718 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4719 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004720 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004721 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4722 /* Option forceclose is set, or either side wants to close,
4723 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004724 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004725 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004726 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004727 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004728 channel_shutr_now(chn);
4729 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004730 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004731 }
4732 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004733 /* The last possible modes are keep-alive and tunnel. Tunnel will
4734 * need to forward remaining data. Keep-alive will need to monitor
4735 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004736 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004737 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004738 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004739 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4740 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004741 }
4742
Christopher Faulet4be98032017-07-18 10:48:24 +02004743 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004744 }
4745
4746 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4747 http_msg_closing:
4748 /* nothing else to forward, just waiting for the output buffer
4749 * to be empty and for the shutw_now to take effect.
4750 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004751 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004752 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4753 goto http_msg_closed;
4754 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004755 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004756 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004757 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004758 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004759 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004760 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004761 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004762 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004763 }
4764
4765 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4766 http_msg_closed:
4767 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004768 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004769 channel_auto_close(chn);
4770 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004771 goto wait_other_side;
4772 }
4773
Christopher Faulet4be98032017-07-18 10:48:24 +02004774 check_channel_flags:
4775 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4776 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4777 /* if we've just closed an output, let's switch */
4778 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4779 goto http_msg_closing;
4780 }
4781
Willy Tarreau610ecce2010-01-04 21:15:02 +01004782 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004783 /* We force the response to leave immediately if we're waiting for the
4784 * other side, since there is no pending shutdown to push it out.
4785 */
4786 if (!channel_is_empty(chn))
4787 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004788 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004789}
4790
4791
Christopher Faulet894da4c2017-07-18 11:29:07 +02004792/* Resync the request and response state machines. */
4793void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004794{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004795 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004796#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004797 int old_req_state = txn->req.msg_state;
4798 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004799#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004800
Willy Tarreau610ecce2010-01-04 21:15:02 +01004801 http_sync_req_state(s);
4802 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004803 if (!http_sync_res_state(s))
4804 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004805 if (!http_sync_req_state(s))
4806 break;
4807 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004808
Christopher Faulet894da4c2017-07-18 11:29:07 +02004809 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4810 "req->analysers=0x%08x res->analysers=0x%08x\n",
4811 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004812 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4813 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004814 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004815
4816
Willy Tarreau610ecce2010-01-04 21:15:02 +01004817 /* OK, both state machines agree on a compatible state.
4818 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004819 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4820 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004821 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4822 * means we must abort the request.
4823 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4824 * corresponding channel.
4825 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4826 * on the response with server-close mode means we've completed one
4827 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004828 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004829 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4830 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004831 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004832 channel_auto_close(&s->req);
4833 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004834 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004835 channel_auto_close(&s->res);
4836 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004837 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004838 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4839 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004840 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004841 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004842 channel_auto_close(&s->res);
4843 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004844 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004845 channel_abort(&s->req);
4846 channel_auto_close(&s->req);
4847 channel_auto_read(&s->req);
4848 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004849 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004850 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4851 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4852 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4853 s->req.analysers &= AN_REQ_FLT_END;
4854 if (HAS_REQ_DATA_FILTERS(s))
4855 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4856 }
4857 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4858 s->res.analysers &= AN_RES_FLT_END;
4859 if (HAS_RSP_DATA_FILTERS(s))
4860 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4861 }
4862 channel_auto_close(&s->req);
4863 channel_auto_read(&s->req);
4864 channel_auto_close(&s->res);
4865 channel_auto_read(&s->res);
4866 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004867 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4868 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004869 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004870 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4871 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4872 /* server-close/keep-alive: terminate this transaction,
4873 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004874 * a fresh-new transaction, but only once we're sure there's
4875 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004876 * another request. They must not hold any pending output data
4877 * and the response buffer must realigned
4878 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004879 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004880 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004881 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004882 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004883 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004884 else {
4885 s->req.analysers = AN_REQ_FLT_END;
4886 s->res.analysers = AN_RES_FLT_END;
4887 txn->flags |= TX_WAIT_CLEANUP;
4888 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004889 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004890}
4891
Willy Tarreaud98cf932009-12-27 22:54:55 +01004892/* This function is an analyser which forwards request body (including chunk
4893 * sizes if any). It is called as soon as we must forward, even if we forward
4894 * zero byte. The only situation where it must not be called is when we're in
4895 * tunnel mode and we want to forward till the close. It's used both to forward
4896 * remaining data and to resync after end of body. It expects the msg_state to
4897 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004898 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004899 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004900 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004901 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004902int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004903{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004904 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004905 struct http_txn *txn = s->txn;
4906 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004907 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004908
Christopher Faulet45073512018-07-20 10:16:29 +02004909 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Christopher Faulet814d2702017-03-30 11:33:44 +02004910 now_ms, __FUNCTION__,
4911 s,
4912 req,
4913 req->rex, req->wex,
4914 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004915 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004916 req->analysers);
4917
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004918 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4919 return 0;
4920
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004921 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004922 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004923 /* Output closed while we were sending data. We must abort and
4924 * wake the other side up.
4925 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004926 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004927 msg->msg_state = HTTP_MSG_ERROR;
4928 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004929 return 1;
4930 }
4931
Willy Tarreaud98cf932009-12-27 22:54:55 +01004932 /* Note that we don't have to send 100-continue back because we don't
4933 * need the data to complete our job, and it's up to the server to
4934 * decide whether to return 100, 417 or anything else in return of
4935 * an "Expect: 100-continue" header.
4936 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004937 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004938 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4939 ? HTTP_MSG_CHUNK_SIZE
4940 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004941
4942 /* TODO/filters: when http-buffer-request option is set or if a
4943 * rule on url_param exists, the first chunk size could be
4944 * already parsed. In that case, msg->next is after the chunk
4945 * size (including the CRLF after the size). So this case should
4946 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004947 }
4948
Willy Tarreau7ba23542014-04-17 21:50:00 +02004949 /* Some post-connect processing might want us to refrain from starting to
4950 * forward data. Currently, the only reason for this is "balance url_param"
4951 * whichs need to parse/process the request after we've enabled forwarding.
4952 */
4953 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004954 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004955 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004956 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004957 channel_dont_close(req); /* don't fail on early shutr */
4958 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004959 }
4960 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4961 }
4962
Willy Tarreau80a92c02014-03-12 10:41:13 +01004963 /* in most states, we should abort in case of early close */
4964 channel_auto_close(req);
4965
Willy Tarreauefdf0942014-04-24 20:08:57 +02004966 if (req->to_forward) {
4967 /* We can't process the buffer's contents yet */
4968 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004969 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004970 }
4971
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004972 if (msg->msg_state < HTTP_MSG_DONE) {
4973 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4974 ? http_msg_forward_chunked_body(s, msg)
4975 : http_msg_forward_body(s, msg));
4976 if (!ret)
4977 goto missing_data_or_waiting;
4978 if (ret < 0)
4979 goto return_bad_req;
4980 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004981
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004982 /* other states, DONE...TUNNEL */
4983 /* we don't want to forward closes on DONE except in tunnel mode. */
4984 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4985 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004986
Christopher Faulet894da4c2017-07-18 11:29:07 +02004987 http_resync_states(s);
4988 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004989 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4990 if (req->flags & CF_SHUTW) {
4991 /* request errors are most likely due to the
4992 * server aborting the transfer. */
4993 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004994 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004995 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004996 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004997 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004998 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004999 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005000 }
5001
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005002 /* If "option abortonclose" is set on the backend, we want to monitor
5003 * the client's connection and forward any shutdown notification to the
5004 * server, which will decide whether to close or to go on processing the
5005 * request. We only do that in tunnel mode, and not in other modes since
5006 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01005007 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005008 channel_auto_read(req);
5009 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5010 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5011 s->si[1].flags |= SI_FL_NOLINGER;
5012 channel_auto_close(req);
5013 }
5014 else if (s->txn->meth == HTTP_METH_POST) {
5015 /* POST requests may require to read extra CRLF sent by broken
5016 * browsers and which could cause an RST to be sent upon close
5017 * on some systems (eg: Linux). */
5018 channel_auto_read(req);
5019 }
5020 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005021
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005022 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005023 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005024 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005025 if (!(s->flags & SF_ERR_MASK))
5026 s->flags |= SF_ERR_CLICL;
5027 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005028 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005029 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005030 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005031 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005032 }
5033
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005034 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5035 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005036 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005037 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005038
5039 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005040 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005041
Willy Tarreauba20dfc2018-05-16 11:35:05 +02005042 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005043 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005044 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005045 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005046
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005047 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005048 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01005049 * And when content-length is used, we never want to let the possible
5050 * shutdown be forwarded to the other side, as the state machine will
5051 * take care of it once the client responds. It's also important to
5052 * prevent TIME_WAITs from accumulating on the backend side, and for
5053 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005054 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01005055 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005056 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005057
Willy Tarreau5c620922011-05-11 19:56:11 +02005058 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005059 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005060 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005061 * modes are already handled by the stream sock layer. We must not do
5062 * this in content-length mode because it could present the MSG_MORE
5063 * flag with the last block of forwarded data, which would cause an
5064 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005065 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005066 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005067 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005068
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005069 return 0;
5070
Willy Tarreaud98cf932009-12-27 22:54:55 +01005071 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005072 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005073 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005074 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02005075
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005076 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005077 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005078 txn->req.msg_state = HTTP_MSG_ERROR;
5079 if (txn->status) {
5080 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005081 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005082 } else {
5083 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005084 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005085 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005086 req->analysers &= AN_REQ_FLT_END;
5087 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 +01005088
Willy Tarreaue7dff022015-04-03 01:14:29 +02005089 if (!(s->flags & SF_ERR_MASK))
5090 s->flags |= SF_ERR_PRXCOND;
5091 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005092 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005093 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005094 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005095 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005096 }
5097 return 0;
5098
5099 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005100 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005101 txn->req.msg_state = HTTP_MSG_ERROR;
5102 if (txn->status) {
5103 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005104 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005105 } else {
5106 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005107 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005108 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005109 req->analysers &= AN_REQ_FLT_END;
5110 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 +01005111
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005112 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5113 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005114 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005115 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005116
Willy Tarreaue7dff022015-04-03 01:14:29 +02005117 if (!(s->flags & SF_ERR_MASK))
5118 s->flags |= SF_ERR_SRVCL;
5119 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005120 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005121 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005122 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005123 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005124 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005125 return 0;
5126}
5127
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005128/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5129 * processing can continue on next analysers, or zero if it either needs more
5130 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005131 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005132 * when it has nothing left to do, and may remove any analyser when it wants to
5133 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005134 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005135int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005136{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005137 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005138 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005139 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005140 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005141 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005142 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005143 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005144
Christopher Faulet45073512018-07-20 10:16:29 +02005145 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005146 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005147 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005148 rep,
5149 rep->rex, rep->wex,
5150 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005151 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005152 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005153
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005154 /*
5155 * Now parse the partial (or complete) lines.
5156 * We will check the response syntax, and also join multi-line
5157 * headers. An index of all the lines will be elaborated while
5158 * parsing.
5159 *
5160 * For the parsing, we use a 28 states FSM.
5161 *
5162 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02005163 * ci_head(rep) = beginning of response
5164 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
5165 * ci_tail(rep) = end of input data
5166 * msg->eol = end of current header or line (LF or CRLF)
5167 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005168 */
5169
Willy Tarreau628c40c2014-04-24 19:11:26 +02005170 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005171 /* There's a protected area at the end of the buffer for rewriting
5172 * purposes. We don't want to start to parse the request if the
5173 * protected area is affected, because we may have to move processed
5174 * data later, which is much more complicated.
5175 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02005176 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005177 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005178 /* some data has still not left the buffer, wake us once that's done */
5179 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5180 goto abort_response;
5181 channel_dont_close(rep);
5182 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005183 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005184 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005185 }
5186
Willy Tarreau188e2302018-06-15 11:11:53 +02005187 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005188 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005189 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02005190
Willy Tarreauf37954d2018-06-15 18:31:02 +02005191 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01005192 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005193 }
5194
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005195 /* 1: we might have to print this header in debug mode */
5196 if (unlikely((global.mode & MODE_DEBUG) &&
5197 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005198 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005199 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005200
Willy Tarreauf37954d2018-06-15 18:31:02 +02005201 sol = ci_head(rep);
5202 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005203 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005204
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005205 sol += hdr_idx_first_pos(&txn->hdr_idx);
5206 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005207
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005208 while (cur_idx) {
5209 eol = sol + txn->hdr_idx.v[cur_idx].len;
5210 debug_hdr("srvhdr", s, sol, eol);
5211 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5212 cur_idx = txn->hdr_idx.v[cur_idx].next;
5213 }
5214 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005215
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005216 /*
5217 * Now we quickly check if we have found a full valid response.
5218 * If not so, we check the FD and buffer states before leaving.
5219 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005220 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005221 * responses are checked first.
5222 *
5223 * Depending on whether the client is still there or not, we
5224 * may send an error response back or not. Note that normally
5225 * we should only check for HTTP status there, and check I/O
5226 * errors somewhere else.
5227 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005228
Willy Tarreau655dce92009-11-08 13:10:58 +01005229 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005230 /* Invalid response */
5231 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5232 /* we detected a parsing error. We want to archive this response
5233 * in the dedicated proxy area for later troubleshooting.
5234 */
5235 hdr_response_bad:
5236 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005237 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005238
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005239 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005240 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005241 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005242 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005243 }
Willy Tarreau64648412010-03-05 10:41:54 +01005244 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005245 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005246 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005247 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005248 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005249 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005250 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005251
Willy Tarreaue7dff022015-04-03 01:14:29 +02005252 if (!(s->flags & SF_ERR_MASK))
5253 s->flags |= SF_ERR_PRXCOND;
5254 if (!(s->flags & SF_FINST_MASK))
5255 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005256
5257 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005258 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005259
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005260 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02005261 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005262 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02005263 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005264 goto hdr_response_bad;
5265 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005266
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005267 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005268 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005269 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005270 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005271 else if (txn->flags & TX_NOT_FIRST)
5272 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005273
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005274 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005275 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005276 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005277 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005278 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005279
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005280 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005281 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005282 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005283
5284 /* Check to see if the server refused the early data.
5285 * If so, just send a 425
5286 */
5287 if (objt_cs(s->si[1].end)) {
5288 struct connection *conn = objt_cs(s->si[1].end)->conn;
5289
5290 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5291 txn->status = 425;
5292 }
5293
Willy Tarreau350f4872014-11-28 14:42:25 +01005294 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005295 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005296 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005297
Willy Tarreaue7dff022015-04-03 01:14:29 +02005298 if (!(s->flags & SF_ERR_MASK))
5299 s->flags |= SF_ERR_SRVCL;
5300 if (!(s->flags & SF_FINST_MASK))
5301 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005302 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005303 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005304
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005305 /* read timeout : return a 504 to the client. */
5306 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005307 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005308 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005309
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005310 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005311 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005312 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005313 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005314 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005315
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005316 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005317 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005318 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005319 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005320 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005321 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005322
Willy Tarreaue7dff022015-04-03 01:14:29 +02005323 if (!(s->flags & SF_ERR_MASK))
5324 s->flags |= SF_ERR_SRVTO;
5325 if (!(s->flags & SF_FINST_MASK))
5326 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005327 return 0;
5328 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005329
Willy Tarreauf003d372012-11-26 13:35:37 +01005330 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005331 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005332 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5333 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005334 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005335 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005336
Christopher Faulet0184ea72017-01-05 14:06:34 +01005337 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005338 channel_auto_close(rep);
5339
5340 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005341 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005342 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005343
Willy Tarreaue7dff022015-04-03 01:14:29 +02005344 if (!(s->flags & SF_ERR_MASK))
5345 s->flags |= SF_ERR_CLICL;
5346 if (!(s->flags & SF_FINST_MASK))
5347 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005348
Willy Tarreau87b09662015-04-03 00:22:06 +02005349 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005350 return 0;
5351 }
5352
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005353 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005354 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005355 if (msg->msg_state >= HTTP_MSG_RPVER || 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 Tarreau6b726ad2013-12-15 19:31:37 +01005357 else if (txn->flags & TX_NOT_FIRST)
5358 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005359
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005360 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005361 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005362 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005363 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005364 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005365
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005366 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005367 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005368 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005369 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005370 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005371 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005372
Willy Tarreaue7dff022015-04-03 01:14:29 +02005373 if (!(s->flags & SF_ERR_MASK))
5374 s->flags |= SF_ERR_SRVCL;
5375 if (!(s->flags & SF_FINST_MASK))
5376 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005377 return 0;
5378 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005379
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005380 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005381 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005382 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005383 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005384 else if (txn->flags & TX_NOT_FIRST)
5385 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005386
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005387 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005388 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005389 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005390
Willy Tarreaue7dff022015-04-03 01:14:29 +02005391 if (!(s->flags & SF_ERR_MASK))
5392 s->flags |= SF_ERR_CLICL;
5393 if (!(s->flags & SF_FINST_MASK))
5394 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005395
Willy Tarreau87b09662015-04-03 00:22:06 +02005396 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005397 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005398 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005399
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005400 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005401 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005402 return 0;
5403 }
5404
5405 /* More interesting part now : we know that we have a complete
5406 * response which at least looks like HTTP. We have an indicator
5407 * of each header's length, so we can parse them quickly.
5408 */
5409
5410 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005411 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005412
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005413 /*
5414 * 1: get the status code
5415 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005416 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005417 if (n < 1 || n > 5)
5418 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005419 /* when the client triggers a 4xx from the server, it's most often due
5420 * to a missing object or permission. These events should be tracked
5421 * because if they happen often, it may indicate a brute force or a
5422 * vulnerability scan.
5423 */
5424 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005425 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005426
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005427 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005428 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005429
Willy Tarreau91852eb2015-05-01 13:26:00 +02005430 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5431 * exactly one digit "." one digit. This check may be disabled using
5432 * option accept-invalid-http-response.
5433 */
5434 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5435 if (msg->sl.st.v_l != 8) {
5436 msg->err_pos = 0;
5437 goto hdr_response_bad;
5438 }
5439
Willy Tarreauf37954d2018-06-15 18:31:02 +02005440 if (ci_head(rep)[4] != '/' ||
5441 !isdigit((unsigned char)ci_head(rep)[5]) ||
5442 ci_head(rep)[6] != '.' ||
5443 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02005444 msg->err_pos = 4;
5445 goto hdr_response_bad;
5446 }
5447 }
5448
Willy Tarreau5b154472009-12-21 20:11:07 +01005449 /* check if the response is HTTP/1.1 or above */
5450 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02005451 ((ci_head(rep)[5] > '1') ||
5452 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005453 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005454
5455 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005456 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 +01005457
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005458 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005459 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005460
Willy Tarreauf37954d2018-06-15 18:31:02 +02005461 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005462
Willy Tarreau39650402010-03-15 19:44:39 +01005463 /* Adjust server's health based on status code. Note: status codes 501
5464 * and 505 are triggered on demand by client request, so we must not
5465 * count them as server failures.
5466 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005467 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005468 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005469 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005470 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005471 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005472 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005473
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005474 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005475 * We may be facing a 100-continue response, or any other informational
5476 * 1xx response which is non-final, in which case this is not the right
5477 * response, and we're waiting for the next one. Let's allow this response
5478 * to go to the client and wait for the next one. There's an exception for
5479 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005480 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005481 if (txn->status < 200 &&
5482 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005483 hdr_idx_init(&txn->hdr_idx);
5484 msg->next -= channel_forward(rep, msg->next);
5485 msg->msg_state = HTTP_MSG_RPBEFORE;
5486 txn->status = 0;
5487 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005488 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005489 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005490 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005491
Willy Tarreaua14ad722017-07-07 11:36:32 +02005492 /*
5493 * 2: check for cacheability.
5494 */
5495
5496 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005497 case 200:
5498 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005499 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005500 case 206:
5501 case 300:
5502 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005503 case 404:
5504 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005505 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005506 case 414:
5507 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005508 break;
5509 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005510 /* RFC7231#6.1:
5511 * Responses with status codes that are defined as
5512 * cacheable by default (e.g., 200, 203, 204, 206,
5513 * 300, 301, 404, 405, 410, 414, and 501 in this
5514 * specification) can be reused by a cache with
5515 * heuristic expiration unless otherwise indicated
5516 * by the method definition or explicit cache
5517 * controls [RFC7234]; all other status codes are
5518 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005519 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005520 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005521 break;
5522 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005523
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005524 /*
5525 * 3: we may need to capture headers
5526 */
5527 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005528 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02005529 capture_headers(ci_head(rep), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005530 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005531
Willy Tarreau557f1992015-05-01 10:05:17 +02005532 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5533 * by RFC7230#3.3.3 :
5534 *
5535 * The length of a message body is determined by one of the following
5536 * (in order of precedence):
5537 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005538 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5539 * the connection will become a tunnel immediately after the empty
5540 * line that concludes the header fields. A client MUST ignore
5541 * any Content-Length or Transfer-Encoding header fields received
5542 * in such a message. Any 101 response (Switching Protocols) is
5543 * managed in the same manner.
5544 *
5545 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005546 * (Informational), 204 (No Content), or 304 (Not Modified) status
5547 * code is always terminated by the first empty line after the
5548 * header fields, regardless of the header fields present in the
5549 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005550 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005551 * 3. If a Transfer-Encoding header field is present and the chunked
5552 * transfer coding (Section 4.1) is the final encoding, the message
5553 * body length is determined by reading and decoding the chunked
5554 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005555 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005556 * If a Transfer-Encoding header field is present in a response and
5557 * the chunked transfer coding is not the final encoding, the
5558 * message body length is determined by reading the connection until
5559 * it is closed by the server. If a Transfer-Encoding header field
5560 * is present in a request and the chunked transfer coding is not
5561 * the final encoding, the message body length cannot be determined
5562 * reliably; the server MUST respond with the 400 (Bad Request)
5563 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005564 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005565 * If a message is received with both a Transfer-Encoding and a
5566 * Content-Length header field, the Transfer-Encoding overrides the
5567 * Content-Length. Such a message might indicate an attempt to
5568 * perform request smuggling (Section 9.5) or response splitting
5569 * (Section 9.4) and ought to be handled as an error. A sender MUST
5570 * remove the received Content-Length field prior to forwarding such
5571 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005572 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005573 * 4. If a message is received without Transfer-Encoding and with
5574 * either multiple Content-Length header fields having differing
5575 * field-values or a single Content-Length header field having an
5576 * invalid value, then the message framing is invalid and the
5577 * recipient MUST treat it as an unrecoverable error. If this is a
5578 * request message, the server MUST respond with a 400 (Bad Request)
5579 * status code and then close the connection. If this is a response
5580 * message received by a proxy, the proxy MUST close the connection
5581 * to the server, discard the received response, and send a 502 (Bad
5582 * Gateway) response to the client. If this is a response message
5583 * received by a user agent, the user agent MUST close the
5584 * connection to the server and discard the received response.
5585 *
5586 * 5. If a valid Content-Length header field is present without
5587 * Transfer-Encoding, its decimal value defines the expected message
5588 * body length in octets. If the sender closes the connection or
5589 * the recipient times out before the indicated number of octets are
5590 * received, the recipient MUST consider the message to be
5591 * incomplete and close the connection.
5592 *
5593 * 6. If this is a request message and none of the above are true, then
5594 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005595 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005596 * 7. Otherwise, this is a response message without a declared message
5597 * body length, so the message body length is determined by the
5598 * number of octets received prior to the server closing the
5599 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005600 */
5601
5602 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005603 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005604 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005605 * FIXME: should we parse anyway and return an error on chunked encoding ?
5606 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005607 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5608 txn->status == 101)) {
5609 /* Either we've established an explicit tunnel, or we're
5610 * switching the protocol. In both cases, we're very unlikely
5611 * to understand the next protocols. We have to switch to tunnel
5612 * mode, so that we transfer the request and responses then let
5613 * this protocol pass unmodified. When we later implement specific
5614 * parsers for such protocols, we'll want to check the Upgrade
5615 * header which contains information about that protocol for
5616 * responses with status 101 (eg: see RFC2817 about TLS).
5617 */
5618 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5619 msg->flags |= HTTP_MSGF_XFER_LEN;
5620 goto end;
5621 }
5622
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005623 if (txn->meth == HTTP_METH_HEAD ||
5624 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005625 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005626 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005627 goto skip_content_length;
5628 }
5629
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005630 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005631 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005632 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005633 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005634 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5635 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005636 /* bad transfer-encoding (chunked followed by something else) */
5637 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005638 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005639 break;
5640 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005641 }
5642
Willy Tarreau1c913912015-04-30 10:57:51 +02005643 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005644 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005645 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005646 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005647 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5648 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005649 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005650 signed long long cl;
5651
Willy Tarreauad14f752011-09-02 20:33:27 +02005652 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005653 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005654 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005655 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005656
Willy Tarreauad14f752011-09-02 20:33:27 +02005657 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005658 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005659 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005660 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005661
Willy Tarreauad14f752011-09-02 20:33:27 +02005662 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005663 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005664 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005665 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005666
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005667 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005668 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005669 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005670 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005671
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005672 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005673 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005674 }
5675
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005676 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005677 /* Now we have to check if we need to modify the Connection header.
5678 * This is more difficult on the response than it is on the request,
5679 * because we can have two different HTTP versions and we don't know
5680 * how the client will interprete a response. For instance, let's say
5681 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5682 * HTTP/1.1 response without any header. Maybe it will bound itself to
5683 * HTTP/1.0 because it only knows about it, and will consider the lack
5684 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5685 * the lack of header as a keep-alive. Thus we will use two flags
5686 * indicating how a request MAY be understood by the client. In case
5687 * of multiple possibilities, we'll fix the header to be explicit. If
5688 * ambiguous cases such as both close and keepalive are seen, then we
5689 * will fall back to explicit close. Note that we won't take risks with
5690 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005691 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005692 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005693 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5694 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5695 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5696 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005697 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005698
Willy Tarreau70dffda2014-01-30 03:07:23 +01005699 /* this situation happens when combining pretend-keepalive with httpclose. */
5700 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005701 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005702 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005703 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5704
Willy Tarreau60466522010-01-18 19:08:45 +01005705 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005706 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005707 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5708 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005709
Willy Tarreau60466522010-01-18 19:08:45 +01005710 /* now adjust header transformations depending on current state */
5711 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5712 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5713 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005714 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005715 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005716 }
Willy Tarreau60466522010-01-18 19:08:45 +01005717 else { /* SCL / KAL */
5718 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005719 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005720 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005721 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005722
Willy Tarreau60466522010-01-18 19:08:45 +01005723 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005724 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005725
Willy Tarreau60466522010-01-18 19:08:45 +01005726 /* Some keep-alive responses are converted to Server-close if
5727 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005728 */
Willy Tarreau60466522010-01-18 19:08:45 +01005729 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5730 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005731 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005732 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005733 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005734 }
5735
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005736 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005737 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005738 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005739
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005740 /* end of job, return OK */
5741 rep->analysers &= ~an_bit;
5742 rep->analyse_exp = TICK_ETERNITY;
5743 channel_auto_close(rep);
5744 return 1;
5745
5746 abort_keep_alive:
5747 /* A keep-alive request to the server failed on a network error.
5748 * The client is required to retry. We need to close without returning
5749 * any other information so that the client retries.
5750 */
5751 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005752 rep->analysers &= AN_RES_FLT_END;
5753 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005754 channel_auto_close(rep);
5755 s->logs.logwait = 0;
5756 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005757 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005758 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005759 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005760 return 0;
5761}
5762
5763/* This function performs all the processing enabled for the current response.
5764 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005765 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005766 * other functions. It works like process_request (see indications above).
5767 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005768int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005769{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005770 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005771 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005772 struct http_msg *msg = &txn->rsp;
5773 struct proxy *cur_proxy;
5774 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005775 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005776
Christopher Faulet45073512018-07-20 10:16:29 +02005777 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005778 now_ms, __FUNCTION__,
5779 s,
5780 rep,
5781 rep->rex, rep->wex,
5782 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005783 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005784 rep->analysers);
5785
5786 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5787 return 0;
5788
Willy Tarreau70730dd2014-04-24 18:06:27 +02005789 /* The stats applet needs to adjust the Connection header but we don't
5790 * apply any filter there.
5791 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005792 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5793 rep->analysers &= ~an_bit;
5794 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005795 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005796 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005797
Willy Tarreau58975672014-04-24 21:13:57 +02005798 /*
5799 * We will have to evaluate the filters.
5800 * As opposed to version 1.2, now they will be evaluated in the
5801 * filters order and not in the header order. This means that
5802 * each filter has to be validated among all headers.
5803 *
5804 * Filters are tried with ->be first, then with ->fe if it is
5805 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005806 *
5807 * Maybe we are in resume condiion. In this case I choose the
5808 * "struct proxy" which contains the rule list matching the resume
5809 * pointer. If none of theses "struct proxy" match, I initialise
5810 * the process with the first one.
5811 *
5812 * In fact, I check only correspondance betwwen the current list
5813 * pointer and the ->fe rule list. If it doesn't match, I initialize
5814 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005815 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005816 if (s->current_rule_list == &sess->fe->http_res_rules)
5817 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005818 else
5819 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005820 while (1) {
5821 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005822
Willy Tarreau58975672014-04-24 21:13:57 +02005823 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005824 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005825 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005826
Willy Tarreau51d861a2015-05-22 17:30:48 +02005827 if (ret == HTTP_RULE_RES_BADREQ)
5828 goto return_srv_prx_502;
5829
5830 if (ret == HTTP_RULE_RES_DONE) {
5831 rep->analysers &= ~an_bit;
5832 rep->analyse_exp = TICK_ETERNITY;
5833 return 1;
5834 }
5835 }
5836
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005837 /* we need to be called again. */
5838 if (ret == HTTP_RULE_RES_YIELD) {
5839 channel_dont_close(rep);
5840 return 0;
5841 }
5842
Willy Tarreau58975672014-04-24 21:13:57 +02005843 /* try headers filters */
5844 if (rule_set->rsp_exp != NULL) {
5845 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5846 return_bad_resp:
5847 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005848 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005849 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005850 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005851 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005852 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005853 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005854 txn->status = 502;
5855 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005856 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005857 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005858 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005859 if (!(s->flags & SF_ERR_MASK))
5860 s->flags |= SF_ERR_PRXCOND;
5861 if (!(s->flags & SF_FINST_MASK))
5862 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005863 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005864 }
Willy Tarreau58975672014-04-24 21:13:57 +02005865 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005866
Willy Tarreau58975672014-04-24 21:13:57 +02005867 /* has the response been denied ? */
5868 if (txn->flags & TX_SVDENY) {
5869 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005870 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005871
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005872 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5873 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005874 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005875 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005876
Willy Tarreau58975672014-04-24 21:13:57 +02005877 goto return_srv_prx_502;
5878 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005879
Willy Tarreau58975672014-04-24 21:13:57 +02005880 /* add response headers from the rule sets in the same order */
5881 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005882 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005883 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005884 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005885 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005886 ret = acl_pass(ret);
5887 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5888 ret = !ret;
5889 if (!ret)
5890 continue;
5891 }
5892 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5893 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005894 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005895
Willy Tarreau58975672014-04-24 21:13:57 +02005896 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005897 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005898 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005899 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005900 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005901
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005902 /* After this point, this anayzer can't return yield, so we can
5903 * remove the bit corresponding to this analyzer from the list.
5904 *
5905 * Note that the intermediate returns and goto found previously
5906 * reset the analyzers.
5907 */
5908 rep->analysers &= ~an_bit;
5909 rep->analyse_exp = TICK_ETERNITY;
5910
Willy Tarreau58975672014-04-24 21:13:57 +02005911 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005912 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005913 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005914
Willy Tarreau58975672014-04-24 21:13:57 +02005915 /*
5916 * Now check for a server cookie.
5917 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005918 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005919 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005920
Willy Tarreau58975672014-04-24 21:13:57 +02005921 /*
5922 * Check for cache-control or pragma headers if required.
5923 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005924 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005925 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005926
Willy Tarreau58975672014-04-24 21:13:57 +02005927 /*
5928 * Add server cookie in the response if needed
5929 */
5930 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5931 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005932 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005933 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5934 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5935 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5936 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5937 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005938 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005939 /* the server is known, it's not the one the client requested, or the
5940 * cookie's last seen date needs to be refreshed. We have to
5941 * insert a set-cookie here, except if we want to insert only on POST
5942 * requests and this one isn't. Note that servers which don't have cookies
5943 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005944 */
Willy Tarreau58975672014-04-24 21:13:57 +02005945 if (!objt_server(s->target)->cookie) {
5946 chunk_printf(&trash,
5947 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5948 s->be->cookie_name);
5949 }
5950 else {
5951 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005952
Willy Tarreau58975672014-04-24 21:13:57 +02005953 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5954 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005955 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5956 s30tob64((date.tv_sec+3) >> 2,
5957 trash.area + trash.data);
5958 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005959
Willy Tarreau58975672014-04-24 21:13:57 +02005960 if (s->be->cookie_maxlife) {
5961 /* emit first_date, which is either the original one or
5962 * the current date.
5963 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005964 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005965 s30tob64(txn->cookie_first_date ?
5966 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005967 (date.tv_sec+3) >> 2,
5968 trash.area + trash.data);
5969 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005970 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005971 }
Willy Tarreau58975672014-04-24 21:13:57 +02005972 chunk_appendf(&trash, "; path=/");
5973 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005974
Willy Tarreau58975672014-04-24 21:13:57 +02005975 if (s->be->cookie_domain)
5976 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005977
Willy Tarreau58975672014-04-24 21:13:57 +02005978 if (s->be->ck_opts & PR_CK_HTTPONLY)
5979 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005980
Willy Tarreau58975672014-04-24 21:13:57 +02005981 if (s->be->ck_opts & PR_CK_SECURE)
5982 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005983
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005984 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005985 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005986
Willy Tarreau58975672014-04-24 21:13:57 +02005987 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005988 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005989 /* the server did not change, only the date was updated */
5990 txn->flags |= TX_SCK_UPDATED;
5991 else
5992 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005993
Willy Tarreau58975672014-04-24 21:13:57 +02005994 /* Here, we will tell an eventual cache on the client side that we don't
5995 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5996 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5997 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005998 */
Willy Tarreau58975672014-04-24 21:13:57 +02005999 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006000
Willy Tarreau58975672014-04-24 21:13:57 +02006001 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006002
Willy Tarreau58975672014-04-24 21:13:57 +02006003 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6004 "Cache-control: private", 22) < 0))
6005 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006006 }
Willy Tarreau58975672014-04-24 21:13:57 +02006007 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006008
Willy Tarreau58975672014-04-24 21:13:57 +02006009 /*
6010 * Check if result will be cacheable with a cookie.
6011 * We'll block the response if security checks have caught
6012 * nasty things such as a cacheable cookie.
6013 */
6014 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6015 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6016 (s->be->options & PR_O_CHK_CACHE)) {
6017 /* we're in presence of a cacheable response containing
6018 * a set-cookie header. We'll block it as requested by
6019 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006020 */
Willy Tarreau58975672014-04-24 21:13:57 +02006021 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006022 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01006023
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006024 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
6025 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006026 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02006027 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006028
Christopher Faulet767a84b2017-11-24 16:50:31 +01006029 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6030 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02006031 send_log(s->be, LOG_ALERT,
6032 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6033 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6034 goto return_srv_prx_502;
6035 }
Willy Tarreau03945942009-12-22 16:50:27 +01006036
Willy Tarreau70730dd2014-04-24 18:06:27 +02006037 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006038 /*
6039 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6040 * If an "Upgrade" token is found, the header is left untouched in order
6041 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006042 * if anything but "Upgrade" is present in the Connection header. We don't
6043 * want to touch any 101 response either since it's switching to another
6044 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006045 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006046 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006047 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006048 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006049 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6050 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006051
Willy Tarreau58975672014-04-24 21:13:57 +02006052 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6053 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6054 /* we want a keep-alive response here. Keep-alive header
6055 * required if either side is not 1.1.
6056 */
6057 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6058 want_flags |= TX_CON_KAL_SET;
6059 }
6060 else {
6061 /* we want a close response here. Close header required if
6062 * the server is 1.1, regardless of the client.
6063 */
6064 if (msg->flags & HTTP_MSGF_VER_11)
6065 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006066 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006067
Willy Tarreau58975672014-04-24 21:13:57 +02006068 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6069 http_change_connection_header(txn, msg, want_flags);
6070 }
6071
6072 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02006073 /* Always enter in the body analyzer */
6074 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
6075 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006076
Willy Tarreau58975672014-04-24 21:13:57 +02006077 /* if the user wants to log as soon as possible, without counting
6078 * bytes from the server, then this is the right moment. We have
6079 * to temporarily assign bytes_out to log what we currently have.
6080 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006081 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006082 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6083 s->logs.bytes_out = txn->rsp.eoh;
6084 s->do_log(s);
6085 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006086 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006087 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006088}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006089
Willy Tarreaud98cf932009-12-27 22:54:55 +01006090/* This function is an analyser which forwards response body (including chunk
6091 * sizes if any). It is called as soon as we must forward, even if we forward
6092 * zero byte. The only situation where it must not be called is when we're in
6093 * tunnel mode and we want to forward till the close. It's used both to forward
6094 * remaining data and to resync after end of body. It expects the msg_state to
6095 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006096 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006097 *
6098 * It is capable of compressing response data both in content-length mode and
6099 * in chunked mode. The state machines follows different flows depending on
6100 * whether content-length and chunked modes are used, since there are no
6101 * trailers in content-length :
6102 *
6103 * chk-mode cl-mode
6104 * ,----- BODY -----.
6105 * / \
6106 * V size > 0 V chk-mode
6107 * .--> SIZE -------------> DATA -------------> CRLF
6108 * | | size == 0 | last byte |
6109 * | v final crlf v inspected |
6110 * | TRAILERS -----------> DONE |
6111 * | |
6112 * `----------------------------------------------'
6113 *
6114 * Compression only happens in the DATA state, and must be flushed in final
6115 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6116 * is performed at once on final states for all bytes parsed, or when leaving
6117 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006118 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006119int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006120{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006121 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006122 struct http_txn *txn = s->txn;
6123 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006124 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006125
Christopher Faulet45073512018-07-20 10:16:29 +02006126 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Christopher Faulet814d2702017-03-30 11:33:44 +02006127 now_ms, __FUNCTION__,
6128 s,
6129 res,
6130 res->rex, res->wex,
6131 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02006132 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02006133 res->analysers);
6134
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006135 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6136 return 0;
6137
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006138 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02006139 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006140 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006141 /* Output closed while we were sending data. We must abort and
6142 * wake the other side up.
6143 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006144 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006145 msg->msg_state = HTTP_MSG_ERROR;
6146 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006147 return 1;
6148 }
6149
Willy Tarreau4fe41902010-06-07 22:27:41 +02006150 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006151 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006152
Christopher Fauletd7c91962015-04-30 11:48:27 +02006153 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006154 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6155 ? HTTP_MSG_CHUNK_SIZE
6156 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006157 }
6158
Willy Tarreauefdf0942014-04-24 20:08:57 +02006159 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006160 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006161 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006162 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006163 }
6164
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006165 if (msg->msg_state < HTTP_MSG_DONE) {
6166 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6167 ? http_msg_forward_chunked_body(s, msg)
6168 : http_msg_forward_body(s, msg));
6169 if (!ret)
6170 goto missing_data_or_waiting;
6171 if (ret < 0)
6172 goto return_bad_res;
6173 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006174
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006175 /* other states, DONE...TUNNEL */
6176 /* for keep-alive we don't want to forward closes on DONE */
6177 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6178 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6179 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006180
Christopher Faulet894da4c2017-07-18 11:29:07 +02006181 http_resync_states(s);
6182 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006183 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6184 if (res->flags & CF_SHUTW) {
6185 /* response errors are most likely due to the
6186 * client aborting the transfer. */
6187 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006188 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006189 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006190 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 +01006191 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006192 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006193 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006194 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006195 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006196
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006197 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006198 if (res->flags & CF_SHUTW)
6199 goto aborted_xfer;
6200
6201 /* stop waiting for data if the input is closed before the end. If the
6202 * client side was already closed, it means that the client has aborted,
6203 * so we don't want to count this as a server abort. Otherwise it's a
6204 * server abort.
6205 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006206 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006207 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006208 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006209 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02006210 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006211 if (!(s->flags & SF_ERR_MASK))
6212 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006213 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006214 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006215 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006216 goto return_bad_res_stats_ok;
6217 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006218 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006219
Willy Tarreau40dba092010-03-04 18:14:51 +01006220 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006221 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006222 goto return_bad_res;
6223
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006224 /* When TE: chunked is used, we need to get there again to parse
6225 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006226 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6227 * or if there are filters registered on the stream, we don't want to
6228 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006229 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006230 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006231 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006232 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6233 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006234 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006235
Willy Tarreau5c620922011-05-11 19:56:11 +02006236 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006237 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006238 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006239 * modes are already handled by the stream sock layer. We must not do
6240 * this in content-length mode because it could present the MSG_MORE
6241 * flag with the last block of forwarded data, which would cause an
6242 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006243 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006244 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006245 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006246
Willy Tarreau87b09662015-04-03 00:22:06 +02006247 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006248 return 0;
6249
Willy Tarreau40dba092010-03-04 18:14:51 +01006250 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006251 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006252 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006253 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006254
6255 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006256 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006257 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006258 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006259 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006260 res->analysers &= AN_RES_FLT_END;
6261 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 +01006262 if (objt_server(s->target))
6263 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006264
Willy Tarreaue7dff022015-04-03 01:14:29 +02006265 if (!(s->flags & SF_ERR_MASK))
6266 s->flags |= SF_ERR_PRXCOND;
6267 if (!(s->flags & SF_FINST_MASK))
6268 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006269 return 0;
6270
6271 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006272 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006273 txn->rsp.msg_state = HTTP_MSG_ERROR;
6274 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006275 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006276 res->analysers &= AN_RES_FLT_END;
6277 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 +01006278
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006279 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6280 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006281 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006282 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006283
Willy Tarreaue7dff022015-04-03 01:14:29 +02006284 if (!(s->flags & SF_ERR_MASK))
6285 s->flags |= SF_ERR_CLICL;
6286 if (!(s->flags & SF_FINST_MASK))
6287 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006288 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006289}
6290
6291
6292static inline int
6293http_msg_forward_body(struct stream *s, struct http_msg *msg)
6294{
6295 struct channel *chn = msg->chn;
6296 int ret;
6297
6298 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6299
6300 if (msg->msg_state == HTTP_MSG_ENDING)
6301 goto ending;
6302
6303 /* Neither content-length, nor transfer-encoding was found, so we must
6304 * read the body until the server connection is closed. In that case, we
6305 * eat data as they come. Of course, this happens for response only. */
6306 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006307 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006308 msg->chunk_len += len;
6309 msg->body_len += len;
6310 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006311 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02006312 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01006313 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006314 msg->next += ret;
6315 msg->chunk_len -= ret;
6316 if (msg->chunk_len) {
6317 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006318 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006319 chn->flags |= CF_WAKE_WRITE;
6320 goto missing_data_or_waiting;
6321 }
6322
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006323 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6324 * always set for a request. */
6325 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6326 /* The server still sending data that should be filtered */
6327 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6328 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006329 msg->msg_state = HTTP_MSG_TUNNEL;
6330 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006331 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006332
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006333 msg->msg_state = HTTP_MSG_ENDING;
6334
6335 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006336 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006337 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006338 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6339 /* default_ret */ msg->next,
6340 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006341 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006342 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006343 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6344 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006345 if (msg->next)
6346 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006347
Christopher Fauletda02e172015-12-04 09:25:05 +01006348 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6349 /* default_ret */ 1,
6350 /* on_error */ goto error,
6351 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006352 if (msg->msg_state == HTTP_MSG_ENDING)
6353 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006354 return 1;
6355
6356 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006357 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006358 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6359 /* default_ret */ msg->next,
6360 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006361 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006362 msg->next -= ret;
6363 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6364 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006365 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006366 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006367 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006368 return 0;
6369 error:
6370 return -1;
6371}
6372
6373static inline int
6374http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6375{
6376 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006377 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006378 int ret;
6379
6380 /* Here we have the guarantee to be in one of the following state:
6381 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6382 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6383
6384 switch_states:
6385 switch (msg->msg_state) {
6386 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006387 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02006388 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01006389 /* on_error */ goto error);
6390 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006391 msg->chunk_len -= ret;
6392 if (msg->chunk_len) {
6393 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006394 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006395 chn->flags |= CF_WAKE_WRITE;
6396 goto missing_data_or_waiting;
6397 }
6398
6399 /* nothing left to forward for this chunk*/
6400 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6401 /* fall through for HTTP_MSG_CHUNK_CRLF */
6402
6403 case HTTP_MSG_CHUNK_CRLF:
6404 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006405 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006406 if (ret == 0)
6407 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006408 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006409 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02006410 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006411 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006412 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006413 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006414 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006415 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6416 /* fall through for HTTP_MSG_CHUNK_SIZE */
6417
6418 case HTTP_MSG_CHUNK_SIZE:
6419 /* read the chunk size and assign it to ->chunk_len,
6420 * then set ->next to point to the body and switch to
6421 * DATA or TRAILERS state.
6422 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006423 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006424 if (ret == 0)
6425 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006426 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006427 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006428 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006429 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006430 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006431 }
6432
6433 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006434 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006435 msg->chunk_len = chunk;
6436 msg->body_len += chunk;
6437
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006438 if (msg->chunk_len) {
6439 msg->msg_state = HTTP_MSG_DATA;
6440 goto switch_states;
6441 }
6442 msg->msg_state = HTTP_MSG_TRAILERS;
6443 /* fall through for HTTP_MSG_TRAILERS */
6444
6445 case HTTP_MSG_TRAILERS:
6446 ret = http_forward_trailers(msg);
6447 if (ret < 0)
6448 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006449 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6450 /* default_ret */ 1,
6451 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006452 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006453 if (!ret)
6454 goto missing_data_or_waiting;
6455 break;
6456
6457 case HTTP_MSG_ENDING:
6458 goto ending;
6459
6460 default:
6461 /* This should no happen in this function */
6462 goto error;
6463 }
6464
6465 msg->msg_state = HTTP_MSG_ENDING;
6466 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006467 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006468 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006469 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006470 /* default_ret */ msg->next,
6471 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006472 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006473 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006474 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6475 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006476 if (msg->next)
6477 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006478
Christopher Fauletda02e172015-12-04 09:25:05 +01006479 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006480 /* default_ret */ 1,
6481 /* on_error */ goto error,
6482 /* on_wait */ goto waiting);
6483 msg->msg_state = HTTP_MSG_DONE;
6484 return 1;
6485
6486 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006487 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006488 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006489 /* default_ret */ msg->next,
6490 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006491 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006492 msg->next -= ret;
6493 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6494 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006495 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006496 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006497 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006498 return 0;
6499
6500 chunk_parsing_error:
6501 if (msg->err_pos >= 0) {
6502 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006503 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006504 msg->msg_state, strm_fe(s));
6505 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006506 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006507 msg, msg->msg_state, s->be);
6508 }
6509 error:
6510 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006511}
6512
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006514/* Iterate the same filter through all request headers.
6515 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516 * Since it can manage the switch to another backend, it updates the per-proxy
6517 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006518 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006519int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006520{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006521 char *cur_ptr, *cur_end, *cur_next;
6522 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006523 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006524 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006525 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006527 last_hdr = 0;
6528
Willy Tarreauf37954d2018-06-15 18:31:02 +02006529 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006530 old_idx = 0;
6531
6532 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006533 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006534 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006535 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006536 (exp->action == ACT_ALLOW ||
6537 exp->action == ACT_DENY ||
6538 exp->action == ACT_TARPIT))
6539 return 0;
6540
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006541 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006542 if (!cur_idx)
6543 break;
6544
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006545 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006546 cur_ptr = cur_next;
6547 cur_end = cur_ptr + cur_hdr->len;
6548 cur_next = cur_end + cur_hdr->cr + 1;
6549
6550 /* Now we have one header between cur_ptr and cur_end,
6551 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006552 */
6553
Willy Tarreau15a53a42015-01-21 13:39:42 +01006554 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006555 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006556 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006557 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006558 last_hdr = 1;
6559 break;
6560
6561 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006562 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006563 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006564 break;
6565
6566 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006567 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006568 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006569 break;
6570
6571 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006572 len = exp_replace(trash.area,
6573 trash.size, cur_ptr,
6574 exp->replace, pmatch);
6575 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006576 return -1;
6577
Willy Tarreau6e27be12018-08-22 04:46:47 +02006578 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006580 /* FIXME: if the user adds a newline in the replacement, the
6581 * index will not be recalculated for now, and the new line
6582 * will not be counted as a new header.
6583 */
6584
6585 cur_end += delta;
6586 cur_next += delta;
6587 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006588 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006589 break;
6590
6591 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006592 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006593 cur_next += delta;
6594
Willy Tarreaufa355d42009-11-29 18:12:29 +01006595 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006596 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6597 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006598 cur_hdr->len = 0;
6599 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006600 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006601 break;
6602
6603 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006604 }
6605
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006606 /* keep the link from this header to next one in case of later
6607 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006608 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006609 old_idx = cur_idx;
6610 }
6611 return 0;
6612}
6613
6614
6615/* Apply the filter to the request line.
6616 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6617 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006618 * Since it can manage the switch to another backend, it updates the per-proxy
6619 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006620 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006621int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006622{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006623 char *cur_ptr, *cur_end;
6624 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006625 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006626 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006627
Willy Tarreau3d300592007-03-18 18:34:41 +01006628 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006629 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006630 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006631 (exp->action == ACT_ALLOW ||
6632 exp->action == ACT_DENY ||
6633 exp->action == ACT_TARPIT))
6634 return 0;
6635 else if (exp->action == ACT_REMOVE)
6636 return 0;
6637
6638 done = 0;
6639
Willy Tarreauf37954d2018-06-15 18:31:02 +02006640 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006641 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006642
6643 /* Now we have the request line between cur_ptr and cur_end */
6644
Willy Tarreau15a53a42015-01-21 13:39:42 +01006645 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006646 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006647 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006648 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006649 done = 1;
6650 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006652 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006653 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006654 done = 1;
6655 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006656
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006657 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006658 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006659 done = 1;
6660 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006662 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006663 len = exp_replace(trash.area, trash.size,
6664 cur_ptr, exp->replace, pmatch);
6665 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006666 return -1;
6667
Willy Tarreau6e27be12018-08-22 04:46:47 +02006668 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006670 /* FIXME: if the user adds a newline in the replacement, the
6671 * index will not be recalculated for now, and the new line
6672 * will not be counted as a new header.
6673 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006674
Willy Tarreaufa355d42009-11-29 18:12:29 +01006675 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006676 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006677 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006678 HTTP_MSG_RQMETH,
6679 cur_ptr, cur_end + 1,
6680 NULL, NULL);
6681 if (unlikely(!cur_end))
6682 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006683
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006684 /* we have a full request and we know that we have either a CR
6685 * or an LF at <ptr>.
6686 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006687 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6688 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006689 /* there is no point trying this regex on headers */
6690 return 1;
6691 }
6692 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006693 return done;
6694}
Willy Tarreau97de6242006-12-27 17:18:38 +01006695
Willy Tarreau58f10d72006-12-04 02:26:12 +01006696
Willy Tarreau58f10d72006-12-04 02:26:12 +01006697
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006698/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006699 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006700 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701 * unparsable request. Since it can manage the switch to another backend, it
6702 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006703 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006704int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006705{
Willy Tarreau192252e2015-04-04 01:47:55 +02006706 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006707 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006708 struct hdr_exp *exp;
6709
6710 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006711 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006712
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006713 /*
6714 * The interleaving of transformations and verdicts
6715 * makes it difficult to decide to continue or stop
6716 * the evaluation.
6717 */
6718
Willy Tarreau6c123b12010-01-28 20:22:06 +01006719 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6720 break;
6721
Willy Tarreau3d300592007-03-18 18:34:41 +01006722 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006723 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006724 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006725 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006726
6727 /* if this filter had a condition, evaluate it now and skip to
6728 * next filter if the condition does not match.
6729 */
6730 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006731 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006732 ret = acl_pass(ret);
6733 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6734 ret = !ret;
6735
6736 if (!ret)
6737 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006738 }
6739
6740 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006741 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006742 if (unlikely(ret < 0))
6743 return -1;
6744
6745 if (likely(ret == 0)) {
6746 /* The filter did not match the request, it can be
6747 * iterated through all headers.
6748 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006749 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6750 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006751 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006752 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006753 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754}
6755
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006756
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006757/* Find the end of a cookie value contained between <s> and <e>. It works the
6758 * same way as with headers above except that the semi-colon also ends a token.
6759 * See RFC2965 for more information. Note that it requires a valid header to
6760 * return a valid result.
6761 */
6762char *find_cookie_value_end(char *s, const char *e)
6763{
6764 int quoted, qdpair;
6765
6766 quoted = qdpair = 0;
6767 for (; s < e; s++) {
6768 if (qdpair) qdpair = 0;
6769 else if (quoted) {
6770 if (*s == '\\') qdpair = 1;
6771 else if (*s == '"') quoted = 0;
6772 }
6773 else if (*s == '"') quoted = 1;
6774 else if (*s == ',' || *s == ';') return s;
6775 }
6776 return s;
6777}
6778
6779/* Delete a value in a header between delimiters <from> and <next> in buffer
6780 * <buf>. The number of characters displaced is returned, and the pointer to
6781 * the first delimiter is updated if required. The function tries as much as
6782 * possible to respect the following principles :
6783 * - replace <from> delimiter by the <next> one unless <from> points to a
6784 * colon, in which case <next> is simply removed
6785 * - set exactly one space character after the new first delimiter, unless
6786 * there are not enough characters in the block being moved to do so.
6787 * - remove unneeded spaces before the previous delimiter and after the new
6788 * one.
6789 *
6790 * It is the caller's responsibility to ensure that :
6791 * - <from> points to a valid delimiter or the colon ;
6792 * - <next> points to a valid delimiter or the final CR/LF ;
6793 * - there are non-space chars before <from> ;
6794 * - there is a CR/LF at or after <next>.
6795 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006796int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006797{
6798 char *prev = *from;
6799
6800 if (*prev == ':') {
6801 /* We're removing the first value, preserve the colon and add a
6802 * space if possible.
6803 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006804 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006805 next++;
6806 prev++;
6807 if (prev < next)
6808 *prev++ = ' ';
6809
Willy Tarreau2235b262016-11-05 15:50:20 +01006810 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006811 next++;
6812 } else {
6813 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006814 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006815 prev--;
6816 *from = prev;
6817
6818 /* copy the delimiter and if possible a space if we're
6819 * not at the end of the line.
6820 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006821 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006822 *prev++ = *next++;
6823 if (prev + 1 < next)
6824 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006825 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006826 next++;
6827 }
6828 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006829 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006830}
6831
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006832/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006833 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006834 * desirable to call it only when needed. This code is quite complex because
6835 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6836 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006837 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006838void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006839{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006840 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006841 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006842 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006843 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006844 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6845 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006846
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006847 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006848 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006849 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006850
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006851 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006852 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006853 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006854
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006855 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006856 hdr_beg = hdr_next;
6857 hdr_end = hdr_beg + cur_hdr->len;
6858 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006859
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006860 /* We have one full header between hdr_beg and hdr_end, and the
6861 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006862 * "Cookie:" headers.
6863 */
6864
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006865 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006866 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006867 old_idx = cur_idx;
6868 continue;
6869 }
6870
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006871 del_from = NULL; /* nothing to be deleted */
6872 preserve_hdr = 0; /* assume we may kill the whole header */
6873
Willy Tarreau58f10d72006-12-04 02:26:12 +01006874 /* Now look for cookies. Conforming to RFC2109, we have to support
6875 * attributes whose name begin with a '$', and associate them with
6876 * the right cookie, if we want to delete this cookie.
6877 * So there are 3 cases for each cookie read :
6878 * 1) it's a special attribute, beginning with a '$' : ignore it.
6879 * 2) it's a server id cookie that we *MAY* want to delete : save
6880 * some pointers on it (last semi-colon, beginning of cookie...)
6881 * 3) it's an application cookie : we *MAY* have to delete a previous
6882 * "special" cookie.
6883 * At the end of loop, if a "special" cookie remains, we may have to
6884 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006885 * *MUST* delete it.
6886 *
6887 * Note: RFC2965 is unclear about the processing of spaces around
6888 * the equal sign in the ATTR=VALUE form. A careful inspection of
6889 * the RFC explicitly allows spaces before it, and not within the
6890 * tokens (attrs or values). An inspection of RFC2109 allows that
6891 * too but section 10.1.3 lets one think that spaces may be allowed
6892 * after the equal sign too, resulting in some (rare) buggy
6893 * implementations trying to do that. So let's do what servers do.
6894 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6895 * allowed quoted strings in values, with any possible character
6896 * after a backslash, including control chars and delimitors, which
6897 * causes parsing to become ambiguous. Browsers also allow spaces
6898 * within values even without quotes.
6899 *
6900 * We have to keep multiple pointers in order to support cookie
6901 * removal at the beginning, middle or end of header without
6902 * corrupting the header. All of these headers are valid :
6903 *
6904 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6905 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6906 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6907 * | | | | | | | | |
6908 * | | | | | | | | hdr_end <--+
6909 * | | | | | | | +--> next
6910 * | | | | | | +----> val_end
6911 * | | | | | +-----------> val_beg
6912 * | | | | +--------------> equal
6913 * | | | +----------------> att_end
6914 * | | +---------------------> att_beg
6915 * | +--------------------------> prev
6916 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006917 */
6918
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006919 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6920 /* Iterate through all cookies on this line */
6921
6922 /* find att_beg */
6923 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006924 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006925 att_beg++;
6926
6927 /* find att_end : this is the first character after the last non
6928 * space before the equal. It may be equal to hdr_end.
6929 */
6930 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006931
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006932 while (equal < hdr_end) {
6933 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006934 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006935 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006936 continue;
6937 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006938 }
6939
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006940 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6941 * is between <att_beg> and <equal>, both may be identical.
6942 */
6943
6944 /* look for end of cookie if there is an equal sign */
6945 if (equal < hdr_end && *equal == '=') {
6946 /* look for the beginning of the value */
6947 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006948 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006949 val_beg++;
6950
6951 /* find the end of the value, respecting quotes */
6952 next = find_cookie_value_end(val_beg, hdr_end);
6953
6954 /* make val_end point to the first white space or delimitor after the value */
6955 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006956 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006957 val_end--;
6958 } else {
6959 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006960 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006961
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006962 /* We have nothing to do with attributes beginning with '$'. However,
6963 * they will automatically be removed if a header before them is removed,
6964 * since they're supposed to be linked together.
6965 */
6966 if (*att_beg == '$')
6967 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006968
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006969 /* Ignore cookies with no equal sign */
6970 if (equal == next) {
6971 /* This is not our cookie, so we must preserve it. But if we already
6972 * scheduled another cookie for removal, we cannot remove the
6973 * complete header, but we can remove the previous block itself.
6974 */
6975 preserve_hdr = 1;
6976 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006977 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006978 val_end += delta;
6979 next += delta;
6980 hdr_end += delta;
6981 hdr_next += delta;
6982 cur_hdr->len += delta;
6983 http_msg_move_end(&txn->req, delta);
6984 prev = del_from;
6985 del_from = NULL;
6986 }
6987 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006988 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006989
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006990 /* if there are spaces around the equal sign, we need to
6991 * strip them otherwise we'll get trouble for cookie captures,
6992 * or even for rewrites. Since this happens extremely rarely,
6993 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006994 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006995 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6996 int stripped_before = 0;
6997 int stripped_after = 0;
6998
6999 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007000 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007001 equal += stripped_before;
7002 val_beg += stripped_before;
7003 }
7004
7005 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007006 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007007 val_beg += stripped_after;
7008 stripped_before += stripped_after;
7009 }
7010
7011 val_end += stripped_before;
7012 next += stripped_before;
7013 hdr_end += stripped_before;
7014 hdr_next += stripped_before;
7015 cur_hdr->len += stripped_before;
7016 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007017 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007018 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007019
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007020 /* First, let's see if we want to capture this cookie. We check
7021 * that we don't already have a client side cookie, because we
7022 * can only capture one. Also as an optimisation, we ignore
7023 * cookies shorter than the declared name.
7024 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007025 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7026 (val_end - att_beg >= sess->fe->capture_namelen) &&
7027 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007028 int log_len = val_end - att_beg;
7029
Willy Tarreaubafbe012017-11-24 17:34:44 +01007030 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007031 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007032 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007033 if (log_len > sess->fe->capture_len)
7034 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007035 memcpy(txn->cli_cookie, att_beg, log_len);
7036 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007037 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007038 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007039
Willy Tarreaubca99692010-10-06 19:25:55 +02007040 /* Persistence cookies in passive, rewrite or insert mode have the
7041 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007042 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007043 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007044 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007045 * For cookies in prefix mode, the form is :
7046 *
7047 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007048 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007049 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7050 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7051 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007052 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007053
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007054 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7055 * have the server ID between val_beg and delim, and the original cookie between
7056 * delim+1 and val_end. Otherwise, delim==val_end :
7057 *
7058 * Cookie: NAME=SRV; # in all but prefix modes
7059 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7060 * | || || | |+-> next
7061 * | || || | +--> val_end
7062 * | || || +---------> delim
7063 * | || |+------------> val_beg
7064 * | || +-------------> att_end = equal
7065 * | |+-----------------> att_beg
7066 * | +------------------> prev
7067 * +-------------------------> hdr_beg
7068 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007069
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007070 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007071 for (delim = val_beg; delim < val_end; delim++)
7072 if (*delim == COOKIE_DELIM)
7073 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007074 } else {
7075 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007076 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007077 /* Now check if the cookie contains a date field, which would
7078 * appear after a vertical bar ('|') just after the server name
7079 * and before the delimiter.
7080 */
7081 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7082 if (vbar1) {
7083 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007084 * right is the last seen date. It is a base64 encoded
7085 * 30-bit value representing the UNIX date since the
7086 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007087 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007088 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007089 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007090 if (val_end - vbar1 >= 5) {
7091 val = b64tos30(vbar1);
7092 if (val > 0)
7093 txn->cookie_last_date = val << 2;
7094 }
7095 /* look for a second vertical bar */
7096 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7097 if (vbar1 && (val_end - vbar1 > 5)) {
7098 val = b64tos30(vbar1 + 1);
7099 if (val > 0)
7100 txn->cookie_first_date = val << 2;
7101 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007102 }
7103 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104
Willy Tarreauf64d1412010-10-07 20:06:11 +02007105 /* if the cookie has an expiration date and the proxy wants to check
7106 * it, then we do that now. We first check if the cookie is too old,
7107 * then only if it has expired. We detect strict overflow because the
7108 * time resolution here is not great (4 seconds). Cookies with dates
7109 * in the future are ignored if their offset is beyond one day. This
7110 * allows an admin to fix timezone issues without expiring everyone
7111 * and at the same time avoids keeping unwanted side effects for too
7112 * long.
7113 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007114 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7115 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007116 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007117 txn->flags &= ~TX_CK_MASK;
7118 txn->flags |= TX_CK_OLD;
7119 delim = val_beg; // let's pretend we have not found the cookie
7120 txn->cookie_first_date = 0;
7121 txn->cookie_last_date = 0;
7122 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007123 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7124 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007125 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007126 txn->flags &= ~TX_CK_MASK;
7127 txn->flags |= TX_CK_EXPIRED;
7128 delim = val_beg; // let's pretend we have not found the cookie
7129 txn->cookie_first_date = 0;
7130 txn->cookie_last_date = 0;
7131 }
7132
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007133 /* Here, we'll look for the first running server which supports the cookie.
7134 * This allows to share a same cookie between several servers, for example
7135 * to dedicate backup servers to specific servers only.
7136 * However, to prevent clients from sticking to cookie-less backup server
7137 * when they have incidentely learned an empty cookie, we simply ignore
7138 * empty cookies and mark them as invalid.
7139 * The same behaviour is applied when persistence must be ignored.
7140 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007141 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007142 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007143
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007144 while (srv) {
7145 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7146 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007147 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007148 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007149 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007150 /* we found the server and we can use it */
7151 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007152 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007153 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007154 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007155 break;
7156 } else {
7157 /* we found a server, but it's down,
7158 * mark it as such and go on in case
7159 * another one is available.
7160 */
7161 txn->flags &= ~TX_CK_MASK;
7162 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007163 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007164 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007165 srv = srv->next;
7166 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007167
Willy Tarreauf64d1412010-10-07 20:06:11 +02007168 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007169 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007170 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007171 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007172 txn->flags |= TX_CK_UNUSED;
7173 else
7174 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007175 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007176
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007177 /* depending on the cookie mode, we may have to either :
7178 * - delete the complete cookie if we're in insert+indirect mode, so that
7179 * the server never sees it ;
7180 * - remove the server id from the cookie value, and tag the cookie as an
7181 * application cookie so that it does not get accidentely removed later,
7182 * if we're in cookie prefix mode
7183 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007184 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007185 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007186
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007187 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007188 val_end += delta;
7189 next += delta;
7190 hdr_end += delta;
7191 hdr_next += delta;
7192 cur_hdr->len += delta;
7193 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007194
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007195 del_from = NULL;
7196 preserve_hdr = 1; /* we want to keep this cookie */
7197 }
7198 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007199 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007200 del_from = prev;
7201 }
7202 } else {
7203 /* This is not our cookie, so we must preserve it. But if we already
7204 * scheduled another cookie for removal, we cannot remove the
7205 * complete header, but we can remove the previous block itself.
7206 */
7207 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007208
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007209 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007210 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007211 if (att_beg >= del_from)
7212 att_beg += delta;
7213 if (att_end >= del_from)
7214 att_end += delta;
7215 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007216 val_end += delta;
7217 next += delta;
7218 hdr_end += delta;
7219 hdr_next += delta;
7220 cur_hdr->len += delta;
7221 http_msg_move_end(&txn->req, delta);
7222 prev = del_from;
7223 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007224 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007225 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007226
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007227 /* continue with next cookie on this header line */
7228 att_beg = next;
7229 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007230
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007231 /* There are no more cookies on this line.
7232 * We may still have one (or several) marked for deletion at the
7233 * end of the line. We must do this now in two ways :
7234 * - if some cookies must be preserved, we only delete from the
7235 * mark to the end of line ;
7236 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007237 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007238 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007239 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007240 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007241 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007242 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007243 cur_hdr->len += delta;
7244 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007245 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007246
7247 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007248 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7249 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007250 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007251 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007252 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007253 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007254 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007255 }
7256
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007257 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007258 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007259 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007260}
7261
7262
Willy Tarreaua15645d2007-03-18 16:22:39 +01007263/* Iterate the same filter through all response headers contained in <rtr>.
7264 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7265 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007266int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007267{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268 char *cur_ptr, *cur_end, *cur_next;
7269 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007270 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02007272 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273
7274 last_hdr = 0;
7275
Willy Tarreauf37954d2018-06-15 18:31:02 +02007276 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007277 old_idx = 0;
7278
7279 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007280 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007282 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007283 (exp->action == ACT_ALLOW ||
7284 exp->action == ACT_DENY))
7285 return 0;
7286
7287 cur_idx = txn->hdr_idx.v[old_idx].next;
7288 if (!cur_idx)
7289 break;
7290
7291 cur_hdr = &txn->hdr_idx.v[cur_idx];
7292 cur_ptr = cur_next;
7293 cur_end = cur_ptr + cur_hdr->len;
7294 cur_next = cur_end + cur_hdr->cr + 1;
7295
7296 /* Now we have one header between cur_ptr and cur_end,
7297 * and the next header starts at cur_next.
7298 */
7299
Willy Tarreau15a53a42015-01-21 13:39:42 +01007300 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301 switch (exp->action) {
7302 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007303 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007304 last_hdr = 1;
7305 break;
7306
7307 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007308 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007309 last_hdr = 1;
7310 break;
7311
7312 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02007313 len = exp_replace(trash.area,
7314 trash.size, cur_ptr,
7315 exp->replace, pmatch);
7316 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06007317 return -1;
7318
Willy Tarreau6e27be12018-08-22 04:46:47 +02007319 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
7320
Willy Tarreaua15645d2007-03-18 16:22:39 +01007321 /* FIXME: if the user adds a newline in the replacement, the
7322 * index will not be recalculated for now, and the new line
7323 * will not be counted as a new header.
7324 */
7325
7326 cur_end += delta;
7327 cur_next += delta;
7328 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007329 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330 break;
7331
7332 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007333 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007334 cur_next += delta;
7335
Willy Tarreaufa355d42009-11-29 18:12:29 +01007336 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7338 txn->hdr_idx.used--;
7339 cur_hdr->len = 0;
7340 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007341 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007342 break;
7343
7344 }
7345 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007346
7347 /* keep the link from this header to next one in case of later
7348 * removal of next header.
7349 */
7350 old_idx = cur_idx;
7351 }
7352 return 0;
7353}
7354
7355
7356/* Apply the filter to the status line in the response buffer <rtr>.
7357 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7358 * or -1 if a replacement resulted in an invalid status line.
7359 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007360int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007361{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007362 char *cur_ptr, *cur_end;
7363 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007364 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02007365 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007366
Willy Tarreau3d300592007-03-18 18:34:41 +01007367 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007368 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007369 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007370 (exp->action == ACT_ALLOW ||
7371 exp->action == ACT_DENY))
7372 return 0;
7373 else if (exp->action == ACT_REMOVE)
7374 return 0;
7375
7376 done = 0;
7377
Willy Tarreauf37954d2018-06-15 18:31:02 +02007378 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007379 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007380
7381 /* Now we have the status line between cur_ptr and cur_end */
7382
Willy Tarreau15a53a42015-01-21 13:39:42 +01007383 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007384 switch (exp->action) {
7385 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007386 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007387 done = 1;
7388 break;
7389
7390 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007391 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007392 done = 1;
7393 break;
7394
7395 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02007396 len = exp_replace(trash.area, trash.size,
7397 cur_ptr, exp->replace, pmatch);
7398 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06007399 return -1;
7400
Willy Tarreau6e27be12018-08-22 04:46:47 +02007401 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
7402
Willy Tarreaua15645d2007-03-18 16:22:39 +01007403 /* FIXME: if the user adds a newline in the replacement, the
7404 * index will not be recalculated for now, and the new line
7405 * will not be counted as a new header.
7406 */
7407
Willy Tarreaufa355d42009-11-29 18:12:29 +01007408 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007409 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007410 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007411 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412 cur_ptr, cur_end + 1,
7413 NULL, NULL);
7414 if (unlikely(!cur_end))
7415 return -1;
7416
7417 /* we have a full respnse and we know that we have either a CR
7418 * or an LF at <ptr>.
7419 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007420 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007421 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007422 /* there is no point trying this regex on headers */
7423 return 1;
7424 }
7425 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007426 return done;
7427}
7428
7429
7430
7431/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007432 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007433 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7434 * unparsable response.
7435 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007436int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007437{
Willy Tarreau192252e2015-04-04 01:47:55 +02007438 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007439 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007440 struct hdr_exp *exp;
7441
7442 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007443 int ret;
7444
7445 /*
7446 * The interleaving of transformations and verdicts
7447 * makes it difficult to decide to continue or stop
7448 * the evaluation.
7449 */
7450
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007451 if (txn->flags & TX_SVDENY)
7452 break;
7453
Willy Tarreau3d300592007-03-18 18:34:41 +01007454 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007455 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7456 exp->action == ACT_PASS)) {
7457 exp = exp->next;
7458 continue;
7459 }
7460
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007461 /* if this filter had a condition, evaluate it now and skip to
7462 * next filter if the condition does not match.
7463 */
7464 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007465 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007466 ret = acl_pass(ret);
7467 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7468 ret = !ret;
7469 if (!ret)
7470 continue;
7471 }
7472
Willy Tarreaua15645d2007-03-18 16:22:39 +01007473 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007474 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007475 if (unlikely(ret < 0))
7476 return -1;
7477
7478 if (likely(ret == 0)) {
7479 /* The filter did not match the response, it can be
7480 * iterated through all headers.
7481 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007482 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7483 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007484 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007485 }
7486 return 0;
7487}
7488
7489
Willy Tarreaua15645d2007-03-18 16:22:39 +01007490/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007491 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007492 * desirable to call it only when needed. This function is also used when we
7493 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007494 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007495void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007496{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007497 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007498 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007499 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007500 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007501 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007502 char *hdr_beg, *hdr_end, *hdr_next;
7503 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007504
Willy Tarreaua15645d2007-03-18 16:22:39 +01007505 /* Iterate through the headers.
7506 * we start with the start line.
7507 */
7508 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007509 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007510
7511 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7512 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007513 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007514
7515 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007516 hdr_beg = hdr_next;
7517 hdr_end = hdr_beg + cur_hdr->len;
7518 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007519
Willy Tarreau24581ba2010-08-31 22:39:35 +02007520 /* We have one full header between hdr_beg and hdr_end, and the
7521 * next header starts at hdr_next. We're only interested in
7522 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007523 */
7524
Willy Tarreau24581ba2010-08-31 22:39:35 +02007525 is_cookie2 = 0;
7526 prev = hdr_beg + 10;
7527 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007528 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007529 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7530 if (!val) {
7531 old_idx = cur_idx;
7532 continue;
7533 }
7534 is_cookie2 = 1;
7535 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007536 }
7537
Willy Tarreau24581ba2010-08-31 22:39:35 +02007538 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7539 * <prev> points to the colon.
7540 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007541 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007542
Willy Tarreau24581ba2010-08-31 22:39:35 +02007543 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7544 * check-cache is enabled) and we are not interested in checking
7545 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007546 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007547 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007548 return;
7549
Willy Tarreau24581ba2010-08-31 22:39:35 +02007550 /* OK so now we know we have to process this response cookie.
7551 * The format of the Set-Cookie header is slightly different
7552 * from the format of the Cookie header in that it does not
7553 * support the comma as a cookie delimiter (thus the header
7554 * cannot be folded) because the Expires attribute described in
7555 * the original Netscape's spec may contain an unquoted date
7556 * with a comma inside. We have to live with this because
7557 * many browsers don't support Max-Age and some browsers don't
7558 * support quoted strings. However the Set-Cookie2 header is
7559 * clean.
7560 *
7561 * We have to keep multiple pointers in order to support cookie
7562 * removal at the beginning, middle or end of header without
7563 * corrupting the header (in case of set-cookie2). A special
7564 * pointer, <scav> points to the beginning of the set-cookie-av
7565 * fields after the first semi-colon. The <next> pointer points
7566 * either to the end of line (set-cookie) or next unquoted comma
7567 * (set-cookie2). All of these headers are valid :
7568 *
7569 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7570 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7571 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7572 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7573 * | | | | | | | | | |
7574 * | | | | | | | | +-> next hdr_end <--+
7575 * | | | | | | | +------------> scav
7576 * | | | | | | +--------------> val_end
7577 * | | | | | +--------------------> val_beg
7578 * | | | | +----------------------> equal
7579 * | | | +------------------------> att_end
7580 * | | +----------------------------> att_beg
7581 * | +------------------------------> prev
7582 * +-----------------------------------------> hdr_beg
7583 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007584
Willy Tarreau24581ba2010-08-31 22:39:35 +02007585 for (; prev < hdr_end; prev = next) {
7586 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007587
Willy Tarreau24581ba2010-08-31 22:39:35 +02007588 /* find att_beg */
7589 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007590 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007591 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007592
Willy Tarreau24581ba2010-08-31 22:39:35 +02007593 /* find att_end : this is the first character after the last non
7594 * space before the equal. It may be equal to hdr_end.
7595 */
7596 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007597
Willy Tarreau24581ba2010-08-31 22:39:35 +02007598 while (equal < hdr_end) {
7599 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7600 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007601 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007602 continue;
7603 att_end = equal;
7604 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007605
Willy Tarreau24581ba2010-08-31 22:39:35 +02007606 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7607 * is between <att_beg> and <equal>, both may be identical.
7608 */
7609
7610 /* look for end of cookie if there is an equal sign */
7611 if (equal < hdr_end && *equal == '=') {
7612 /* look for the beginning of the value */
7613 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007614 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007615 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007616
Willy Tarreau24581ba2010-08-31 22:39:35 +02007617 /* find the end of the value, respecting quotes */
7618 next = find_cookie_value_end(val_beg, hdr_end);
7619
7620 /* make val_end point to the first white space or delimitor after the value */
7621 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007622 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007623 val_end--;
7624 } else {
7625 /* <equal> points to next comma, semi-colon or EOL */
7626 val_beg = val_end = next = equal;
7627 }
7628
7629 if (next < hdr_end) {
7630 /* Set-Cookie2 supports multiple cookies, and <next> points to
7631 * a colon or semi-colon before the end. So skip all attr-value
7632 * pairs and look for the next comma. For Set-Cookie, since
7633 * commas are permitted in values, skip to the end.
7634 */
7635 if (is_cookie2)
7636 next = find_hdr_value_end(next, hdr_end);
7637 else
7638 next = hdr_end;
7639 }
7640
7641 /* Now everything is as on the diagram above */
7642
7643 /* Ignore cookies with no equal sign */
7644 if (equal == val_end)
7645 continue;
7646
7647 /* If there are spaces around the equal sign, we need to
7648 * strip them otherwise we'll get trouble for cookie captures,
7649 * or even for rewrites. Since this happens extremely rarely,
7650 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007651 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007652 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7653 int stripped_before = 0;
7654 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007655
Willy Tarreau24581ba2010-08-31 22:39:35 +02007656 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007657 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007658 equal += stripped_before;
7659 val_beg += stripped_before;
7660 }
7661
7662 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007663 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007664 val_beg += stripped_after;
7665 stripped_before += stripped_after;
7666 }
7667
7668 val_end += stripped_before;
7669 next += stripped_before;
7670 hdr_end += stripped_before;
7671 hdr_next += stripped_before;
7672 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007673 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007674 }
7675
7676 /* First, let's see if we want to capture this cookie. We check
7677 * that we don't already have a server side cookie, because we
7678 * can only capture one. Also as an optimisation, we ignore
7679 * cookies shorter than the declared name.
7680 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007681 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007682 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007683 (val_end - att_beg >= sess->fe->capture_namelen) &&
7684 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007685 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007686 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007687 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007688 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007689 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007690 if (log_len > sess->fe->capture_len)
7691 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007692 memcpy(txn->srv_cookie, att_beg, log_len);
7693 txn->srv_cookie[log_len] = 0;
7694 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007695 }
7696
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007697 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007698 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007699 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007700 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7701 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007702 /* assume passive cookie by default */
7703 txn->flags &= ~TX_SCK_MASK;
7704 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007705
7706 /* If the cookie is in insert mode on a known server, we'll delete
7707 * this occurrence because we'll insert another one later.
7708 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007709 * a direct access.
7710 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007711 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007712 /* The "preserve" flag was set, we don't want to touch the
7713 * server's cookie.
7714 */
7715 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007716 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007717 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007718 /* this cookie must be deleted */
7719 if (*prev == ':' && next == hdr_end) {
7720 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007721 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007722 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7723 txn->hdr_idx.used--;
7724 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007725 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007726 hdr_next += delta;
7727 http_msg_move_end(&txn->rsp, delta);
7728 /* note: while both invalid now, <next> and <hdr_end>
7729 * are still equal, so the for() will stop as expected.
7730 */
7731 } else {
7732 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007733 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007734 next = prev;
7735 hdr_end += delta;
7736 hdr_next += delta;
7737 cur_hdr->len += delta;
7738 http_msg_move_end(&txn->rsp, delta);
7739 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007740 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007741 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007742 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007743 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007744 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007745 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007746 * with this server since we know it.
7747 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007748 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007749 next += delta;
7750 hdr_end += delta;
7751 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007752 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007753 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007754
Willy Tarreauf1348312010-10-07 15:54:11 +02007755 txn->flags &= ~TX_SCK_MASK;
7756 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007757 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007758 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007759 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007760 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007761 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007762 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007763 next += delta;
7764 hdr_end += delta;
7765 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007766 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007767 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007768
Willy Tarreau827aee92011-03-10 16:55:02 +01007769 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007770 txn->flags &= ~TX_SCK_MASK;
7771 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007772 }
7773 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007774 /* that's done for this cookie, check the next one on the same
7775 * line when next != hdr_end (only if is_cookie2).
7776 */
7777 }
7778 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007780 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007781}
7782
7783
Willy Tarreaua15645d2007-03-18 16:22:39 +01007784/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007785 * Parses the Cache-Control and Pragma request header fields to determine if
7786 * the request may be served from the cache and/or if it is cacheable. Updates
7787 * s->txn->flags.
7788 */
7789void check_request_for_cacheability(struct stream *s, struct channel *chn)
7790{
7791 struct http_txn *txn = s->txn;
7792 char *p1, *p2;
7793 char *cur_ptr, *cur_end, *cur_next;
7794 int pragma_found;
7795 int cc_found;
7796 int cur_idx;
7797
7798 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7799 return; /* nothing more to do here */
7800
7801 cur_idx = 0;
7802 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007803 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007804
7805 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7806 struct hdr_idx_elem *cur_hdr;
7807 int val;
7808
7809 cur_hdr = &txn->hdr_idx.v[cur_idx];
7810 cur_ptr = cur_next;
7811 cur_end = cur_ptr + cur_hdr->len;
7812 cur_next = cur_end + cur_hdr->cr + 1;
7813
7814 /* We have one full header between cur_ptr and cur_end, and the
7815 * next header starts at cur_next.
7816 */
7817
7818 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7819 if (val) {
7820 if ((cur_end - (cur_ptr + val) >= 8) &&
7821 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7822 pragma_found = 1;
7823 continue;
7824 }
7825 }
7826
William Lallemand8a16fe02018-05-22 11:04:33 +02007827 /* Don't use the cache and don't try to store if we found the
7828 * Authorization header */
7829 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7830 if (val) {
7831 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7832 txn->flags |= TX_CACHE_IGNORE;
7833 continue;
7834 }
7835
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007836 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7837 if (!val)
7838 continue;
7839
7840 /* OK, right now we know we have a cache-control header at cur_ptr */
7841 cc_found = 1;
7842 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7843
7844 if (p1 >= cur_end) /* no more info */
7845 continue;
7846
7847 /* p1 is at the beginning of the value */
7848 p2 = p1;
7849 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7850 p2++;
7851
7852 /* we have a complete value between p1 and p2. We don't check the
7853 * values after max-age, max-stale nor min-fresh, we simply don't
7854 * use the cache when they're specified.
7855 */
7856 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7857 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7858 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7859 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7860 txn->flags |= TX_CACHE_IGNORE;
7861 continue;
7862 }
7863
7864 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7865 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7866 continue;
7867 }
7868 }
7869
7870 /* RFC7234#5.4:
7871 * When the Cache-Control header field is also present and
7872 * understood in a request, Pragma is ignored.
7873 * When the Cache-Control header field is not present in a
7874 * request, caches MUST consider the no-cache request
7875 * pragma-directive as having the same effect as if
7876 * "Cache-Control: no-cache" were present.
7877 */
7878 if (!cc_found && pragma_found)
7879 txn->flags |= TX_CACHE_IGNORE;
7880}
7881
7882/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007883 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007884 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007885void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007886{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007887 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007888 char *p1, *p2;
7889
7890 char *cur_ptr, *cur_end, *cur_next;
7891 int cur_idx;
7892
Willy Tarreau12b32f22017-12-21 16:08:09 +01007893 if (txn->status < 200) {
7894 /* do not try to cache interim responses! */
7895 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007896 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007897 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007898
7899 /* Iterate through the headers.
7900 * we start with the start line.
7901 */
7902 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007903 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007904
7905 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7906 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007907 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007908
7909 cur_hdr = &txn->hdr_idx.v[cur_idx];
7910 cur_ptr = cur_next;
7911 cur_end = cur_ptr + cur_hdr->len;
7912 cur_next = cur_end + cur_hdr->cr + 1;
7913
7914 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007915 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007916 */
7917
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007918 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7919 if (val) {
7920 if ((cur_end - (cur_ptr + val) >= 8) &&
7921 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7922 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7923 return;
7924 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007925 }
7926
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007927 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7928 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007929 continue;
7930
7931 /* OK, right now we know we have a cache-control header at cur_ptr */
7932
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007933 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007934
7935 if (p1 >= cur_end) /* no more info */
7936 continue;
7937
7938 /* p1 is at the beginning of the value */
7939 p2 = p1;
7940
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007941 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007942 p2++;
7943
7944 /* we have a complete value between p1 and p2 */
7945 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007946 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7947 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7948 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7949 continue;
7950 }
7951
Willy Tarreaua15645d2007-03-18 16:22:39 +01007952 /* we have something of the form no-cache="set-cookie" */
7953 if ((cur_end - p1 >= 21) &&
7954 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7955 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007956 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007957 continue;
7958 }
7959
7960 /* OK, so we know that either p2 points to the end of string or to a comma */
7961 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007962 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007963 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007964 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007965 return;
7966 }
7967
7968 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007969 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007970 continue;
7971 }
7972 }
7973}
7974
Willy Tarreau58f10d72006-12-04 02:26:12 +01007975
Willy Tarreaub2513902006-12-17 14:52:38 +01007976/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007977 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007978 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007979 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007980 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007981 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007982 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007983 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007984 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007985int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007986{
7987 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007988 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007989 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007990
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007991 if (!uri_auth)
7992 return 0;
7993
Cyril Bonté70be45d2010-10-12 00:14:35 +02007994 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007995 return 0;
7996
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007997 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007998 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007999 return 0;
8000
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008001 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008002 return 0;
8003
Willy Tarreaub2513902006-12-17 14:52:38 +01008004 return 1;
8005}
8006
Willy Tarreau4076a152009-04-02 15:18:36 +02008007/*
8008 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008009 * By default it tries to report the error position as msg->err_pos. However if
8010 * this one is not set, it will then report msg->next, which is the last known
8011 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008012 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008013 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02008014void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008015 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02008016 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008017{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008018 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008019 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008020 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008021
Christopher Faulet2a944ee2017-11-07 10:42:54 +01008022 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf37954d2018-06-15 18:31:02 +02008023 es->len = MIN(ci_data(chn), global.tune.bufsize);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008024 len1 = b_wrap(&chn->buf) - ci_head(chn);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008025 len1 = MIN(len1, es->len);
8026 len2 = es->len - len1; /* remaining data if buffer wraps */
8027
Willy Tarreauf3764b72016-03-31 13:45:10 +02008028 if (!es->buf)
8029 es->buf = malloc(global.tune.bufsize);
8030
8031 if (es->buf) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02008032 memcpy(es->buf, ci_head(chn), len1);
Willy Tarreauf3764b72016-03-31 13:45:10 +02008033 if (len2)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008034 memcpy(es->buf + len1, b_orig(&chn->buf), len2);
Willy Tarreauf3764b72016-03-31 13:45:10 +02008035 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008036
Willy Tarreau4076a152009-04-02 15:18:36 +02008037 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008038 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008039 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008040 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008041
Willy Tarreau4076a152009-04-02 15:18:36 +02008042 es->when = date; // user-visible date
8043 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008044 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008045 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008046 if (objt_conn(sess->origin))
8047 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008048 else
8049 memset(&es->src, 0, sizeof(es->src));
8050
Willy Tarreau078272e2010-12-12 12:46:33 +01008051 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008052 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008053 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008054 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008055 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008056 es->m_flags = msg->flags;
Willy Tarreauf37954d2018-06-15 18:31:02 +02008057 es->b_out = co_data(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008058 es->b_wrap = b_wrap(&chn->buf) - ci_head(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008059 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008060 es->m_clen = msg->chunk_len;
8061 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01008062 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02008063}
Willy Tarreaub2513902006-12-17 14:52:38 +01008064
Willy Tarreau294c4732011-12-16 21:35:50 +01008065/* 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
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008072 * -1. The value fetch stops at commas, so this function is suited for use with
8073 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008074 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008075 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008076unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008077 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008078 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008079{
Willy Tarreau294c4732011-12-16 21:35:50 +01008080 struct hdr_ctx local_ctx;
8081 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008082 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008083 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008084 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008085
Willy Tarreau294c4732011-12-16 21:35:50 +01008086 if (!ctx) {
8087 local_ctx.idx = 0;
8088 ctx = &local_ctx;
8089 }
8090
Willy Tarreaubce70882009-09-07 11:51:47 +02008091 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008092 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008093 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008094 occ--;
8095 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008096 *vptr = ctx->line + ctx->val;
8097 *vlen = ctx->vlen;
8098 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008099 }
8100 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008101 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008102 }
8103
8104 /* negative occurrence, we scan all the list then walk back */
8105 if (-occ > MAX_HDR_HISTORY)
8106 return 0;
8107
Willy Tarreau294c4732011-12-16 21:35:50 +01008108 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02008109 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008110 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8111 len_hist[hist_ptr] = ctx->vlen;
8112 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008113 hist_ptr = 0;
8114 found++;
8115 }
8116 if (-occ > found)
8117 return 0;
8118 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008119 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8120 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8121 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008122 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008123 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008124 if (hist_ptr >= MAX_HDR_HISTORY)
8125 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008126 *vptr = ptr_hist[hist_ptr];
8127 *vlen = len_hist[hist_ptr];
8128 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008129}
8130
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008131/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8132 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8133 * performed over the whole headers. Otherwise it must contain a valid header
8134 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8135 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8136 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8137 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8138 * -1. This function differs from http_get_hdr() in that it only returns full
8139 * line header values and does not stop at commas.
8140 * The return value is 0 if nothing was found, or non-zero otherwise.
8141 */
8142unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8143 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008144 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008145{
8146 struct hdr_ctx local_ctx;
8147 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008148 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008149 unsigned int hist_ptr;
8150 int found;
8151
8152 if (!ctx) {
8153 local_ctx.idx = 0;
8154 ctx = &local_ctx;
8155 }
8156
8157 if (occ >= 0) {
8158 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008159 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008160 occ--;
8161 if (occ <= 0) {
8162 *vptr = ctx->line + ctx->val;
8163 *vlen = ctx->vlen;
8164 return 1;
8165 }
8166 }
8167 return 0;
8168 }
8169
8170 /* negative occurrence, we scan all the list then walk back */
8171 if (-occ > MAX_HDR_HISTORY)
8172 return 0;
8173
8174 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02008175 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008176 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8177 len_hist[hist_ptr] = ctx->vlen;
8178 if (++hist_ptr >= MAX_HDR_HISTORY)
8179 hist_ptr = 0;
8180 found++;
8181 }
8182 if (-occ > found)
8183 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008184
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008185 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008186 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8187 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8188 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008189 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008190 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008191 if (hist_ptr >= MAX_HDR_HISTORY)
8192 hist_ptr -= MAX_HDR_HISTORY;
8193 *vptr = ptr_hist[hist_ptr];
8194 *vlen = len_hist[hist_ptr];
8195 return 1;
8196}
8197
Willy Tarreaubaaee002006-06-26 02:48:02 +02008198/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008199 * Print a debug line with a header. Always stop at the first CR or LF char,
8200 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8201 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008202 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008203void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008204{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008205 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008206 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008207
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008208 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008209 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008210 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02008211 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 +02008212
8213 for (max = 0; start + max < end; max++)
8214 if (start[max] == '\r' || start[max] == '\n')
8215 break;
8216
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008217 UBOUND(max, trash.size - trash.data - 3);
8218 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
8219 trash.area[trash.data++] = '\n';
8220 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008221}
8222
Willy Tarreaueee5b512015-04-03 23:46:31 +02008223
8224/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8225 * The hdr_idx is allocated as well. In case of allocation failure, everything
8226 * allocated is freed and NULL is returned. Otherwise the new transaction is
8227 * assigned to the stream and returned.
8228 */
8229struct http_txn *http_alloc_txn(struct stream *s)
8230{
8231 struct http_txn *txn = s->txn;
8232
8233 if (txn)
8234 return txn;
8235
Willy Tarreaubafbe012017-11-24 17:34:44 +01008236 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008237 if (!txn)
8238 return txn;
8239
8240 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008241 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008242 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008243 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008244 return NULL;
8245 }
8246
8247 s->txn = txn;
8248 return txn;
8249}
8250
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008251void http_txn_reset_req(struct http_txn *txn)
8252{
8253 txn->req.flags = 0;
8254 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8255 txn->req.next = 0;
8256 txn->req.chunk_len = 0LL;
8257 txn->req.body_len = 0LL;
8258 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8259}
8260
8261void http_txn_reset_res(struct http_txn *txn)
8262{
8263 txn->rsp.flags = 0;
8264 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8265 txn->rsp.next = 0;
8266 txn->rsp.chunk_len = 0LL;
8267 txn->rsp.body_len = 0LL;
8268 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8269}
8270
Willy Tarreau0937bc42009-12-22 15:03:09 +01008271/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008272 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008273 * the required fields are properly allocated and that we only need to (re)init
8274 * them. This should be used before processing any new request.
8275 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008276void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008277{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008278 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008279 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008280
8281 txn->flags = 0;
8282 txn->status = -1;
8283
Willy Tarreauf64d1412010-10-07 20:06:11 +02008284 txn->cookie_first_date = 0;
8285 txn->cookie_last_date = 0;
8286
Willy Tarreaueee5b512015-04-03 23:46:31 +02008287 txn->srv_cookie = NULL;
8288 txn->cli_cookie = NULL;
8289 txn->uri = NULL;
8290
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008291 http_txn_reset_req(txn);
8292 http_txn_reset_res(txn);
8293
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008294 txn->req.chn = &s->req;
8295 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008296
8297 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008298
8299 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8300 if (fe->options2 & PR_O2_REQBUG_OK)
8301 txn->req.err_pos = -1; /* let buggy requests pass */
8302
Willy Tarreau0937bc42009-12-22 15:03:09 +01008303 if (txn->hdr_idx.v)
8304 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008305
8306 vars_init(&s->vars_txn, SCOPE_TXN);
8307 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008308}
8309
8310/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008311void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008312{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008313 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008314 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008315
8316 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008317 pool_free(pool_head_requri, txn->uri);
8318 pool_free(pool_head_capture, txn->cli_cookie);
8319 pool_free(pool_head_capture, txn->srv_cookie);
8320 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008321
William Lallemanda73203e2012-03-12 12:48:57 +01008322 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008323 txn->uri = NULL;
8324 txn->srv_cookie = NULL;
8325 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008326
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008327 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008328 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008329 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008330 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008331 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008332 }
8333
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008334 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008335 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008336 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008337 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008338 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008339 }
8340
Willy Tarreau6204cd92016-03-10 16:33:04 +01008341 vars_prune(&s->vars_txn, s->sess, s);
8342 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008343}
8344
8345/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008346void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008347{
8348 http_end_txn(s);
8349 http_init_txn(s);
8350
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008351 /* reinitialise the current rule list pointer to NULL. We are sure that
8352 * any rulelist match the NULL pointer.
8353 */
8354 s->current_rule_list = NULL;
8355
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008356 s->be = strm_fe(s);
8357 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008358 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008359 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008360 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008361 /* re-init store persistence */
8362 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008363 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008364
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008365 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008366
Willy Tarreau739cfba2010-01-25 23:11:14 +01008367 /* We must trim any excess data from the response buffer, because we
8368 * may have blocked an invalid response from a server that we don't
8369 * want to accidentely forward once we disable the analysers, nor do
8370 * we want those data to come along with next response. A typical
8371 * example of such data would be from a buggy server responding to
8372 * a HEAD with some data, or sending more than the advertised
8373 * content-length.
8374 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008375 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008376 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01008377
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008378 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02008379 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008380
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008381 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008382 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008383
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008384 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008385 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008386
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008387 s->req.rex = TICK_ETERNITY;
8388 s->req.wex = TICK_ETERNITY;
8389 s->req.analyse_exp = TICK_ETERNITY;
8390 s->res.rex = TICK_ETERNITY;
8391 s->res.wex = TICK_ETERNITY;
8392 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008393 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008394}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008395
Sasha Pachev218f0642014-06-16 12:05:59 -06008396void free_http_res_rules(struct list *r)
8397{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008398 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008399
8400 list_for_each_entry_safe(pr, tr, r, list) {
8401 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008402 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008403 free(pr);
8404 }
8405}
8406
8407void free_http_req_rules(struct list *r)
8408{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008409 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008410
8411 list_for_each_entry_safe(pr, tr, r, list) {
8412 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008413 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008414 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008415
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008416 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008417 free(pr);
8418 }
8419}
8420
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008421/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008422struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008423{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008424 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008425 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008426 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008427 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008428
Vincent Bernat02779b62016-04-03 13:48:43 +02008429 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008430 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008431 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008432 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008433 }
8434
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008435 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008436 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008437 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008438 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008439 int code;
8440 int hc;
8441
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008442 if (!strcmp(args[0], "tarpit")) {
8443 rule->action = ACT_HTTP_REQ_TARPIT;
8444 rule->deny_status = HTTP_ERR_500;
8445 }
8446 else {
8447 rule->action = ACT_ACTION_DENY;
8448 rule->deny_status = HTTP_ERR_403;
8449 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008450 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008451 if (strcmp(args[cur_arg], "deny_status") == 0) {
8452 cur_arg++;
8453 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008454 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8455 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008456 goto out_err;
8457 }
8458
8459 code = atol(args[cur_arg]);
8460 cur_arg++;
8461 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8462 if (http_err_codes[hc] == code) {
8463 rule->deny_status = hc;
8464 break;
8465 }
8466 }
8467
8468 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008469 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8470 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008471 }
8472 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008473 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008474 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008475 cur_arg = 1;
8476
8477 while(*args[cur_arg]) {
8478 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008479 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008480 cur_arg+=2;
8481 continue;
8482 } else
8483 break;
8484 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008485 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008486 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008487 cur_arg = 1;
8488
8489 if (!*args[cur_arg] ||
8490 (*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 +01008491 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8492 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008493 goto out_err;
8494 }
8495 rule->arg.nice = atoi(args[cur_arg]);
8496 if (rule->arg.nice < -1024)
8497 rule->arg.nice = -1024;
8498 else if (rule->arg.nice > 1024)
8499 rule->arg.nice = 1024;
8500 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008501 } else if (!strcmp(args[0], "set-tos")) {
8502#ifdef IP_TOS
8503 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008504 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008505 cur_arg = 1;
8506
8507 if (!*args[cur_arg] ||
8508 (*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 +01008509 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8510 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008511 goto out_err;
8512 }
8513
8514 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8515 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008516 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8517 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008518 goto out_err;
8519 }
8520 cur_arg++;
8521#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008522 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 +02008523 goto out_err;
8524#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008525 } else if (!strcmp(args[0], "set-mark")) {
8526#ifdef SO_MARK
8527 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008528 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008529 cur_arg = 1;
8530
8531 if (!*args[cur_arg] ||
8532 (*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 +01008533 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8534 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008535 goto out_err;
8536 }
8537
8538 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8539 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008540 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8541 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008542 goto out_err;
8543 }
8544 cur_arg++;
8545 global.last_checks |= LSTCHK_NETADM;
8546#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008547 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 +02008548 goto out_err;
8549#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008550 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008551 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008552 cur_arg = 1;
8553
8554 if (!*args[cur_arg] ||
8555 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8556 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008557 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8558 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008559 goto out_err;
8560 }
8561 if (strcmp(args[cur_arg], "silent") == 0)
8562 rule->arg.loglevel = -1;
8563 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8564 goto bad_log_level;
8565 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008566 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008567 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008568 cur_arg = 1;
8569
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008570 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8571 (*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 +01008572 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8573 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008574 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008575 }
8576
8577 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8578 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8579 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008580
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008581 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008582 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008583 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 +01008584 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008585 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8586 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008587 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008588 goto out_err;
8589 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008590 free(proxy->conf.lfs_file);
8591 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8592 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008593 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008594 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008595 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008596 cur_arg = 1;
8597
8598 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008599 (*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 +01008600 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8601 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008602 goto out_err;
8603 }
8604
8605 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8606 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8607 LIST_INIT(&rule->arg.hdr_add.fmt);
8608
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008609 error = NULL;
8610 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008611 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8612 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008613 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008614 goto out_err;
8615 }
8616
8617 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008618 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008619 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 +01008620 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008621 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8622 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008623 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008624 goto out_err;
8625 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008626
8627 free(proxy->conf.lfs_file);
8628 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8629 proxy->conf.lfs_line = proxy->conf.args.line;
8630 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008631 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008632 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008633 cur_arg = 1;
8634
8635 if (!*args[cur_arg] ||
8636 (*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 +01008637 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8638 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008639 goto out_err;
8640 }
8641
8642 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8643 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8644
8645 proxy->conf.args.ctx = ARGC_HRQ;
8646 free(proxy->conf.lfs_file);
8647 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8648 proxy->conf.lfs_line = proxy->conf.args.line;
8649 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008650 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008651 struct sample_expr *expr;
8652 unsigned int where;
8653 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008654 unsigned int tsc_num;
8655 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008656
8657 cur_arg = 1;
8658 proxy->conf.args.ctx = ARGC_TRK;
8659
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008660 tsc_num_str = &args[0][8];
8661 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8662 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8663 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8664 free(err);
8665 goto out_err;
8666 }
8667
Willy Tarreau09448f72014-06-25 18:12:15 +02008668 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8669 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008670 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8671 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008672 free(err);
8673 goto out_err;
8674 }
8675
8676 where = 0;
8677 if (proxy->cap & PR_CAP_FE)
8678 where |= SMP_VAL_FE_HRQ_HDR;
8679 if (proxy->cap & PR_CAP_BE)
8680 where |= SMP_VAL_BE_HRQ_HDR;
8681
8682 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008683 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8684 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8685 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8686 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008687 free(expr);
8688 goto out_err;
8689 }
8690
8691 if (strcmp(args[cur_arg], "table") == 0) {
8692 cur_arg++;
8693 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008694 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8695 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008696 free(expr);
8697 goto out_err;
8698 }
8699 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008700 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008701 cur_arg++;
8702 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008703 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008704 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008705 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008706 } else if (strcmp(args[0], "redirect") == 0) {
8707 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008708 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008709
Willy Tarreaube4653b2015-05-28 15:26:58 +02008710 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008711 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8712 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008713 goto out_err;
8714 }
8715
8716 /* this redirect rule might already contain a parsed condition which
8717 * we'll pass to the http-request rule.
8718 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008719 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008720 rule->arg.redir = redir;
8721 rule->cond = redir->cond;
8722 redir->cond = NULL;
8723 cur_arg = 2;
8724 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008725 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8726 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008727 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008728 /*
8729 * '+ 8' for 'add-acl('
8730 * '- 9' for 'add-acl(' + trailing ')'
8731 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008732 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008733
8734 cur_arg = 1;
8735
8736 if (!*args[cur_arg] ||
8737 (*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 +01008738 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8739 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008740 goto out_err;
8741 }
8742
8743 LIST_INIT(&rule->arg.map.key);
8744 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008745 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008746 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008747 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008748 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8749 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008750 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008751 goto out_err;
8752 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008753 free(proxy->conf.lfs_file);
8754 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8755 proxy->conf.lfs_line = proxy->conf.args.line;
8756 cur_arg += 1;
8757 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8758 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008759 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008760 /*
8761 * '+ 8' for 'del-acl('
8762 * '- 9' for 'del-acl(' + trailing ')'
8763 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008764 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008765
8766 cur_arg = 1;
8767
8768 if (!*args[cur_arg] ||
8769 (*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 +01008770 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8771 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008772 goto out_err;
8773 }
8774
8775 LIST_INIT(&rule->arg.map.key);
8776 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008777 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008778 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008779 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008780 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8781 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008782 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008783 goto out_err;
8784 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008785 free(proxy->conf.lfs_file);
8786 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8787 proxy->conf.lfs_line = proxy->conf.args.line;
8788 cur_arg += 1;
8789 } else if (strncmp(args[0], "del-map", 7) == 0) {
8790 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008791 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008792 /*
8793 * '+ 8' for 'del-map('
8794 * '- 9' for 'del-map(' + trailing ')'
8795 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008796 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008797
8798 cur_arg = 1;
8799
8800 if (!*args[cur_arg] ||
8801 (*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 +01008802 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8803 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008804 goto out_err;
8805 }
8806
8807 LIST_INIT(&rule->arg.map.key);
8808 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008809 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008810 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008811 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008812 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8813 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008814 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008815 goto out_err;
8816 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008817 free(proxy->conf.lfs_file);
8818 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8819 proxy->conf.lfs_line = proxy->conf.args.line;
8820 cur_arg += 1;
8821 } else if (strncmp(args[0], "set-map", 7) == 0) {
8822 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008823 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008824 /*
8825 * '+ 8' for 'set-map('
8826 * '- 9' for 'set-map(' + trailing ')'
8827 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008828 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008829
8830 cur_arg = 1;
8831
8832 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8833 (*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 +01008834 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8835 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008836 goto out_err;
8837 }
8838
8839 LIST_INIT(&rule->arg.map.key);
8840 LIST_INIT(&rule->arg.map.value);
8841 proxy->conf.args.ctx = ARGC_HRQ;
8842
8843 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008844 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008845 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008846 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008847 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8848 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008849 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008850 goto out_err;
8851 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008852
8853 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008854 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008855 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 +01008856 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008857 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8858 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008859 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008860 goto out_err;
8861 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008862 free(proxy->conf.lfs_file);
8863 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8864 proxy->conf.lfs_line = proxy->conf.args.line;
8865
8866 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008867 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8868 char *errmsg = NULL;
8869 cur_arg = 1;
8870 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008871 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008872 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008873 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008874 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8875 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008876 free(errmsg);
8877 goto out_err;
8878 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008879 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008880 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008881 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8882 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8883 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8884 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008885 file, linenum, *trash.area ? ", " : "", trash.area,
8886 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008887 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008888 }
8889
8890 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8891 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008892 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008893
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008894 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008895 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8896 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008897 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008898 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008899 }
8900 rule->cond = cond;
8901 }
8902 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008903 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8904 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8905 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008906 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008907 }
8908
8909 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008910 out_err:
8911 free(rule);
8912 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008913}
8914
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008915/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008916struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008917{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008918 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008919 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008920 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008921 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008922
8923 rule = calloc(1, sizeof(*rule));
8924 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008925 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008926 goto out_err;
8927 }
8928
8929 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008930 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008931 cur_arg = 1;
8932 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008933 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008934 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008935 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008936 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008937 cur_arg = 1;
8938
8939 if (!*args[cur_arg] ||
8940 (*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 +01008941 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8942 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008943 goto out_err;
8944 }
8945 rule->arg.nice = atoi(args[cur_arg]);
8946 if (rule->arg.nice < -1024)
8947 rule->arg.nice = -1024;
8948 else if (rule->arg.nice > 1024)
8949 rule->arg.nice = 1024;
8950 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008951 } else if (!strcmp(args[0], "set-tos")) {
8952#ifdef IP_TOS
8953 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008954 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008955 cur_arg = 1;
8956
8957 if (!*args[cur_arg] ||
8958 (*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 +01008959 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8960 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008961 goto out_err;
8962 }
8963
8964 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8965 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008966 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8967 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008968 goto out_err;
8969 }
8970 cur_arg++;
8971#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008972 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 +02008973 goto out_err;
8974#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008975 } else if (!strcmp(args[0], "set-mark")) {
8976#ifdef SO_MARK
8977 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008978 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008979 cur_arg = 1;
8980
8981 if (!*args[cur_arg] ||
8982 (*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 +01008983 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8984 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008985 goto out_err;
8986 }
8987
8988 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8989 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008990 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8991 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008992 goto out_err;
8993 }
8994 cur_arg++;
8995 global.last_checks |= LSTCHK_NETADM;
8996#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008997 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 +02008998 goto out_err;
8999#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009000 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009001 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009002 cur_arg = 1;
9003
9004 if (!*args[cur_arg] ||
9005 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9006 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01009007 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9008 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009009 goto out_err;
9010 }
9011 if (strcmp(args[cur_arg], "silent") == 0)
9012 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009013 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009014 goto bad_log_level;
9015 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009016 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009017 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009018 cur_arg = 1;
9019
9020 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9021 (*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 +01009022 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9023 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009024 goto out_err;
9025 }
9026
9027 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9028 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9029 LIST_INIT(&rule->arg.hdr_add.fmt);
9030
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009031 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009032 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009033 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 +01009034 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009035 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9036 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009037 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009038 goto out_err;
9039 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009040 free(proxy->conf.lfs_file);
9041 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9042 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009043 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009044 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009045 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009046 cur_arg = 1;
9047
9048 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009049 (*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 +01009050 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
9051 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06009052 goto out_err;
9053 }
9054
9055 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9056 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9057 LIST_INIT(&rule->arg.hdr_add.fmt);
9058
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009059 error = NULL;
9060 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009061 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9062 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009063 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009064 goto out_err;
9065 }
9066
9067 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009068 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009069 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 +01009070 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009071 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9072 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009073 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009074 goto out_err;
9075 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009076
9077 free(proxy->conf.lfs_file);
9078 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9079 proxy->conf.lfs_line = proxy->conf.args.line;
9080 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009081 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009082 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009083 cur_arg = 1;
9084
9085 if (!*args[cur_arg] ||
9086 (*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 +01009087 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9088 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009089 goto out_err;
9090 }
9091
9092 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9093 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9094
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009095 proxy->conf.args.ctx = ARGC_HRS;
9096 free(proxy->conf.lfs_file);
9097 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9098 proxy->conf.lfs_line = proxy->conf.args.line;
9099 cur_arg += 1;
9100 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9101 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009102 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009103 /*
9104 * '+ 8' for 'add-acl('
9105 * '- 9' for 'add-acl(' + trailing ')'
9106 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009107 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009108
9109 cur_arg = 1;
9110
9111 if (!*args[cur_arg] ||
9112 (*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 +01009113 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9114 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009115 goto out_err;
9116 }
9117
9118 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009119 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009120 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009121 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009122 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009123 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9124 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009125 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009126 goto out_err;
9127 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009128 free(proxy->conf.lfs_file);
9129 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9130 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009131
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009132 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009133 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9134 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009135 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009136 /*
9137 * '+ 8' for 'del-acl('
9138 * '- 9' for 'del-acl(' + trailing ')'
9139 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009140 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009141
9142 cur_arg = 1;
9143
9144 if (!*args[cur_arg] ||
9145 (*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 +01009146 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9147 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009148 goto out_err;
9149 }
9150
9151 LIST_INIT(&rule->arg.map.key);
9152 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009153 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009154 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009155 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009156 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9157 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009158 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009159 goto out_err;
9160 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009161 free(proxy->conf.lfs_file);
9162 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9163 proxy->conf.lfs_line = proxy->conf.args.line;
9164 cur_arg += 1;
9165 } else if (strncmp(args[0], "del-map", 7) == 0) {
9166 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009167 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009168 /*
9169 * '+ 8' for 'del-map('
9170 * '- 9' for 'del-map(' + trailing ')'
9171 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009172 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009173
9174 cur_arg = 1;
9175
9176 if (!*args[cur_arg] ||
9177 (*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 +01009178 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9179 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009180 goto out_err;
9181 }
9182
9183 LIST_INIT(&rule->arg.map.key);
9184 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009185 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009186 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009187 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009188 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9189 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009190 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009191 goto out_err;
9192 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009193 free(proxy->conf.lfs_file);
9194 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9195 proxy->conf.lfs_line = proxy->conf.args.line;
9196 cur_arg += 1;
9197 } else if (strncmp(args[0], "set-map", 7) == 0) {
9198 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009199 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009200 /*
9201 * '+ 8' for 'set-map('
9202 * '- 9' for 'set-map(' + trailing ')'
9203 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009204 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009205
9206 cur_arg = 1;
9207
9208 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9209 (*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 +01009210 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9211 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009212 goto out_err;
9213 }
9214
9215 LIST_INIT(&rule->arg.map.key);
9216 LIST_INIT(&rule->arg.map.value);
9217
9218 proxy->conf.args.ctx = ARGC_HRS;
9219
9220 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009221 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009222 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009223 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009224 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9225 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009226 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009227 goto out_err;
9228 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009229
9230 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009231 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009232 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 +01009233 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009234 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9235 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009236 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009237 goto out_err;
9238 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009239
9240 free(proxy->conf.lfs_file);
9241 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9242 proxy->conf.lfs_line = proxy->conf.args.line;
9243
9244 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009245 } else if (strcmp(args[0], "redirect") == 0) {
9246 struct redirect_rule *redir;
9247 char *errmsg = NULL;
9248
9249 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009250 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9251 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009252 goto out_err;
9253 }
9254
9255 /* this redirect rule might already contain a parsed condition which
9256 * we'll pass to the http-request rule.
9257 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009258 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009259 rule->arg.redir = redir;
9260 rule->cond = redir->cond;
9261 redir->cond = NULL;
9262 cur_arg = 2;
9263 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009264 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009265 struct sample_expr *expr;
9266 unsigned int where;
9267 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009268 unsigned int tsc_num;
9269 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009270
9271 cur_arg = 1;
9272 proxy->conf.args.ctx = ARGC_TRK;
9273
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009274 tsc_num_str = &args[0][8];
9275 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
9276 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9277 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9278 free(err);
9279 goto out_err;
9280 }
9281
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009282 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9283 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009284 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9285 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009286 free(err);
9287 goto out_err;
9288 }
9289
9290 where = 0;
9291 if (proxy->cap & PR_CAP_FE)
9292 where |= SMP_VAL_FE_HRS_HDR;
9293 if (proxy->cap & PR_CAP_BE)
9294 where |= SMP_VAL_BE_HRS_HDR;
9295
9296 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009297 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9298 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9299 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9300 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009301 free(expr);
9302 goto out_err;
9303 }
9304
9305 if (strcmp(args[cur_arg], "table") == 0) {
9306 cur_arg++;
9307 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009308 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9309 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009310 free(expr);
9311 goto out_err;
9312 }
9313 /* we copy the table name for now, it will be resolved later */
9314 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9315 cur_arg++;
9316 }
9317 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009318 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02009319 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009320 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9321 char *errmsg = NULL;
9322 cur_arg = 1;
9323 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009324 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009325 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009326 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009327 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9328 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009329 free(errmsg);
9330 goto out_err;
9331 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009332 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009333 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009334 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9335 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9336 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9337 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009338 file, linenum, *trash.area ? ", " : "", trash.area,
9339 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009340 goto out_err;
9341 }
9342
9343 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9344 struct acl_cond *cond;
9345 char *errmsg = NULL;
9346
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009347 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009348 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9349 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009350 free(errmsg);
9351 goto out_err;
9352 }
9353 rule->cond = cond;
9354 }
9355 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009356 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9357 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9358 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009359 goto out_err;
9360 }
9361
9362 return rule;
9363 out_err:
9364 free(rule);
9365 return NULL;
9366}
9367
Willy Tarreau4baae242012-12-27 12:00:31 +01009368/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009369 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009370 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9371 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009372 */
9373struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009374 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009375{
9376 struct redirect_rule *rule;
9377 int cur_arg;
9378 int type = REDIRECT_TYPE_NONE;
9379 int code = 302;
9380 const char *destination = NULL;
9381 const char *cookie = NULL;
9382 int cookie_set = 0;
9383 unsigned int flags = REDIRECT_FLAG_NONE;
9384 struct acl_cond *cond = NULL;
9385
9386 cur_arg = 0;
9387 while (*(args[cur_arg])) {
9388 if (strcmp(args[cur_arg], "location") == 0) {
9389 if (!*args[cur_arg + 1])
9390 goto missing_arg;
9391
9392 type = REDIRECT_TYPE_LOCATION;
9393 cur_arg++;
9394 destination = args[cur_arg];
9395 }
9396 else if (strcmp(args[cur_arg], "prefix") == 0) {
9397 if (!*args[cur_arg + 1])
9398 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009399 type = REDIRECT_TYPE_PREFIX;
9400 cur_arg++;
9401 destination = args[cur_arg];
9402 }
9403 else if (strcmp(args[cur_arg], "scheme") == 0) {
9404 if (!*args[cur_arg + 1])
9405 goto missing_arg;
9406
9407 type = REDIRECT_TYPE_SCHEME;
9408 cur_arg++;
9409 destination = args[cur_arg];
9410 }
9411 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9412 if (!*args[cur_arg + 1])
9413 goto missing_arg;
9414
9415 cur_arg++;
9416 cookie = args[cur_arg];
9417 cookie_set = 1;
9418 }
9419 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9420 if (!*args[cur_arg + 1])
9421 goto missing_arg;
9422
9423 cur_arg++;
9424 cookie = args[cur_arg];
9425 cookie_set = 0;
9426 }
9427 else if (strcmp(args[cur_arg], "code") == 0) {
9428 if (!*args[cur_arg + 1])
9429 goto missing_arg;
9430
9431 cur_arg++;
9432 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009433 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009434 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009435 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009436 args[cur_arg - 1], args[cur_arg]);
9437 return NULL;
9438 }
9439 }
9440 else if (!strcmp(args[cur_arg],"drop-query")) {
9441 flags |= REDIRECT_FLAG_DROP_QS;
9442 }
9443 else if (!strcmp(args[cur_arg],"append-slash")) {
9444 flags |= REDIRECT_FLAG_APPEND_SLASH;
9445 }
9446 else if (strcmp(args[cur_arg], "if") == 0 ||
9447 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009448 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009449 if (!cond) {
9450 memprintf(errmsg, "error in condition: %s", *errmsg);
9451 return NULL;
9452 }
9453 break;
9454 }
9455 else {
9456 memprintf(errmsg,
9457 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9458 args[cur_arg]);
9459 return NULL;
9460 }
9461 cur_arg++;
9462 }
9463
9464 if (type == REDIRECT_TYPE_NONE) {
9465 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9466 return NULL;
9467 }
9468
Willy Tarreaube4653b2015-05-28 15:26:58 +02009469 if (dir && type != REDIRECT_TYPE_LOCATION) {
9470 memprintf(errmsg, "response only supports redirect type 'location'");
9471 return NULL;
9472 }
9473
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009474 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009475 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009476 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009477
9478 if (!use_fmt) {
9479 /* old-style static redirect rule */
9480 rule->rdr_str = strdup(destination);
9481 rule->rdr_len = strlen(destination);
9482 }
9483 else {
9484 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009485
9486 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9487 * if prefix == "/", we don't want to add anything, otherwise it
9488 * makes it hard for the user to configure a self-redirection.
9489 */
Godbachd9722032014-12-18 15:44:58 +08009490 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009491 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009492 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9493 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 +01009494 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9495 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009496 return NULL;
9497 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009498 free(curproxy->conf.lfs_file);
9499 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9500 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009501 }
9502 }
9503
Willy Tarreau4baae242012-12-27 12:00:31 +01009504 if (cookie) {
9505 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9506 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9507 */
9508 rule->cookie_len = strlen(cookie);
9509 if (cookie_set) {
9510 rule->cookie_str = malloc(rule->cookie_len + 10);
9511 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9512 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9513 rule->cookie_len += 9;
9514 } else {
9515 rule->cookie_str = malloc(rule->cookie_len + 21);
9516 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9517 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9518 rule->cookie_len += 20;
9519 }
9520 }
9521 rule->type = type;
9522 rule->code = code;
9523 rule->flags = flags;
9524 LIST_INIT(&rule->list);
9525 return rule;
9526
9527 missing_arg:
9528 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9529 return NULL;
9530}
9531
Willy Tarreau8797c062007-05-07 00:55:35 +02009532/************************************************************************/
9533/* The code below is dedicated to ACL parsing and matching */
9534/************************************************************************/
9535
9536
Willy Tarreau14174bc2012-04-16 14:34:04 +02009537/* This function ensures that the prerequisites for an L7 fetch are ready,
9538 * which means that a request or response is ready. If some data is missing,
9539 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009540 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9541 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009542 *
9543 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009544 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9545 * decide whether or not an HTTP message is present ;
9546 * 0 if the requested data cannot be fetched or if it is certain that
9547 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009548 * 1 if an HTTP message is ready
9549 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009550int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009551 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009552{
Willy Tarreau192252e2015-04-04 01:47:55 +02009553 struct http_txn *txn;
9554 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009555
Willy Tarreaube508f12016-03-10 11:47:01 +01009556 /* Note: it is possible that <s> is NULL when called before stream
9557 * initialization (eg: tcp-request connection), so this function is the
9558 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009559 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009560 if (!s)
9561 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009562
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009563 if (!s->txn) {
9564 if (unlikely(!http_alloc_txn(s)))
9565 return 0; /* not enough memory */
9566 http_init_txn(s);
9567 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009568 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009569 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009570
9571 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009572 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009573
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009574 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009575 /* If the buffer does not leave enough free space at the end,
9576 * we must first realign it.
9577 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009578 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
9579 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009580 channel_slow_realign(&s->req, trash.area);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009581
Willy Tarreau14174bc2012-04-16 14:34:04 +02009582 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009583 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009584 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009585
9586 /* Try to decode HTTP request */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009587 if (likely(msg->next < ci_data(&s->req)))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009588 http_msg_analyzer(msg, &txn->hdr_idx);
9589
9590 /* Still no valid request ? */
9591 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009592 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau23752332018-06-15 14:54:53 +02009593 channel_full(&s->req, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009594 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009595 }
9596 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009597 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009598 return 0;
9599 }
9600
9601 /* OK we just got a valid HTTP request. We have some minor
9602 * preparation to perform so that further checks can rely
9603 * on HTTP tests.
9604 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009605
9606 /* If the request was parsed but was too large, we must absolutely
9607 * return an error so that it is not processed. At the moment this
9608 * cannot happen, but if the parsers are to change in the future,
9609 * we want this check to be maintained.
9610 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009611 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009612 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009613 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009614 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009615 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009616 return 1;
9617 }
9618
Willy Tarreauf37954d2018-06-15 18:31:02 +02009619 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009620 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009621 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009622
Willy Tarreau506d0502013-07-06 13:29:24 +02009623 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9624 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009625 }
9626
Willy Tarreau506d0502013-07-06 13:29:24 +02009627 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009628 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009629 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009630
9631 /* otherwise everything's ready for the request */
9632 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009633 else {
9634 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009635 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9636 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009637 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009638 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009639 }
9640
9641 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009642 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009643 return 1;
9644}
Willy Tarreau8797c062007-05-07 00:55:35 +02009645
Willy Tarreau8797c062007-05-07 00:55:35 +02009646/* 1. Check on METHOD
9647 * We use the pre-parsed method if it is known, and store its number as an
9648 * integer. If it is unknown, we use the pointer and the length.
9649 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009650static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009651{
9652 int len, meth;
9653
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009654 len = strlen(text);
9655 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009656
9657 pattern->val.i = meth;
9658 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009659 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009660 pattern->len = len;
9661 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009662 else {
9663 pattern->ptr.str = NULL;
9664 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009665 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009666 return 1;
9667}
9668
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009669/* This function fetches the method of current HTTP request and stores
9670 * it in the global pattern struct as a chunk. There are two possibilities :
9671 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9672 * in <len> and <ptr> is NULL ;
9673 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9674 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009675 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009676 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009677static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009678smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009679{
9680 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009681 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009682
Willy Tarreau24e32d82012-04-23 23:55:44 +02009683 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009684
Willy Tarreaube508f12016-03-10 11:47:01 +01009685 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009686 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009687 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009688 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009689 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009690 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9691 /* ensure the indexes are not affected */
9692 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009693 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009694 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
9695 smp->data.u.meth.str.area = ci_head(txn->req.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009696 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009697 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009698 return 1;
9699}
9700
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009701/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009702static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009703{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009704 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009705 struct pattern_list *lst;
9706 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009707
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009708 list_for_each_entry(lst, &expr->patterns, list) {
9709 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009710
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009711 /* well-known method */
9712 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009713 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009714 return pattern;
9715 else
9716 continue;
9717 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009718
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009719 /* Other method, we must compare the strings */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009720 if (pattern->len != smp->data.u.meth.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009721 continue;
9722
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009723 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009724 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0) ||
9725 (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009726 return pattern;
9727 }
9728 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009729}
9730
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009731static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009732smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009733{
Willy Tarreaube508f12016-03-10 11:47:01 +01009734 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009735 char *ptr;
9736 int len;
9737
Willy Tarreauc0239e02012-04-16 14:42:55 +02009738 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009739
Willy Tarreaube508f12016-03-10 11:47:01 +01009740 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009741 len = txn->req.sl.rq.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009742 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009743
9744 while ((len-- > 0) && (*ptr++ != '/'));
9745 if (len <= 0)
9746 return 0;
9747
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009748 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009749 smp->data.u.str.area = ptr;
9750 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009751
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009752 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009753 return 1;
9754}
9755
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009756static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009757smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009758{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009759 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009760 char *ptr;
9761 int len;
9762
Willy Tarreauc0239e02012-04-16 14:42:55 +02009763 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009764
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009765 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009766 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9767 return 0;
9768
Willy Tarreau8797c062007-05-07 00:55:35 +02009769 len = txn->rsp.sl.st.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009770 ptr = ci_head(txn->rsp.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009771
9772 while ((len-- > 0) && (*ptr++ != '/'));
9773 if (len <= 0)
9774 return 0;
9775
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009776 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009777 smp->data.u.str.area = ptr;
9778 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009779
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009780 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009781 return 1;
9782}
9783
9784/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009785static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009786smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009787{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009788 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009789 char *ptr;
9790 int len;
9791
Willy Tarreauc0239e02012-04-16 14:42:55 +02009792 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009793
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009794 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009795 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9796 return 0;
9797
Willy Tarreau8797c062007-05-07 00:55:35 +02009798 len = txn->rsp.sl.st.c_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009799 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009800
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009801 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009802 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009803 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009804 return 1;
9805}
9806
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009807static int
9808smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9809{
9810 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9811 return 0;
9812
9813 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009814 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009815 return 0;
9816 smp->strm->unique_id[0] = '\0';
9817 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009818 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009819 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9820
9821 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009822 smp->data.u.str.area = smp->strm->unique_id;
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009823 smp->flags = SMP_F_CONST;
9824 return 1;
9825}
9826
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009827/* Returns a string block containing all headers including the
9828 * empty line wich separes headers from the body. This is useful
9829 * form some headers analysis.
9830 */
9831static int
9832smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9833{
9834 struct http_msg *msg;
9835 struct hdr_idx *idx;
9836 struct http_txn *txn;
9837
9838 CHECK_HTTP_MESSAGE_FIRST();
9839
9840 txn = smp->strm->txn;
9841 idx = &txn->hdr_idx;
9842 msg = &txn->req;
9843
9844 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009845 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
9846 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Willy Tarreauf37954d2018-06-15 18:31:02 +02009847 (ci_head(msg->chn)[msg->eoh] == '\r');
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009848
9849 return 1;
9850}
9851
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009852/* Returns the header request in a length/value encoded format.
9853 * This is useful for exchanges with the SPOE.
9854 *
9855 * A "length value" is a multibyte code encoding numbers. It uses the
9856 * SPOE format. The encoding is the following:
9857 *
9858 * Each couple "header name" / "header value" is composed
9859 * like this:
9860 * "length value" "header name bytes"
9861 * "length value" "header value bytes"
9862 * When the last header is reached, the header name and the header
9863 * value are empty. Their length are 0
9864 */
9865static int
9866smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9867{
9868 struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +02009869 struct buffer *temp;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009870 struct hdr_idx *idx;
9871 const char *cur_ptr, *cur_next, *p;
9872 int old_idx, cur_idx;
9873 struct hdr_idx_elem *cur_hdr;
9874 const char *hn, *hv;
9875 int hnl, hvl;
9876 int ret;
9877 struct http_txn *txn;
9878 char *buf;
9879 char *end;
9880
9881 CHECK_HTTP_MESSAGE_FIRST();
9882
9883 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009884 buf = temp->area;
9885 end = temp->area + temp->size;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009886
9887 txn = smp->strm->txn;
9888 idx = &txn->hdr_idx;
9889 msg = &txn->req;
9890
9891 /* Build array of headers. */
9892 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009893 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009894 while (1) {
9895 cur_idx = idx->v[old_idx].next;
9896 if (!cur_idx)
9897 break;
9898 old_idx = cur_idx;
9899
9900 cur_hdr = &idx->v[cur_idx];
9901 cur_ptr = cur_next;
9902 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9903
9904 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9905 * and the next header starts at cur_next. We'll check
9906 * this header in the list as well as against the default
9907 * rule.
9908 */
9909
9910 /* look for ': *'. */
9911 hn = cur_ptr;
9912 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9913 if (p >= cur_ptr+cur_hdr->len)
9914 continue;
9915 hnl = p - hn;
9916 p++;
9917 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9918 p++;
9919 if (p >= cur_ptr + cur_hdr->len)
9920 continue;
9921 hv = p;
9922 hvl = cur_ptr + cur_hdr->len-p;
9923
9924 /* encode the header name. */
9925 ret = encode_varint(hnl, &buf, end);
9926 if (ret == -1)
9927 return 0;
9928 if (buf + hnl > end)
9929 return 0;
9930 memcpy(buf, hn, hnl);
9931 buf += hnl;
9932
9933 /* encode and copy the value. */
9934 ret = encode_varint(hvl, &buf, end);
9935 if (ret == -1)
9936 return 0;
9937 if (buf + hvl > end)
9938 return 0;
9939 memcpy(buf, hv, hvl);
9940 buf += hvl;
9941 }
9942
9943 /* encode the end of the header list with empty
9944 * header name and header value.
9945 */
9946 ret = encode_varint(0, &buf, end);
9947 if (ret == -1)
9948 return 0;
9949 ret = encode_varint(0, &buf, end);
9950 if (ret == -1)
9951 return 0;
9952
9953 /* Initialise sample data which will be filled. */
9954 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009955 smp->data.u.str.area = temp->area;
9956 smp->data.u.str.data = buf - temp->area;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009957 smp->data.u.str.size = temp->size;
9958
9959 return 1;
9960}
9961
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009962/* returns the longest available part of the body. This requires that the body
9963 * has been waited for using http-buffer-request.
9964 */
9965static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009966smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009967{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009968 struct http_msg *msg;
9969 unsigned long len;
9970 unsigned long block1;
9971 char *body;
Willy Tarreau83061a82018-07-13 11:56:34 +02009972 struct buffer *temp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009973
9974 CHECK_HTTP_MESSAGE_FIRST();
9975
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009976 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009977 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009978 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009979 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009980
9981 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +02009982 body = c_ptr(msg->chn, -http_data_rewind(msg));
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009983
9984 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009985 if (block1 > b_wrap(&msg->chn->buf) - body)
9986 block1 = b_wrap(&msg->chn->buf) - body;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009987
9988 if (block1 == len) {
9989 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009990 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009991 smp->data.u.str.area = body;
9992 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009993 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9994 }
9995 else {
9996 /* buffer is wrapped, we need to defragment it */
9997 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009998 memcpy(temp->area, body, block1);
9999 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
10000 len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010001 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010002 smp->data.u.str.area = temp->area;
10003 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010004 smp->flags = SMP_F_VOL_TEST;
10005 }
10006 return 1;
10007}
10008
10009
10010/* returns the available length of the body. This requires that the body
10011 * has been waited for using http-buffer-request.
10012 */
10013static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010014smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010015{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010016 struct http_msg *msg;
10017
10018 CHECK_HTTP_MESSAGE_FIRST();
10019
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010020 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010021 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010022 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010023 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010024
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010025 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010026 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010027
10028 smp->flags = SMP_F_VOL_TEST;
10029 return 1;
10030}
10031
10032
10033/* returns the advertised length of the body, or the advertised size of the
10034 * chunks available in the buffer. This requires that the body has been waited
10035 * for using http-buffer-request.
10036 */
10037static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010038smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010039{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010040 struct http_msg *msg;
10041
10042 CHECK_HTTP_MESSAGE_FIRST();
10043
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010044 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010045 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010046 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010047 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010048
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010049 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010050 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010051
10052 smp->flags = SMP_F_VOL_TEST;
10053 return 1;
10054}
10055
10056
Willy Tarreau8797c062007-05-07 00:55:35 +020010057/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010058static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010059smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010060{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010061 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010062
Willy Tarreauc0239e02012-04-16 14:42:55 +020010063 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010064
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010065 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010066 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010067 smp->data.u.str.data = txn->req.sl.rq.u_l;
10068 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010069 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010070 return 1;
10071}
10072
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010073static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010074smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010075{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010076 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010077 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010078
Willy Tarreauc0239e02012-04-16 14:42:55 +020010079 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010080
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010081 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010082 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 +020010083 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010084 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010085
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010086 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010087 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010088 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010089 return 1;
10090}
10091
10092static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010093smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010094{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010095 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010096 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010097
Willy Tarreauc0239e02012-04-16 14:42:55 +020010098 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010099
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010100 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010101 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 +020010102 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10103 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010104
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010105 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010106 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010107 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010108 return 1;
10109}
10110
Willy Tarreau185b5c42012-04-26 15:11:51 +020010111/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10112 * Accepts an optional argument of type string containing the header field name,
10113 * and an optional argument of type signed or unsigned integer to request an
10114 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010115 * headers are considered from the first one. It does not stop on commas and
10116 * returns full lines instead (useful for User-Agent or Date for example).
10117 */
10118static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010119smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010120{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010121 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010122 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010123 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010124 int occ = 0;
10125 const char *name_str = NULL;
10126 int name_len = 0;
10127
10128 if (!ctx) {
10129 /* first call */
10130 ctx = &static_hdr_ctx;
10131 ctx->idx = 0;
10132 smp->ctx.a[0] = ctx;
10133 }
10134
10135 if (args) {
10136 if (args[0].type != ARGT_STR)
10137 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010138 name_str = args[0].data.str.area;
10139 name_len = args[0].data.str.data;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010140
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010141 if (args[1].type == ARGT_SINT)
10142 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010143 }
10144
10145 CHECK_HTTP_MESSAGE_FIRST();
10146
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010147 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010148 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 +020010149
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010150 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10151 /* search for header from the beginning */
10152 ctx->idx = 0;
10153
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010154 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010155 /* no explicit occurrence and single fetch => last header by default */
10156 occ = -1;
10157
10158 if (!occ)
10159 /* prepare to report multiple occurrences for ACL fetches */
10160 smp->flags |= SMP_F_NOT_LAST;
10161
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010162 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010163 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010164 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010165 return 1;
10166
10167 smp->flags &= ~SMP_F_NOT_LAST;
10168 return 0;
10169}
10170
10171/* 6. Check on HTTP header count. The number of occurrences is returned.
10172 * Accepts exactly 1 argument of type string. It does not stop on commas and
10173 * returns full lines instead (useful for User-Agent or Date for example).
10174 */
10175static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010176smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010177{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010178 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010179 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010180 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010181 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010182 const char *name = NULL;
10183 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010184
Willy Tarreau601a4d12015-04-01 19:16:09 +020010185 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010186 name = args->data.str.area;
10187 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010188 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010189
10190 CHECK_HTTP_MESSAGE_FIRST();
10191
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010192 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010193 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 +020010194
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010195 ctx.idx = 0;
10196 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010197 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010198 cnt++;
10199
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010200 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010201 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010202 smp->flags = SMP_F_VOL_HDR;
10203 return 1;
10204}
10205
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010206static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010207smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010208{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010209 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010210 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010211 const struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +020010212 struct buffer *temp;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010213 char del = ',';
10214
10215 if (args && args->type == ARGT_STR)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010216 del = *args[0].data.str.area;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010217
10218 CHECK_HTTP_MESSAGE_FIRST();
10219
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010220 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010221 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 +020010222
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010223 temp = get_trash_chunk();
10224
10225 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010226 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010227 if (temp->data)
10228 temp->area[temp->data++] = del;
10229 memcpy(temp->area + temp->data, ctx.line, ctx.del);
10230 temp->data += ctx.del;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010231 }
10232
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010233 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010234 smp->data.u.str.area = temp->area;
10235 smp->data.u.str.data = temp->data;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010236 smp->flags = SMP_F_VOL_HDR;
10237 return 1;
10238}
10239
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010240/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10241 * Accepts an optional argument of type string containing the header field name,
10242 * and an optional argument of type signed or unsigned integer to request an
10243 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010244 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010245 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010246static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010247smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010248{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010249 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010250 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010251 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010252 int occ = 0;
10253 const char *name_str = NULL;
10254 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010255
Willy Tarreaua890d072013-04-02 12:01:06 +020010256 if (!ctx) {
10257 /* first call */
10258 ctx = &static_hdr_ctx;
10259 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010260 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010261 }
10262
Willy Tarreau185b5c42012-04-26 15:11:51 +020010263 if (args) {
10264 if (args[0].type != ARGT_STR)
10265 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010266 name_str = args[0].data.str.area;
10267 name_len = args[0].data.str.data;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010268
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010269 if (args[1].type == ARGT_SINT)
10270 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010271 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010272
Willy Tarreaue333ec92012-04-16 16:26:40 +020010273 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010274
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010275 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010276 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 +020010277
Willy Tarreau185b5c42012-04-26 15:11:51 +020010278 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010279 /* search for header from the beginning */
10280 ctx->idx = 0;
10281
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010282 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010283 /* no explicit occurrence and single fetch => last header by default */
10284 occ = -1;
10285
10286 if (!occ)
10287 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010288 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010289
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010290 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010291 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010292 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010293 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010294
Willy Tarreau37406352012-04-23 16:16:37 +020010295 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010296 return 0;
10297}
10298
Willy Tarreauc11416f2007-06-17 16:58:38 +020010299/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010300 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010301 */
10302static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010303smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010304{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010305 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010306 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010307 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010308 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010309 const char *name = NULL;
10310 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010311
Willy Tarreau601a4d12015-04-01 19:16:09 +020010312 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010313 name = args->data.str.area;
10314 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010315 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010316
Willy Tarreaue333ec92012-04-16 16:26:40 +020010317 CHECK_HTTP_MESSAGE_FIRST();
10318
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010319 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010320 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 +020010321
Willy Tarreau33a7e692007-06-10 19:45:56 +020010322 ctx.idx = 0;
10323 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010324 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010325 cnt++;
10326
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010327 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010328 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010329 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010330 return 1;
10331}
10332
Willy Tarreau185b5c42012-04-26 15:11:51 +020010333/* Fetch an HTTP header's integer value. The integer value is returned. It
10334 * takes a mandatory argument of type string and an optional one of type int
10335 * to designate a specific occurrence. It returns an unsigned integer, which
10336 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010337 */
10338static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010339smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010340{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010341 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010342
Willy Tarreauf853c462012-04-23 18:53:56 +020010343 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010344 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010345 smp->data.u.sint = strl2ic(smp->data.u.str.area,
10346 smp->data.u.str.data);
Willy Tarreauf853c462012-04-23 18:53:56 +020010347 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010348
Willy Tarreaud53e2422012-04-16 17:21:11 +020010349 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010350}
10351
Cyril Bonté69fa9922012-10-25 00:01:06 +020010352/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10353 * and an optional one of type int to designate a specific occurrence.
10354 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010355 */
10356static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010357smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010358{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010359 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010360
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010361 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010362 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010363 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010364 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010365 } else {
Willy Tarreau83061a82018-07-13 11:56:34 +020010366 struct buffer *temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010367 if (smp->data.u.str.data < temp->size - 1) {
10368 memcpy(temp->area, smp->data.u.str.area,
10369 smp->data.u.str.data);
10370 temp->area[smp->data.u.str.data] = '\0';
10371 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010372 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010373 break;
10374 }
10375 }
10376 }
10377
Willy Tarreaud53e2422012-04-16 17:21:11 +020010378 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010379 if (!(smp->flags & SMP_F_NOT_LAST))
10380 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010381 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010382 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010383}
10384
Willy Tarreau737b0c12007-06-10 21:28:46 +020010385/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10386 * the first '/' after the possible hostname, and ends before the possible '?'.
10387 */
10388static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010389smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010390{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010391 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010392 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010393
Willy Tarreauc0239e02012-04-16 14:42:55 +020010394 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010395
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010396 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010397 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010398 ptr = http_get_path(txn);
10399 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010400 return 0;
10401
10402 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010403 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010404 smp->data.u.str.area = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010405
10406 while (ptr < end && *ptr != '?')
10407 ptr++;
10408
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010409 smp->data.u.str.data = ptr - smp->data.u.str.area;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010410 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010411 return 1;
10412}
10413
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010414/* This produces a concatenation of the first occurrence of the Host header
10415 * followed by the path component if it begins with a slash ('/'). This means
10416 * that '*' will not be added, resulting in exactly the first Host entry.
10417 * If no Host header is found, then the path is returned as-is. The returned
10418 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010419 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010420 */
10421static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010422smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010423{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010424 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010425 char *ptr, *end, *beg;
10426 struct hdr_ctx ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +020010427 struct buffer *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010428
10429 CHECK_HTTP_MESSAGE_FIRST();
10430
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010431 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010432 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010433 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010434 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010435
10436 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010437 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010438 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010439 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010440 smp->data.u.str.area = temp->area;
10441 smp->data.u.str.data = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010442
10443 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020010444 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010445 beg = http_get_path(txn);
10446 if (!beg)
10447 beg = end;
10448
10449 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10450
10451 if (beg < ptr && *beg == '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010452 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
10453 ptr - beg);
10454 smp->data.u.str.data += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010455 }
10456
10457 smp->flags = SMP_F_VOL_1ST;
10458 return 1;
10459}
10460
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010461/* This produces a 32-bit hash of the concatenation of the first occurrence of
10462 * the Host header followed by the path component if it begins with a slash ('/').
10463 * This means that '*' will not be added, resulting in exactly the first Host
10464 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010465 * is hashed using the path hash followed by a full avalanche hash and provides a
10466 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010467 * high-traffic sites without having to store whole paths.
10468 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010469int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010470smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010471{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010472 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010473 struct hdr_ctx ctx;
10474 unsigned int hash = 0;
10475 char *ptr, *beg, *end;
10476 int len;
10477
10478 CHECK_HTTP_MESSAGE_FIRST();
10479
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010480 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010481 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010482 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010483 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10484 ptr = ctx.line + ctx.val;
10485 len = ctx.vlen;
10486 while (len--)
10487 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10488 }
10489
10490 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020010491 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010492 beg = http_get_path(txn);
10493 if (!beg)
10494 beg = end;
10495
10496 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10497
10498 if (beg < ptr && *beg == '/') {
10499 while (beg < ptr)
10500 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10501 }
10502 hash = full_hash(hash);
10503
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010504 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010505 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010506 smp->flags = SMP_F_VOL_1ST;
10507 return 1;
10508}
10509
Willy Tarreau4a550602012-12-09 14:53:32 +010010510/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010511 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10512 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10513 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010514 * that in environments where IPv6 is insignificant, truncating the output to
10515 * 8 bytes would still work.
10516 */
10517static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010518smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010519{
Willy Tarreau83061a82018-07-13 11:56:34 +020010520 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010521 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010522
10523 if (!cli_conn)
10524 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010525
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010526 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010527 return 0;
10528
Willy Tarreau47ca5452012-12-23 20:22:19 +010010529 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010530 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
10531 temp->data += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010532
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010533 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010534 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010535 memcpy(temp->area + temp->data,
10536 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
10537 4);
10538 temp->data += 4;
Willy Tarreau4a550602012-12-09 14:53:32 +010010539 break;
10540 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010541 memcpy(temp->area + temp->data,
10542 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
10543 16);
10544 temp->data += 16;
Willy Tarreau4a550602012-12-09 14:53:32 +010010545 break;
10546 default:
10547 return 0;
10548 }
10549
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010550 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010551 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010552 return 1;
10553}
10554
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010555/* Extracts the query string, which comes after the question mark '?'. If no
10556 * question mark is found, nothing is returned. Otherwise it returns a sample
10557 * of type string carrying the whole query string.
10558 */
10559static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010560smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010561{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010562 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010563 char *ptr, *end;
10564
10565 CHECK_HTTP_MESSAGE_FIRST();
10566
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010567 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010568 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010569 end = ptr + txn->req.sl.rq.u_l;
10570
10571 /* look up the '?' */
10572 do {
10573 if (ptr == end)
10574 return 0;
10575 } while (*ptr++ != '?');
10576
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010577 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010578 smp->data.u.str.area = ptr;
10579 smp->data.u.str.data = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010580 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10581 return 1;
10582}
10583
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010584static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010585smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010586{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010587 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10588 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10589 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010590
Willy Tarreau24e32d82012-04-23 23:55:44 +020010591 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010592
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010593 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010594 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010595 return 1;
10596}
10597
Willy Tarreau7f18e522010-10-22 20:04:13 +020010598/* return a valid test if the current request is the first one on the connection */
10599static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010600smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010601{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010602 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010603 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010604 return 1;
10605}
10606
Willy Tarreau34db1082012-04-19 17:16:54 +020010607/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010608static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010609smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010610{
10611
Willy Tarreau24e32d82012-04-23 23:55:44 +020010612 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010613 return 0;
10614
Willy Tarreauc0239e02012-04-16 14:42:55 +020010615 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010616
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010617 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010618 return 0;
10619
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010620 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010621 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010622 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010623 return 1;
10624}
Willy Tarreau8797c062007-05-07 00:55:35 +020010625
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010626/* Accepts exactly 1 argument of type userlist */
10627static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010628smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010629{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010630 if (!args || args->type != ARGT_USR)
10631 return 0;
10632
10633 CHECK_HTTP_MESSAGE_FIRST();
10634
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010635 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010636 return 0;
10637
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010638 /* if the user does not belong to the userlist or has a wrong password,
10639 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010640 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010641 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010642 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10643 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010644 return 0;
10645
10646 /* pat_match_auth() will need the user list */
10647 smp->ctx.a[0] = args->data.usr;
10648
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010649 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010650 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010651 smp->data.u.str.area = smp->strm->txn->auth.user;
10652 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010653
10654 return 1;
10655}
10656
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010657/* Try to find the next occurrence of a cookie name in a cookie header value.
10658 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10659 * the cookie value is returned into *value and *value_l, and the function
10660 * returns a pointer to the next pointer to search from if the value was found.
10661 * Otherwise if the cookie was not found, NULL is returned and neither value
10662 * nor value_l are touched. The input <hdr> string should first point to the
10663 * header's value, and the <hdr_end> pointer must point to the first character
10664 * not part of the value. <list> must be non-zero if value may represent a list
10665 * of values (cookie headers). This makes it faster to abort parsing when no
10666 * list is expected.
10667 */
David Carlier4686f792015-09-25 14:10:50 +010010668char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010669extract_cookie_value(char *hdr, const char *hdr_end,
10670 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010671 char **value, size_t *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010672{
10673 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10674 char *next;
10675
10676 /* we search at least a cookie name followed by an equal, and more
10677 * generally something like this :
10678 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10679 */
10680 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10681 /* Iterate through all cookies on this line */
10682
Willy Tarreau2235b262016-11-05 15:50:20 +010010683 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010684 att_beg++;
10685
10686 /* find att_end : this is the first character after the last non
10687 * space before the equal. It may be equal to hdr_end.
10688 */
10689 equal = att_end = att_beg;
10690
10691 while (equal < hdr_end) {
10692 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10693 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010694 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010695 continue;
10696 att_end = equal;
10697 }
10698
10699 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10700 * is between <att_beg> and <equal>, both may be identical.
10701 */
10702
10703 /* look for end of cookie if there is an equal sign */
10704 if (equal < hdr_end && *equal == '=') {
10705 /* look for the beginning of the value */
10706 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010707 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010708 val_beg++;
10709
10710 /* find the end of the value, respecting quotes */
10711 next = find_cookie_value_end(val_beg, hdr_end);
10712
10713 /* make val_end point to the first white space or delimitor after the value */
10714 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010715 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010716 val_end--;
10717 } else {
10718 val_beg = val_end = next = equal;
10719 }
10720
10721 /* We have nothing to do with attributes beginning with '$'. However,
10722 * they will automatically be removed if a header before them is removed,
10723 * since they're supposed to be linked together.
10724 */
10725 if (*att_beg == '$')
10726 continue;
10727
10728 /* Ignore cookies with no equal sign */
10729 if (equal == next)
10730 continue;
10731
10732 /* Now we have the cookie name between att_beg and att_end, and
10733 * its value between val_beg and val_end.
10734 */
10735
10736 if (att_end - att_beg == cookie_name_l &&
10737 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10738 /* let's return this value and indicate where to go on from */
10739 *value = val_beg;
10740 *value_l = val_end - val_beg;
10741 return next + 1;
10742 }
10743
10744 /* Set-Cookie headers only have the name in the first attr=value part */
10745 if (!list)
10746 break;
10747 }
10748
10749 return NULL;
10750}
10751
William Lallemanda43ba4e2014-01-28 18:14:25 +010010752/* Fetch a captured HTTP request header. The index is the position of
10753 * the "capture" option in the configuration file
10754 */
10755static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010756smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010757{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010758 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010759 int idx;
10760
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010761 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010762 return 0;
10763
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010764 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010765
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010766 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 +010010767 return 0;
10768
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010769 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010770 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010771 smp->data.u.str.area = smp->strm->req_cap[idx];
10772 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010773
10774 return 1;
10775}
10776
10777/* Fetch a captured HTTP response header. The index is the position of
10778 * the "capture" option in the configuration file
10779 */
10780static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010781smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010782{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010783 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010784 int idx;
10785
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010786 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010787 return 0;
10788
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010789 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010790
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010791 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 +010010792 return 0;
10793
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010794 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010795 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010796 smp->data.u.str.area = smp->strm->res_cap[idx];
10797 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010798
10799 return 1;
10800}
10801
William Lallemand65ad6e12014-01-31 15:08:02 +010010802/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10803static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010804smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010805{
Willy Tarreau83061a82018-07-13 11:56:34 +020010806 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010807 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010808 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010809
Willy Tarreau15e91e12015-04-04 00:52:09 +020010810 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010811 return 0;
10812
William Lallemand96a77852014-02-05 00:30:02 +010010813 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010814
William Lallemand96a77852014-02-05 00:30:02 +010010815 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10816 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010817
William Lallemand96a77852014-02-05 00:30:02 +010010818 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010819 temp->area = txn->uri;
10820 temp->data = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010821 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010822 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010823 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010824
10825 return 1;
10826
10827}
10828
10829/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10830static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010831smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010832{
Willy Tarreau83061a82018-07-13 11:56:34 +020010833 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010834 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010835 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010836
Willy Tarreau15e91e12015-04-04 00:52:09 +020010837 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010838 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010839
William Lallemand65ad6e12014-01-31 15:08:02 +010010840 ptr = txn->uri;
10841
10842 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10843 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010844
William Lallemand65ad6e12014-01-31 15:08:02 +010010845 if (!*ptr)
10846 return 0;
10847
10848 ptr++; /* skip the space */
10849
10850 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010851 ptr = temp->area = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010852 if (!ptr)
10853 return 0;
10854 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10855 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010856
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010857 smp->data.u.str = *temp;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010858 smp->data.u.str.data = ptr - temp->area;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010859 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010860 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010861
10862 return 1;
10863}
10864
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010865/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10866 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10867 */
10868static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010869smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010870{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010871 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010872
Willy Tarreau15e91e12015-04-04 00:52:09 +020010873 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010874 return 0;
10875
10876 if (txn->req.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010877 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010878 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010879 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010880
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010881 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010882 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010883 smp->flags = SMP_F_CONST;
10884 return 1;
10885
10886}
10887
10888/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10889 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10890 */
10891static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010892smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010893{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010894 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010895
Willy Tarreau15e91e12015-04-04 00:52:09 +020010896 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010897 return 0;
10898
10899 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010900 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010901 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010902 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010903
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010904 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010905 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010906 smp->flags = SMP_F_CONST;
10907 return 1;
10908
10909}
10910
William Lallemand65ad6e12014-01-31 15:08:02 +010010911
Willy Tarreaue333ec92012-04-16 16:26:40 +020010912/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010913 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010914 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010915 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010916 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010917 * Accepts exactly 1 argument of type string. If the input options indicate
10918 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010919 * The returned sample is of type CSTR. Can be used to parse cookies in other
10920 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010921 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010922int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010923{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010924 struct http_txn *txn;
10925 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010926 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010927 const struct http_msg *msg;
10928 const char *hdr_name;
10929 int hdr_name_len;
10930 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010931 int occ = 0;
10932 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010933
Willy Tarreau24e32d82012-04-23 23:55:44 +020010934 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010935 return 0;
10936
Willy Tarreaua890d072013-04-02 12:01:06 +020010937 if (!ctx) {
10938 /* first call */
10939 ctx = &static_hdr_ctx;
10940 ctx->idx = 0;
10941 smp->ctx.a[2] = ctx;
10942 }
10943
Willy Tarreaue333ec92012-04-16 16:26:40 +020010944 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010945
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010946 txn = smp->strm->txn;
10947 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010948
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010949 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010950 msg = &txn->req;
10951 hdr_name = "Cookie";
10952 hdr_name_len = 6;
10953 } else {
10954 msg = &txn->rsp;
10955 hdr_name = "Set-Cookie";
10956 hdr_name_len = 10;
10957 }
10958
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010959 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010960 /* no explicit occurrence and single fetch => last cookie by default */
10961 occ = -1;
10962
10963 /* OK so basically here, either we want only one value and it's the
10964 * last one, or we want to iterate over all of them and we fetch the
10965 * next one.
10966 */
10967
Willy Tarreauf37954d2018-06-15 18:31:02 +020010968 sol = ci_head(msg->chn);
Willy Tarreau37406352012-04-23 16:16:37 +020010969 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010970 /* search for the header from the beginning, we must first initialize
10971 * the search parameters.
10972 */
Willy Tarreau37406352012-04-23 16:16:37 +020010973 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010974 ctx->idx = 0;
10975 }
10976
Willy Tarreau28376d62012-04-26 21:26:10 +020010977 smp->flags |= SMP_F_VOL_HDR;
10978
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010979 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010980 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10981 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010982 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10983 goto out;
10984
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010985 if (ctx->vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010986 continue;
10987
Willy Tarreau37406352012-04-23 16:16:37 +020010988 smp->ctx.a[0] = ctx->line + ctx->val;
10989 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010990 }
10991
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010992 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010993 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010994 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010995 args->data.str.area,
10996 args->data.str.data,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010997 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010998 &smp->data.u.str.area,
10999 &smp->data.u.str.data);
Willy Tarreau37406352012-04-23 16:16:37 +020011000 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011001 found = 1;
11002 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011003 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011004 smp->flags |= SMP_F_NOT_LAST;
11005 return 1;
11006 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011007 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011008 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011009 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011010 /* all cookie headers and values were scanned. If we're looking for the
11011 * last occurrence, we may return it now.
11012 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011013 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011014 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011015 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011016}
11017
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011018/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011019 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011020 * multiple cookies may be parsed on the same line. The returned sample is of
11021 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011022 */
11023static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011024smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011025{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011026 struct http_txn *txn;
11027 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011028 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011029 const struct http_msg *msg;
11030 const char *hdr_name;
11031 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011032 int cnt;
11033 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011034 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011035
Willy Tarreau24e32d82012-04-23 23:55:44 +020011036 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011037 return 0;
11038
Willy Tarreaue333ec92012-04-16 16:26:40 +020011039 CHECK_HTTP_MESSAGE_FIRST();
11040
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011041 txn = smp->strm->txn;
11042 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011043
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011044 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011045 msg = &txn->req;
11046 hdr_name = "Cookie";
11047 hdr_name_len = 6;
11048 } else {
11049 msg = &txn->rsp;
11050 hdr_name = "Set-Cookie";
11051 hdr_name_len = 10;
11052 }
11053
Willy Tarreauf37954d2018-06-15 18:31:02 +020011054 sol = ci_head(msg->chn);
Willy Tarreau46787ed2012-04-11 17:28:40 +020011055 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011056 ctx.idx = 0;
11057 cnt = 0;
11058
11059 while (1) {
11060 /* Note: val_beg == NULL every time we need to fetch a new header */
11061 if (!val_beg) {
11062 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11063 break;
11064
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011065 if (ctx.vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011066 continue;
11067
11068 val_beg = ctx.line + ctx.val;
11069 val_end = val_beg + ctx.vlen;
11070 }
11071
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011072 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011073 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011074 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011075 args->data.str.area, args->data.str.data,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011076 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011077 &smp->data.u.str.area,
11078 &smp->data.u.str.data))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011079 cnt++;
11080 }
11081 }
11082
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011083 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011084 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011085 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011086 return 1;
11087}
11088
Willy Tarreau51539362012-05-08 12:46:28 +020011089/* Fetch an cookie's integer value. The integer value is returned. It
11090 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11091 */
11092static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011093smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011094{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011095 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011096
11097 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011098 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011099 smp->data.u.sint = strl2ic(smp->data.u.str.area,
11100 smp->data.u.str.data);
Willy Tarreau51539362012-05-08 12:46:28 +020011101 }
11102
11103 return ret;
11104}
11105
Willy Tarreau8797c062007-05-07 00:55:35 +020011106/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011107/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011108/************************************************************************/
11109
David Cournapeau16023ee2010-12-23 20:55:41 +090011110/*
11111 * Given a path string and its length, find the position of beginning of the
11112 * query string. Returns NULL if no query string is found in the path.
11113 *
11114 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11115 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011116 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011117 */
bedis4c75cca2012-10-05 08:38:24 +020011118static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011119{
11120 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011121
bedis4c75cca2012-10-05 08:38:24 +020011122 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011123 return p ? p + 1 : NULL;
11124}
11125
bedis4c75cca2012-10-05 08:38:24 +020011126static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011127{
bedis4c75cca2012-10-05 08:38:24 +020011128 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011129}
11130
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011131/* after increasing a pointer value, it can exceed the first buffer
11132 * size. This function transform the value of <ptr> according with
11133 * the expected position. <chunks> is an array of the one or two
11134 * avalaible chunks. The first value is the start of the first chunk,
11135 * the second value if the end+1 of the first chunks. The third value
11136 * is NULL or the start of the second chunk and the fourth value is
11137 * the end+1 of the second chunk. The function returns 1 if does a
11138 * wrap, else returns 0.
11139 */
11140static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11141{
11142 if (*ptr < chunks[1])
11143 return 0;
11144 if (!chunks[2])
11145 return 0;
11146 *ptr = chunks[2] + ( *ptr - chunks[1] );
11147 return 1;
11148}
11149
David Cournapeau16023ee2010-12-23 20:55:41 +090011150/*
11151 * Given a url parameter, find the starting position of the first occurence,
11152 * or NULL if the parameter is not found.
11153 *
11154 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11155 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011156 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011157 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011158 * or the second chunk. The caller must be check the position before using the
11159 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011160 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011161static const char *
11162find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011163 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011164 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011165{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011166 const char *pos, *last, *equal;
11167 const char **bufs = chunks;
11168 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011169
David Cournapeau16023ee2010-12-23 20:55:41 +090011170
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011171 pos = bufs[0];
11172 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011173 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011174 /* Check the equal. */
11175 equal = pos + url_param_name_l;
11176 if (fix_pointer_if_wrap(chunks, &equal)) {
11177 if (equal >= chunks[3])
11178 return NULL;
11179 } else {
11180 if (equal >= chunks[1])
11181 return NULL;
11182 }
11183 if (*equal == '=') {
11184 if (pos + url_param_name_l > last) {
11185 /* process wrap case, we detect a wrap. In this case, the
11186 * comparison is performed in two parts.
11187 */
11188
11189 /* This is the end, we dont have any other chunk. */
11190 if (bufs != chunks || !bufs[2])
11191 return NULL;
11192
11193 /* Compute the length of each part of the comparison. */
11194 l1 = last - pos;
11195 l2 = url_param_name_l - l1;
11196
11197 /* The second buffer is too short to contain the compared string. */
11198 if (bufs[2] + l2 > bufs[3])
11199 return NULL;
11200
11201 if (memcmp(pos, url_param_name, l1) == 0 &&
11202 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11203 return pos;
11204
11205 /* Perform wrapping and jump the string who fail the comparison. */
11206 bufs += 2;
11207 pos = bufs[0] + l2;
11208 last = bufs[1];
11209
11210 } else {
11211 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011212 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11213 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011214 pos += url_param_name_l + 1;
11215 if (fix_pointer_if_wrap(chunks, &pos))
11216 last = bufs[2];
11217 }
11218 }
11219
11220 while (1) {
11221 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011222 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011223 pos++;
11224 if (pos < last)
11225 break;
11226 /* process buffer wrapping. */
11227 if (bufs != chunks || !bufs[2])
11228 return NULL;
11229 bufs += 2;
11230 pos = bufs[0];
11231 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011232 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011233 pos++;
11234 }
11235 return NULL;
11236}
11237
11238/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011239 * Given a url parameter name and a query string, find the next value.
11240 * An empty url_param_name matches the first available parameter.
11241 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11242 * respectively provide a pointer to the value and its end.
11243 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011244 */
11245static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011246find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011247 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011248 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011249{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011250 const char *arg_start, *qs_end;
11251 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011252
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011253 arg_start = chunks[0];
11254 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011255 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011256 /* Looks for an argument name. */
11257 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011258 url_param_name, url_param_name_l,
11259 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011260 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011261 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011262 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011263 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011264 if (!arg_start)
11265 return 0;
11266
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011267 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011268 while (1) {
11269 /* looks for the first argument. */
11270 value_start = memchr(arg_start, '=', qs_end - arg_start);
11271 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011272 /* Check for wrapping. */
11273 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011274 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011275 chunks[2]) {
11276 arg_start = chunks[2];
11277 qs_end = chunks[3];
11278 continue;
11279 }
11280 return 0;
11281 }
11282 break;
11283 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011284 value_start++;
11285 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011286 else {
11287 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011288 value_start = arg_start + url_param_name_l + 1;
11289
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011290 /* Check for pointer wrapping. */
11291 if (fix_pointer_if_wrap(chunks, &value_start)) {
11292 /* Update the end pointer. */
11293 qs_end = chunks[3];
11294
11295 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011296 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011297 return 0;
11298 }
11299 }
11300
David Cournapeau16023ee2010-12-23 20:55:41 +090011301 value_end = value_start;
11302
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011303 while (1) {
11304 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11305 value_end++;
11306 if (value_end < qs_end)
11307 break;
11308 /* process buffer wrapping. */
11309 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011310 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011311 chunks[2]) {
11312 value_end = chunks[2];
11313 qs_end = chunks[3];
11314 continue;
11315 }
11316 break;
11317 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011318
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011319 *vstart = value_start;
11320 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011321 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011322}
11323
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011324/* This scans a URL-encoded query string. It takes an optionally wrapping
11325 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11326 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11327 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011328 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011329static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011330smp_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 +090011331{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011332 const char *vstart, *vend;
Willy Tarreau83061a82018-07-13 11:56:34 +020011333 struct buffer *temp;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011334 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011335
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011336 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011337 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011338 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011339 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011340 return 0;
11341
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011342 /* Create sample. If the value is contiguous, return the pointer as CONST,
11343 * if the value is wrapped, copy-it in a buffer.
11344 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011345 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011346 if (chunks[2] &&
11347 vstart >= chunks[0] && vstart <= chunks[1] &&
11348 vend >= chunks[2] && vend <= chunks[3]) {
11349 /* Wrapped case. */
11350 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011351 memcpy(temp->area, vstart, chunks[1] - vstart);
11352 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
11353 vend - chunks[2]);
11354 smp->data.u.str.area = temp->area;
11355 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011356 } else {
11357 /* Contiguous case. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011358 smp->data.u.str.area = (char *)vstart;
11359 smp->data.u.str.data = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011360 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11361 }
11362
11363 /* Update context, check wrapping. */
11364 chunks[0] = vend;
11365 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11366 chunks[1] = chunks[3];
11367 chunks[2] = NULL;
11368 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011369
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011370 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011371 smp->flags |= SMP_F_NOT_LAST;
11372
David Cournapeau16023ee2010-12-23 20:55:41 +090011373 return 1;
11374}
11375
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011376/* This function iterates over each parameter of the query string. It uses
11377 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011378 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11379 * An optional parameter name is passed in args[0], otherwise any parameter is
11380 * considered. It supports an optional delimiter argument for the beginning of
11381 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011382 */
11383static int
11384smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11385{
11386 struct http_msg *msg;
11387 char delim = '?';
11388 const char *name;
11389 int name_len;
11390
Dragan Dosen26f77e52015-05-25 10:02:11 +020011391 if (!args ||
11392 (args[0].type && args[0].type != ARGT_STR) ||
11393 (args[1].type && args[1].type != ARGT_STR))
11394 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011395
Dragan Dosen26f77e52015-05-25 10:02:11 +020011396 name = "";
11397 name_len = 0;
11398 if (args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011399 name = args->data.str.area;
11400 name_len = args->data.str.data;
Dragan Dosen26f77e52015-05-25 10:02:11 +020011401 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011402
Dragan Dosen26f77e52015-05-25 10:02:11 +020011403 if (args[1].type)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011404 delim = *args[1].data.str.area;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011405
Dragan Dosen26f77e52015-05-25 10:02:11 +020011406 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011407 CHECK_HTTP_MESSAGE_FIRST();
11408
11409 msg = &smp->strm->txn->req;
11410
Willy Tarreauf37954d2018-06-15 18:31:02 +020011411 smp->ctx.a[0] = find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011412 msg->sl.rq.u_l, delim);
11413 if (!smp->ctx.a[0])
11414 return 0;
11415
Willy Tarreauf37954d2018-06-15 18:31:02 +020011416 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011417
11418 /* Assume that the context is filled with NULL pointer
11419 * before the first call.
11420 * smp->ctx.a[2] = NULL;
11421 * smp->ctx.a[3] = NULL;
11422 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011423 }
11424
11425 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11426}
11427
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011428/* This function iterates over each parameter of the body. This requires
11429 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011430 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11431 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11432 * optional second part if the body wraps at the end of the buffer. An optional
11433 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011434 */
11435static int
11436smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11437{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011438 struct http_msg *msg;
11439 unsigned long len;
11440 unsigned long block1;
11441 char *body;
11442 const char *name;
11443 int name_len;
11444
11445 if (!args || (args[0].type && args[0].type != ARGT_STR))
11446 return 0;
11447
11448 name = "";
11449 name_len = 0;
11450 if (args[0].type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011451 name = args[0].data.str.area;
11452 name_len = args[0].data.str.data;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011453 }
11454
11455 if (!smp->ctx.a[0]) { // first call, find the query string
11456 CHECK_HTTP_MESSAGE_FIRST();
11457
11458 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011459 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011460 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011461 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011462
11463 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +020011464 body = c_ptr(msg->chn, -http_data_rewind(msg));
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011465
11466 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011467 if (block1 > b_wrap(&msg->chn->buf) - body)
11468 block1 = b_wrap(&msg->chn->buf) - body;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011469
11470 if (block1 == len) {
11471 /* buffer is not wrapped (or empty) */
11472 smp->ctx.a[0] = body;
11473 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011474
11475 /* Assume that the context is filled with NULL pointer
11476 * before the first call.
11477 * smp->ctx.a[2] = NULL;
11478 * smp->ctx.a[3] = NULL;
11479 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011480 }
11481 else {
11482 /* buffer is wrapped, we need to defragment it */
11483 smp->ctx.a[0] = body;
11484 smp->ctx.a[1] = body + block1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011485 smp->ctx.a[2] = b_orig(&msg->chn->buf);
11486 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011487 }
11488 }
11489 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11490}
11491
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011492/* Return the signed integer value for the specified url parameter (see url_param
11493 * above).
11494 */
11495static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011496smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011497{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011498 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011499
11500 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011501 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011502 smp->data.u.sint = strl2ic(smp->data.u.str.area,
11503 smp->data.u.str.data);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011504 }
11505
11506 return ret;
11507}
11508
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011509/* This produces a 32-bit hash of the concatenation of the first occurrence of
11510 * the Host header followed by the path component if it begins with a slash ('/').
11511 * This means that '*' will not be added, resulting in exactly the first Host
11512 * entry. If no Host header is found, then the path is used. The resulting value
11513 * is hashed using the url hash followed by a full avalanche hash and provides a
11514 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11515 * high-traffic sites without having to store whole paths.
11516 * this differs from the base32 functions in that it includes the url parameters
11517 * as well as the path
11518 */
11519static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011520smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011521{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011522 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011523 struct hdr_ctx ctx;
11524 unsigned int hash = 0;
11525 char *ptr, *beg, *end;
11526 int len;
11527
11528 CHECK_HTTP_MESSAGE_FIRST();
11529
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011530 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011531 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011532 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011533 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11534 ptr = ctx.line + ctx.val;
11535 len = ctx.vlen;
11536 while (len--)
11537 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11538 }
11539
11540 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020011541 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 +000011542 beg = http_get_path(txn);
11543 if (!beg)
11544 beg = end;
11545
11546 for (ptr = beg; ptr < end ; ptr++);
11547
11548 if (beg < ptr && *beg == '/') {
11549 while (beg < ptr)
11550 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11551 }
11552 hash = full_hash(hash);
11553
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011554 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011555 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011556 smp->flags = SMP_F_VOL_1ST;
11557 return 1;
11558}
11559
11560/* This concatenates the source address with the 32-bit hash of the Host and
11561 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11562 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11563 * on the source address length. The URL hash is stored before the address so
11564 * that in environments where IPv6 is insignificant, truncating the output to
11565 * 8 bytes would still work.
11566 */
11567static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011568smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011569{
Willy Tarreau83061a82018-07-13 11:56:34 +020011570 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011571 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011572
Dragan Dosendb5af612016-06-16 11:23:01 +020011573 if (!cli_conn)
11574 return 0;
11575
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011576 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011577 return 0;
11578
11579 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011580 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
11581 temp->data += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011582
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011583 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011584 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011585 memcpy(temp->area + temp->data,
11586 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
11587 4);
11588 temp->data += 4;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011589 break;
11590 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011591 memcpy(temp->area + temp->data,
11592 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
11593 16);
11594 temp->data += 16;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011595 break;
11596 default:
11597 return 0;
11598 }
11599
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011600 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011601 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011602 return 1;
11603}
11604
Willy Tarreau185b5c42012-04-26 15:11:51 +020011605/* This function is used to validate the arguments passed to any "hdr" fetch
11606 * keyword. These keywords support an optional positive or negative occurrence
11607 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11608 * is assumed that the types are already the correct ones. Returns 0 on error,
11609 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11610 * error message in case of error, that the caller is responsible for freeing.
11611 * The initial location must either be freeable or NULL.
11612 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011613int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011614{
11615 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011616 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011617 return 0;
11618 }
11619 return 1;
11620}
11621
Willy Tarreau276fae92013-07-25 14:36:01 +020011622/* takes an UINT value on input supposed to represent the time since EPOCH,
11623 * adds an optional offset found in args[0] and emits a string representing
11624 * the date in RFC-1123/5322 format.
11625 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011626static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011627{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011628 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011629 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
Willy Tarreau83061a82018-07-13 11:56:34 +020011630 struct buffer *temp;
Willy Tarreau276fae92013-07-25 14:36:01 +020011631 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011632 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011633 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011634
11635 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011636 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011637 curr_date += args[0].data.sint;
11638
11639 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011640 if (!tm)
11641 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011642
11643 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011644 temp->data = snprintf(temp->area, temp->size - temp->data,
11645 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11646 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon],
11647 1900+tm->tm_year,
11648 tm->tm_hour, tm->tm_min, tm->tm_sec);
Willy Tarreau276fae92013-07-25 14:36:01 +020011649
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011650 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011651 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011652 return 1;
11653}
11654
Thierry FOURNIERad903512014-04-11 17:51:01 +020011655/* Match language range with language tag. RFC2616 14.4:
11656 *
11657 * A language-range matches a language-tag if it exactly equals
11658 * the tag, or if it exactly equals a prefix of the tag such
11659 * that the first tag character following the prefix is "-".
11660 *
11661 * Return 1 if the strings match, else return 0.
11662 */
11663static inline int language_range_match(const char *range, int range_len,
11664 const char *tag, int tag_len)
11665{
11666 const char *end = range + range_len;
11667 const char *tend = tag + tag_len;
11668 while (range < end) {
11669 if (*range == '-' && tag == tend)
11670 return 1;
11671 if (*range != *tag || tag == tend)
11672 return 0;
11673 range++;
11674 tag++;
11675 }
11676 /* Return true only if the last char of the tag is matched. */
11677 return tag == tend;
11678}
11679
11680/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011681static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011682{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011683 const char *al = smp->data.u.str.area;
11684 const char *end = al + smp->data.u.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011685 const char *token;
11686 int toklen;
11687 int qvalue;
11688 const char *str;
11689 const char *w;
11690 int best_q = 0;
11691
11692 /* Set the constant to the sample, because the output of the
11693 * function will be peek in the constant configuration string.
11694 */
11695 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011696 smp->data.u.str.size = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011697 smp->data.u.str.area = "";
11698 smp->data.u.str.data = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011699
11700 /* Parse the accept language */
11701 while (1) {
11702
11703 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011704 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011705 al++;
11706 if (al >= end)
11707 break;
11708
11709 /* Start of the fisrt word. */
11710 token = al;
11711
11712 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011713 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011714 al++;
11715 if (al == token)
11716 goto expect_comma;
11717
11718 /* Length of the token. */
11719 toklen = al - token;
11720 qvalue = 1000;
11721
11722 /* Check if the token exists in the list. If the token not exists,
11723 * jump to the next token.
11724 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011725 str = args[0].data.str.area;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011726 w = str;
11727 while (1) {
11728 if (*str == ';' || *str == '\0') {
11729 if (language_range_match(token, toklen, w, str-w))
11730 goto look_for_q;
11731 if (*str == '\0')
11732 goto expect_comma;
11733 w = str + 1;
11734 }
11735 str++;
11736 }
11737 goto expect_comma;
11738
11739look_for_q:
11740
11741 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011742 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011743 al++;
11744 if (al >= end)
11745 goto process_value;
11746
11747 /* If ',' is found, process the result */
11748 if (*al == ',')
11749 goto process_value;
11750
11751 /* If the character is different from ';', look
11752 * for the end of the header part in best effort.
11753 */
11754 if (*al != ';')
11755 goto expect_comma;
11756
11757 /* Assumes that the char is ';', now expect "q=". */
11758 al++;
11759
11760 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011761 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011762 al++;
11763 if (al >= end)
11764 goto process_value;
11765
11766 /* Expect 'q'. If no 'q', continue in best effort */
11767 if (*al != 'q')
11768 goto process_value;
11769 al++;
11770
11771 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011772 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011773 al++;
11774 if (al >= end)
11775 goto process_value;
11776
11777 /* Expect '='. If no '=', continue in best effort */
11778 if (*al != '=')
11779 goto process_value;
11780 al++;
11781
11782 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011783 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011784 al++;
11785 if (al >= end)
11786 goto process_value;
11787
11788 /* Parse the q value. */
11789 qvalue = parse_qvalue(al, &al);
11790
11791process_value:
11792
11793 /* If the new q value is the best q value, then store the associated
11794 * language in the response. If qvalue is the biggest value (1000),
11795 * break the process.
11796 */
11797 if (qvalue > best_q) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011798 smp->data.u.str.area = (char *)w;
11799 smp->data.u.str.data = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011800 if (qvalue >= 1000)
11801 break;
11802 best_q = qvalue;
11803 }
11804
11805expect_comma:
11806
11807 /* Expect comma or end. If the end is detected, quit the loop. */
11808 while (al < end && *al != ',')
11809 al++;
11810 if (al >= end)
11811 break;
11812
11813 /* Comma is found, jump it and restart the analyzer. */
11814 al++;
11815 }
11816
11817 /* Set default value if required. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011818 if (smp->data.u.str.data == 0 && args[1].type == ARGT_STR) {
11819 smp->data.u.str.area = args[1].data.str.area;
11820 smp->data.u.str.data = args[1].data.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011821 }
11822
11823 /* Return true only if a matching language was found. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011824 return smp->data.u.str.data != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011825}
11826
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011827/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011828static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011829{
Willy Tarreau9c768fd2018-08-22 05:08:57 +020011830 int len;
11831
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011832 /* If the constant flag is set or if not size is avalaible at
11833 * the end of the buffer, copy the string in other buffer
11834 * before decoding.
11835 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011836 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.data) {
Willy Tarreau83061a82018-07-13 11:56:34 +020011837 struct buffer *str = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011838 memcpy(str->area, smp->data.u.str.area, smp->data.u.str.data);
11839 smp->data.u.str.area = str->area;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011840 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011841 smp->flags &= ~SMP_F_CONST;
11842 }
11843
11844 /* Add final \0 required by url_decode(), and convert the input string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011845 smp->data.u.str.area[smp->data.u.str.data] = '\0';
Willy Tarreau9c768fd2018-08-22 05:08:57 +020011846 len = url_decode(smp->data.u.str.area);
11847 if (len < 0)
11848 return 0;
11849 smp->data.u.str.data = len;
11850 return 1;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011851}
11852
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011853static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11854{
11855 struct proxy *fe = strm_fe(smp->strm);
11856 int idx, i;
11857 struct cap_hdr *hdr;
11858 int len;
11859
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011860 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011861 return 0;
11862
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011863 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011864
11865 /* Check the availibity of the capture id. */
11866 if (idx > fe->nb_req_cap - 1)
11867 return 0;
11868
11869 /* Look for the original configuration. */
11870 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11871 hdr != NULL && i != idx ;
11872 i--, hdr = hdr->next);
11873 if (!hdr)
11874 return 0;
11875
11876 /* check for the memory allocation */
11877 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011878 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011879 if (smp->strm->req_cap[hdr->index] == NULL)
11880 return 0;
11881
11882 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011883 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011884 if (len > hdr->len)
11885 len = hdr->len;
11886
11887 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011888 memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011889 smp->strm->req_cap[idx][len] = '\0';
11890
11891 return 1;
11892}
11893
11894static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11895{
11896 struct proxy *fe = strm_fe(smp->strm);
11897 int idx, i;
11898 struct cap_hdr *hdr;
11899 int len;
11900
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011901 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011902 return 0;
11903
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011904 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011905
11906 /* Check the availibity of the capture id. */
11907 if (idx > fe->nb_rsp_cap - 1)
11908 return 0;
11909
11910 /* Look for the original configuration. */
11911 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11912 hdr != NULL && i != idx ;
11913 i--, hdr = hdr->next);
11914 if (!hdr)
11915 return 0;
11916
11917 /* check for the memory allocation */
11918 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011919 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011920 if (smp->strm->res_cap[hdr->index] == NULL)
11921 return 0;
11922
11923 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011924 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011925 if (len > hdr->len)
11926 len = hdr->len;
11927
11928 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011929 memcpy(smp->strm->res_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011930 smp->strm->res_cap[idx][len] = '\0';
11931
11932 return 1;
11933}
11934
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011935/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011936 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011937 * the relevant part of the request line accordingly. Then it updates various
11938 * pointers to the next elements which were moved, and the total buffer length.
11939 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011940 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11941 * error, though this can be revisited when this code is finally exploited.
11942 *
11943 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11944 * query string and 3 to replace uri.
11945 *
11946 * In query string case, the mark question '?' must be set at the start of the
11947 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011948 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011949int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011950 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011951{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011952 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011953 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011954 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011955 int delta;
11956
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011957 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011958 case 0: // method
Willy Tarreauf37954d2018-06-15 18:31:02 +020011959 cur_ptr = ci_head(&s->req);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011960 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11961
11962 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011963 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011964 txn->req.sl.rq.m_l += delta;
11965 txn->req.sl.rq.u += delta;
11966 txn->req.sl.rq.v += delta;
11967 break;
11968
11969 case 1: // path
11970 cur_ptr = http_get_path(txn);
11971 if (!cur_ptr)
Willy Tarreauf37954d2018-06-15 18:31:02 +020011972 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011973
11974 cur_end = cur_ptr;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011975 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 +010011976 cur_end++;
11977
11978 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011979 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011980 txn->req.sl.rq.u_l += delta;
11981 txn->req.sl.rq.v += delta;
11982 break;
11983
11984 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011985 offset = 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011986 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011987 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11988 while (cur_ptr < cur_end && *cur_ptr != '?')
11989 cur_ptr++;
11990
11991 /* skip the question mark or indicate that we must insert it
11992 * (but only if the format string is not empty then).
11993 */
11994 if (cur_ptr < cur_end)
11995 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011996 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011997 offset = 0;
11998
11999 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012000 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012001 txn->req.sl.rq.u_l += delta;
12002 txn->req.sl.rq.v += delta;
12003 break;
12004
12005 case 3: // uri
Willy Tarreauf37954d2018-06-15 18:31:02 +020012006 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012007 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12008
12009 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012010 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012011 txn->req.sl.rq.u_l += delta;
12012 txn->req.sl.rq.v += delta;
12013 break;
12014
12015 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012016 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012017 }
12018
12019 /* commit changes and adjust end of message */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020012020 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012021 txn->req.sl.rq.l += delta;
12022 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012023 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012024 return 0;
12025}
12026
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012027/* This function replace the HTTP status code and the associated message. The
12028 * variable <status> contains the new status code. This function never fails.
12029 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012030void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012031{
12032 struct http_txn *txn = s->txn;
12033 char *cur_ptr, *cur_end;
12034 int delta;
12035 char *res;
12036 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012037 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012038 int msg_len;
12039
12040 chunk_reset(&trash);
12041
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012042 res = ultoa_o(status, trash.area, trash.size);
12043 c_l = res - trash.area;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012044
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012045 trash.area[c_l] = ' ';
12046 trash.data = c_l + 1;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012047
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012048 /* Do we have a custom reason format string? */
12049 if (msg == NULL)
12050 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012051 msg_len = strlen(msg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012052 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
12053 trash.data += msg_len;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012054
Willy Tarreauf37954d2018-06-15 18:31:02 +020012055 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
12056 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012057
12058 /* commit changes and adjust message */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012059 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
12060 trash.data);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012061
12062 /* adjust res line offsets and lengths */
12063 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12064 txn->rsp.sl.st.c_l = c_l;
12065 txn->rsp.sl.st.r_l = msg_len;
12066
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012067 delta = trash.data - (cur_end - cur_ptr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012068 txn->rsp.sl.st.l += delta;
12069 txn->hdr_idx.v[0].len += delta;
12070 http_msg_move_end(&txn->rsp, delta);
12071}
12072
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012073/* This function executes one of the set-{method,path,query,uri} actions. It
12074 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012075 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012076 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012077 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12078 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012079 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012080enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012081 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012082{
Willy Tarreau83061a82018-07-13 11:56:34 +020012083 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +020012084 enum act_return ret = ACT_RET_ERR;
12085
12086 replace = alloc_trash_chunk();
12087 if (!replace)
12088 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012089
12090 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012091 if (rule->arg.http.action == 2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012092 replace->area[replace->data++] = '?';
12093 replace->data += build_logline(s, replace->area + replace->data,
12094 replace->size - replace->data,
12095 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012096
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012097 http_replace_req_line(rule->arg.http.action, replace->area,
12098 replace->data, px, s);
Dragan Dosen2ae327e2017-10-26 11:25:10 +020012099
12100 ret = ACT_RET_CONT;
12101
12102leave:
12103 free_trash_chunk(replace);
12104 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012105}
12106
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012107/* This function is just a compliant action wrapper for "set-status". */
12108enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012109 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012110{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012111 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012112 return ACT_RET_CONT;
12113}
12114
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012115/* parse an http-request action among :
12116 * set-method
12117 * set-path
12118 * set-query
12119 * set-uri
12120 *
12121 * All of them accept a single argument of type string representing a log-format.
12122 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12123 * 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 +020012124 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012125 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012126enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12127 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012128{
12129 int cur_arg = *orig_arg;
12130
Thierry FOURNIER42148732015-09-02 17:17:33 +020012131 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012132
12133 switch (args[0][4]) {
12134 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012135 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012136 rule->action_ptr = http_action_set_req_line;
12137 break;
12138 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012139 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012140 rule->action_ptr = http_action_set_req_line;
12141 break;
12142 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012143 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012144 rule->action_ptr = http_action_set_req_line;
12145 break;
12146 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012147 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012148 rule->action_ptr = http_action_set_req_line;
12149 break;
12150 default:
12151 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012152 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012153 }
12154
12155 if (!*args[cur_arg] ||
12156 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12157 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012158 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012159 }
12160
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012161 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012162 px->conf.args.ctx = ARGC_HRQ;
12163 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
12164 (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 +010012165 return ACT_RET_PRS_ERR;
12166 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012167
12168 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012169 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012170}
12171
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012172/* parse set-status action:
12173 * This action accepts a single argument of type int representing
12174 * an http status code. It returns ACT_RET_PRS_OK on success,
12175 * ACT_RET_PRS_ERR on error.
12176 */
12177enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12178 struct act_rule *rule, char **err)
12179{
12180 char *error;
12181
Thierry FOURNIER42148732015-09-02 17:17:33 +020012182 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012183 rule->action_ptr = action_http_set_status;
12184
12185 /* Check if an argument is available */
12186 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012187 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012188 return ACT_RET_PRS_ERR;
12189 }
12190
12191 /* convert status code as integer */
12192 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12193 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12194 memprintf(err, "expects an integer status code between 100 and 999");
12195 return ACT_RET_PRS_ERR;
12196 }
12197
12198 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012199
12200 /* set custom reason string */
12201 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12202 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12203 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12204 (*orig_arg)++;
12205 rule->arg.status.reason = strdup(args[*orig_arg]);
12206 (*orig_arg)++;
12207 }
12208
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012209 return ACT_RET_PRS_OK;
12210}
12211
Willy Tarreau53275e82017-11-24 07:52:01 +010012212/* This function executes the "reject" HTTP action. It clears the request and
12213 * response buffer without sending any response. It can be useful as an HTTP
12214 * alternative to the silent-drop action to defend against DoS attacks, and may
12215 * also be used with HTTP/2 to close a connection instead of just a stream.
12216 * The txn status is unchanged, indicating no response was sent. The termination
12217 * flags will indicate "PR". It always returns ACT_RET_STOP.
12218 */
12219enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
12220 struct session *sess, struct stream *s, int flags)
12221{
12222 channel_abort(&s->req);
12223 channel_abort(&s->res);
12224 s->req.analysers = 0;
12225 s->res.analysers = 0;
12226
12227 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
12228 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
12229 if (sess->listener && sess->listener->counters)
12230 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
12231
12232 if (!(s->flags & SF_ERR_MASK))
12233 s->flags |= SF_ERR_PRXCOND;
12234 if (!(s->flags & SF_FINST_MASK))
12235 s->flags |= SF_FINST_R;
12236
12237 return ACT_RET_CONT;
12238}
12239
12240/* parse the "reject" action:
12241 * This action takes no argument and returns ACT_RET_PRS_OK on success,
12242 * ACT_RET_PRS_ERR on error.
12243 */
12244enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
12245 struct act_rule *rule, char **err)
12246{
12247 rule->action = ACT_CUSTOM;
12248 rule->action_ptr = http_action_reject;
12249 return ACT_RET_PRS_OK;
12250}
12251
Willy Tarreaua9083d02015-05-08 15:27:59 +020012252/* This function executes the "capture" action. It executes a fetch expression,
12253 * turns the result into a string and puts it in a capture slot. It always
12254 * returns 1. If an error occurs the action is cancelled, but the rule
12255 * processing continues.
12256 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012257enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012258 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012259{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012260 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012261 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012262 char **cap = s->req_cap;
12263 int len;
12264
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012265 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 +020012266 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012267 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012268
12269 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012270 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012271
12272 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012273 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012274
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012275 len = key->data.u.str.data;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012276 if (len > h->len)
12277 len = h->len;
12278
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012279 memcpy(cap[h->index], key->data.u.str.area, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012280 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012281 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012282}
12283
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012284/* This function executes the "capture" action and store the result in a
12285 * capture slot if exists. It executes a fetch expression, turns the result
12286 * into a string and puts it in a capture slot. It always returns 1. If an
12287 * error occurs the action is cancelled, but the rule processing continues.
12288 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012289enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012290 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012291{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012292 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012293 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012294 char **cap = s->req_cap;
12295 struct proxy *fe = strm_fe(s);
12296 int len;
12297 int i;
12298
12299 /* Look for the original configuration. */
12300 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012301 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012302 i--, h = h->next);
12303 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012304 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012305
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012306 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 +020012307 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012308 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012309
12310 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012311 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012312
12313 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012314 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012315
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012316 len = key->data.u.str.data;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012317 if (len > h->len)
12318 len = h->len;
12319
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012320 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012321 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012322 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012323}
12324
Christopher Faulet29730ba2017-09-18 15:26:32 +020012325/* Check an "http-request capture" action.
12326 *
12327 * The function returns 1 in success case, otherwise, it returns 0 and err is
12328 * filled.
12329 */
12330int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12331{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012332 if (rule->action_ptr != http_action_req_capture_by_id)
12333 return 1;
12334
Christopher Faulet29730ba2017-09-18 15:26:32 +020012335 if (rule->arg.capid.idx >= px->nb_req_cap) {
12336 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12337 rule->arg.capid.idx);
12338 return 0;
12339 }
12340
12341 return 1;
12342}
12343
Willy Tarreaua9083d02015-05-08 15:27:59 +020012344/* parse an "http-request capture" action. It takes a single argument which is
12345 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012346 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012347 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012348 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012349enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12350 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012351{
12352 struct sample_expr *expr;
12353 struct cap_hdr *hdr;
12354 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012355 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012356
12357 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12358 if (strcmp(args[cur_arg], "if") == 0 ||
12359 strcmp(args[cur_arg], "unless") == 0)
12360 break;
12361
12362 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012363 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012364 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012365 }
12366
Willy Tarreaua9083d02015-05-08 15:27:59 +020012367 cur_arg = *orig_arg;
12368 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12369 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012370 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012371
12372 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12373 memprintf(err,
12374 "fetch method '%s' extracts information from '%s', none of which is available here",
12375 args[cur_arg-1], sample_src_names(expr->fetch->use));
12376 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012377 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012378 }
12379
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012380 if (!args[cur_arg] || !*args[cur_arg]) {
12381 memprintf(err, "expects 'len or 'id'");
12382 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012383 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012384 }
12385
Willy Tarreaua9083d02015-05-08 15:27:59 +020012386 if (strcmp(args[cur_arg], "len") == 0) {
12387 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012388
12389 if (!(px->cap & PR_CAP_FE)) {
12390 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012391 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012392 }
12393
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012394 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012395
Willy Tarreaua9083d02015-05-08 15:27:59 +020012396 if (!args[cur_arg]) {
12397 memprintf(err, "missing length value");
12398 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012399 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012400 }
12401 /* we copy the table name for now, it will be resolved later */
12402 len = atoi(args[cur_arg]);
12403 if (len <= 0) {
12404 memprintf(err, "length must be > 0");
12405 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012406 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012407 }
12408 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012409
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012410 if (!len) {
12411 memprintf(err, "a positive 'len' argument is mandatory");
12412 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012413 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012414 }
12415
Vincent Bernat02779b62016-04-03 13:48:43 +020012416 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012417 hdr->next = px->req_cap;
12418 hdr->name = NULL; /* not a header capture */
12419 hdr->namelen = 0;
12420 hdr->len = len;
12421 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12422 hdr->index = px->nb_req_cap++;
12423
12424 px->req_cap = hdr;
12425 px->to_log |= LW_REQHDR;
12426
Thierry FOURNIER42148732015-09-02 17:17:33 +020012427 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012428 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012429 rule->arg.cap.expr = expr;
12430 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012431 }
12432
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012433 else if (strcmp(args[cur_arg], "id") == 0) {
12434 int id;
12435 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012436
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012437 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012438
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012439 if (!args[cur_arg]) {
12440 memprintf(err, "missing id value");
12441 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012442 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012443 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012444
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012445 id = strtol(args[cur_arg], &error, 10);
12446 if (*error != '\0') {
12447 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12448 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012449 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012450 }
12451 cur_arg++;
12452
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012453 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012454
Thierry FOURNIER42148732015-09-02 17:17:33 +020012455 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012456 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012457 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012458 rule->arg.capid.expr = expr;
12459 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012460 }
12461
12462 else {
12463 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12464 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012465 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012466 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012467
12468 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012469 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012470}
12471
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012472/* This function executes the "capture" action and store the result in a
12473 * capture slot if exists. It executes a fetch expression, turns the result
12474 * into a string and puts it in a capture slot. It always returns 1. If an
12475 * error occurs the action is cancelled, but the rule processing continues.
12476 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012477enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012478 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012479{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012480 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012481 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012482 char **cap = s->res_cap;
12483 struct proxy *fe = strm_fe(s);
12484 int len;
12485 int i;
12486
12487 /* Look for the original configuration. */
12488 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012489 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012490 i--, h = h->next);
12491 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012492 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012493
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012494 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 +020012495 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012496 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012497
12498 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012499 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012500
12501 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012502 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012503
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012504 len = key->data.u.str.data;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012505 if (len > h->len)
12506 len = h->len;
12507
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012508 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012509 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012510 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012511}
12512
Christopher Faulet29730ba2017-09-18 15:26:32 +020012513/* Check an "http-response capture" action.
12514 *
12515 * The function returns 1 in success case, otherwise, it returns 0 and err is
12516 * filled.
12517 */
12518int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12519{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012520 if (rule->action_ptr != http_action_res_capture_by_id)
12521 return 1;
12522
Christopher Faulet29730ba2017-09-18 15:26:32 +020012523 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12524 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12525 rule->arg.capid.idx);
12526 return 0;
12527 }
12528
12529 return 1;
12530}
12531
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012532/* parse an "http-response capture" action. It takes a single argument which is
12533 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12534 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012535 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012536 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012537enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12538 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012539{
12540 struct sample_expr *expr;
12541 int cur_arg;
12542 int id;
12543 char *error;
12544
12545 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12546 if (strcmp(args[cur_arg], "if") == 0 ||
12547 strcmp(args[cur_arg], "unless") == 0)
12548 break;
12549
12550 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012551 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012552 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012553 }
12554
12555 cur_arg = *orig_arg;
12556 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12557 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012558 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012559
12560 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12561 memprintf(err,
12562 "fetch method '%s' extracts information from '%s', none of which is available here",
12563 args[cur_arg-1], sample_src_names(expr->fetch->use));
12564 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012565 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012566 }
12567
12568 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012569 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012570 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012571 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012572 }
12573
12574 if (strcmp(args[cur_arg], "id") != 0) {
12575 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12576 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012577 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012578 }
12579
12580 cur_arg++;
12581
12582 if (!args[cur_arg]) {
12583 memprintf(err, "missing id value");
12584 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012585 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012586 }
12587
12588 id = strtol(args[cur_arg], &error, 10);
12589 if (*error != '\0') {
12590 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12591 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012592 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012593 }
12594 cur_arg++;
12595
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012596 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012597
Thierry FOURNIER42148732015-09-02 17:17:33 +020012598 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012599 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012600 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012601 rule->arg.capid.expr = expr;
12602 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012603
12604 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012605 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012606}
12607
William Lallemand73025dd2014-04-24 14:38:37 +020012608/*
12609 * Return the struct http_req_action_kw associated to a keyword.
12610 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012611struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012612{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012613 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012614}
12615
12616/*
12617 * Return the struct http_res_action_kw associated to a keyword.
12618 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012619struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012620{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012621 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012622}
12623
Willy Tarreau12207b32016-11-22 19:48:51 +010012624
12625/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12626 * now.
12627 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012628static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau12207b32016-11-22 19:48:51 +010012629{
12630 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12631 return 1;
12632
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012633 if (*args[2]) {
12634 struct proxy *px;
12635
12636 px = proxy_find_by_name(args[2], 0, 0);
12637 if (px)
12638 appctx->ctx.errors.iid = px->uuid;
12639 else
12640 appctx->ctx.errors.iid = atoi(args[2]);
12641
12642 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012643 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012644 appctx->ctx.cli.msg = "No such proxy.\n";
12645 appctx->st0 = CLI_ST_PRINT;
12646 return 1;
12647 }
12648 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012649 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012650 appctx->ctx.errors.iid = -1; // dump all proxies
12651
Willy Tarreau35069f82016-11-25 09:16:37 +010012652 appctx->ctx.errors.flag = 0;
12653 if (strcmp(args[3], "request") == 0)
12654 appctx->ctx.errors.flag |= 4; // ignore response
12655 else if (strcmp(args[3], "response") == 0)
12656 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012657 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012658 return 0;
12659}
12660
12661/* This function dumps all captured errors onto the stream interface's
12662 * read buffer. It returns 0 if the output buffer is full and it needs
12663 * to be called again, otherwise non-zero.
12664 */
12665static int cli_io_handler_show_errors(struct appctx *appctx)
12666{
12667 struct stream_interface *si = appctx->owner;
12668 extern const char *monthname[12];
12669
12670 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12671 return 1;
12672
12673 chunk_reset(&trash);
12674
12675 if (!appctx->ctx.errors.px) {
12676 /* the function had not been called yet, let's prepare the
12677 * buffer for a response.
12678 */
12679 struct tm tm;
12680
12681 get_localtime(date.tv_sec, &tm);
12682 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12683 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12684 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12685 error_snapshot_id);
12686
Willy Tarreau06d80a92017-10-19 14:32:15 +020012687 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012688 /* Socket buffer full. Let's try again later from the same point */
12689 si_applet_cant_put(si);
12690 return 0;
12691 }
12692
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012693 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012694 appctx->ctx.errors.bol = 0;
12695 appctx->ctx.errors.ptr = -1;
12696 }
12697
12698 /* we have two inner loops here, one for the proxy, the other one for
12699 * the buffer.
12700 */
12701 while (appctx->ctx.errors.px) {
12702 struct error_snapshot *es;
12703
Willy Tarreau35069f82016-11-25 09:16:37 +010012704 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012705 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012706 if (appctx->ctx.errors.flag & 2) // skip req
12707 goto next;
12708 }
12709 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012710 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012711 if (appctx->ctx.errors.flag & 4) // skip resp
12712 goto next;
12713 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012714
12715 if (!es->when.tv_sec)
12716 goto next;
12717
12718 if (appctx->ctx.errors.iid >= 0 &&
12719 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12720 es->oe->uuid != appctx->ctx.errors.iid)
12721 goto next;
12722
12723 if (appctx->ctx.errors.ptr < 0) {
12724 /* just print headers now */
12725
12726 char pn[INET6_ADDRSTRLEN];
12727 struct tm tm;
12728 int port;
12729
12730 get_localtime(es->when.tv_sec, &tm);
12731 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12732 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12733 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12734
12735 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12736 case AF_INET:
12737 case AF_INET6:
12738 port = get_host_port(&es->src);
12739 break;
12740 default:
12741 port = 0;
12742 }
12743
Willy Tarreau35069f82016-11-25 09:16:37 +010012744 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012745 case 0:
12746 chunk_appendf(&trash,
12747 " frontend %s (#%d): invalid request\n"
12748 " backend %s (#%d)",
12749 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12750 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12751 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12752 break;
12753 case 1:
12754 chunk_appendf(&trash,
12755 " backend %s (#%d): invalid response\n"
12756 " frontend %s (#%d)",
12757 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12758 es->oe->id, es->oe->uuid);
12759 break;
12760 }
12761
12762 chunk_appendf(&trash,
12763 ", server %s (#%d), event #%u\n"
12764 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012765 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012766 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12767 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12768 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12769 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12770 es->ev_id,
12771 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012772 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012773 es->m_clen, es->m_blen,
12774 es->b_flags, es->b_out, es->b_tot,
12775 es->len, es->b_wrap, es->pos);
12776
Willy Tarreau06d80a92017-10-19 14:32:15 +020012777 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012778 /* Socket buffer full. Let's try again later from the same point */
12779 si_applet_cant_put(si);
12780 return 0;
12781 }
12782 appctx->ctx.errors.ptr = 0;
12783 appctx->ctx.errors.sid = es->sid;
12784 }
12785
12786 if (appctx->ctx.errors.sid != es->sid) {
12787 /* the snapshot changed while we were dumping it */
12788 chunk_appendf(&trash,
12789 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012790 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012791 si_applet_cant_put(si);
12792 return 0;
12793 }
12794 goto next;
12795 }
12796
12797 /* OK, ptr >= 0, so we have to dump the current line */
12798 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12799 int newptr;
12800 int newline;
12801
12802 newline = appctx->ctx.errors.bol;
12803 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12804 if (newptr == appctx->ctx.errors.ptr)
12805 return 0;
12806
Willy Tarreau06d80a92017-10-19 14:32:15 +020012807 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012808 /* Socket buffer full. Let's try again later from the same point */
12809 si_applet_cant_put(si);
12810 return 0;
12811 }
12812 appctx->ctx.errors.ptr = newptr;
12813 appctx->ctx.errors.bol = newline;
12814 };
12815 next:
12816 appctx->ctx.errors.bol = 0;
12817 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012818 appctx->ctx.errors.flag ^= 1;
12819 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012820 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012821 }
12822
12823 /* dump complete */
12824 return 1;
12825}
12826
12827/* register cli keywords */
12828static struct cli_kw_list cli_kws = {{ },{
12829 { { "show", "errors", NULL },
12830 "show errors : report last request and response errors for each proxy",
12831 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12832 },
12833 {{},}
12834}};
12835
Willy Tarreau4a568972010-05-12 08:08:50 +020012836/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012837/* All supported ACL keywords must be declared here. */
12838/************************************************************************/
12839
12840/* Note: must not be declared <const> as its list will be overwritten.
12841 * Please take care of keeping this list alphabetically sorted.
12842 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012843static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012844 { "base", "base", PAT_MATCH_STR },
12845 { "base_beg", "base", PAT_MATCH_BEG },
12846 { "base_dir", "base", PAT_MATCH_DIR },
12847 { "base_dom", "base", PAT_MATCH_DOM },
12848 { "base_end", "base", PAT_MATCH_END },
12849 { "base_len", "base", PAT_MATCH_LEN },
12850 { "base_reg", "base", PAT_MATCH_REG },
12851 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012852
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012853 { "cook", "req.cook", PAT_MATCH_STR },
12854 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12855 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12856 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12857 { "cook_end", "req.cook", PAT_MATCH_END },
12858 { "cook_len", "req.cook", PAT_MATCH_LEN },
12859 { "cook_reg", "req.cook", PAT_MATCH_REG },
12860 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012861
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012862 { "hdr", "req.hdr", PAT_MATCH_STR },
12863 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12864 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12865 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12866 { "hdr_end", "req.hdr", PAT_MATCH_END },
12867 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12868 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12869 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012870
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012871 /* these two declarations uses strings with list storage (in place
12872 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12873 * and delete functions are relative to the list management. The parse
12874 * and match method are related to the corresponding fetch methods. This
12875 * is very particular ACL declaration mode.
12876 */
12877 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12878 { "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 +020012879
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012880 { "path", "path", PAT_MATCH_STR },
12881 { "path_beg", "path", PAT_MATCH_BEG },
12882 { "path_dir", "path", PAT_MATCH_DIR },
12883 { "path_dom", "path", PAT_MATCH_DOM },
12884 { "path_end", "path", PAT_MATCH_END },
12885 { "path_len", "path", PAT_MATCH_LEN },
12886 { "path_reg", "path", PAT_MATCH_REG },
12887 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012888
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012889 { "req_ver", "req.ver", PAT_MATCH_STR },
12890 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012891
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012892 { "scook", "res.cook", PAT_MATCH_STR },
12893 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12894 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12895 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12896 { "scook_end", "res.cook", PAT_MATCH_END },
12897 { "scook_len", "res.cook", PAT_MATCH_LEN },
12898 { "scook_reg", "res.cook", PAT_MATCH_REG },
12899 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012900
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012901 { "shdr", "res.hdr", PAT_MATCH_STR },
12902 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12903 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12904 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12905 { "shdr_end", "res.hdr", PAT_MATCH_END },
12906 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12907 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12908 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012909
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012910 { "url", "url", PAT_MATCH_STR },
12911 { "url_beg", "url", PAT_MATCH_BEG },
12912 { "url_dir", "url", PAT_MATCH_DIR },
12913 { "url_dom", "url", PAT_MATCH_DOM },
12914 { "url_end", "url", PAT_MATCH_END },
12915 { "url_len", "url", PAT_MATCH_LEN },
12916 { "url_reg", "url", PAT_MATCH_REG },
12917 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012918
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012919 { "urlp", "urlp", PAT_MATCH_STR },
12920 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12921 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12922 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12923 { "urlp_end", "urlp", PAT_MATCH_END },
12924 { "urlp_len", "urlp", PAT_MATCH_LEN },
12925 { "urlp_reg", "urlp", PAT_MATCH_REG },
12926 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012927
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012928 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012929}};
12930
12931/************************************************************************/
12932/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012933/************************************************************************/
12934/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012935static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012936 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012937 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012938 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12939
Willy Tarreau87b09662015-04-03 00:22:06 +020012940 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012941 { "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 +020012942
12943 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012944 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12945 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12946 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012947
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012948 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12949 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012950
Willy Tarreau409bcde2013-01-08 00:31:00 +010012951 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12952 * are only here to match the ACL's name, are request-only and are used
12953 * for ACL compatibility only.
12954 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012955 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12956 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012957 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12958 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012959
12960 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12961 * only here to match the ACL's name, are request-only and are used for
12962 * ACL compatibility only.
12963 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012964 { "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 +020012965 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012966 { "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 +020012967 { "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 +010012968
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012969 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012970 { "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 +010012971 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012972 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012973 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012974 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012975
12976 /* HTTP protocol on the request path */
12977 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012978 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012979
12980 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012981 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12982 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012983
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012984 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012985 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12986 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012987 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012988
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012989 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012990 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12991
Willy Tarreau18ed2562013-01-14 15:56:36 +010012992 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012993 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12994 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012995
Willy Tarreau18ed2562013-01-14 15:56:36 +010012996 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012997 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012998 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12999 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013000
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013001 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013002 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013003 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013004 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013005 { "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 +010013006 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013007 { "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 +010013008
13009 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013010 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013011 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13012 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013013
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013014 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013015 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013016 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013017 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013018 { "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 +010013019 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013020 { "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 +010013021
Willy Tarreau409bcde2013-01-08 00:31:00 +010013022 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013023 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013024 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13025 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013026 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013027
13028 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013029 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013030 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013031 { "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 +020013032 { "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 +010013033
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013034 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013035 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013036 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013037 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013038 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013039 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013040 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013041 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13042 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013043 { "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 +010013044 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013045}};
13046
Willy Tarreau8797c062007-05-07 00:55:35 +020013047
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013048/************************************************************************/
13049/* All supported converter keywords must be declared here. */
13050/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013051/* Note: must not be declared <const> as its list will be overwritten */
13052static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013053 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013054 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013055 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13056 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013057 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013058 { NULL, NULL, 0, 0, 0 },
13059}};
13060
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013061
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013062/************************************************************************/
13063/* All supported http-request action keywords must be declared here. */
13064/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013065struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013066 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013067 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010013068 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013069 { "set-method", parse_set_req_line },
13070 { "set-path", parse_set_req_line },
13071 { "set-query", parse_set_req_line },
13072 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013073 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013074 }
13075};
13076
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013077struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013078 .kw = {
13079 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013080 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013081 { NULL, NULL }
13082 }
13083};
13084
Willy Tarreau8797c062007-05-07 00:55:35 +020013085__attribute__((constructor))
13086static void __http_protocol_init(void)
13087{
13088 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013089 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013090 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013091 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013092 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013093 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013094}
13095
13096
Willy Tarreau58f10d72006-12-04 02:26:12 +010013097/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013098 * Local variables:
13099 * c-indent-level: 8
13100 * c-basic-offset: 8
13101 * End:
13102 */